1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2025-02-20 19:54:19 +01:00

[util] Add helpers to pack/unpack data to/from larger units

This commit is contained in:
Philip Rebohle 2019-04-04 05:48:42 +02:00
parent 9d26031dcb
commit d2395180af
No known key found for this signature in database
GPG Key ID: C8CC613427A31C99

View File

@ -6,6 +6,8 @@
#include <intrin.h>
#endif
#include "util_likely.h"
namespace dxvk::bit {
template<typename T>
@ -46,5 +48,23 @@ namespace dxvk::bit {
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;
}
}