1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
41 #if defined(__POPCNT__)
42 #include <popcntintrin.h>
45 #include "c99_compat.h"
53 * Find first bit set in word. Least significant bit is 1.
54 * Return 0 if no bits set.
56 #ifdef HAVE___BUILTIN_FFS
57 #define ffs __builtin_ffs
58 #elif defined(_MSC_VER) && (_M_IX86 || _M_ARM || _M_AMD64 || _M_IA64)
63 if (_BitScanForward(&index
, i
))
73 #ifdef HAVE___BUILTIN_FFSLL
74 #define ffsll __builtin_ffsll
75 #elif defined(_MSC_VER) && (_M_AMD64 || _M_ARM || _M_IA64)
77 ffsll(long long int i
)
80 if (_BitScanForward64(&index
, i
))
87 ffsll(long long int val
);
91 /* Destructively loop over all of the bits in a mask as in:
94 * int i = u_bit_scan(&mymask);
95 * ... process element i
100 u_bit_scan(unsigned *mask
)
102 const int i
= ffs(*mask
) - 1;
108 u_bit_scan64(uint64_t *mask
)
110 const int i
= ffsll(*mask
) - 1;
111 *mask
^= (((uint64_t)1) << i
);
115 /* Determine if an unsigned value is a power of two.
118 * Zero is treated as a power of two.
121 util_is_power_of_two_or_zero(unsigned v
)
123 return (v
& (v
- 1)) == 0;
126 /* Determine if an unsigned value is a power of two.
129 * Zero is \b not treated as a power of two.
132 util_is_power_of_two_nonzero(unsigned v
)
134 /* __POPCNT__ is different from HAVE___BUILTIN_POPCOUNT. The latter
135 * indicates the existence of the __builtin_popcount function. The former
136 * indicates that _mm_popcnt_u32 exists and is a native instruction.
138 * The other alternative is to use SSE 4.2 compile-time flags. This has
139 * two drawbacks. First, there is currently no build infrastructure for
140 * SSE 4.2 (only 4.1), so that would have to be added. Second, some AMD
141 * CPUs support POPCNT but not SSE 4.2 (e.g., Barcelona).
144 return _mm_popcnt_u32(v
) == 1;
146 return v
!= 0 && (v
& (v
- 1)) == 0;
150 /* For looping over a bitmask when you want to loop over consecutive bits
151 * manually, for example:
154 * int start, count, i;
156 * u_bit_scan_consecutive_range(&mask, &start, &count);
158 * for (i = 0; i < count; i++)
159 * ... process element (start+i)
163 u_bit_scan_consecutive_range(unsigned *mask
, int *start
, int *count
)
165 if (*mask
== 0xffffffff) {
171 *start
= ffs(*mask
) - 1;
172 *count
= ffs(~(*mask
>> *start
)) - 1;
173 *mask
&= ~(((1u << *count
) - 1) << *start
);
177 u_bit_scan_consecutive_range64(uint64_t *mask
, int *start
, int *count
)
179 if (*mask
== ~0ull) {
185 *start
= ffsll(*mask
) - 1;
186 *count
= ffsll(~(*mask
>> *start
)) - 1;
187 *mask
&= ~(((((uint64_t)1) << *count
) - 1) << *start
);
192 * Find last bit set in a word. The least significant bit is 1.
193 * Return 0 if no bits are set.
194 * Essentially ffs() in the reverse direction.
196 static inline unsigned
197 util_last_bit(unsigned u
)
199 #if defined(HAVE___BUILTIN_CLZ)
200 return u
== 0 ? 0 : 32 - __builtin_clz(u
);
201 #elif defined(_MSC_VER) && (_M_IX86 || _M_ARM || _M_AMD64 || _M_IA64)
203 if (_BitScanReverse(&index
, u
))
218 * Find last bit set in a word. The least significant bit is 1.
219 * Return 0 if no bits are set.
220 * Essentially ffsll() in the reverse direction.
222 static inline unsigned
223 util_last_bit64(uint64_t u
)
225 #if defined(HAVE___BUILTIN_CLZLL)
226 return u
== 0 ? 0 : 64 - __builtin_clzll(u
);
227 #elif defined(_MSC_VER) && (_M_AMD64 || _M_ARM || _M_IA64)
229 if (_BitScanReverse64(&index
, u
))
244 * Find last bit in a word that does not match the sign bit. The least
245 * significant bit is 1.
246 * Return 0 if no bits are set.
248 static inline unsigned
249 util_last_bit_signed(int i
)
252 return util_last_bit(i
);
254 return util_last_bit(~(unsigned)i
);
257 /* Returns a bitfield in which the first count bits starting at start are
260 static inline unsigned
261 u_bit_consecutive(unsigned start
, unsigned count
)
263 assert(start
+ count
<= 32);
266 return ((1u << count
) - 1) << start
;
269 static inline uint64_t
270 u_bit_consecutive64(unsigned start
, unsigned count
)
272 assert(start
+ count
<= 64);
275 return (((uint64_t)1 << count
) - 1) << start
;
283 #endif /* BITSCAN_H */