PostgreSQL Source Code  git master
pg_bitutils.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define pg_leftmost_one_pos_size_t   pg_leftmost_one_pos64
 
#define pg_nextpower2_size_t   pg_nextpower2_64
 
#define pg_prevpower2_size_t   pg_prevpower2_64
 

Functions

static int pg_leftmost_one_pos32 (uint32 word)
 
static int pg_leftmost_one_pos64 (uint64 word)
 
static int pg_rightmost_one_pos32 (uint32 word)
 
static int pg_rightmost_one_pos64 (uint64 word)
 
static uint32 pg_nextpower2_32 (uint32 num)
 
static uint64 pg_nextpower2_64 (uint64 num)
 
static uint32 pg_prevpower2_32 (uint32 num)
 
static uint64 pg_prevpower2_64 (uint64 num)
 
static uint32 pg_ceil_log2_32 (uint32 num)
 
static uint64 pg_ceil_log2_64 (uint64 num)
 
int pg_popcount32 (uint32 word)
 
int pg_popcount64 (uint64 word)
 
uint64 pg_popcount (const char *buf, int bytes)
 
static uint32 pg_rotate_right32 (uint32 word, int n)
 
static uint32 pg_rotate_left32 (uint32 word, int n)
 

Variables

PGDLLIMPORT const uint8 pg_leftmost_one_pos [256]
 
PGDLLIMPORT const uint8 pg_rightmost_one_pos [256]
 
PGDLLIMPORT const uint8 pg_number_of_ones [256]
 

Macro Definition Documentation

◆ pg_leftmost_one_pos_size_t

#define pg_leftmost_one_pos_size_t   pg_leftmost_one_pos64

Definition at line 334 of file pg_bitutils.h.

◆ pg_nextpower2_size_t

#define pg_nextpower2_size_t   pg_nextpower2_64

Definition at line 335 of file pg_bitutils.h.

◆ pg_prevpower2_size_t

#define pg_prevpower2_size_t   pg_prevpower2_64

Definition at line 336 of file pg_bitutils.h.

Function Documentation

◆ pg_ceil_log2_32()

static uint32 pg_ceil_log2_32 ( uint32  num)
inlinestatic

Definition at line 254 of file pg_bitutils.h.

255 {
256  if (num < 2)
257  return 0;
258  else
259  return pg_leftmost_one_pos32(num - 1) + 1;
260 }
static int pg_leftmost_one_pos32(uint32 word)
Definition: pg_bitutils.h:41

References pg_leftmost_one_pos32().

Referenced by _hash_spareindex(), and my_log2().

◆ pg_ceil_log2_64()

static uint64 pg_ceil_log2_64 ( uint64  num)
inlinestatic

Definition at line 267 of file pg_bitutils.h.

268 {
269  if (num < 2)
270  return 0;
271  else
272  return pg_leftmost_one_pos64(num - 1) + 1;
273 }
static int pg_leftmost_one_pos64(uint64 word)
Definition: pg_bitutils.h:71

References pg_leftmost_one_pos64().

Referenced by GetHugePageSize(), and my_log2().

◆ pg_leftmost_one_pos32()

static int pg_leftmost_one_pos32 ( uint32  word)
inlinestatic

Definition at line 41 of file pg_bitutils.h.

42 {
43 #ifdef HAVE__BUILTIN_CLZ
44  Assert(word != 0);
45 
46  return 31 - __builtin_clz(word);
47 #elif defined(_MSC_VER)
48  unsigned long result;
49  bool non_zero;
50 
51  non_zero = _BitScanReverse(&result, word);
52  Assert(non_zero);
53  return (int) result;
54 #else
55  int shift = 32 - 8;
56 
57  Assert(word != 0);
58 
59  while ((word >> shift) == 0)
60  shift -= 8;
61 
62  return shift + pg_leftmost_one_pos[(word >> shift) & 255];
63 #endif /* HAVE__BUILTIN_CLZ */
64 }
Assert(fmt[strlen(fmt) - 1] !='\n')
PGDLLIMPORT const uint8 pg_leftmost_one_pos[256]
Definition: pg_bitutils.c:34
static void word(struct vars *v, int dir, struct state *lp, struct state *rp)
Definition: regcomp.c:1476

