1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2024-12-02 10:24:12 +01:00

[util] Optimize popcnt operation

This commit is contained in:
Philip Rebohle 2019-05-09 16:37:45 +02:00
parent a1feaa6748
commit dcd75a4f09
No known key found for this signature in database
GPG Key ID: C8CC613427A31C99

View File

@ -14,12 +14,18 @@ namespace dxvk::bit {
T extract(T value, uint32_t fst, uint32_t lst) { T extract(T value, uint32_t fst, uint32_t lst) {
return (value >> fst) & ~(~T(0) << (lst - fst + 1)); return (value >> fst) & ~(~T(0) << (lst - fst + 1));
} }
inline uint32_t popcntStep(uint32_t n, uint32_t mask, uint32_t shift) {
return (n & mask) + ((n & ~mask) >> shift);
}
template<typename T> inline uint32_t popcnt(uint32_t n) {
T popcnt(T value) { n = popcntStep(n, 0x55555555, 1);
return value != 0 n = popcntStep(n, 0x33333333, 2);
? (value & 1) + popcnt(value >> 1) n = popcntStep(n, 0x0F0F0F0F, 4);
: 0; n = popcntStep(n, 0x00FF00FF, 8);
n = popcntStep(n, 0x0000FFFF, 16);
return n;
} }
inline uint32_t tzcnt(uint32_t n) { inline uint32_t tzcnt(uint32_t n) {