util: Re-indent util_sign_extend, comment, and add asserts
The current implementation depends on both of these things for correctness. If width > 64, you get UB wrap-around and, if val >= (1 << width), the subtract trick won't work. 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:
parent
20016aa8f6
commit
6fee4584ea
|
@ -580,17 +580,21 @@ util_bswap16(uint16_t n)
|
|||
}
|
||||
|
||||
/**
|
||||
* Extend sign.
|
||||
* Sign-extend a number
|
||||
*
|
||||
* The bit at position `width - 1` is replicated to all the higher bits.
|
||||
* This assumes and asserts that the value fits into `width` bits.
|
||||
*/
|
||||
static inline int64_t
|
||||
util_sign_extend(uint64_t val, unsigned width)
|
||||
{
|
||||
assert(width > 0);
|
||||
if (val & (UINT64_C(1) << (width - 1))) {
|
||||
return -(int64_t)((UINT64_C(1) << width) - val);
|
||||
} else {
|
||||
return val;
|
||||
}
|
||||
assert(width > 0 && width <= 64);
|
||||
assert(width == 64 || val < (1ull << width));
|
||||
if (val & (UINT64_C(1) << (width - 1))) {
|
||||
return -(int64_t)((UINT64_C(1) << width) - val);
|
||||
} else {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void*
|
||||
|
|
Loading…
Reference in New Issue