BitMath.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_BITMATH_HPP
  26. #define CAT_BITMATH_HPP
  27. #include <cat/Platform.hpp>
  28. #if defined(CAT_OS_WINDOWS)
  29. # include <cat/port/WindowsInclude.hpp>
  30. #endif
  31. namespace cat {
  32. // Next highest power of two (e.g. 13 -> 16)
  33. CAT_INLINE u32 NextHighestPow2(u32 n)
  34. {
  35. n |= n >> 1;
  36. n |= n >> 2;
  37. n |= n >> 4;
  38. n |= n >> 8;
  39. n |= n >> 16;
  40. return n + 1;
  41. }
  42. CAT_INLINE u64 NextHighestPow2(u64 n)
  43. {
  44. n |= n >> 1;
  45. n |= n >> 2;
  46. n |= n >> 4;
  47. n |= n >> 8;
  48. n |= n >> 16;
  49. n |= n >> 32;
  50. return n + 1;
  51. }
  52. // Bit Scan Forward (BSF)
  53. // Scans from bit 0 to MSB
  54. // Undefined when input is zero
  55. CAT_INLINE u32 BSF32(u32 x);
  56. CAT_INLINE u32 BSF64(u64 x);
  57. // Bit Scan Reverse (BSR)
  58. // Scans from MSB to bit 0
  59. // Undefined when input is zero
  60. CAT_INLINE u32 BSR32(u32 x);
  61. CAT_INLINE u32 BSR64(u64 x);
  62. // Returns the count of bits set in the input for types up to 128 bits
  63. template<typename T> CAT_INLINE T BitCount(T v)
  64. {
  65. // From Stanford Bit Twiddling Hacks collection
  66. // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
  67. v = v - ((v >> 1) & (T)~(T)0/3);
  68. v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3);
  69. v = (v + (v >> 4)) & (T)~(T)0/255*15;
  70. return (T)(v * ((T)~(T)0/255)) >> ((sizeof(v) - 1) * 8);
  71. }
  72. // Reconstruct a 32-bit or 64-bit counter that increments by one each time,
  73. // given a truncated sample of its low bits, and the last accepted value of the counter.
  74. template<int BITS, typename T> CAT_INLINE T ReconstructCounter(T last_accepted_count, u32 partial_low_bits)
  75. {
  76. const u32 IV_MSB = (1 << BITS); // BITS < 32
  77. const u32 IV_MASK = (IV_MSB - 1);
  78. s32 diff = partial_low_bits - (u32)(last_accepted_count & IV_MASK);
  79. return ((last_accepted_count & ~(T)IV_MASK) | partial_low_bits)
  80. - (((IV_MSB >> 1) - (diff & IV_MASK)) & IV_MSB)
  81. + (diff & IV_MSB);
  82. }
  83. u32 BSF32(u32 x)
  84. {
  85. #if defined(CAT_COMPILER_MSVC) && defined(CAT_WORD_64) && !defined(CAT_DEBUG)
  86. u32 index;
  87. _BitScanForward((unsigned long*)&index, x);
  88. return index;
  89. #elif defined(CAT_ASM_INTEL) && defined(CAT_ISA_X86)
  90. CAT_ASM_BEGIN
  91. BSF eax, [x]
  92. CAT_ASM_END
  93. #elif defined(CAT_ASM_ATT) && defined(CAT_ISA_X86)
  94. u32 retval;
  95. CAT_ASM_BEGIN
  96. "BSFl %1, %%eax"
  97. : "=a" (retval)
  98. : "r" (x)
  99. : "cc"
  100. CAT_ASM_END
  101. return retval;
  102. #else
  103. return BSR32(x ^ (x - 1));
  104. #endif
  105. }
  106. u32 BSR32(u32 x)
  107. {
  108. #if defined(CAT_COMPILER_MSVC) && defined(CAT_WORD_64) && !defined(CAT_DEBUG)
  109. u32 index;
  110. _BitScanReverse((unsigned long*)&index, x);
  111. return index;
  112. #elif defined(CAT_ASM_INTEL) && defined(CAT_ISA_X86)
  113. CAT_ASM_BEGIN
  114. BSR eax, [x]
  115. CAT_ASM_END
  116. #elif defined(CAT_ASM_ATT) && defined(CAT_ISA_X86)
  117. u32 retval;
  118. CAT_ASM_BEGIN
  119. "BSRl %1, %%eax"
  120. : "=a" (retval)
  121. : "r" (x)
  122. : "cc"
  123. CAT_ASM_END
  124. return retval;
  125. #else
  126. // Adapted from the Stanford Bit Twiddling Hacks collection
  127. register u32 shift, r;
  128. r = (x > 0xFFFF) << 4; x >>= r;
  129. shift = (x > 0xFF) << 3; x >>= shift; r |= shift;
  130. shift = (x > 0xF) << 2; x >>= shift; r |= shift;
  131. shift = (x > 0x3) << 1; x >>= shift; r |= shift;
  132. r |= (x >> 1);
  133. return r;
  134. #endif
  135. }
  136. u32 BSF64(u64 x)
  137. {
  138. #if defined(CAT_COMPILER_MSVC) && !defined(CAT_DEBUG) && defined(CAT_WORD_64)
  139. u32 index;
  140. _BitScanForward64((unsigned long*)&index, x);
  141. return index;
  142. #elif defined(CAT_ASM_ATT) && defined(CAT_WORD_64) && defined(CAT_ISA_X86)
  143. u32 retval;
  144. CAT_ASM_BEGIN
  145. "BSFq %1, %%rax"
  146. : "=a" (retval)
  147. : "r" (x)
  148. : "cc"
  149. CAT_ASM_END
  150. return retval;
  151. #else
  152. return BSR64(x ^ (x - 1));
  153. #endif
  154. }
  155. u32 BSR64(u64 x)
  156. {
  157. #if defined(CAT_COMPILER_MSVC) && !defined(CAT_DEBUG) && defined(CAT_WORD_64)
  158. u32 index;
  159. _BitScanReverse64((unsigned long*)&index, x);
  160. return index;
  161. #elif defined(CAT_ASM_ATT) && defined(CAT_WORD_64) && defined(CAT_ISA_X86)
  162. u32 retval;
  163. CAT_ASM_BEGIN
  164. "BSRq %1, %%rax"
  165. : "=a" (retval)
  166. : "r" (x)
  167. : "cc"
  168. CAT_ASM_END
  169. return retval;
  170. #else
  171. // Adapted from the Stanford Bit Twiddling Hacks collection
  172. register u32 shift, r;
  173. r = (x > 0xFFFFFFFF) << 5; x >>= r;
  174. shift = (x > 0xFFFF) << 4; x >>= shift; r |= shift;
  175. shift = (x > 0xFF) << 3; x >>= shift; r |= shift;
  176. shift = (x > 0xF) << 2; x >>= shift; r |= shift;
  177. shift = (x > 0x3) << 1; x >>= shift; r |= shift;
  178. r |= (u32)(x >> 1);
  179. return r;
  180. #endif
  181. }
  182. } // namespace cat
  183. #endif // CAT_BITMATH_HPP
粤ICP备19079148号