util: Use shifts in util_sign_extend

As long as we left-shift the unsigned version, this has no undefined
behavior and is fewer instructions.  The only tricky bit is that a right
shift of a negative number is technically implementation-defined (not
undefined) behavior in C.  However, if it's ever anything other than an
arithmatic right-shift, there's lots of other places where Mesa will
break today.

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:30:48 -05:00 committed by Marge Bot
parent 6fee4584ea
commit 53eeb1e238
1 changed files with 3 additions and 6 deletions

View File

@ -589,12 +589,9 @@ static inline int64_t
util_sign_extend(uint64_t val, unsigned width)
{
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;
}
assert(width == 64 || val < (UINT64_C(1) << width));
unsigned shift = 64 - width;
return (int64_t)(val << shift) >> shift;
}
static inline void*