nir: Extract float_is_half tests in common code

Signed-off-by: Lorenzo Rossi <lorenzo.rossi@collabora.com>
Reviewed-by: Eric R. Smith <eric.smith@collabora.com>
Reviewed-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/41096>
This commit is contained in:
Lorenzo Rossi 2026-04-21 19:10:32 +02:00 committed by Marge Bot
parent e092e945a7
commit 89436db611
2 changed files with 16 additions and 3 deletions

View file

@ -476,9 +476,7 @@ static bool
const_is_f16(nir_scalar scalar)
{
double value = nir_scalar_as_float(scalar);
uint16_t fp16_val = _mesa_float_to_half(value);
bool is_denorm = (fp16_val & 0x7fff) != 0 && (fp16_val & 0x7fff) <= 0x3ff;
return value == _mesa_half_to_float(fp16_val) && !is_denorm;
return _mesa_float_is_half(value);
}
static bool

View file

@ -129,6 +129,21 @@ _mesa_half_is_negative(uint16_t h)
return !!(h & 0x8000);
}
static inline bool
_mesa_float_is_half(double val)
{
/* val parameter is double to prevent implicit double->float cast. We have
* to cast to float because that's what _mesa_float_to_half expects and we
* don't have any readily available _double_to_half function. This may
* introduce double-rounding errors, however this is ok because the final
* check is done at double precision, any rounding will fail to produce the
* original value.
*/
uint16_t fp16_val = _mesa_float_to_half((float) val);
bool is_denorm = (fp16_val & 0x7fff) != 0 && (fp16_val & 0x7fff) <= 0x3ff;
return val == (double) _mesa_half_to_float(fp16_val) && !is_denorm;
}
#ifdef __cplusplus