util,nir: Move mask_sign_extend from opt_load_store_vectorize to util

While we're moving it, reformat a bit to make it match util_sign_extend
better.

Reviewed-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Reviewed-by: Kristian H. Kristensen <hoegsberg@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17214>
This commit is contained in:
Jason Ekstrand
2022-06-23 12:55:40 -05:00
committed by Marge Bot
parent a307bc8556
commit efc63ea02d
2 changed files with 19 additions and 12 deletions
+16 -3
View File
@@ -579,6 +579,21 @@ util_bswap16(uint16_t n)
(n << 8);
}
/**
* Mask and sign-extend a number
*
* The bit at position `width - 1` is replicated to all the higher bits.
* This makes no assumptions about the high bits of the value and will
* overwrite them with the sign bit.
*/
static inline int64_t
util_mask_sign_extend(uint64_t val, unsigned width)
{
assert(width > 0 && width <= 64);
unsigned shift = 64 - width;
return (int64_t)(val << shift) >> shift;
}
/**
* Sign-extend a number
*
@@ -588,10 +603,8 @@ util_bswap16(uint16_t n)
static inline int64_t
util_sign_extend(uint64_t val, unsigned width)
{
assert(width > 0 && width <= 64);
assert(width == 64 || val < (UINT64_C(1) << width));
unsigned shift = 64 - width;
return (int64_t)(val << shift) >> shift;
return util_mask_sign_extend(val, width);
}
static inline void*