References Assert(), pg_leftmost_one_pos, and word().

Referenced by _hash_get_oldblock_from_newbucket(), _hash_init_metabuffer(), add_paths_to_append_rel(), AllocSetFreeIndex(), decimalLength32(), generate_union_paths(), make_main_region_dsm_handle(), pg_ceil_log2_32(), pg_nextpower2_32(), pg_prevpower2_32(), and rho().

◆ pg_leftmost_one_pos64()

static int pg_leftmost_one_pos64 ( uint64  word)
inlinestatic

Definition at line 71 of file pg_bitutils.h.

72 {
73 #ifdef HAVE__BUILTIN_CLZ
74  Assert(word != 0);
75 
76 #if defined(HAVE_LONG_INT_64)
77  return 63 - __builtin_clzl(word);
78 #elif defined(HAVE_LONG_LONG_INT_64)
79  return 63 - __builtin_clzll(word);
80 #else
81 #error must have a working 64-bit integer datatype
82 #endif /* HAVE_LONG_INT_64 */
83 
84 #elif defined(_MSC_VER)
85  unsigned long result;
86  bool non_zero;
87 
88  non_zero = _BitScanReverse64(&result, word);
89  Assert(non_zero);
90  return (int) result;
91 #else
92  int shift = 64 - 8;
93 
94  Assert(word != 0);
95 
96  while ((word >> shift) == 0)
97  shift -= 8;
98 
99  return shift + pg_leftmost_one_pos[(word >> shift) & 255];
100 #endif /* HAVE__BUILTIN_CLZ */
101 }

References Assert(), pg_leftmost_one_pos, and word().

Referenced by decimalLength64(), permute(), pg_ceil_log2_64(), pg_nextpower2_64(), pg_prevpower2_64(), and pg_prng_uint64_range().

◆ pg_nextpower2_32()

static uint32 pg_nextpower2_32 ( uint32  num)
inlinestatic

Definition at line 185 of file pg_bitutils.h.

186 {
187  Assert(num > 0 && num <= PG_UINT32_MAX / 2 + 1);
188 
189  /*
190  * A power 2 number has only 1 bit set. Subtracting 1 from such a number
191  * will turn on all previous bits resulting in no common bits being set
192  * between num and num-1.
193  */
194  if ((num & (num - 1)) == 0)
195  return num; /* already power 2 */
196 
197  return ((uint32) 1) << (pg_leftmost_one_pos32(num) + 1);
198 }
unsigned int uint32
Definition: c.h:490
#define PG_UINT32_MAX
Definition: c.h:574

References Assert(), pg_leftmost_one_pos32(), and PG_UINT32_MAX.

Referenced by _h_spoolinit(), _hash_init_metabuffer(), accumArrayResultArr(), array_agg_array_combine(), array_agg_combine(), bottomup_sort_and_shrink(), bottomup_sort_and_shrink_cmp(), enlarge_list(), ensure_record_cache_typmod_slot_exists(), ExecChooseHashTableSize(), ExecHashBuildSkewHash(), ExecParallelHashIncreaseNumBatches(), ginHeapTupleFastCollect(), LWLockRegisterTranche(), new_list(), RequestNamedLWLockTranche(), and table_block_parallelscan_startblock_init().

◆ pg_nextpower2_64()

static uint64 pg_nextpower2_64 ( uint64  num)
inlinestatic

Definition at line 208 of file pg_bitutils.h.

