mirror of
https://github.com/gbdev/rgbds.git
synced 2026-10-06 04:05:38 +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
|
||||
|
||||
@@ -57,6 +57,7 @@ enum RPNCommand {
|
||||
RPN_LOW = 0x71,
|
||||
RPN_BITWIDTH = 0x72,
|
||||
RPN_TZCOUNT = 0x73,
|
||||
RPN_POPCOUNT = 0x74,
|
||||
|
||||
RPN_CONST = 0x80,
|
||||
RPN_SYM = 0x81
|
||||
|
||||
@@ -23,5 +23,6 @@ int32_t op_low(int32_t value);
|
||||
|
||||
int32_t op_bitwidth(int32_t value);
|
||||
int32_t op_tzcount(int32_t value);
|
||||
int32_t op_popcount(int32_t value);
|
||||
|
||||
#endif // RGBDS_OP_MATH_HPP
|
||||
Reference in new issue
Block a user