1
0
mirror of https://github.com/EduApps-CDG/OpenDX synced 2024-12-30 09:45:37 +01:00
OpenDX/src/util/util_bit.h

76 lines
1.8 KiB
C
Raw Normal View History

#pragma once
2018-05-19 08:26:25 +01:00
#ifndef _MSC_VER
2018-05-09 20:37:49 +02:00
#include <x86intrin.h>
2018-05-19 08:26:25 +01:00
#else
#include <intrin.h>
#endif
2018-05-09 20:37:49 +02:00
#include "util_likely.h"
namespace dxvk::bit {
template<typename T>
T extract(T value, uint32_t fst, uint32_t lst) {
return (value >> fst) & ~(~T(0) << (lst - fst + 1));
}
2019-05-09 16:37:45 +02:00
inline uint32_t popcntStep(uint32_t n, uint32_t mask, uint32_t shift) {
return (n & mask) + ((n & ~mask) >> shift);
}
2019-05-09 16:37:45 +02:00
inline uint32_t popcnt(uint32_t n) {
n = popcntStep(n, 0x55555555, 1);
n = popcntStep(n, 0x33333333, 2);
n = popcntStep(n, 0x0F0F0F0F, 4);
n = popcntStep(n, 0x00FF00FF, 8);
n = popcntStep(n, 0x0000FFFF, 16);
return n;
}
2018-05-09 20:09:09 +02:00
inline uint32_t tzcnt(uint32_t n) {
#if defined(_MSC_VER) && !defined(__clang__)
2018-05-19 08:26:25 +01:00
return _tzcnt_u32(n);
#elif defined(__BMI__)
2018-05-09 20:09:09 +02:00
return __tzcnt_u32(n);
#elif defined(__GNUC__) || defined(__clang__)
2018-05-09 20:09:09 +02:00
uint32_t res;
uint32_t tmp;
asm (
"mov $32, %1;"
"bsf %2, %0;"
2018-05-09 20:09:09 +02:00
"cmovz %1, %0;"
: "=&r" (res), "=&r" (tmp)
: "r" (n));
return res;
#else
uint32_t r = 31;
n &= -n;
r -= (n & 0x0000FFFF) ? 16 : 0;
r -= (n & 0x00FF00FF) ? 8 : 0;
r -= (n & 0x0F0F0F0F) ? 4 : 0;
r -= (n & 0x33333333) ? 2 : 0;
r -= (n & 0x55555555) ? 1 : 0;
return n != 0 ? r : 32;
#endif
}
template<typename T>
uint32_t pack(T& dst, uint32_t& shift, T src, uint32_t count) {
constexpr uint32_t Bits = 8 * sizeof(T);
if (likely(shift < Bits))
dst |= src << shift;
shift += count;
return shift > Bits ? shift - Bits : 0;
}
template<typename T>
uint32_t unpack(T& dst, T src, uint32_t& shift, uint32_t count) {
constexpr uint32_t Bits = 8 * sizeof(T);
if (likely(shift < Bits))
dst = (src >> shift) & ((T(1) << count) - 1);
shift += count;
return shift > Bits ? shift - Bits : 0;
}
}