forked from WojciechMula/sse-popcount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popcnt-builtin.cpp
68 lines (60 loc) · 1.85 KB
/
popcnt-builtin.cpp
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
#include <assert.h>
// Note: this emits a popcnt with clang 3.4 but not with clang 3.0
uint64_t builtin_popcnt(const uint64_t* buf, int len) {
uint64_t cnt = 0;
for (int i = 0; i < len; ++i) {
cnt += __builtin_popcountll(buf[i]);
}
return cnt;
}
uint64_t builtin_popcnt32(const uint64_t* buf64, int len64) {
uint64_t cnt = 0;
const uint32_t* buf = (const uint32_t*) buf64;
int len = len64 * 2;
for (int i = 0; i < len; ++i) {
cnt += __builtin_popcount(buf[i]);
}
return cnt;
}
uint64_t builtin_popcnt_unrolled(const uint64_t* buf, int len) {
assert(len % 4 == 0);
uint64_t cnt = 0;
for (int i = 0; i < len; i+=4) {
cnt += __builtin_popcountll(buf[i]);
cnt += __builtin_popcountll(buf[i+1]);
cnt += __builtin_popcountll(buf[i+2]);
cnt += __builtin_popcountll(buf[i+3]);
}
return cnt;
}
uint64_t builtin_popcnt_unrolled32(const uint64_t* buf64, int len64) {
const uint32_t* buf = (const uint32_t*) buf64;
int len = len64 * 2;
assert(len % 4 == 0);
uint64_t cnt = 0;
for (int i = 0; i < len; i+=4) {
cnt += __builtin_popcount(buf[i]);
cnt += __builtin_popcount(buf[i+1]);
cnt += __builtin_popcount(buf[i+2]);
cnt += __builtin_popcount(buf[i+3]);
}
return cnt;
}
// Attempt to work around false depdency errata.
// gcc is too smart to fall for this and re-creates the dependency unless
// compiled with -funroll-loops or something similar.
// This works with clang, though.
uint64_t builtin_popcnt_unrolled_errata(const uint64_t* buf, int len) {
assert(len % 4 == 0);
uint64_t cnt[4];
for (int i = 0; i < 4; ++i) {
cnt[i] = 0;
}
for (int i = 0; i < len; i+=4) {
cnt[0] += __builtin_popcountll(buf[i]);
cnt[1] += __builtin_popcountll(buf[i+1]);
cnt[2] += __builtin_popcountll(buf[i+2]);
cnt[3] += __builtin_popcountll(buf[i+3]);
}
return cnt[0] + cnt[1] + cnt[2] + cnt[3];
}