209 {
210  Assert(num > 0 && num <= PG_UINT64_MAX / 2 + 1);
211 
212  /*
213  * A power 2 number has only 1 bit set. Subtracting 1 from such a number
214  * will turn on all previous bits resulting in no common bits being set
215  * between num and num-1.
216  */
217  if ((num & (num - 1)) == 0)
218  return num; /* already power 2 */
219 
220  return ((uint64) 1) << (pg_leftmost_one_pos64(num) + 1);
221 }
#define PG_UINT64_MAX
Definition: c.h:577

References Assert(), pg_leftmost_one_pos64(), and PG_UINT64_MAX.

◆ pg_popcount()

uint64 pg_popcount ( const char *  buf,
int  bytes 
)

Definition at line 296 of file pg_bitutils.c.

297 {
298  uint64 popcnt = 0;
299 
300 #if SIZEOF_VOID_P >= 8
301  /* Process in 64-bit chunks if the buffer is aligned. */
302  if (buf == (const char *) TYPEALIGN(8, buf))
303  {
304  const uint64 *words = (const uint64 *) buf;
305 
306  while (bytes >= 8)
307  {
308  popcnt += pg_popcount64(*words++);
309  bytes -= 8;
310  }
311 
312  buf = (const char *) words;
313  }
314 #else
315  /* Process in 32-bit chunks if the buffer is aligned. */
316  if (buf == (const char *) TYPEALIGN(4, buf))
317  {
318  const uint32 *words = (const uint32 *) buf;
319 
320  while (bytes >= 4)
321  {
322  popcnt += pg_popcount32(*words++);
323  bytes -= 4;
324  }
325 
326  buf = (const char *) words;
327  }
328 #endif
329 
330  /* Process any remaining bytes */
331  while (bytes--)
332  popcnt += pg_number_of_ones[(unsigned char) *buf++];
333 
334  return popcnt;
335 }
#define TYPEALIGN(ALIGNVAL, LEN)
Definition: c.h:788
const uint8 pg_number_of_ones[256]
Definition: pg_bitutils.c:87
int pg_popcount64(uint64 word)
Definition: pg_bitutils.c:284
int pg_popcount32(uint32 word)
Definition: pg_bitutils.c:278
static char * buf
Definition: pg_test_fsync.c:67

References buf, pg_number_of_ones, pg_popcount32(), pg_popcount64(), and TYPEALIGN.

Referenced by bit_bit_count(), bloom_prop_bits_set(), bytea_bit_count(), GetWALBlockInfo(), heap_tuple_infomask_flags(), process_pipe_input(), and sizebitvec().

◆ pg_popcount32()

int pg_popcount32 ( uint32  word)

Definition at line 278 of file pg_bitutils.c.

279 {
280  return pg_popcount32_slow(word);
281 }
static int pg_popcount32_slow(uint32 word)
Definition: pg_bitutils.c:223

References pg_popcount32_slow(), and word().

Referenced by pg_popcount(), and plan_single_revoke().

◆ pg_popcount64()

int pg_popcount64 ( uint64  word)

Definition at line 284 of file pg_bitutils.c.

285 {
286  return pg_popcount64_slow(word);
287 }
static int pg_popcount64_slow(uint64 word)
Definition: pg_bitutils.c:245

References pg_popcount64_slow(), and word().

Referenced by pg_popcount(), and visibilitymap_count().

◆ pg_prevpower2_32()

static uint32 pg_prevpower2_32 ( uint32  num)
inlinestatic

Definition at line 231 of file pg_bitutils.h.

232 {
233  return ((uint32) 1) << pg_leftmost_one_pos32(num);
234 }

References pg_leftmost_one_pos32().

◆ pg_prevpower2_64()

static uint64 pg_prevpower2_64 ( uint64  num)
inlinestatic

Definition at line 244 of file pg_bitutils.h.

245 {
246  return ((uint64) 1) << pg_leftmost_one_pos64(num);
247 }

References pg_leftmost_one_pos64().

◆ pg_rightmost_one_pos32()

static int pg_rightmost_one_pos32 ( uint32  word)
inlinestatic

