shader: V_FRACT_F64/F16 of tiny negative inputs — the tests expect 1.0, RDNA returns the largest value below 1.0 #1209

Closed
opened 2026-11-26 09:47:52 +00:00 by github-bot · 3 comments
Owner

@⁠Nawid3333 on GitHub · 2026-10-08 12:50 UTC · original

On an AMD RX 9070 XT (Windows 11, driver 26.8.1), --cmp-eq-f64-only fails at one edge of
VectorFractF64CapturedAndEdges: for 0x8000000000000001 (-2^-1074, the negative denormal closest to zero)
the test expects 0x3ff0000000000000 (1.0) and gets 0x3fefffffffffffff. Every other case in the test
matches.

VectorFractF16CapturedAndEdges has the same edge. For the halves 0x8001, 0x83ff and 0x8c00,
x - floor(x) is 1 - 2^-24, 1 - 1023 * 2^-24 and 1 - 2^-12, which round to 1.0 in half precision (the last
one sits exactly between 0x3bff and 1.0, and the tie goes to 1.0), and the test expects 0x3c00. Every
half in [-2^-12, 0) rounds to 1.0 this way; the test covers these three. On AMD they currently come out as
0x3bff, but only because the driver's half pack truncates (#1204 fixes that rounding, after which they
come out as 1.0 and match the test).

Kyty emits GLSLstd450Fract for both, so the result is x - floor(x) rounded to the instruction's
precision, which is 1.0 at these edges.

I think the hardware never returns 1.0 here:

  • The RDNA2 ISA reference guide (12.8, V_FRACT_F64, _F32 and _F16) says the instruction "complies with
    the DX specification of fract", and DirectX's frc gives a result in [+0, 1) for every finite input
    (https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/frc--sm4---asm-).
  • LLVM's AMDGPU backend relies on the same thing: on every target without the SI/CI fract bug it replaces
    minnum(x - floor(x), nextafter(1.0, -1.0)) with the hardware v_fract (matchFractPatImpl in
    AMDGPUCodeGenPrepare.cpp), and IntrinsicsAMDGPU.td says v_fract "may return 1.0" only on SI/CI.

So on RDNA2, v_fract_f64(-2^-1074) should be 0x3fefffffffffffff, and v_fract_f16 of those three halves
0x3bff: the largest values below 1.0.

Did you capture these edge values on real hardware? If so, I'm wrong and AMD's driver is the one that
differs. If not, I'm happy to send a PR for it:

  • the expectations become 0x3fefffffffffffff and, for the three half cases, 0x3bff;
  • Kyty clamps the result below 1.0 after rounding to the instruction's precision, keeping NaN inputs NaN,
    so every host returns the same value.

