diff options
| author | Julius Ikkala <julius.ikkala@gmail.com> | 2025-08-21 01:13:52 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-20 22:13:52 +0000 |
| commit | cbd73dde3dd2da790bb663385a229ce22965c43c (patch) | |
| tree | 515a6847aa3dc5163845d834e94dfcb8de9ca56d /source | |
| parent | 619de903b70e08a7ca8471419e8eb7e4dd43ca9d (diff) | |
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.
Diffstat (limited to 'source')
| -rw-r--r-- | source/slang/hlsl.meta.slang | 32 |
1 files changed, 25 insertions, 7 deletions
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<T>(bit_cast<half>(uint16_t(1))); - return x + ((x < y) ? delta : -delta); + uint16_t val = bit_cast<uint16_t>(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<T>(nextval); } if (T is float) { - T delta = __realCast<T>(bit_cast<float>(uint32_t(1))); - return x + ((x < y) ? delta : -delta); - } - T delta = __realCast<T>(bit_cast<double>(uint64_t(1))); - return x + ((x < y) ? delta : -delta); + uint32_t val = bit_cast<uint32_t>(x); + if((val >> 31) != 0) + delta = -delta; + uint32_t nextval = val + uint32_t(delta); + if(((val^nextval) >> 31) != 0) + nextval += 0x80000002u; + return bit_cast<T>(nextval); + } + uint64_t val = bit_cast<uint64_t>(x); + if((val >> 63) != 0) + delta = -delta; + uint64_t nextval = val + uint64_t(delta); + if(((val^nextval) >> 63) != 0) + nextval += 0x8000000000000002ull; + return bit_cast<T>(nextval); } } |
