util/format_pack: Fix packing of signed 1010102 SSCALED formats

Previously, [SU]SCALED formats would hit the integer path and we would
generate:

    ((uint32_t)CLAMP(src[i], min, max)) & MASK

This is fine for unsigned scaled formats.  However, for signed formats,
a negative float value cast to an unsigned integer yields undefined
results.  On x86, it implicitly clamps to 0.  This change makes us
generate:

    ((uint32_t)(int32_t)CLAMP(src[i], min, max)) & MASK

hich gets us correct casting.

Reviewed-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/28793>
This commit is contained in:
Faith Ekstrand
2024-04-17 12:37:14 -05:00
committed by Marge Bot
parent 2a9f4618c5
commit b187be5b1c

View File

@@ -410,13 +410,21 @@ def conversion_expr(src_channel,
# Convert double or float to non-float
if dst_channel.type != FLOAT:
if dst_channel.norm or dst_channel.type == FIXED:
dst_one = get_one(dst_channel)
if dst_channel.size <= 23:
value = 'util_iround(%s * 0x%x)' % (value, dst_one)
else:
# bigger than single precision mantissa, use double
value = '(%s * (double)0x%x)' % (value, dst_one)
if not dst_channel.pure:
if dst_channel.norm or dst_channel.type == FIXED:
dst_one = get_one(dst_channel)
if dst_channel.size <= 23:
value = 'util_iround(%s * 0x%x)' % (value, dst_one)
else:
# bigger than single precision mantissa, use double
value = '(%s * (double)0x%x)' % (value, dst_one)
# Cast to an integer with the correct signedness first
if dst_channel.type == UNSIGNED:
value = '(uint%u_t)(%s) ' % (max(dst_channel.size, 32), value)
elif dst_channel.type == SIGNED:
value = '(int%u_t)(%s) ' % (max(dst_channel.size, 32), value)
value = '(%s)%s' % (dst_native_type, value)
else:
# Cast double to float when converting to either half or float