util: Add util_bitpack_[su]fixed_clamp helpers

These clamp the value to the fixed-point range instead of asserting.

Reviewed-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18062>
This commit is contained in:
Jason Ekstrand 2022-08-15 09:13:17 -05:00 committed by Marge Bot
parent 570f35aa3d
commit 4615153d71

View file

@ -136,6 +136,24 @@ util_bitpack_sfixed(float v, uint32_t start, uint32_t end,
return (int_val & mask) << start;
}
ALWAYS_INLINE static uint64_t
util_bitpack_sfixed_clamp(float v, uint32_t start, uint32_t end,
uint32_t fract_bits)
{
util_bitpack_validate_value(v);
const float factor = (1 << fract_bits);
const int total_bits = end - start + 1;
const float min = u_intN_min(total_bits) / factor;
const float max = u_intN_max(total_bits) / factor;
const int64_t int_val = llroundf(CLAMP(v, min, max) * factor);
const uint64_t mask = ~0ull >> (64 - (end - start + 1));
return (int_val & mask) << start;
}
ALWAYS_INLINE static uint64_t
util_bitpack_sfixed_nonzero(float v, uint32_t start, uint32_t end,
uint32_t fract_bits)
@ -164,6 +182,23 @@ util_bitpack_ufixed(float v, uint32_t start, ASSERTED uint32_t end,
return uint_val << start;
}
ALWAYS_INLINE static uint64_t
util_bitpack_ufixed_clamp(float v, uint32_t start, ASSERTED uint32_t end,
uint32_t fract_bits)
{
util_bitpack_validate_value(v);
const float factor = (1 << fract_bits);
const int total_bits = end - start + 1;
const float min = 0.0f;
const float max = u_uintN_max(total_bits) / factor;
const uint64_t uint_val = llroundf(CLAMP(v, min, max) * factor);
return uint_val << start;
}
ALWAYS_INLINE static uint64_t
util_bitpack_ufixed_nonzero(float v, uint32_t start, uint32_t end,
uint32_t fract_bits)