aboutsummaryrefslogtreecommitdiff
path: root/src/port/pg_bitutils_hwpopcnt.c
blob: 516efd586ddda622c0b2739d126776fe251c87b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*-------------------------------------------------------------------------
 *
 * pg_bitutils_hwpopcnt.c
 *	  CPU-optimized implementation of pg_popcount variants
 *
 * This file must be compiled with a compiler-specific flag to enable the
 * POPCNT instruction.
 *
 * Portions Copyright (c) 2019, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *	  src/port/pg_bitutils_hwpopcnt.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "port/pg_bitutils.h"

int
pg_popcount32_hw(uint32 word)
{
	return __builtin_popcount(word);
}

int
pg_popcount64_hw(uint64 word)
{
#if defined(HAVE_LONG_INT_64)
	return __builtin_popcountl(word);
#elif defined(HAVE_LONG_LONG_INT_64)
	return __builtin_popcountll(word);
#else
#error must have a working 64-bit integer datatype
#endif
}