8 #ifndef _STRUS_BASE_BIT_OPERATIONS_HPP_INCLUDED
9 #define _STRUS_BASE_BIT_OPERATIONS_HPP_INCLUDED
18 static inline unsigned int bitCount( uint32_t v)
21 return __builtin_popcount( v);
24 v = v - ((v >> 1) & 0x55555555);
25 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
26 return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
30 static inline unsigned int bitCount( uint64_t x)
33 return __builtin_popcountll( x);
36 const uint64_t m1 = 0x5555555555555555;
37 const uint64_t m2 = 0x3333333333333333;
38 const uint64_t m4 = 0x0f0f0f0f0f0f0f0f;
39 const uint64_t m8 = 0x00ff00ff00ff00ff;
40 const uint64_t m16 = 0x0000ffff0000ffff;
41 const uint64_t m32 = 0x00000000ffffffff;
42 const uint64_t hff = 0xffffffffffffffff;
43 const uint64_t h01 = 0x0101010101010101;
46 x = (x & m2) + ((x >> 2) & m2);
47 x = (x + (x >> 4)) & m4;
57 asm(
" bsr %1, %0 \n" :
"=r"(result) :
"r"(idx) );
63 if ((xx & 0xFFff0000)) { ee +=16; xx >>=16; }
64 if ((xx & 0x0000Ff00)) { ee += 8; xx >>= 8; }
65 if ((xx & 0x000000F0)) { ee += 4; xx >>= 4; }
66 if ((xx & 0x0000000C)) { ee += 2; xx >>= 2; }
67 if ((xx & 0x00000002)) { ee += 1; }
78 asm(
" bsr %1, %0 \n" :
"=r"(result) :
"r"(xx) );
82 if ((xx & 0xFf00)) { ee += 8; xx >>= 8; }
83 if ((xx & 0x00F0)) { ee += 4; xx >>= 4; }
84 if ((xx & 0x000C)) { ee += 2; xx >>= 2; }
85 if ((xx & 0x0002)) { ee += 1; }
96 asm(
" bsr %1, %0 \n" :
"=r"(result) :
"r"(xx) );
100 if ((xx & 0xF0)) { ee += 4; xx >>= 4; }
101 if ((xx & 0x0C)) { ee += 2; xx >>= 2; }
102 if ((xx & 0x02)) { ee += 1; }
112 asm(
" bsf %1, %0 \n" :
"=r"(result) :
"r"(idx) );
124 asm(
" bsfq %1, %0 \n" :
"=r"(result) :
"r"(idx) );
125 return (
unsigned int)(result+1);
126 #elif __LONG_MAX__ == 0x7FffFFff || defined __OpenBSD__
128 uint32_t result_incr = 0;
129 uint32_t idx_lo = idx;
133 idx_lo = (idx >> 32);
135 uint32_t result = ffs( idx_lo);
136 return (
unsigned int)(result+result_incr);
144 uint64_t mask = ((uint64_t)1<<bi)-1;
145 return ((bitset &~ mask) << 1) | ((uint64_t)1<<bi) | (bitset & mask);
static unsigned int bitCount(uint64_t x)
Definition: bitOperations.hpp:30
static unsigned int bitCount(uint32_t v)
Definition: bitOperations.hpp:18
static unsigned int bitScanReverse(const uint32_t &idx)
Definition: bitOperations.hpp:52
static unsigned int bitScanForward(const uint64_t &idx)
Definition: bitOperations.hpp:119
static unsigned int bitScanReverse(const uint8_t &idx)
Definition: bitOperations.hpp:90
Definition: bitset.hpp:31
static unsigned int bitScanForward(const uint32_t &idx)
Definition: bitOperations.hpp:107
static unsigned int bitScanReverse(const uint16_t &idx)
Definition: bitOperations.hpp:72
Definition: bitOperations.hpp:16
static uint64_t bitInsert(const uint64_t &bitset, unsigned int bi)
Definition: bitOperations.hpp:142