/**************************************************************************1*2* Copyright 2008 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/262728#ifndef BITSCAN_H29#define BITSCAN_H3031#include <assert.h>32#include <stdint.h>33#include <stdbool.h>34#include <string.h>3536#if defined(_MSC_VER)37#include <intrin.h>38#endif3940#if defined(__POPCNT__)41#include <popcntintrin.h>42#endif4344#include "c99_compat.h"4546#ifdef __cplusplus47extern "C" {48#endif495051/**52* Find first bit set in word. Least significant bit is 1.53* Return 0 if no bits set.54*/55#ifdef HAVE___BUILTIN_FFS56#define ffs __builtin_ffs57#elif defined(_MSC_VER) && (_M_IX86 || _M_ARM || _M_AMD64 || _M_IA64)58static inline59int ffs(int i)60{61unsigned long index;62if (_BitScanForward(&index, i))63return index + 1;64else65return 0;66}67#else68extern69int ffs(int i);70#endif7172#ifdef HAVE___BUILTIN_FFSLL73#define ffsll __builtin_ffsll74#elif defined(_MSC_VER) && (_M_AMD64 || _M_ARM64 || _M_IA64)75static inline int76ffsll(long long int i)77{78unsigned long index;79if (_BitScanForward64(&index, i))80return index + 1;81else82return 0;83}84#else85extern int86ffsll(long long int val);87#endif888990/* Destructively loop over all of the bits in a mask as in:91*92* while (mymask) {93* int i = u_bit_scan(&mymask);94* ... process element i95* }96*97*/98static inline int99u_bit_scan(unsigned *mask)100{101const int i = ffs(*mask) - 1;102*mask ^= (1u << i);103return i;104}105106#define u_foreach_bit(b, dword) \107for (uint32_t __dword = (dword), b; \108((b) = ffs(__dword) - 1, __dword); \109__dword &= ~(1 << (b)))110111static inline int112u_bit_scan64(uint64_t *mask)113{114const int i = ffsll(*mask) - 1;115*mask ^= (((uint64_t)1) << i);116return i;117}118119#define u_foreach_bit64(b, dword) \120for (uint64_t __dword = (dword), b; \121((b) = ffsll(__dword) - 1, __dword); \122__dword &= ~(1ull << (b)))123124/* Determine if an unsigned value is a power of two.125*126* \note127* Zero is treated as a power of two.128*/129static inline bool130util_is_power_of_two_or_zero(unsigned v)131{132return (v & (v - 1)) == 0;133}134135/* Determine if an uint64_t value is a power of two.136*137* \note138* Zero is treated as a power of two.139*/140static inline bool141util_is_power_of_two_or_zero64(uint64_t v)142{143return (v & (v - 1)) == 0;144}145146/* Determine if an unsigned value is a power of two.147*148* \note149* Zero is \b not treated as a power of two.150*/151static inline bool152util_is_power_of_two_nonzero(unsigned v)153{154/* __POPCNT__ is different from HAVE___BUILTIN_POPCOUNT. The latter155* indicates the existence of the __builtin_popcount function. The former156* indicates that _mm_popcnt_u32 exists and is a native instruction.157*158* The other alternative is to use SSE 4.2 compile-time flags. This has159* two drawbacks. First, there is currently no build infrastructure for160* SSE 4.2 (only 4.1), so that would have to be added. Second, some AMD161* CPUs support POPCNT but not SSE 4.2 (e.g., Barcelona).162*/163#ifdef __POPCNT__164return _mm_popcnt_u32(v) == 1;165#else166return v != 0 && (v & (v - 1)) == 0;167#endif168}169170/* For looping over a bitmask when you want to loop over consecutive bits171* manually, for example:172*173* while (mask) {174* int start, count, i;175*176* u_bit_scan_consecutive_range(&mask, &start, &count);177*178* for (i = 0; i < count; i++)179* ... process element (start+i)180* }181*/182static inline void183u_bit_scan_consecutive_range(unsigned *mask, int *start, int *count)184{185if (*mask == 0xffffffff) {186*start = 0;187*count = 32;188*mask = 0;189return;190}191*start = ffs(*mask) - 1;192*count = ffs(~(*mask >> *start)) - 1;193*mask &= ~(((1u << *count) - 1) << *start);194}195196static inline void197u_bit_scan_consecutive_range64(uint64_t *mask, int *start, int *count)198{199if (*mask == ~0ull) {200*start = 0;201*count = 64;202*mask = 0;203return;204}205*start = ffsll(*mask) - 1;206*count = ffsll(~(*mask >> *start)) - 1;207*mask &= ~(((((uint64_t)1) << *count) - 1) << *start);208}209210211/**212* Find last bit set in a word. The least significant bit is 1.213* Return 0 if no bits are set.214* Essentially ffs() in the reverse direction.215*/216static inline unsigned217util_last_bit(unsigned u)218{219#if defined(HAVE___BUILTIN_CLZ)220return u == 0 ? 0 : 32 - __builtin_clz(u);221#elif defined(_MSC_VER) && (_M_IX86 || _M_ARM || _M_AMD64 || _M_IA64)222unsigned long index;223if (_BitScanReverse(&index, u))224return index + 1;225else226return 0;227#else228unsigned r = 0;229while (u) {230r++;231u >>= 1;232}233return r;234#endif235}236237/**238* Find last bit set in a word. The least significant bit is 1.239* Return 0 if no bits are set.240* Essentially ffsll() in the reverse direction.241*/242static inline unsigned243util_last_bit64(uint64_t u)244{245#if defined(HAVE___BUILTIN_CLZLL)246return u == 0 ? 0 : 64 - __builtin_clzll(u);247#elif defined(_MSC_VER) && (_M_AMD64 || _M_ARM64 || _M_IA64)248unsigned long index;249if (_BitScanReverse64(&index, u))250return index + 1;251else252return 0;253#else254unsigned r = 0;255while (u) {256r++;257u >>= 1;258}259return r;260#endif261}262263/**264* Find last bit in a word that does not match the sign bit. The least265* significant bit is 1.266* Return 0 if no bits are set.267*/268static inline unsigned269util_last_bit_signed(int i)270{271if (i >= 0)272return util_last_bit(i);273else274return util_last_bit(~(unsigned)i);275}276277/* Returns a bitfield in which the first count bits starting at start are278* set.279*/280static inline unsigned281u_bit_consecutive(unsigned start, unsigned count)282{283assert(start + count <= 32);284if (count == 32)285return ~0;286return ((1u << count) - 1) << start;287}288289static inline uint64_t290u_bit_consecutive64(unsigned start, unsigned count)291{292assert(start + count <= 64);293if (count == 64)294return ~(uint64_t)0;295return (((uint64_t)1 << count) - 1) << start;296}297298/**299* Return number of bits set in n.300*/301static inline unsigned302util_bitcount(unsigned n)303{304#if defined(HAVE___BUILTIN_POPCOUNT)305return __builtin_popcount(n);306#else307/* K&R classic bitcount.308*309* For each iteration, clear the LSB from the bitfield.310* Requires only one iteration per set bit, instead of311* one iteration per bit less than highest set bit.312*/313unsigned bits;314for (bits = 0; n; bits++) {315n &= n - 1;316}317return bits;318#endif319}320321static inline unsigned322util_bitcount64(uint64_t n)323{324#ifdef HAVE___BUILTIN_POPCOUNTLL325return __builtin_popcountll(n);326#else327return util_bitcount(n) + util_bitcount(n >> 32);328#endif329}330331#ifdef __cplusplus332}333#endif334335#endif /* BITSCAN_H */336337338