Skip to content

Commit c135b09

Browse files
committed
Add option to use __builtin_popcount, as suggested in PR#5.
1 parent b9672ac commit c135b09

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/tbprobe.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,29 @@ typedef HANDLE map_t;
121121
#include <nmmintrin.h>
122122
#define popcount(x) (int)_mm_popcnt_u64((x))
123123
#else
124+
// try to use a builtin
125+
#if defined (__has_builtin)
126+
#if __has_builtin(__builtin_popcountll)
127+
#define popcount(x) __builtin_popcountll((x))
128+
#else
129+
#define TB_SOFTWARE_POP_COUNT
130+
#endif
131+
#else
124132
#define TB_SOFTWARE_POP_COUNT
125133
#endif
134+
#endif
126135

127136
#ifdef TB_SOFTWARE_POP_COUNT
128-
// Not a recognized compiler/architecture that has popcount:
129-
// fall back to a software popcount. This one is still reasonably
130-
// fast.
137+
// Not a recognized compiler/architecture that has popcount, and
138+
// no builtin available: fall back to a software popcount. This one
139+
// is still reasonably fast.
131140
static inline unsigned tb_software_popcount(uint64_t x)
132141
{
133142
x = x - ((x >> 1) & 0x5555555555555555ull);
134143
x = (x & 0x3333333333333333ull) + ((x >> 2) & 0x3333333333333333ull);
135144
x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0full;
136145
return (x * 0x0101010101010101ull) >> 56;
137146
}
138-
139147
#define popcount(x) tb_software_popcount(x)
140148
#endif
141149

@@ -370,7 +378,7 @@ static void unmap_file(void *data, map_t size)
370378
{
371379
if (!data) return;
372380
if (munmap(data, size) != 0) {
373-
perror("munmap");
381+
perror("munmap");
374382
}
375383
}
376384
#else

0 commit comments

Comments
 (0)