AI use (per the README's AI policy): Claude (Anthropic) found the failing cases with a nightly test run
on my machine and looked up the ISA guide and the LLVM sources; I reviewed the findings.

**@⁠Nawid3333** on GitHub · 2026-10-08 12:50 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1209) On an AMD RX 9070 XT (Windows 11, driver 26.8.1), `--cmp-eq-f64-only` fails at one edge of `VectorFractF64CapturedAndEdges`: for `0x8000000000000001` (-2^-1074, the negative denormal closest to zero) the test expects `0x3ff0000000000000` (1.0) and gets `0x3fefffffffffffff`. Every other case in the test matches. `VectorFractF16CapturedAndEdges` has the same edge. For the halves `0x8001`, `0x83ff` and `0x8c00`, `x - floor(x)` is 1 - 2^-24, 1 - 1023 * 2^-24 and 1 - 2^-12, which round to 1.0 in half precision (the last one sits exactly between `0x3bff` and 1.0, and the tie goes to 1.0), and the test expects `0x3c00`. Every half in [-2^-12, 0) rounds to 1.0 this way; the test covers these three. On AMD they currently come out as `0x3bff`, but only because the driver's half pack truncates (#1204 fixes that rounding, after which they come out as 1.0 and match the test). Kyty emits `GLSLstd450Fract` for both, so the result is `x - floor(x)` rounded to the instruction's precision, which is 1.0 at these edges. I think the hardware never returns 1.0 here: - The RDNA2 ISA reference guide (12.8, `V_FRACT_F64`, `_F32` and `_F16`) says the instruction "complies with the DX specification of fract", and DirectX's `frc` gives a result in [+0, 1) for every finite input (https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/frc--sm4---asm-). - LLVM's AMDGPU backend relies on the same thing: on every target without the SI/CI fract bug it replaces `minnum(x - floor(x), nextafter(1.0, -1.0))` with the hardware `v_fract` (`matchFractPatImpl` in `AMDGPUCodeGenPrepare.cpp`), and `IntrinsicsAMDGPU.td` says `v_fract` "may return 1.0" only on SI/CI. So on RDNA2, `v_fract_f64(-2^-1074)` should be `0x3fefffffffffffff`, and `v_fract_f16` of those three halves `0x3bff`: the largest values below 1.0. Did you capture these edge values on real hardware? If so, I'm wrong and AMD's driver is the one that differs. If not, I'm happy to send a PR for it: - the expectations become `0x3fefffffffffffff` and, for the three half cases, `0x3bff`; - Kyty clamps the result below 1.0 after rounding to the instruction's precision, keeping NaN inputs NaN, so every host returns the same value. **AI use** (per the README's AI policy): Claude (Anthropic) found the failing cases with a nightly test run on my machine and looked up the ISA guide and the LLVM sources; I reviewed the findings.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:47:52 +00:00
Author
Owner

@⁠Nawid3333 on GitHub · 2026-10-16 12:00 UTC · original

Hardware results for this. A small compute shader runs fract() on f64, f32 and f16 inputs; VK_AMD_shader_info shows each call compiles to a single v_fract_f64/v_fract_f32/v_fract_f16, so these are the instruction's own results. RX 9070 XT (RDNA4) and Ryzen 5 7600X iGPU (RDNA2), driver 26.8.1, Windows 11, both identical:

input V_FRACT (both GPUs) x - floor(x), rounded to nearest even
f64 0x8000000000000001 (-2^-1074), 0xbc90000000000000 (-2^-54) 0x3fefffffffffffff 0x3ff0000000000000
f32 0xb3000000 (-2^-25), 0xb2ffffff, 0x80000001 (-2^-149)* 0x3f7fffff 0x3f800000
f16 0x8001, 0x83ff, 0x8c00, 0x8bff 0x3bff 0x3c00

* only with DenormPreserve for 32 bits; without it the result is 0.

The other inputs I tried (-1.2, 1.2, -0, -(1 - 2^-53), +2^-1074, -(2^52 + 1)) match x - floor(x), and the results are the same with SignedZeroInfNanPreserve 32/64, which Kyty's modules set. So the hardware returned a value below 1.0 for every finite input I tried, while VectorFractF64CapturedAndEdges and VectorFractF16CapturedAndEdges expect 1.0 at these edges.

Kyty emits GLSL.std.450 Fract (V_FRACT_F16 on the widened f32 value). Vulkan's precision table requires fract to be correctly rounded, which gives 1.0 here, but both AMD drivers return the hardware value, so the result depends on the host. The f16 case gives 0x3bff on AMD today only because AMD's PackHalf2x16 truncates (the 1-ulp errors in #1204); with round-to-nearest-even it becomes 0x3c00. To match the hardware on every host (if the PS5 behaves like RDNA2/4), Kyty could round to the instruction's precision and then clamp to the largest value below 1.0. With #1204's rounding plus that clamp, and the two tests expecting the hardware values, both cases pass on the 9070 XT.

Probe shader
layout(local_size_x = 64) in;
layout(std430, set = 0, binding = 0) buffer Io { uint w[]; };
void main() {
  const uint i = gl_LocalInvocationIndex;
  if (i < w[0]) {
    const uvec2 r = unpackDouble2x32(fract(packDouble2x32(uvec2(w[16 + 2 * i], w[17 + 2 * i]))));
    w[64 + 2 * i] = r.x; w[65 + 2 * i] = r.y;
  }
  if (i < w[1]) w[160 + i] = floatBitsToUint(fract(uintBitsToFloat(w[128 + i])));
  if (i < w[2]) {
    const f16vec2 h = unpackFloat2x16(w[256 + i]);
    w[288 + i] = packFloat2x16(f16vec2(fract(h.x), float16_t(0.0)));
  }
}

While running these tests I also found a few bugs in AMD's driver; I'm reporting them to AMD.

Written with Claude Code (AI). It wrote the probe, ran it on my PC and checked the results; I reviewed this comment before posting it.

**@⁠Nawid3333** on GitHub · 2026-10-16 12:00 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1209#issuecomment-6215026480) Hardware results for this. A small compute shader runs `fract()` on f64, f32 and f16 inputs; `VK_AMD_shader_info` shows each call compiles to a single `v_fract_f64`/`v_fract_f32`/`v_fract_f16`, so these are the instruction's own results. RX 9070 XT (RDNA4) and Ryzen 5 7600X iGPU (RDNA2), driver 26.8.1, Windows 11, both identical: | input | V_FRACT (both GPUs) | x - floor(x), rounded to nearest even | |---|---|---| | f64 `0x8000000000000001` (-2^-1074), `0xbc90000000000000` (-2^-54) | `0x3fefffffffffffff` | `0x3ff0000000000000` | | f32 `0xb3000000` (-2^-25), `0xb2ffffff`, `0x80000001` (-2^-149)* | `0x3f7fffff` | `0x3f800000` | | f16 `0x8001`, `0x83ff`, `0x8c00`, `0x8bff` | `0x3bff` | `0x3c00` | \* only with `DenormPreserve` for 32 bits; without it the result is 0. The other inputs I tried (-1.2, 1.2, -0, -(1 - 2^-53), +2^-1074, -(2^52 + 1)) match `x - floor(x)`, and the results are the same with `SignedZeroInfNanPreserve` 32/64, which Kyty's modules set. So the hardware returned a value below 1.0 for every finite input I tried, while `VectorFractF64CapturedAndEdges` and `VectorFractF16CapturedAndEdges` expect 1.0 at these edges. Kyty emits `GLSL.std.450 Fract` (V_FRACT_F16 on the widened f32 value). Vulkan's precision table requires `fract` to be correctly rounded, which gives 1.0 here, but both AMD drivers return the hardware value, so the result depends on the host. The f16 case gives `0x3bff` on AMD today only because AMD's `PackHalf2x16` truncates (the 1-ulp errors in #1204); with round-to-nearest-even it becomes `0x3c00`. To match the hardware on every host (if the PS5 behaves like RDNA2/4), Kyty could round to the instruction's precision and then clamp to the largest value below 1.0. With #1204's rounding plus that clamp, and the two tests expecting the hardware values, both cases pass on the 9070 XT. <details><summary>Probe shader</summary> ```glsl layout(local_size_x = 64) in; layout(std430, set = 0, binding = 0) buffer Io { uint w[]; }; void main() { const uint i = gl_LocalInvocationIndex; if (i < w[0]) { const uvec2 r = unpackDouble2x32(fract(packDouble2x32(uvec2(w[16 + 2 * i], w[17 + 2 * i])))); w[64 + 2 * i] = r.x; w[65 + 2 * i] = r.y; } if (i < w[1]) w[160 + i] = floatBitsToUint(fract(uintBitsToFloat(w[128 + i]))); if (i < w[2]) { const f16vec2 h = unpackFloat2x16(w[256 + i]); w[288 + i] = packFloat2x16(f16vec2(fract(h.x), float16_t(0.0))); } } ``` </details> While running these tests I also found a few bugs in AMD's driver; I'm reporting them to AMD. Written with Claude Code (AI). It wrote the probe, ran it on my PC and checked the results; I reviewed this comment before posting it.
Author
Owner

@⁠Nawid3333 on GitHub · 2026-10-17 02:46 UTC · original

@⁠Nmzik for the tiny negative inputs: could you confirm which result the tests should expect, 1.0 as they do now, or the largest value below 1.0 that V_FRACT returns on both GPUs here (see my comment above)? I'll change the tests or the emitter to match whichever you choose.

Written with Claude Code (AI); I read this comment before posting it.

**@⁠Nawid3333** on GitHub · 2026-10-17 02:46 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1209#issuecomment-6228664904) @⁠Nmzik for the tiny negative inputs: could you confirm which result the tests should expect, 1.0 as they do now, or the largest value below 1.0 that V_FRACT returns on both GPUs here (see my comment above)? I'll change the tests or the emitter to match whichever you choose. Written with Claude Code (AI); I read this comment before posting it.
Author
Owner

@⁠ZrDaniel07 on GitHub · 2026-10-22 03:18 UTC · original

NVIDIA data point: on RTX 5090 (driver 617.42), main @ 8502d8a, VectorFractF64CapturedAndEdges and VectorFractF16CapturedAndEdges pass with the current expectations (1.0 at the tiny-negative edges). So NVIDIA's GLSLstd450Fract gives x - floor(x) rounded (1.0) there, unlike the AMD hardware v_fract results above. If the expectations change to the largest value below 1.0, the emitter clamp would be needed for NVIDIA to keep passing.

AI use: Claude Code helped me build and run the tests; I checked the output myself.

**@⁠ZrDaniel07** on GitHub · 2026-10-22 03:18 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1209#issuecomment-6331828308) NVIDIA data point: on RTX 5090 (driver 617.42), `main` @ 8502d8a, `VectorFractF64CapturedAndEdges` and `VectorFractF16CapturedAndEdges` pass with the current expectations (1.0 at the tiny-negative edges). So NVIDIA's `GLSLstd450Fract` gives `x - floor(x)` rounded (1.0) there, unlike the AMD hardware `v_fract` results above. If the expectations change to the largest value below 1.0, the emitter clamp would be needed for NVIDIA to keep passing. AI use: Claude Code helped me build and run the tests; I checked the output myself.
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/KytyPS5--KytyPS5#1209
No description provided.