util/half: Use explicit RTNE rounding for the C++ float16_t

Reviewed-by: Erik Faye-Lund <erik.faye-lund@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/41295>
This commit is contained in:
Faith Ekstrand 2026-04-30 11:01:06 -04:00 committed by Marge Bot
parent c6ddfe1a3b
commit 9bf392b67b
2 changed files with 64 additions and 65 deletions

View file

@ -25,7 +25,6 @@
#ifndef _DOUBLE_H_
#define _DOUBLE_H_
#include "half_float.h"
#include "u_math.h"
#ifdef __cplusplus
@ -48,68 +47,6 @@ _mesa_double_to_float_rtne(double val)
return _mesa_double_to_float(val);
}
/*
* We round down from double to half float by going through float in between,
* but this can give us inaccurate results in some cases.
* One such case is 0x40ee6a0000000001, which should round to 0x7b9b, but
* going through float first turns into 0x7b9a instead. This is because the
* first non-fitting bit is set, so we get a tie, but with the least
* significant bit of the original number set, the tie should break rounding
* up.
* The cast to float, however, turns into 0x47735000, which when going to half
* still ties, but now we lost the tie-up bit, and instead we round to the
* nearest even, which in this case is down.
*
* To fix this, we check if the original would have tied, and if the tie would
* have rounded up, and if both are true, set the least significant bit of the
* intermediate float to 1, so that a tie on the next cast rounds up as well.
* If the rounding already got rid of the tie, that set bit will just be
* truncated anyway and the end result doesn't change.
*
* Another failing case is 0x40effdffffffffff. This one doesn't have the tie
* from double to half, so it just rounds down to 0x7bff (65504.0), but going
* through float first, it turns into 0x477ff000, which does have the tie bit
* for half set, and when that one gets rounded it turns into 0x7c00
* (Infinity).
* The fix for that one is to make sure the intermediate float does not have
* the tie bit set if the original didn't have it.
*/
static inline uint16_t
_mesa_double_to_float16_rtne(double val)
{
int significand_bits16 = 10;
int significand_bits32 = 23;
int significand_bits64 = 52;
int f64_to_16_tie_bit = significand_bits64 - significand_bits16 - 1;
int f32_to_16_tie_bit = significand_bits32 - significand_bits16 - 1;
uint64_t f64_rounds_up_mask = ((1ULL << f64_to_16_tie_bit) - 1);
union di src;
union fi dst;
src.d = val;
dst.f = val;
bool f64_has_tie = (src.ui & (1ULL << f64_to_16_tie_bit)) != 0;
bool f64_rounds_up = (src.ui & f64_rounds_up_mask) != 0;
dst.ui |= (f64_has_tie && f64_rounds_up);
if (!f64_has_tie)
dst.ui &= ~(1U << f32_to_16_tie_bit);
return _mesa_float_to_float16_rtne(dst.f);
}
/*
* double -> float -> half with RTZ doesn't have as many complications as
* RTNE, but we do need to ensure that the double -> float cast also uses RTZ.
*/
static inline uint16_t
_mesa_double_to_float16_rtz(double val)
{
return _mesa_float_to_float16_rtz(_mesa_double_to_float_rtz(val));
}
#ifdef __cplusplus
} /* extern C */
#endif

View file

@ -31,6 +31,7 @@
#include <string.h>
#include "util/detect_arch.h"
#include "util/detect_cc.h"
#include "util/double.h"
#include "util/u_cpu_detect.h"
#if DETECT_ARCH_X86_64
@ -144,6 +145,67 @@ _mesa_float_is_half(double val)
return val == (double) _mesa_half_to_float(fp16_val) && !is_denorm;
}
/*
* We round down from double to half float by going through float in between,
* but this can give us inaccurate results in some cases.
* One such case is 0x40ee6a0000000001, which should round to 0x7b9b, but
* going through float first turns into 0x7b9a instead. This is because the
* first non-fitting bit is set, so we get a tie, but with the least
* significant bit of the original number set, the tie should break rounding
* up.
* The cast to float, however, turns into 0x47735000, which when going to half
* still ties, but now we lost the tie-up bit, and instead we round to the
* nearest even, which in this case is down.
*
* To fix this, we check if the original would have tied, and if the tie would
* have rounded up, and if both are true, set the least significant bit of the
* intermediate float to 1, so that a tie on the next cast rounds up as well.
* If the rounding already got rid of the tie, that set bit will just be
* truncated anyway and the end result doesn't change.
*
* Another failing case is 0x40effdffffffffff. This one doesn't have the tie
* from double to half, so it just rounds down to 0x7bff (65504.0), but going
* through float first, it turns into 0x477ff000, which does have the tie bit
* for half set, and when that one gets rounded it turns into 0x7c00
* (Infinity).
* The fix for that one is to make sure the intermediate float does not have
* the tie bit set if the original didn't have it.
*/
static inline uint16_t
_mesa_double_to_float16_rtne(double val)
{
int significand_bits16 = 10;
int significand_bits32 = 23;
int significand_bits64 = 52;
int f64_to_16_tie_bit = significand_bits64 - significand_bits16 - 1;
int f32_to_16_tie_bit = significand_bits32 - significand_bits16 - 1;
uint64_t f64_rounds_up_mask = ((1ULL << f64_to_16_tie_bit) - 1);
union di src;
union fi dst;
src.d = val;
dst.f = val;
bool f64_has_tie = (src.ui & (1ULL << f64_to_16_tie_bit)) != 0;
bool f64_rounds_up = (src.ui & f64_rounds_up_mask) != 0;
dst.ui |= (f64_has_tie && f64_rounds_up);
if (!f64_has_tie)
dst.ui &= ~(1U << f32_to_16_tie_bit);
return _mesa_float_to_float16_rtne(dst.f);
}
/*
* double -> float -> half with RTZ doesn't have as many complications as
* RTNE, but we do need to ensure that the double -> float cast also uses RTZ.
*/
static inline uint16_t
_mesa_double_to_float16_rtz(double val)
{
return _mesa_float_to_float16_rtz(_mesa_double_to_float_rtz(val));
}
#ifdef __cplusplus
@ -154,8 +216,8 @@ namespace mesa
struct float16_t {
uint16_t bits;
float16_t(float f) : bits(_mesa_float_to_half(f)) {}
float16_t(double d) : bits(_mesa_float_to_half((float)d)) {}
float16_t(float f) : bits(_mesa_float_to_float16_rtne(f)) {}
float16_t(double d) : bits(_mesa_double_to_float16_rtne(d)) {}
float16_t(uint16_t raw_bits) : bits(raw_bits) {}
static float16_t one() { return float16_t(FP16_ONE); }
static float16_t zero() { return float16_t(FP16_ZERO); }