2020-07-01 02:55:47 +00:00
|
|
|
#ifndef COSMOPOLITAN_LIBC_BITS_POPCNT_H_
|
|
|
|
#define COSMOPOLITAN_LIBC_BITS_POPCNT_H_
|
|
|
|
#include "libc/nexgen32e/x86feature.h"
|
|
|
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
|
|
|
COSMOPOLITAN_C_START_
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
size_t _countbits(const void *, size_t);
|
2020-07-01 02:55:47 +00:00
|
|
|
unsigned long popcnt(unsigned long) pureconst;
|
|
|
|
|
2023-05-02 02:43:59 +00:00
|
|
|
#if defined(__GNUC__) && !defined(__STRICT_ANSI__) && defined(__x86_64__)
|
2021-06-15 13:24:46 +00:00
|
|
|
#define popcnt(X) \
|
|
|
|
(__builtin_constant_p(X) ? __builtin_popcountll(X) : ({ \
|
|
|
|
unsigned long PoP = (X); \
|
|
|
|
if (X86_HAVE(POPCNT)) { \
|
|
|
|
asm("popcnt\t%0,%0" : "+r"(PoP) : /* no inputs */ : "cc"); \
|
|
|
|
} else { \
|
|
|
|
PoP = (popcnt)(PoP); \
|
|
|
|
} \
|
|
|
|
PoP; \
|
2020-07-01 02:55:47 +00:00
|
|
|
}))
|
2023-05-02 02:43:59 +00:00
|
|
|
#else
|
|
|
|
#define popcnt(x) __builtin_popcountll(x)
|
2020-07-01 02:55:47 +00:00
|
|
|
#endif /* GNUC && !ANSI */
|
|
|
|
|
|
|
|
COSMOPOLITAN_C_END_
|
|
|
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
|
|
|
#endif /* COSMOPOLITAN_LIBC_BITS_POPCNT_H_ */
|