|
10 | 10 | *
|
11 | 11 | *------------------------------------------------------------------------ -
|
12 | 12 | */
|
13 |
| - |
14 | 13 | #ifndef PG_BITUTILS_H
|
15 | 14 | #define PG_BITUTILS_H
|
16 | 15 |
|
17 |
| -extern int (*pg_popcount32) (uint32 word); |
18 |
| -extern int (*pg_popcount64) (uint64 word); |
19 |
| -extern int (*pg_rightmost_one32) (uint32 word); |
20 |
| -extern int (*pg_rightmost_one64) (uint64 word); |
21 |
| -extern int (*pg_leftmost_one32) (uint32 word); |
22 |
| -extern int (*pg_leftmost_one64) (uint64 word); |
23 |
| - |
| 16 | +extern int (*pg_popcount32) (uint32 word); |
| 17 | +extern int (*pg_popcount64) (uint64 word); |
24 | 18 | extern uint64 pg_popcount(const char *buf, int bytes);
|
25 | 19 |
|
| 20 | +/* in pg_bitutils_hwpopcnt.c */ |
| 21 | +extern int pg_popcount32_hw(uint32 word); |
| 22 | +extern int pg_popcount64_hw(uint64 word); |
| 23 | + |
| 24 | + |
| 25 | +#ifndef HAVE__BUILTIN_CTZ |
| 26 | +/* |
| 27 | + * Array marking the position of the right-most set bit for each value of |
| 28 | + * 1-255. We count the right-most position as the 0th bit, and the |
| 29 | + * left-most the 7th bit. The 0th index of the array must not be used. |
| 30 | + */ |
| 31 | +static const uint8 rightmost_one_pos[256] = { |
| 32 | + 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 33 | + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 34 | + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 35 | + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 36 | + 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 37 | + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 38 | + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 39 | + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 40 | + 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 41 | + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 42 | + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 43 | + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 44 | + 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 45 | + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 46 | + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 47 | + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 |
| 48 | +}; |
| 49 | +#endif /* !HAVE__BUILTIN_CTZ */ |
| 50 | + |
| 51 | +/* |
| 52 | + * pg_rightmost_one32 |
| 53 | + * Returns the number of trailing 0-bits in word, starting at the least |
| 54 | + * significant bit position. word must not be 0. |
| 55 | + */ |
| 56 | +static inline int |
| 57 | +pg_rightmost_one32(uint32 word) |
| 58 | +{ |
| 59 | + int result = 0; |
| 60 | + |
| 61 | + Assert(word != 0); |
| 62 | + |
| 63 | +#ifdef HAVE__BUILTIN_CTZ |
| 64 | + result = __builtin_ctz(word); |
| 65 | +#else |
| 66 | + while ((word & 255) == 0) |
| 67 | + { |
| 68 | + word >>= 8; |
| 69 | + result += 8; |
| 70 | + } |
| 71 | + result += rightmost_one_pos[word & 255]; |
| 72 | +#endif /* HAVE__BUILTIN_CTZ */ |
| 73 | + |
| 74 | + return result; |
| 75 | +} |
| 76 | + |
| 77 | +/* |
| 78 | + * pg_rightmost_one64 |
| 79 | + * Returns the number of trailing 0-bits in word, starting at the least |
| 80 | + * significant bit position. word must not be 0. |
| 81 | + */ |
| 82 | +static inline int |
| 83 | +pg_rightmost_one64(uint64 word) |
| 84 | +{ |
| 85 | + int result = 0; |
| 86 | + |
| 87 | + Assert(word != 0); |
| 88 | +#ifdef HAVE__BUILTIN_CTZ |
| 89 | +#if defined(HAVE_LONG_INT_64) |
| 90 | + return __builtin_ctzl(word); |
| 91 | +#elif defined(HAVE_LONG_LONG_INT_64) |
| 92 | + return __builtin_ctzll(word); |
| 93 | +#else |
| 94 | +#error must have a working 64-bit integer datatype |
| 95 | +#endif |
| 96 | +#else /* HAVE__BUILTIN_CTZ */ |
| 97 | + while ((word & 255) == 0) |
| 98 | + { |
| 99 | + word >>= 8; |
| 100 | + result += 8; |
| 101 | + } |
| 102 | + result += rightmost_one_pos[word & 255]; |
| 103 | +#endif |
| 104 | + |
| 105 | + return result; |
| 106 | +} |
| 107 | + |
| 108 | +#ifndef HAVE__BUILTIN_CLZ |
| 109 | +/* |
| 110 | + * Array marking the position of the left-most set bit for each value of |
| 111 | + * 1-255. We count the right-most position as the 0th bit, and the |
| 112 | + * left-most the 7th bit. The 0th index of the array must not be used. |
| 113 | + */ |
| 114 | +static const uint8 leftmost_one_pos[256] = { |
| 115 | + 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, |
| 116 | + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 117 | + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 118 | + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 119 | + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 120 | + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 121 | + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 122 | + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 123 | + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 124 | + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 125 | + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 126 | + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 127 | + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 128 | + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 129 | + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 130 | + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 |
| 131 | +}; |
| 132 | +#endif /* !HAVE_BUILTIN_CLZ */ |
| 133 | + |
| 134 | +/* |
| 135 | + * pg_leftmost_one32 |
| 136 | + * Returns the 0-based position of the most significant set bit in word |
| 137 | + * measured from the least significant bit. word must not be 0. |
| 138 | + */ |
| 139 | +static inline int |
| 140 | +pg_leftmost_one32(uint32 word) |
| 141 | +{ |
| 142 | +#ifdef HAVE__BUILTIN_CLZ |
| 143 | + Assert(word != 0); |
| 144 | + |
| 145 | + return 31 - __builtin_clz(word); |
| 146 | +#else |
| 147 | + int shift = 32 - 8; |
| 148 | + |
| 149 | + Assert(word != 0); |
| 150 | + |
| 151 | + while ((word >> shift) == 0) |
| 152 | + shift -= 8; |
| 153 | + |
| 154 | + return shift + leftmost_one_pos[(word >> shift) & 255]; |
| 155 | +#endif /* HAVE__BUILTIN_CLZ */ |
| 156 | +} |
| 157 | + |
| 158 | +/* |
| 159 | + * pg_leftmost_one64 |
| 160 | + * Returns the 0-based position of the most significant set bit in word |
| 161 | + * measured from the least significant bit. word must not be 0. |
| 162 | + */ |
| 163 | +static inline int |
| 164 | +pg_leftmost_one64(uint64 word) |
| 165 | +{ |
| 166 | +#ifdef HAVE__BUILTIN_CLZ |
| 167 | + Assert(word != 0); |
| 168 | +#if defined(HAVE_LONG_INT_64) |
| 169 | + return 63 - __builtin_clzl(word); |
| 170 | +#elif defined(HAVE_LONG_LONG_INT_64) |
| 171 | + return 63 - __builtin_clzll(word); |
| 172 | +#else |
| 173 | +#error must have a working 64-bit integer datatype |
| 174 | +#endif |
| 175 | +#else /* HAVE__BUILTIN_CLZ */ |
| 176 | + int shift = 64 - 8; |
| 177 | + |
| 178 | + Assert(word != 0); |
| 179 | + while ((word >> shift) == 0) |
| 180 | + shift -= 8; |
| 181 | + |
| 182 | + return shift + leftmost_one_pos[(word >> shift) & 255]; |
| 183 | +#endif /* !HAVE__BUIILTIN_CLZ */ |
| 184 | +} |
| 185 | + |
26 | 186 | #endif /* PG_BITUTILS_H */
|
0 commit comments