From cbd73dde3dd2da790bb663385a229ce22965c43c Mon Sep 17 00:00:00 2001 From: Julius Ikkala Date: Thu, 21 Aug 2025 01:13:52 +0300 Subject: Fix nextafter() (#8195) Fixes #8185. The previous implementation is incorrect and basically only works in the `x = 0` case. `delta` was the smallest possible positive value representable as a float, but that's below the rounding error of addition with almost all reasonably sized floats. This fixed implementation is based on bit twiddling instead. I've checked the float case against the C++ `nextafterf` with both a -inf -> inf and inf -> -inf sweep, in addition to the test included in this PR. --- source/slang/hlsl.meta.slang | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'source/slang') diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index c2b3fc436..7f8488236 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -12367,18 +12367,36 @@ T nextafter(T x, T y) if (isnan(x)) return x; if (isnan(y)) return y; if (x == y) return y; + + int delta = x < y ? 1 : -1; + if (T is half) { - T delta = __realCast(bit_cast(uint16_t(1))); - return x + ((x < y) ? delta : -delta); + uint16_t val = bit_cast(x); + if((val >> 15) != 0) // If we're negative, -1 acts like +1 on the float. + delta = -delta; + uint16_t nextval = val + uint16_t(delta); + if(((val^nextval) >> 15) != 0) // If sign bit changed + nextval += 0x8002; // Correct the overflow + return bit_cast(nextval); } if (T is float) { - T delta = __realCast(bit_cast(uint32_t(1))); - return x + ((x < y) ? delta : -delta); - } - T delta = __realCast(bit_cast(uint64_t(1))); - return x + ((x < y) ? delta : -delta); + uint32_t val = bit_cast(x); + if((val >> 31) != 0) + delta = -delta; + uint32_t nextval = val + uint32_t(delta); + if(((val^nextval) >> 31) != 0) + nextval += 0x80000002u; + return bit_cast(nextval); + } + uint64_t val = bit_cast(x); + if((val >> 63) != 0) + delta = -delta; + uint64_t nextval = val + uint64_t(delta); + if(((val^nextval) >> 63) != 0) + nextval += 0x8000000000000002ull; + return bit_cast(nextval); } } -- cgit v1.2.3