From d2395180af89f163dd308bc69deeb82d341acb3f Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Thu, 4 Apr 2019 05:48:42 +0200 Subject: [PATCH] [util] Add helpers to pack/unpack data to/from larger units --- src/util/util_bit.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/util/util_bit.h b/src/util/util_bit.h index 23b8cc655..9d9cdfc36 100644 --- a/src/util/util_bit.h +++ b/src/util/util_bit.h @@ -6,6 +6,8 @@ #include #endif +#include "util_likely.h" + namespace dxvk::bit { template @@ -46,5 +48,23 @@ namespace dxvk::bit { return n != 0 ? r : 32; #endif } + + template + 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 + 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; + } } \ No newline at end of file