ilog2.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. # undef extern_inline
  39. # define extern_inline
  40. # define inline_prototypes
  41. #endif
  42. #ifdef inline_prototypes
  43. extern int const_func ilog2_32(uint32_t v);
  44. extern int const_func ilog2_64(uint64_t v);
  45. extern int const_func ilog2_64(uint64_t vv);
  46. extern int const_func alignlog2_32(uint32_t v);
  47. extern int const_func alignlog2_64(uint64_t v);
  48. #endif
  49. #ifdef extern_inline
  50. #define ROUND(v, a, w) \
  51. do { \
  52. if (v & (((UINT32_C(1) << w) - 1) << w)) { \
  53. a += w; \
  54. v >>= w; \
  55. } \
  56. } while (0)
  57. #if defined(__GNUC__) && defined(__x86_64__)
  58. extern_inline int const_func ilog2_32(uint32_t v)
  59. {
  60. int n;
  61. __asm__("bsrl %1,%0"
  62. : "=r" (n)
  63. : "rm" (v), "0" (0));
  64. return n;
  65. }
  66. #elif defined(__GNUC__) && defined(__i386__)
  67. extern_inline int const_func ilog2_32(uint32_t v)
  68. {
  69. int n;
  70. #ifdef __i686__
  71. __asm__("bsrl %1,%0 ; cmovz %2,%0\n"
  72. : "=&r" (n)
  73. : "rm" (v), "r" (0));
  74. #else
  75. __asm__("bsrl %1,%0 ; jnz 1f ; xorl %0,%0\n"
  76. "1:"
  77. : "=&r" (n)
  78. : "rm" (v));
  79. #endif
  80. return n;
  81. }
  82. #elif defined(HAVE___BUILTIN_CLZ) && INT_MAX == 2147483647
  83. extern_inline int const_func ilog2_32(uint32_t v)
  84. {
  85. if (!v)
  86. return 0;
  87. return __builtin_clz(v) ^ 31;
  88. }
  89. #elif defined(HAVE__BITSCANREVERSE)
  90. extern_inline int const_func ilog2_32(uint32_t v)
  91. {
  92. unsigned long ix;
  93. return _BitScanReverse(&ix, v) ? v : 0;
  94. }
  95. #else
  96. extern_inline int const_func ilog2_32(uint32_t v)
  97. {
  98. int p = 0;
  99. ROUND(v, p, 16);
  100. ROUND(v, p, 8);
  101. ROUND(v, p, 4);
  102. ROUND(v, p, 2);
  103. ROUND(v, p, 1);
  104. return p;
  105. }
  106. #endif
  107. #if defined(__GNUC__) && defined(__x86_64__)
  108. extern_inline int const_func ilog2_64(uint64_t v)
  109. {
  110. uint64_t n;
  111. __asm__("bsrq %1,%0"
  112. : "=r" (n)
  113. : "rm" (v), "0" (UINT64_C(0)));
  114. return n;
  115. }
  116. #elif defined(HAVE__BUILTIN_CLZLL) && LLONG_MAX == 9223372036854775807LL
  117. extern_inline int const_func ilog2_64(uint64_t v)
  118. {
  119. if (!v)
  120. return 0;
  121. return __builtin_clzll(v) ^ 63;
  122. }
  123. #elif defined(HAVE__BITSCANREVERSE64)
  124. extern_inline int const_func ilog2_64(uint64_t v)
  125. {
  126. unsigned long ix;
  127. return _BitScanReverse64(&ix, v) ? ix : 0;
  128. }
  129. #else
  130. extern_inline int const_func ilog2_64(uint64_t vv)
  131. {
  132. int p = 0;
  133. uint32_t v;
  134. v = vv >> 32;
  135. if (v)
  136. p += 32;
  137. else
  138. v = vv;
  139. ROUND(v, p, 16);
  140. ROUND(v, p, 8);
  141. ROUND(v, p, 4);
  142. ROUND(v, p, 2);
  143. ROUND(v, p, 1);
  144. return p;
  145. }
  146. #endif
  147. /*
  148. * v == 0 ? 0 : is_power2(x) ? ilog2_X(v) : -1
  149. */
  150. extern_inline int const_func alignlog2_32(uint32_t v)
  151. {
  152. if (unlikely(v & (v-1)))
  153. return -1; /* invalid alignment */
  154. return ilog2_32(v);
  155. }
  156. extern_inline int const_func alignlog2_64(uint64_t v)
  157. {
  158. if (unlikely(v & (v-1)))
  159. return -1; /* invalid alignment */
  160. return ilog2_64(v);
  161. }
  162. #undef ROUND
  163. #endif /* extern_inline */
  164. #endif /* ILOG2_H */