Skip to content

docs/design.md: v_sqrt_f64 on RDNA2 is not correctly rounded (PROC-216) - #4293

Open
etcetera-yt wants to merge 2 commits into
ItsNotRexed:mainfrom
etcetera-yt:proc-216-v-sqrt-f64-rdna2
Open

etcetera-yt wants to merge 2 commits into
ItsNotRexed:mainfrom
etcetera-yt:proc-216-v-sqrt-f64-rdna2

Conversation

@etcetera-yt

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: PROC-216 (claim; @fiestorra's measurement)

What and why

The docs/design.md row for v_rcp_f64, v_rsq_f64 and v_sqrt_f64 now records @fiestorra's RDNA2 measurement:

  • On a gfx1036 (Windows driver 31.0.24002.92, tools/hw_oracle raw kernels, IEEE on, f64 denormals kept), v_sqrt_f64 isn't the correctly rounded root. The four sample words, such as sqrt(4.0) giving 0x4000000004000000 (2.0000000298), are each 2^26 ulps off, so about 26 of the 53 significand bits are right.
  • The translator's lowering, GLSL.std.450 Sqrt (tools/rdna2spirv.py ~6278), is therefore the more accurate one. A difference from the instruction on RDNA2 hardware isn't a translator bug.
  • v_rcp_f64 and v_rsq_f64 were exact, but only on powers of two, so the row says their accuracy is unmeasured.
  • None of the three occurs in the 26,503 game listings.

Docs only: one row, no code change. Nothing is left for this item.

#3642 (@kapitche) also edits docs/design.md for these three ops. It adds a separate "f64 literal sources" row and doesn't touch this one.

Tested

Windows 10 19045, Python 3.14. The numbers are @fiestorra's measurement on AMD hardware; I have no RDNA2 GPU, so I didn't re-measure them. I checked the row against the measurement comment and against the translator's lowering in tools/rdna2spirv.py.

python -I tests/test_docs_design_rows.py
python -I tests/test_docs_links.py
python -I tests/test_docs_repo_paths.py

All three are OK on main 03b09a08 and on this PR.

Not tested: re-measuring on RDNA2 (needs an AMD GPU and tools/hw_oracle); NVIDIA GeForce RTX 5060 Ti (driver 621.14) is the only GPU here, and the change is docs only.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. (The 26,503 figure is a count only.)
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (This is a measurement note on an existing row, with its source.)

AI-assisted (Claude Code).

🤖 Generated with Claude Code

@fiestorra measured v_sqrt_f64 on a gfx1036 with tools/hw_oracle: each
sample is 2^26 ulps from the exact root (about 26 of 53 significand
bits). The v_rcp_f64/v_rsq_f64/v_sqrt_f64 row now records that, that
GLSL.std.450 Sqrt (the translator's lowering) is the more accurate one,
that rcp/rsq were only tried on powers of two, and that none of the
three occurs in the 26,503 listings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Kam471

Kam471 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Duplicate note: #4292 (\ded95210) documents the same PROC-216 finding in the same \docs/design.md\ row. Docs tests pass at both heads (6 links + 2 design rows). This PR has the PROC-216 claim (2026-11-23T00:34:20Z) and the fuller measurement/attribution, so under the coordination rule it should land and #4292 close; #4292 opened 17 seconds earlier but has no claim link.

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct the per-sample ULP statement in the documented measurement before merging.

Comment thread docs/design.md Outdated
| `v_cvt_f32_f64` output modifiers | Convert to f32, apply `mul:2`, `mul:4`, or `div:2` if present, then apply `clamp` to [0, 1] if requested. [LLVM GFX10 assembler tests](https://github.com/llvm/llvm-project/blob/main/llvm/test/MC/AMDGPU/gfx10_asm_vop1.s#L3044-L3054) confirm the modifier encodings. `tests/test_f64_conversion.py` checks the resulting SPIR-V data flow. |
| `v_add_f64` output modifiers | The double sum is scaled by `mul:2`, `mul:4` or `div:2` if present, then clamped to [0, 1] if `clamp` is set, as for f32 (`clamp_mods_f64`, GLSL.std.450 `FClamp` on the double, so a NaN result and the sign of a -0 result are left to the driver, also as for f32). **Until 2026-11-22 both were ignored.** Source: LLVM AMDGPU defines it as `VOP3Inst<"v_add_f64", VOP3_Profile<VOP_F64_F64_F64>>` (VOP3Instructions.td), and `VOPProfile` sets `HasClamp` (f64 is a modifier type) and `HasOMod` (floating-point destination) (SIInstrInfo.td). It is the only f64 arithmetic the translator handles; `v_cvt_f32_f64` has the row above. `tests/test_f64_output_modifiers.py` checks the SPIR-V data flow; `runtime/test/shaders/ps_f64_mods.asm` is the GPU test (needs `shaderFloat64`, which `vk_harness` now enables when the fragment shader declares `Float64`), passed on an NVIDIA RTX 6000 Ada (driver 615.71.09, Linux; 4096/4096 pixels exact, and the `main` translator gives the predicted old words). |
| `v_rcp_f64`, `v_rsq_f64`, `v_sqrt_f64` | Double-precision reciprocal (`OpFDiv %double 1.0 x`), inverse square root (`GLSL.std.450 InverseSqrt`), and square root (`GLSL.std.450 Sqrt`), requiring `Float64` (`shaderFloat64`). VOP3 input modifiers (`abs`, `neg`) apply to the double operand, and output modifiers (`mul:2`, `mul:4`, `div:2`, then `clamp` to [0, 1]) apply via `clamp_mods_f64` (LLVM `VOP1Inst` / `VOP_F64_F64` in `VOP1Instructions.td`, `HasClamp` and `HasOMod` via `VOPProfile`). Special-value edge cases (`±0`, negative inputs, NaN) follow the driver's SPIR-V `OpFDiv`/`InverseSqrt`/`Sqrt` rules, as for the f32 forms. Checked by `tests/test_f64_transcendentals.py` (SPIR-V data flow and `spirv-val`). |
| `v_rcp_f64`, `v_rsq_f64`, `v_sqrt_f64` | Double-precision reciprocal (`OpFDiv %double 1.0 x`), inverse square root (`GLSL.std.450 InverseSqrt`), and square root (`GLSL.std.450 Sqrt`), requiring `Float64` (`shaderFloat64`). VOP3 input modifiers (`abs`, `neg`) apply to the double operand, and output modifiers (`mul:2`, `mul:4`, `div:2`, then `clamp` to [0, 1]) apply via `clamp_mods_f64` (LLVM `VOP1Inst` / `VOP_F64_F64` in `VOP1Instructions.td`, `HasClamp` and `HasOMod` via `VOPProfile`). Special-value edge cases (`±0`, negative inputs, NaN) follow the driver's SPIR-V `OpFDiv`/`InverseSqrt`/`Sqrt` rules, as for the f32 forms. Checked by `tests/test_f64_transcendentals.py` (SPIR-V data flow and `spirv-val`). **Measured on RDNA2** (gfx1036 iGPU, Windows driver 31.0.24002.92, `tools/hw_oracle` raw kernels, MODE with IEEE on and f64 denormals kept): `v_sqrt_f64` is not the correctly rounded root. `sqrt(4.0)` stored `0x4000000004000000` (2.0000000298), `sqrt(9.0)` `0x4008000004000000`, `sqrt(0.25)` `0x3fe0000004000000` and `sqrt(2.0)` `0x3ff6a09e64000000` (binary64: 1.4142135624), each 2^26 ulps from the exact result, so about 26 of the 53 significand bits are right. `GLSL.std.450 Sqrt` is the more accurate lowering, and a difference from the instruction on RDNA2 hardware is not a translator bug. `v_rcp_f64` and `v_rsq_f64` returned exact values, but only powers of two were tried, so their accuracy is unmeasured. None of the three occurs in the 26,503 game listings ([measurement](https://github.com/ItsNotRexed/Leonida/issues/4084#issuecomment-7088412190), @fiestorra). |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Report the measured ULP error per sample accurately. The first three values differ from the correctly rounded results by exactly +2^26 ulps, but the sqrt(2.0) result 0x3ff6a09e64000000 is −41,892,813 ulps from 0x3ff6a09e667f3bcd, not 2^26. Please give the per-sample differences or qualify the text as approximate; otherwise the row materially overstates the last measurement.

The first three samples are +2^26 ulps from the correctly rounded root,
but sqrt(2.0) is -41,892,813 ulps (about -2^25.3) from 0x3ff6a09e667f3bcd,
not 2^26 (@Flyander's review).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@etcetera-yt

Copy link
Copy Markdown
Contributor Author

Thanks @Flyander, you're right. sqrt(2.0)'s 0x3ff6a09e64000000 is −41,892,813 ulps from 0x3ff6a09e667f3bcd, not 2^26. I had carried "each 2^26 ulps" over from the measurement comment.

Second commit f41cabff: the row now says the first three samples are exactly +2^26 ulps from the correctly rounded root, and sqrt(2.0) is −41,892,813 ulps (about −2^25.3), so only about 26 of the 53 significand bits are right. I re-checked all four differences against Python's math.sqrt bit patterns (+67108864, +67108864, +67108864, -41892813). The docs tests are still OK (tests/test_docs_design_rows.py, tests/test_docs_links.py, tests/test_docs_repo_paths.py).

Thanks also @Kam471 for the overlap note on #4292. AI-assisted (Claude Code).

@kmorales13

Copy link
Copy Markdown
Contributor

Independent re-measurement on gfx1036 through the Linux DRM launcher (not the Windows HIP one): all four words reproduce bit for bit. Also applies to #4371, which edits the same row.

Setup: CachyOS Linux, Ryzen 7 7700X iGPU (gfx1036, 0x164e), libdrm_amdgpu 2.4.134, clang 23.1.5. I used raw tools/hw_oracle kernels (v_mov_b32 ×2, the op on v[0:1], global_store_dwordx2) in wave32 and wave64, in two MODEs: ieee_mode 1 + denorm_mode_16_64 3, and the game's default (ieee 0, dx10_clamp 1, denorm16_64 3). I ran it twice. Both runs were identical, and wave32 and wave64 agreed on every word.

v_sqrt_f64, the PR's four inputs:

x hw correctly rounded ulps
4.0 0x4000000004000000 0x4000000000000000 +67108864 (2^26)
9.0 0x4008000004000000 0x4008000000000000 +67108864
0.25 0x3fe0000004000000 0x3fe0000000000000 +67108864
2.0 0x3ff6a09e64000000 0x3ff6a09e667f3bcd −41892813

Other inputs also come out off by about 2^24 to 2^27 ulps: 3.0, 1.5, 10.0, 1e-300 and 1e300.

The row says v_rcp_f64 and v_rsq_f64 were unmeasured on non-powers of two. They aren't correctly rounded either:

  • v_rcp_f64 gives 3.0 → 0x3fd5555554000000 (−22369621 ulps), 7.0 → +28760942 and 10.0 → +40265318.
  • v_rsq_f64 gives 2.0 → 0x3fe6a09e60000000 (−109001676), 3.0 → −26227485 and 10.0 → +28817137.
  • Both are exact on the powers of two tried (rcp 2.0, rsq 4.0).

Both MODEs gave the same words, so the error doesn't depend on IEEE mode. No game files. AI-assisted (Claude-Opus).

@etcetera-yt

Copy link
Copy Markdown
Contributor Author

Thanks @kmorales13 for the independent re-measurement on Linux DRM, and for the rcp/rsq numbers. AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants