fix(shader): round v_cvt_f32_f64 in the FLOAT_MODE f32 round mode #21508

Merged
angrysandhill merged 1 commit from upstream-pr/21508 into main 2026-11-27 17:55:35 +00:00
angrysandhill commented 2026-11-16 09:14:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by angrysandhill on 2026-11-16T09:14:41Z.


What

v_cvt_f32_f64 rounded to nearest even whatever the FLOAT_MODE f32 round mode. Measured on RDNA2 (gfx1031, RX 6750 XT; the PS5's gfx1013 is not measured), rounding 1 to 3 rounds the f64 value up, down or toward zero. This is the gap the "v_cvt_f32_f64 f32 result" debt entry recorded after #18487.

What the hardware does in the three directed modes:

  • Inexact values round toward the side the mode names, so 0x3ff0000000000001 gives 0x3f800001 when rounding up and 0x3f800000 otherwise.
  • An overflow gives infinity on the side the mode rounds toward and the largest finite f32 on the other: 0x47f0000000000000 gives 0x7f800000 when rounding up and 0x7f7fffff when rounding down or toward zero.
  • Below the smallest f32 denormal, with f32 denormal results kept, the result is that denormal on the side the mode rounds toward and zero on the other: 0x3680000000000000 gives 0x00000001 when rounding up.
  • An f64 denormal source counts as zero when bit 0 of the f64 denormal field is clear, and as a tiny value when it is set (0x0000000000000001 rounded up gives 0x00000001 only then).

The change:

  • float64Operation: with a known float mode and f32 rounding 1 to 3, the f32 bits are built from the two halves of the f64 source with integer operations: the truncated value, plus one when the value is inexact and the mode rounds away from zero for that sign. Infinities and NaNs keep the existing conversion. The tiny-result flush of #18487 still runs after it, unchanged. Round to nearest is untouched.
  • RoundingUseOf: the conversion becomes Directed32, so a round mode set at run time (s_round_mode, s_setreg) is accepted for it where it threw "does not model directed rounding". It now also declares bit 0 of the f64 denormal field, which the directed result reads.
  • Debt entry: rewritten through a fragment to what is still open: gfx1013 is not measured, and an output modifier that overflows the converted value still gives infinity in these modes.

Not in this change: mul:2 and mul:4 of a value the modifier overflows. gfx1031 rounds that overflow as the mode says and the translation gives infinity. That is the f32 output modifier, which the debt file already records, and #19710 works on directed overflow for the f32 arithmetic.

Overlap: #19505 edits the RoundingUseOf cases directly below the VCvtF32F64 case, so whichever merges second has a small textual conflict there; it is different work (scalar f16 arithmetic). #19605 and #19563 change other functions of Float64Instructions.cpp. No open PR mentions v_cvt_f32_f64.

Tested

Setup: Windows 10 Home 22H2 (10.0.19045), AMD Radeon RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (driver 32.0.21045.16010), WinLibs GCC 15.2.0 r7 UCRT, Release, BUILD_TESTING=ON. main is 0694c4cbd6, which this branch is based on.

RDNA2 measurement (hardware oracle, HIP runtime), compared value by value with the recompiler through HW_ORACLE_RUNTIME=vulkan.

  • Sources: 104 f64 values (52 magnitudes, both signs): zero, f64 denormals, the smallest f64 normal, values around the smallest f32 denormal (2^-151 to 1.5 × 2^-148), the band around 2^-126, values one bit and half an f32 ulp either side of 1, pi, values around the largest finite f32 and 2^128, the largest finite f64, infinity and a NaN.
  • Modes: 128 in all: f32 rounding 0 to 3, the f32 denormal field 0 to 3, the f64 denormal field 0 to 3, IEEE 0 and 1. DX10_CLAMP 1.

Values differing from RDNA2, of 3,328 per round mode:

f32 rounding main this PR
0 (nearest even) 0 0
1 (up) 872 0
2 (down) 872 0
3 (toward zero) 1,056 0

A bit-level model of the rule above predicts all 13,312 measured values.

Tests:

  • New agc_driver_cvt_f32_f64_directed_rounding: 96 sources through 8 forms (plain, -src, |src|, clamp, -|src|, -src with clamp, the _e64 encoding, |src| with clamp) in FLOAT_MODE 0xf1 to 0xf3, 0xd1 to 0xd3, 0xc1 to 0xc3 and 0xb1 to 0xb3: rounding up, down and toward zero with f32 denormals kept, with f32 results flushed, with f32 inputs and results flushed, and with f64 denormal inputs flushed. The expected values are RDNA2's; the recompiler with this change matches all 9,216.
    • Runs: with the emulated flush always and, where Target() uses it, with the hardware flush. On a device without DenormPreserve 32 it leaves out the expected values that are f32 denormals, and the last line says how many. Here both passes run and nothing is left out.
    • On main with only the test files added it fails: v_cvt_f32_f64 directed rounding: emulated flush FLOAT_MODE 0x000000f1 row 2 v_cvt_f32_f64 v10, v[4:5] of 0x0000000000000001 is 0x00000000, expected 0x00000001.
  • agc_driver_cvt_f32_f64_denormal_result: the directed modes no longer leave out 0x381ffffff0000000, the normal source that needs rounding. On main with that row back the test fails: FLOAT_MODE 0x000000d1 IEEE=0 lane 30 v_cvt_f32_f64 v14, -v[4:5] of 0x381ffffff0000000 is 0x81000000, expected 0x80ffffff. mul:2 and mul:4 of the largest finite f32 stay left out (the output modifier, above).
  • agc_shader_mode_register: the run-time rounding check (directed32) now includes the conversion: a round mode set by s_round_mode must translate like the same mode declared, and unlike round to nearest. On main with the test change it fails: instruction (opcode 15) at pc 4 runs with MODE rounding 193 set at run time, and the instruction does not model directed rounding.
  • With this change the three tests and agc_driver_cvt_f32_f64_output_modifier pass.
  • Full ^(agc_|shader|vector_), 4 at a time: 1,940 tests: 1,916 passed, 22 skipped, 1 failed, 1 not run.
    • The failure: agc_driver_stencil_plane_memory fails on main 0694c4cbd6 too, 3 of 3 runs on this device.
    • Not run: agc_driver_upload_cache_read_only_flush does not compile with this toolchain on main (setenv is not declared in UploadCacheReadOnlyFlush.cpp).
    • agc_driver_store_over_untracked passed in this run; it fails intermittently on main on this device (2 of 3 runs).
  • Validation layer: agc_driver_cvt_f32_f64_directed_rounding, agc_driver_cvt_f32_f64_denormal_result, agc_driver_cvt_f32_f64_output_modifier, agc_driver_f64_rounding, agc_driver_f64_rounding_denormal_modes and agc_driver_nan_quieting, one at a time with ctest -V, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer, ANYPS5_NO_SHADER_CACHE=1 and ANYPS5_REQUIRE_VULKAN=1: all pass, every one logs the layer insertion, and no log has a VUID- line or was not found but was requested by env var VK_INSTANCE_LAYERS.
  • python tools/check_conventions.py --base origin/main: all conventions checks passed. tools/debt_fold.py --check stops on main at another fragment (docs-nkwebkit-drop-readded-count-lines.md), so this PR's fragment was checked by hand: its change key occurs in exactly one entry of shader.md.

Not measured: the PS5's gfx1013, and hosts other than this one.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (32.0.21045.16010), Windows 10 22H2
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5334260529 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21508). Originally posted by [angrysandhill](https://github.com/angrysandhill) on 2026-11-16T09:14:41Z. --- ### What `v_cvt_f32_f64` rounded to nearest even whatever the `FLOAT_MODE` f32 round mode. Measured on RDNA2 (gfx1031, RX 6750 XT; the PS5's gfx1013 is not measured), rounding 1 to 3 rounds the f64 value up, down or toward zero. This is the gap the "`v_cvt_f32_f64` f32 result" debt entry recorded after #18487. What the hardware does in the three directed modes: - **Inexact values** round toward the side the mode names, so `0x3ff0000000000001` gives `0x3f800001` when rounding up and `0x3f800000` otherwise. - **An overflow** gives infinity on the side the mode rounds toward and the largest finite f32 on the other: `0x47f0000000000000` gives `0x7f800000` when rounding up and `0x7f7fffff` when rounding down or toward zero. - **Below the smallest f32 denormal**, with f32 denormal results kept, the result is that denormal on the side the mode rounds toward and zero on the other: `0x3680000000000000` gives `0x00000001` when rounding up. - **An f64 denormal source** counts as zero when bit 0 of the f64 denormal field is clear, and as a tiny value when it is set (`0x0000000000000001` rounded up gives `0x00000001` only then). The change: - **`float64Operation`:** with a known float mode and f32 rounding 1 to 3, the f32 bits are built from the two halves of the f64 source with integer operations: the truncated value, plus one when the value is inexact and the mode rounds away from zero for that sign. Infinities and NaNs keep the existing conversion. The tiny-result flush of #18487 still runs after it, unchanged. Round to nearest is untouched. - **`RoundingUseOf`:** the conversion becomes `Directed32`, so a round mode set at run time (`s_round_mode`, `s_setreg`) is accepted for it where it threw "does not model directed rounding". It now also declares bit 0 of the f64 denormal field, which the directed result reads. - **Debt entry:** rewritten through a fragment to what is still open: gfx1013 is not measured, and an output modifier that overflows the converted value still gives infinity in these modes. Not in this change: `mul:2` and `mul:4` of a value the modifier overflows. gfx1031 rounds that overflow as the mode says and the translation gives infinity. That is the f32 output modifier, which the debt file already records, and #19710 works on directed overflow for the f32 arithmetic. Overlap: #19505 edits the `RoundingUseOf` cases directly below the `VCvtF32F64` case, so whichever merges second has a small textual conflict there; it is different work (scalar f16 arithmetic). #19605 and #19563 change other functions of `Float64Instructions.cpp`. No open PR mentions `v_cvt_f32_f64`. ### Tested **Setup:** Windows 10 Home 22H2 (10.0.19045), AMD Radeon RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (driver 32.0.21045.16010), WinLibs GCC 15.2.0 r7 UCRT, Release, `BUILD_TESTING=ON`. `main` is `0694c4cbd6`, which this branch is based on. **RDNA2 measurement** ([hardware oracle](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/HW_ORACLE.md), HIP runtime), compared value by value with the recompiler through `HW_ORACLE_RUNTIME=vulkan`. - **Sources:** 104 f64 values (52 magnitudes, both signs): zero, f64 denormals, the smallest f64 normal, values around the smallest f32 denormal (2^-151 to 1.5 × 2^-148), the band around 2^-126, values one bit and half an f32 ulp either side of 1, pi, values around the largest finite f32 and 2^128, the largest finite f64, infinity and a NaN. - **Modes:** 128 in all: f32 rounding 0 to 3, the f32 denormal field 0 to 3, the f64 denormal field 0 to 3, IEEE 0 and 1. DX10_CLAMP 1. Values differing from RDNA2, of 3,328 per round mode: | f32 rounding | `main` | this PR | |---|---|---| | 0 (nearest even) | 0 | 0 | | 1 (up) | 872 | 0 | | 2 (down) | 872 | 0 | | 3 (toward zero) | 1,056 | 0 | A bit-level model of the rule above predicts all 13,312 measured values. **Tests:** - **New `agc_driver_cvt_f32_f64_directed_rounding`:** 96 sources through 8 forms (plain, `-src`, `|src|`, `clamp`, `-|src|`, `-src` with `clamp`, the `_e64` encoding, `|src|` with `clamp`) in FLOAT_MODE `0xf1` to `0xf3`, `0xd1` to `0xd3`, `0xc1` to `0xc3` and `0xb1` to `0xb3`: rounding up, down and toward zero with f32 denormals kept, with f32 results flushed, with f32 inputs and results flushed, and with f64 denormal inputs flushed. The expected values are RDNA2's; the recompiler with this change matches all 9,216. - **Runs:** with the emulated flush always and, where `Target()` uses it, with the hardware flush. On a device without `DenormPreserve 32` it leaves out the expected values that are f32 denormals, and the last line says how many. Here both passes run and nothing is left out. - **On `main` with only the test files added** it fails: `v_cvt_f32_f64 directed rounding: emulated flush FLOAT_MODE 0x000000f1 row 2 v_cvt_f32_f64 v10, v[4:5] of 0x0000000000000001 is 0x00000000, expected 0x00000001`. - **`agc_driver_cvt_f32_f64_denormal_result`:** the directed modes no longer leave out `0x381ffffff0000000`, the normal source that needs rounding. On `main` with that row back the test fails: `FLOAT_MODE 0x000000d1 IEEE=0 lane 30 v_cvt_f32_f64 v14, -v[4:5] of 0x381ffffff0000000 is 0x81000000, expected 0x80ffffff`. `mul:2` and `mul:4` of the largest finite f32 stay left out (the output modifier, above). - **`agc_shader_mode_register`:** the run-time rounding check (`directed32`) now includes the conversion: a round mode set by `s_round_mode` must translate like the same mode declared, and unlike round to nearest. On `main` with the test change it fails: `instruction (opcode 15) at pc 4 runs with MODE rounding 193 set at run time, and the instruction does not model directed rounding`. - **With this change** the three tests and `agc_driver_cvt_f32_f64_output_modifier` pass. - **Full `^(agc_|shader|vector_)`, 4 at a time:** 1,940 tests: 1,916 passed, 22 skipped, 1 failed, 1 not run. - **The failure:** `agc_driver_stencil_plane_memory` fails on `main` `0694c4cbd6` too, 3 of 3 runs on this device. - **Not run:** `agc_driver_upload_cache_read_only_flush` does not compile with this toolchain on `main` (`setenv` is not declared in `UploadCacheReadOnlyFlush.cpp`). - `agc_driver_store_over_untracked` passed in this run; it fails intermittently on `main` on this device (2 of 3 runs). - **Validation layer:** `agc_driver_cvt_f32_f64_directed_rounding`, `agc_driver_cvt_f32_f64_denormal_result`, `agc_driver_cvt_f32_f64_output_modifier`, `agc_driver_f64_rounding`, `agc_driver_f64_rounding_denormal_modes` and `agc_driver_nan_quieting`, one at a time with `ctest -V`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `VK_LOADER_DEBUG=layer`, `ANYPS5_NO_SHADER_CACHE=1` and `ANYPS5_REQUIRE_VULKAN=1`: all pass, every one logs the layer insertion, and no log has a `VUID-` line or `was not found but was requested by env var VK_INSTANCE_LAYERS`. - `python tools/check_conventions.py --base origin/main`: all conventions checks passed. `tools/debt_fold.py --check` stops on `main` at another fragment (`docs-nkwebkit-drop-readded-count-lines.md`), so this PR's fragment was checked by hand: its `change` key occurs in exactly one entry of `shader.md`. **Not measured:** the PS5's gfx1013, and hosts other than this one. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (32.0.21045.16010), Windows 10 22H2 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-16 10:13:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T10:13:04Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the implementation and regression coverage support merging.

What I ran

Reviewed head e48bb23f4a4e1c60519ebf294be66e8ec4590951. I inspected the prepared diff and targeted code through repo_read.py; I did not execute commands or tests. The PR description reports hardware-oracle and test runs, which I did not independently run.

Results

Check Result Note
Diff and changed code passed Directed conversion and runtime mode coverage inspected
Tests in this review not run Author-reported results only
PS5 gfx1013 behavior not run Not measured in the PR

Negative control

Not run by me. The author reports that the new regression tests fail on main with the test changes alone.

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

No tests were run in this review, and gfx1013/PS5 behavior remains unmeasured as stated by the author. I read every changed file listed in the review bundle, including all 1,343 lines of the new regression test; the prepared manifest listed no excluded changed files.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5918285372 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21508#pullrequestreview-5918285372). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-16T10:13:04Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the implementation and regression coverage support merging. ## What I ran Reviewed head `e48bb23f4a4e1c60519ebf294be66e8ec4590951`. I inspected the prepared diff and targeted code through `repo_read.py`; I did not execute commands or tests. The PR description reports hardware-oracle and test runs, which I did not independently run. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | Directed conversion and runtime mode coverage inspected | | Tests in this review | not run | Author-reported results only | | PS5 gfx1013 behavior | not run | Not measured in the PR | ## Negative control Not run by me. The author reports that the new regression tests fail on `main` with the test changes alone. ## Findings ### Blocking None. ### Non-blocking None. ## Sources - [Float64 conversion change](https://github.com/boykopovar/AnyPS5/blob/e48bb23f4a4e1c60519ebf294be66e8ec4590951/core/shader/recompiler/Translation/src/Float64Instructions.cpp#L40-L70) - [Rounding-mode classification](https://github.com/boykopovar/AnyPS5/blob/e48bb23f4a4e1c60519ebf294be66e8ec4590951/core/shader/recompiler/Translation/src/RoundingMode.cpp#L149-L150) - [Directed rounding regression test](https://github.com/boykopovar/AnyPS5/blob/e48bb23f4a4e1c60519ebf294be66e8ec4590951/core/libs/prx/libSceAgcDriver/tests/execution/CvtF32F64DirectedRounding.cpp#L1269-L1337) ## Not verified No tests were run in this review, and gfx1013/PS5 behavior remains unmeasured as stated by the author. I read every changed file listed in the review bundle, including all 1,343 lines of the new regression test; the prepared manifest listed no excluded changed files. ## To get this to a good state Nothing is required.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
boykopovar/AnyPS5!21508
No description provided.