bitscan.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**************************************************************************
  2. *
  3. * Copyright 2008 VMware, Inc.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21. * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  22. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #ifndef BITSCAN_H
  28. #define BITSCAN_H
  29. #include <assert.h>
  30. #include <stdint.h>
  31. #include <stdbool.h>
  32. #include <string.h>
  33. #if defined(_MSC_VER)
  34. #include <intrin.h>
  35. #endif
  36. #if defined(__POPCNT__)
  37. #include <popcntintrin.h>
  38. #endif
  39. //#include "c99_compat.h"
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /**
  44. * Find first bit set in word. Least significant bit is 1.
  45. * Return 0 if no bits set.
  46. */
  47. #ifdef HAVE___BUILTIN_FFS
  48. #define ffs __builtin_ffs
  49. #elif defined(_MSC_VER) && (_M_IX86 || _M_ARM || _M_AMD64 || _M_IA64)
  50. static inline
  51. int ffs(int i)
  52. {
  53. unsigned long index;
  54. if (_BitScanForward(&index, i))
  55. return index + 1;
  56. else
  57. return 0;
  58. }
  59. #else
  60. extern
  61. int ffs(int i);
  62. #endif
  63. #ifdef HAVE___BUILTIN_FFSLL
  64. #define ffsll __builtin_ffsll
  65. #elif defined(_MSC_VER) && (_M_AMD64 || _M_ARM64 || _M_IA64)
  66. static inline int
  67. ffsll(long long int i)
  68. {
  69. unsigned long index;
  70. if (_BitScanForward64(&index, i))
  71. return index + 1;
  72. else
  73. return 0;
  74. }
  75. #else
  76. extern int
  77. ffsll(long long int val);
  78. #endif
  79. /* Destructively loop over all of the bits in a mask as in:
  80. *
  81. * while (mymask) {
  82. * int i = u_bit_scan(&mymask);
  83. * ... process element i
  84. * }
  85. *
  86. */
  87. static inline int
  88. u_bit_scan(unsigned *mask)
  89. {
  90. const int i = ffs(*mask) - 1;
  91. *mask ^= (1u << i);
  92. return i;
  93. }
  94. static inline int
  95. u_bit_scan64(uint64_t *mask)
  96. {
  97. const int i = ffsll(*mask) - 1;
  98. *mask ^= (((uint64_t)1) << i);
  99. return i;
  100. }
  101. /* Determine if an unsigned value is a power of two.
  102. *
  103. * \note
  104. * Zero is treated as a power of two.
  105. */
  106. static inline bool
  107. util_is_power_of_two_or_zero(unsigned v)
  108. {
  109. return (v & (v - 1)) == 0;
  110. }
  111. /* Determine if an uint64_t value is a power of two.
  112. *
  113. * \note
  114. * Zero is treated as a power of two.
  115. */
  116. static inline bool
  117. util_is_power_of_two_or_zero64(uint64_t v)
  118. {
  119. return (v & (v - 1)) == 0;
  120. }
  121. /* Determine if an unsigned value is a power of two.
  122. *
  123. * \note
  124. * Zero is \b not treated as a power of two.
  125. */
  126. static inline bool
  127. util_is_power_of_two_nonzero(unsigned v)
  128. {
  129. /* __POPCNT__ is different from HAVE___BUILTIN_POPCOUNT. The latter
  130. * indicates the existence of the __builtin_popcount function. The former
  131. * indicates that _mm_popcnt_u32 exists and is a native instruction.
  132. *
  133. * The other alternative is to use SSE 4.2 compile-time flags. This has
  134. * two drawbacks. First, there is currently no build infrastructure for
  135. * SSE 4.2 (only 4.1), so that would have to be added. Second, some AMD
  136. * CPUs support POPCNT but not SSE 4.2 (e.g., Barcelona).
  137. */
  138. #ifdef __POPCNT__
  139. return _mm_popcnt_u32(v) == 1;
  140. #else
  141. return v != 0 && (v & (v - 1)) == 0;
  142. #endif
  143. }
  144. /* For looping over a bitmask when you want to loop over consecutive bits
  145. * manually, for example:
  146. *
  147. * while (mask) {
  148. * int start, count, i;
  149. *
  150. * u_bit_scan_consecutive_range(&mask, &start, &count);
  151. *
  152. * for (i = 0; i < count; i++)
  153. * ... process element (start+i)
  154. * }
  155. */
  156. static inline void
  157. u_bit_scan_consecutive_range(unsigned *mask, int *start, int *count)
  158. {
  159. if (*mask == 0xffffffff) {
  160. *start = 0;
  161. *count = 32;
  162. *mask = 0;
  163. return;
  164. }
  165. *start = ffs(*mask) - 1;
  166. *count = ffs(~(*mask >> *start)) - 1;
  167. *mask &= ~(((1u << *count) - 1) << *start);
  168. }
  169. static inline void
  170. u_bit_scan_consecutive_range64(uint64_t *mask, int *start, int *count)
  171. {
  172. if (*mask == ~0ull) {
  173. *start = 0;
  174. *count = 64;
  175. *mask = 0;
  176. return;
  177. }
  178. *start = ffsll(*mask) - 1;
  179. *count = ffsll(~(*mask >> *start)) - 1;
  180. *mask &= ~(((((uint64_t)1) << *count) - 1) << *start);
  181. }
  182. /**
  183. * Find last bit set in a word. The least significant bit is 1.
  184. * Return 0 if no bits are set.
  185. * Essentially ffs() in the reverse direction.
  186. */
  187. static inline unsigned
  188. util_last_bit(unsigned u)
  189. {
  190. #if defined(HAVE___BUILTIN_CLZ)
  191. return u == 0 ? 0 : 32 - __builtin_clz(u);
  192. #elif defined(_MSC_VER) && (_M_IX86 || _M_ARM || _M_AMD64 || _M_IA64)
  193. unsigned long index;
  194. if (_BitScanReverse(&index, u))
  195. return index + 1;
  196. else
  197. return 0;
  198. #else
  199. unsigned r = 0;
  200. while (u) {
  201. r++;
  202. u >>= 1;
  203. }
  204. return r;
  205. #endif
  206. }
  207. /**
  208. * Find last bit set in a word. The least significant bit is 1.
  209. * Return 0 if no bits are set.
  210. * Essentially ffsll() in the reverse direction.
  211. */
  212. static inline unsigned
  213. util_last_bit64(uint64_t u)
  214. {
  215. #if defined(HAVE___BUILTIN_CLZLL)
  216. return u == 0 ? 0 : 64 - __builtin_clzll(u);
  217. #elif defined(_MSC_VER) && (_M_AMD64 || _M_ARM64 || _M_IA64)
  218. unsigned long index;
  219. if (_BitScanReverse64(&index, u))
  220. return index + 1;
  221. else
  222. return 0;
  223. #else
  224. unsigned r = 0;
  225. while (u) {
  226. r++;
  227. u >>= 1;
  228. }
  229. return r;
  230. #endif
  231. }
  232. /**
  233. * Find last bit in a word that does not match the sign bit. The least
  234. * significant bit is 1.
  235. * Return 0 if no bits are set.
  236. */
  237. static inline unsigned
  238. util_last_bit_signed(int i)
  239. {
  240. if (i >= 0)
  241. return util_last_bit(i);
  242. else
  243. return util_last_bit(~(unsigned)i);
  244. }
  245. /* Returns a bitfield in which the first count bits starting at start are
  246. * set.
  247. */
  248. static inline unsigned
  249. u_bit_consecutive(unsigned start, unsigned count)
  250. {
  251. assert(start + count <= 32);
  252. if (count == 32)
  253. return ~0;
  254. return ((1u << count) - 1) << start;
  255. }
  256. static inline uint64_t
  257. u_bit_consecutive64(unsigned start, unsigned count)
  258. {
  259. assert(start + count <= 64);
  260. if (count == 64)
  261. return ~(uint64_t)0;
  262. return (((uint64_t)1 << count) - 1) << start;
  263. }
  264. /**
  265. * Return number of bits set in n.
  266. */
  267. static inline unsigned
  268. util_bitcount(unsigned n)
  269. {
  270. #if defined(HAVE___BUILTIN_POPCOUNT)
  271. return __builtin_popcount(n);
  272. #else
  273. /* K&R classic bitcount.
  274. *
  275. * For each iteration, clear the LSB from the bitfield.
  276. * Requires only one iteration per set bit, instead of
  277. * one iteration per bit less than highest set bit.
  278. */
  279. unsigned bits;
  280. for (bits = 0; n; bits++) {
  281. n &= n - 1;
  282. }
  283. return bits;
  284. #endif
  285. }
  286. static inline unsigned
  287. util_bitcount64(uint64_t n)
  288. {
  289. #ifdef HAVE___BUILTIN_POPCOUNTLL
  290. return __builtin_popcountll(n);
  291. #else
  292. return util_bitcount(n) + util_bitcount(n >> 32);
  293. #endif
  294. }
  295. #ifdef __cplusplus
  296. }
  297. #endif
  298. #endif /* BITSCAN_H */