ilog2.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2017 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. #ifndef ILOG2_H
  34. #define ILOG2_H
  35. #include "compiler.h"
  36. #include <limits.h>
  37. #ifdef ILOG2_C /* For generating the out-of-line functions */
  38. # ifndef HAVE_MSVC_INLINE
  39. # undef extern_inline
  40. # define extern_inline
  41. # endif
  42. # define inline_prototypes
  43. #endif
  44. #ifdef inline_prototypes
  45. extern unsigned int const_func ilog2_32(uint32_t v);
  46. extern unsigned int const_func ilog2_64(uint64_t v);
  47. extern unsigned int const_func ilog2_64(uint64_t vv);
  48. extern int const_func alignlog2_32(uint32_t v);
  49. extern int const_func alignlog2_64(uint64_t v);
  50. #endif
  51. #ifdef extern_inline
  52. #define ROUND(v, a, w) \
  53. do { \
  54. if (v & (((UINT32_C(1) << w) - 1) << w)) { \
  55. a += w; \
  56. v >>= w; \
  57. } \
  58. } while (0)
  59. #if defined(HAVE___BUILTIN_CLZ) && INT_MAX == 2147483647
  60. extern_inline unsigned int const_func ilog2_32(uint32_t v)
  61. {
  62. if (!v)
  63. return 0;
  64. return __builtin_clz(v) ^ 31;
  65. }
  66. #elif defined(__GNUC__) && defined(__x86_64__)
  67. extern_inline unsigned int const_func ilog2_32(uint32_t v)
  68. {
  69. unsigned int n;
  70. __asm__("bsrl %1,%0"
  71. : "=r" (n)
  72. : "rm" (v), "0" (0));
  73. return n;
  74. }
  75. #elif defined(__GNUC__) && defined(__i386__)
  76. extern_inline unsigned int const_func ilog2_32(uint32_t v)
  77. {
  78. unsigned int n;
  79. #ifdef __i686__
  80. __asm__("bsrl %1,%0 ; cmovz %2,%0\n"
  81. : "=&r" (n)
  82. : "rm" (v), "r" (0));
  83. #else
  84. __asm__("bsrl %1,%0 ; jnz 1f ; xorl %0,%0\n"
  85. "1:"
  86. : "=&r" (n)
  87. : "rm" (v));
  88. #endif
  89. return n;
  90. }
  91. #elif defined(HAVE__BITSCANREVERSE)
  92. extern_inline unsigned int const_func ilog2_32(uint32_t v)
  93. {
  94. unsigned long ix;
  95. return _BitScanReverse(&ix, v) ? v : 0;
  96. }
  97. #else
  98. extern_inline unsigned int const_func ilog2_32(uint32_t v)
  99. {
  100. unsigned int p = 0;
  101. ROUND(v, p, 16);
  102. ROUND(v, p, 8);
  103. ROUND(v, p, 4);
  104. ROUND(v, p, 2);
  105. ROUND(v, p, 1);
  106. return p;
  107. }
  108. #endif
  109. #if defined(HAVE__BUILTIN_CLZLL) && LLONG_MAX == 9223372036854775807LL
  110. extern_inline unsigned int const_func ilog2_64(uint64_t v)
  111. {
  112. if (!v)
  113. return 0;
  114. return __builtin_clzll(v) ^ 63;
  115. }
  116. #elif defined(__GNUC__) && defined(__x86_64__)
  117. extern_inline unsigned int const_func ilog2_64(uint64_t v)
  118. {
  119. uint64_t n;
  120. __asm__("bsrq %1,%0"
  121. : "=r" (n)
  122. : "rm" (v), "0" (UINT64_C(0)));
  123. return n;
  124. }
  125. #elif defined(HAVE__BITSCANREVERSE64)
  126. extern_inline unsigned int const_func ilog2_64(uint64_t v)
  127. {
  128. unsigned long ix;
  129. return _BitScanReverse64(&ix, v) ? ix : 0;
  130. }
  131. #else
  132. extern_inline unsigned int const_func ilog2_64(uint64_t vv)
  133. {
  134. unsigned int p = 0;
  135. uint32_t v;
  136. v = vv >> 32;
  137. if (v)
  138. p += 32;
  139. else
  140. v = vv;
  141. return p + ilog2_32(v);
  142. }
  143. #endif
  144. /*
  145. * v == 0 ? 0 : is_power2(x) ? ilog2_X(v) : -1
  146. */
  147. extern_inline int const_func alignlog2_32(uint32_t v)
  148. {
  149. if (unlikely(v & (v-1)))
  150. return -1; /* invalid alignment */
  151. return ilog2_32(v);
  152. }
  153. extern_inline int const_func alignlog2_64(uint64_t v)
  154. {
  155. if (unlikely(v & (v-1)))
  156. return -1; /* invalid alignment */
  157. return ilog2_64(v);
  158. }
  159. #undef ROUND
  160. #endif /* extern_inline */
  161. #endif /* ILOG2_H */