mirror of
https://github.com/gbdev/rgbds.git
synced 2026-10-05 19:56:18 +00:00
Add POPCOUNT function (#2166)
This commit is contained in:
1 parent
ff6f1699e9
commit
d95e9260e4
13 files changed
+45
-3
No files matched your search
+17
-3
@@ -42,12 +42,13 @@ static inline void unreachable_() {
|
||||
|
||||
// Ideally we'd use `std::bit_width`, but it has insufficient compiler support
|
||||
#ifdef __GNUC__ // GCC or compatible
|
||||
#define ctz __builtin_ctz
|
||||
#define clz __builtin_clz
|
||||
#define ctz __builtin_ctz
|
||||
#define clz __builtin_clz
|
||||
#define popcount __builtin_popcount
|
||||
|
||||
#elif defined(_MSC_VER)
|
||||
#include <intrin.h>
|
||||
#pragma intrinsic(_BitScanReverse, _BitScanForward)
|
||||
#pragma intrinsic(_BitScanReverse, _BitScanForward, __popcnt)
|
||||
|
||||
static inline int ctz(unsigned int x) {
|
||||
assume(x != 0);
|
||||
@@ -63,6 +64,10 @@ static inline int clz(unsigned int x) {
|
||||
return 31 - count;
|
||||
}
|
||||
|
||||
static inline int popcount(unsigned int x) {
|
||||
return __popcnt(x);
|
||||
}
|
||||
|
||||
#else
|
||||
#include <limits.h>
|
||||
|
||||
@@ -85,6 +90,15 @@ static inline int clz(unsigned int x) {
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
static inline int popcount(unsigned int x) {
|
||||
int count = 0;
|
||||
while (x) {
|
||||
x &= x - 1;
|
||||
++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Macros for stringification
|
||||
|
||||
Reference in new issue
Block a user