PostgreSQL Source Code  git master
pg_bitutils.c File Reference
#include "c.h"
#include "port/pg_bitutils.h"
Include dependency graph for pg_bitutils.c:

Go to the source code of this file.

Functions

static int pg_popcount32_slow (uint32 word)
 
static int pg_popcount64_slow (uint64 word)
 
uint64 pg_popcount (const char *buf, int bytes)
 

Variables

const uint8 pg_leftmost_one_pos [256]
 
const uint8 pg_rightmost_one_pos [256]
 
const uint8 pg_number_of_ones [256]
 
int(* pg_popcount32 )(uint32 word) = pg_popcount32_slow
 
int(* pg_popcount64 )(uint64 word) = pg_popcount64_slow
 

Function Documentation

◆ pg_popcount()

uint64 pg_popcount ( const char *  buf,
int  bytes 
)

Definition at line 282 of file pg_bitutils.c.

References pg_number_of_ones, pg_popcount32, pg_popcount64, and TYPEALIGN.

Referenced by bloom_prop_bits_set(), heap_tuple_infomask_flags(), pg_rightmost_one_pos64(), and sizebitvec().

283 {
284  uint64 popcnt = 0;
285 
286 #if SIZEOF_VOID_P >= 8
287  /* Process in 64-bit chunks if the buffer is aligned. */
288  if (buf == (const char *) TYPEALIGN(8, buf))
289  {
290  const uint64 *words = (const uint64 *) buf;
291 
292  while (bytes >= 8)
293  {
294  popcnt += pg_popcount64(*words++);
295  bytes -= 8;
296  }
297 
298  buf = (const char *) words;
299  }
300 #else
301  /* Process in 32-bit chunks if the buffer is aligned. */
302  if (buf == (const char *) TYPEALIGN(4, buf))
303  {
304  const uint32 *words = (const uint32 *) buf;
305 
306  while (bytes >= 4)
307  {
308  popcnt += pg_popcount32(*words++);
309  bytes -= 4;
310  }
311 
312  buf = (const char *) words;
313  }
314 #endif
315 
316  /* Process any remaining bytes */
317  while (bytes--)
318  popcnt += pg_number_of_ones[(unsigned char) *buf++];
319 
320  return popcnt;
321 }
int(* pg_popcount32)(uint32 word)
Definition: pg_bitutils.c:132
def bytes(source, encoding='ascii', errors='strict')
int(* pg_popcount64)(uint64 word)
Definition: pg_bitutils.c:133
static char * buf
Definition: pg_test_fsync.c:68
unsigned int uint32
Definition: c.h:358
const uint8 pg_number_of_ones[256]
Definition: pg_bitutils.c:87
#define TYPEALIGN(ALIGNVAL, LEN)
Definition: c.h:678

◆ pg_popcount32_slow()

static int pg_popcount32_slow ( uint32  word)
static

Definition at line 231 of file pg_bitutils.c.

References pg_number_of_ones.

232 {
233 #ifdef HAVE__BUILTIN_POPCOUNT
234  return __builtin_popcount(word);
235 #else /* !HAVE__BUILTIN_POPCOUNT */
236  int result = 0;
237 
238  while (word != 0)
239  {
240  result += pg_number_of_ones[word & 255];
241  word >>= 8;
242  }
243 
244  return result;
245 #endif /* HAVE__BUILTIN_POPCOUNT */
246 }
const uint8 pg_number_of_ones[256]
Definition: pg_bitutils.c:87
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1246

◆ pg_popcount64_slow()

static int pg_popcount64_slow ( uint64  word)
static

Definition at line 253 of file pg_bitutils.c.

References pg_number_of_ones.

254 {
255 #ifdef HAVE__BUILTIN_POPCOUNT
256 #if defined(HAVE_LONG_INT_64)
257  return __builtin_popcountl(word);
258 #elif defined(HAVE_LONG_LONG_INT_64)
259  return __builtin_popcountll(word);
260 #else
261 #error must have a working 64-bit integer datatype
262 #endif
263 #else /* !HAVE__BUILTIN_POPCOUNT */
264  int result = 0;
265 
266  while (word != 0)
267  {
268  result += pg_number_of_ones[word & 255];
269  word >>= 8;
270  }
271 
272  return result;
273 #endif /* HAVE__BUILTIN_POPCOUNT */
274 }
const uint8 pg_number_of_ones[256]
Definition: pg_bitutils.c:87
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1246

Variable Documentation

◆ pg_leftmost_one_pos

const uint8 pg_leftmost_one_pos[256]
Initial value:
= {
0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 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 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
}

Definition at line 87 of file pg_bitutils.c.

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

◆ pg_popcount32

int(* pg_popcount32) (uint32 word) = pg_popcount32_slow

Definition at line 132 of file pg_bitutils.c.

Referenced by pg_popcount(), and pg_rightmost_one_pos64().

◆ pg_popcount64

int(* pg_popcount64) (uint64 word) = pg_popcount64_slow

Definition at line 133 of file pg_bitutils.c.

Referenced by pg_popcount(), pg_rightmost_one_pos64(), and visibilitymap_count().

◆ pg_rightmost_one_pos

const uint8 pg_rightmost_one_pos[256]
Initial value:
= {
0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
}

Definition at line 62 of file pg_bitutils.c.

Referenced by pg_rightmost_one_pos32(), and pg_rightmost_one_pos64().