-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopcount.c
245 lines (202 loc) · 7.13 KB
/
popcount.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "utils/varbit.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(popcount);
PG_FUNCTION_INFO_V1(popcount32);
PG_FUNCTION_INFO_V1(popcount64);
PG_FUNCTION_INFO_V1(popcountAsm);
PG_FUNCTION_INFO_V1(popcountAsm64);
PG_FUNCTION_INFO_V1(popcount256);
static const uint64_t m1 = 0x5555555555555555; // 0b0101...
static const uint64_t m2 = 0x3333333333333333; // 0b00110011...
static const uint64_t m4 = 0x0f0f0f0f0f0f0f0f; // 0b00001111...
static const uint64_t h01 = 0x0101010101010101; // the sum of 256 to the power of 0, 1, 2, 3...
/**
* Bit count for every 8bit decimal number
**/
static const int bitcount[256] = {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
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,
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,
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
};
/**
* Hamming weight algorithm using 12 arithmetic operations.
* 32bit version of the algorithm.
**/
static int hamming_weight_32bit(uint32_t val) {
val -= ((val >> 1) & 0x55555555);
val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
val = (val + (val >> 4)) & 0x0f0f0f0f;
return (val * 0x01010101) >> 24;
}
/**
* Hamming weight algorithm using 12 arithmetic operations.
* 64bit version of the algorithm.
**/
static int hamming_weight_64bit(uint64_t val)
{
val -= (val >> 1) & m1;
val = (val & m2) + ((val >> 2) & m2);
val = (val + (val >> 4)) & m4;
return (val * h01) >> 56;
}
/**
* Cache lookup algorithm for counting bits set.
**/
Datum
popcount(PG_FUNCTION_ARGS) {
VarBit *a = PG_GETARG_VARBIT_P(0);
unsigned char *pointer = VARBITS(a);
int length = VARBITBYTES(a);
int count = 0;
while (length-- > 0) {
count += bitcount[(int) *pointer++];
}
PG_RETURN_INT32(count);
}
/**
* 32bit Hamming weight / popcount algorithm for counting bits set.
* Requires additional aligning logic for the last 32bit trunk.
**/
Datum
popcount32(PG_FUNCTION_ARGS) {
VarBit *a = PG_GETARG_VARBIT_P(0);
int count = 0;
int length = VARBITBYTES(a);
unsigned char *byte_pointer = VARBITS(a);
uint32_t *position = (uint32_t *) byte_pointer;
uint32_t remainder = 0x0;
for (; length >= 4; length -= 4) {
count += hamming_weight_32bit(*position++);
}
if (length == 0) PG_RETURN_INT32(count);
// special case, non-32bit-aligned varbit length
byte_pointer = (unsigned char *) position;
memcpy((void *) &remainder, (void *) position, length);
count += hamming_weight_32bit(remainder);
PG_RETURN_INT32(count);
}
/**
* 64bit Hamming weight / popcount algorithm for counting bits set.
* Requires additional aligning logic for the last 64bit trunk.
**/
Datum
popcount64(PG_FUNCTION_ARGS) {
VarBit *a = PG_GETARG_VARBIT_P(0);
int count = 0;
int length = VARBITBYTES(a);
unsigned char *byte_pointer = VARBITS(a);
uint64_t *position = (uint64_t *) byte_pointer;
uint64_t remainder = 0x0;
for (; length >= 8; length -= 8) {
count += hamming_weight_64bit(*position++);
}
if (length == 0) PG_RETURN_INT32(count);
// special case, non-64bit-aligned varbit length
byte_pointer = (unsigned char *) position;
memcpy((void *) &remainder, (void *) position, length);
count += hamming_weight_64bit(remainder);
PG_RETURN_INT32(count);
}
/**
* 32bit Hamming weight / popcount algorithm for counting bits set.
* Requires additional aligning logic for the last 32bit trunk.
**/
Datum
popcountAsm(PG_FUNCTION_ARGS) {
VarBit *a = PG_GETARG_VARBIT_P(0);
int count = 0;
int length = VARBITBYTES(a);
unsigned char *byte_pointer = VARBITS(a);
unsigned int *position = (unsigned int *) byte_pointer;
unsigned int remainder = 0x0;
for (; length >= 4; length -= 4) {
count += __builtin_popcount(*position++);
}
if (length == 0) PG_RETURN_INT32(count);
// special case, non-32bit-aligned varbit length
byte_pointer = (unsigned char *) position;
memcpy((void *) &remainder, (void *) position, length);
count += __builtin_popcount(remainder);
PG_RETURN_INT32(count);
}
/**
* 64bit Hamming weight / popcount algorithm for counting bits set.
* Requires additional aligning logic for the last 64bit trunk.
**/
Datum
popcountAsm64(PG_FUNCTION_ARGS) {
VarBit *a = PG_GETARG_VARBIT_P(0);
int count = 0;
int length = VARBITBYTES(a);
unsigned char *byte_pointer = VARBITS(a);
unsigned long *position = (unsigned long *) byte_pointer;
unsigned long remainder = 0x0;
for (; length >= 8; length -= 8) {
count += __builtin_popcountl(*position++);
}
if (length == 0) PG_RETURN_INT32(count);
// special case, non-64bit-aligned varbit length
byte_pointer = (unsigned char *) position;
memcpy((void *) &remainder, (void *) position, length);
count += __builtin_popcountl(remainder);
PG_RETURN_INT32(count);
}
/**
* Inline assembly popcntq instruction.
**/
static inline uint64_t popcntq(uint64_t val) {
__asm__ ("popcnt %1, %0" : "=r" (val) : "0" (val));
return val;
}
/**
* Unrolled POPCNT Assembly instruction for 256bit steps.
* Requires hardware support or will fail.
* Uses memcpy for remainder alignment.
**/
Datum
popcount256(PG_FUNCTION_ARGS) {
VarBit *a = PG_GETARG_VARBIT_P(0);
int length = VARBITBYTES(a);
uint64_t chunks, limit, count = 0, i = 0;
unsigned char *byte_pointer = VARBITS(a);
uint64_t *buffer = (uint64_t *) byte_pointer;
uint64_t remainder = 0x0;
if (!__builtin_cpu_supports("popcnt")){
ereport(ERROR,
(
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("POPCNT instruction not supported."),
errdetail("Support is indicated by CPUID.01H:ECX.POPCNT[Bit 23] flag."),
errhint("Use popcount[|32|64]().")
)
);
}
chunks = length / 8;
limit = chunks - (chunks % 4);
// Unrolled popcntq to avoid False Data Dependency.
for (; i < limit; i += 4) {
count += popcntq(buffer[i]);
count += popcntq(buffer[i+1]);
count += popcntq(buffer[i+2]);
count += popcntq(buffer[i+3]);
}
// Regular popcntq for remaining 64 bit chunks.
for (; i < chunks; i++) {
count += popcntq(buffer[i]);
}
if (length % 8 == 0) PG_RETURN_INT32(count);
// special case, non-64bit-aligned varbit length
buffer += chunks;
byte_pointer = (unsigned char *) buffer;
memcpy((void *) &remainder, (void *) buffer, length % 8);
count += popcntq(remainder);
PG_RETURN_INT32(count);
}