Definition at line 109 of file pg_bitutils.h.

110 {
111 #ifdef HAVE__BUILTIN_CTZ
112  Assert(word != 0);
113 
114  return __builtin_ctz(word);
115 #elif defined(_MSC_VER)
116  unsigned long result;
117  bool non_zero;
118 
119  non_zero = _BitScanForward(&result, word);
120  Assert(non_zero);
121  return (int) result;
122 #else
123  int result = 0;
124 
125  Assert(word != 0);
126 
127  while ((word & 255) == 0)
128  {
129  word >>= 8;
130  result += 8;
131  }
132  result += pg_rightmost_one_pos[word & 255];
133  return result;
134 #endif /* HAVE__BUILTIN_CTZ */
135 }
PGDLLIMPORT const uint8 pg_rightmost_one_pos[256]
Definition: pg_bitutils.c:62

References Assert(), pg_rightmost_one_pos, and word().

Referenced by ProcessProcSignalBarrier().

◆ pg_rightmost_one_pos64()

static int pg_rightmost_one_pos64 ( uint64  word)
inlinestatic

Definition at line 142 of file pg_bitutils.h.

143 {
144 #ifdef HAVE__BUILTIN_CTZ
145  Assert(word != 0);
146 
147 #if defined(HAVE_LONG_INT_64)
148  return __builtin_ctzl(word);
149 #elif defined(HAVE_LONG_LONG_INT_64)
150  return __builtin_ctzll(word);
151 #else
152 #error must have a working 64-bit integer datatype
153 #endif /* HAVE_LONG_INT_64 */
154 
155 #elif defined(_MSC_VER)
156  unsigned long result;
157  bool non_zero;
158 
159  non_zero = _BitScanForward64(&result, word);
160  Assert(non_zero);
161  return (int) result;
162 #else
163  int result = 0;
164 
165  Assert(word != 0);
166 
167  while ((word & 255) == 0)
168  {
169  word >>= 8;
170  result += 8;
171  }
172  result += pg_rightmost_one_pos[word & 255];
173  return result;
174 #endif /* HAVE__BUILTIN_CTZ */
175 }

References Assert(), pg_rightmost_one_pos, and word().

◆ pg_rotate_left32()

static uint32 pg_rotate_left32 ( uint32  word,
int  n 
)
inlinestatic

Definition at line 322 of file pg_bitutils.h.

323 {
324  return (word << n) | (word >> (32 - n));
325 }

References word().

Referenced by CatalogCacheComputeHashValue(), ExecHashGetHashValue(), guc_name_hash(), hash_multirange(), hash_range(), JsonbHashScalarValue(), MemoizeHash_hash(), and TupleHashTableHash_internal().

◆ pg_rotate_right32()

static uint32 pg_rotate_right32 ( uint32  word,
int  n 
)
inlinestatic

Definition at line 316 of file pg_bitutils.h.

317 {
318  return (word >> n) | (word << (32 - n));
319 }

References word().

Referenced by ExecHashGetBucketAndBatch().

Variable Documentation

◆ pg_leftmost_one_pos

PGDLLIMPORT const uint8 pg_leftmost_one_pos[256]
extern

Definition at line 34 of file pg_bitutils.c.

Referenced by AllocSetFreeIndex(), pg_leftmost_one_pos32(), and pg_leftmost_one_pos64().

◆ pg_number_of_ones

PGDLLIMPORT const uint8 pg_number_of_ones[256]
extern

Definition at line 87 of file pg_bitutils.c.

Referenced by hemdistsign(), pg_popcount(), pg_popcount32_slow(), and pg_popcount64_slow().

◆ pg_rightmost_one_pos

PGDLLIMPORT const uint8 pg_rightmost_one_pos[256]
extern

Definition at line 62 of file pg_bitutils.c.

Referenced by pg_rightmost_one_pos32(), and pg_rightmost_one_pos64().