fix(shader): round v_div_fixup overflow and underflow in the MODE round mode #19563

Merged
Nawid3333 merged 1 commit from upstream-pr/19563 into main 2026-11-21 19:59:50 +00:00
Nawid3333 commented 2026-11-13 15:00:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Nawid3333 on 2026-11-13T15:00:26Z.


What

v_div_fixup_f16, v_div_fixup_f32 and v_div_fixup_f64 write the overflow value of the MODE round mode, and v_div_fixup_f32 and v_div_fixup_f64 also its underflow value, as RDNA2 does, instead of an infinity and a signed zero in every mode.

  • Overflow: with an infinite or NaN quotient (src0) and no special case of the denominator or numerator (NaN, 0/0, inf/inf, x/0, inf/y, x/inf, 0/y) and, for f32 and f64, no underflow (below), the result is the largest finite value of the result's sign when the round mode takes that sign toward zero (round toward zero; toward -infinity for a positive result; toward +infinity for a negative one), and infinity otherwise. FP16_OVFL still writes ±0x7bff for the f16 form in every mode. Example: v_div_fixup_f16(0x7c00, 1.0, 1.0) with FLOAT_MODE round16 3 is 0x7bff on hardware; main writes 0x7c00.
  • Underflow (f32 and f64): a result whose numerator and denominator exponents differ by less than -150 (f32) or -1075 (f64) is the smallest denormal of its sign when the round mode takes that sign away from zero and bit 1 of the FLOAT_MODE denormal field of that precision keeps denormal results, and a zero of its sign otherwise. It takes precedence over the overflow value, and the special cases of the denominator and numerator take precedence over both.

The overflow test now checks that src0's exponent field is all ones instead of a NaN src0, so an infinite src0 takes the overflow value too. Before, it gave an infinity of the result's sign, which differs from the hardware where the round mode takes that sign toward zero. At round to nearest even the generated IR is the same as before except for that comparison; the FP16_OVFL clamp of v_div_fixup_f16 moves from a check on the result to the overflow value.

RoundingUseOf lists the three opcodes as modelling a round mode set at run time (s_round_mode, s_setreg_imm32_b32), so a shader that sets a directed mode before them no longer throws does not model directed rounding (f16, f32) or translates v_div_fixup_f64 at round to nearest even (f64). v_div_fixup_f32 in f32 denormal modes 1 and 2 with a directed f32 round mode no longer throws: it flushes its denominator and numerator by bit 0 of the f32 denormal field, as at round to nearest even. docs/dev/debt/shader.md keeps that throw for v_div_scale_f32 only.

Tested

Windows 11 Pro, no title.

Hardware reference: RDNA2 (Ryzen 5 7600X integrated graphics, gfx1036, AMD Windows driver 32.0.21045.16010) through tools/hw-oracle, wave32, one v_div_fixup_* per lane:

  • v_div_fixup_f16 over 2,366 triples (14 quotients x 13 denominators x 13 numerators: ±0, a denormal, 2^-14, 2^-12, ±1, 2^15, ±0x7bff, ±infinity, quiet and signaling NaNs) in all 64 combinations of round16 0-3, f16 denormal mode 0-3, FP16_OVFL 0/1 and IEEE 0/1;
  • v_div_fixup_f32 and v_div_fixup_f64 over 2,197 triples each (13 x 13 x 13 of the same classes plus the f32/f64 limits, 2^±100) and 343 underflow-boundary triples each (numerator and denominator exponents 150/151 and 1075/1076 apart, both signs) in all 32 combinations of the round mode, the denormal mode of that precision and IEEE 0/1.

Every one of the 313,984 results equals the rule above. main differs in 12,832 of them (3,400 f16, 4,716 f32, 4,716 f64; for f32 in denormal modes 1 and 2 it throws instead), all with a directed round mode. The PS5's gfx1013 is not measured.

agc_driver_div_fixup_directed_rounding (new, DivFixupDirectedRounding.cpp) runs the three instructions on 28 rows (9 f16, 9 f32, 10 f64: infinite and NaN quotients of both result signs, x/0 and inf/y infinities, NaN denominators, 0/0, the f32/f64 underflow threshold and the largest finite values) in 18 float modes (round 0-3 x denormal fields 0x00/0xf0 x FP16_OVFL 0/1, and round 1 with f32 denormal modes 1 and 2), then once more after s_round_mode 0xf in a shader declared at round to nearest even. Its expected values are the hardware results of the same instruction words; on hardware the s_round_mode run equals the declared round-toward-zero run.

ctest -V with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1 and ANYPS5_REQUIRE_VULKAN=1, on the Radeon Graphics iGPU and on the RX 9070 XT. Neither log reports was not found but was requested by env var VK_INSTANCE_LAYERS, and none has a VUID- line:

run Radeon Graphics (RDNA2) RX 9070 XT (RDNA4)
agc_driver_div_fixup_directed_rounding, this PR passed passed
same test on main fails: v_div_fixup_f16 FLOAT_MODE 0x5 FP16_OVFL 0 lane 1 is 0xfc00, expected 0xfbff fails: same
same test, this PR with the round-toward-+infinity select of roundedLimit swapped fails: v_div_fixup_f16 FLOAT_MODE 0x5 ... lane 0 is 0x7bff, expected 0x7c00 fails: same
same test, this PR with keepsDenormalResults always false fails: v_div_fixup_f32 FLOAT_MODE 0xf5 ... lane 13 is 0x0, expected 0x1 fails: same
16 neighbours (agc_shader_mode_register, vector_half_double_output_modifiers and every execution test running a v_div_fixup_*: div_fixup_f16, div_fixup_f16_denormal_modes, div_fixup_f64_denormal_modes, div_scale_fixup_f32_denormal_modes, division, division_output_modifiers, division_result_modifiers, f32_denormal_flush, f64_division, f64_division_modifiers, float16_modifiers, float16_ternary_clamp, fp16_overflow, nan_quieting), this PR 16/16 passed 14/16 passed
same 16 on main 16/16 passed 14/16 passed

The two failures on the RX 9070 XT are agc_driver_float16_modifiers and agc_driver_fp16_overflow, both on v_ldexp_f16 (... v_ldexp_f16 ... is 0x00000000, expected 0x00008000), the same on main; v_ldexp_f16 takes no path this PR changes.

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
  • 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 Graphics (Ryzen 5 7600X iGPU, RDNA2), AMD Windows driver 32.0.21045.16010; AMD Radeon RX 9070 XT (RDNA4), AMD Windows driver 32.0.32015.7029; Windows 11 Pro
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5297755712 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19563). Originally posted by [Nawid3333](https://github.com/Nawid3333) on 2026-11-13T15:00:26Z. --- ### What `v_div_fixup_f16`, `v_div_fixup_f32` and `v_div_fixup_f64` write the overflow value of the MODE round mode, and `v_div_fixup_f32` and `v_div_fixup_f64` also its underflow value, as RDNA2 does, instead of an infinity and a signed zero in every mode. - Overflow: with an infinite or NaN quotient (src0) and no special case of the denominator or numerator (NaN, 0/0, inf/inf, x/0, inf/y, x/inf, 0/y) and, for f32 and f64, no underflow (below), the result is the largest finite value of the result's sign when the round mode takes that sign toward zero (round toward zero; toward -infinity for a positive result; toward +infinity for a negative one), and infinity otherwise. `FP16_OVFL` still writes ±0x7bff for the f16 form in every mode. Example: `v_div_fixup_f16(0x7c00, 1.0, 1.0)` with `FLOAT_MODE` round16 3 is 0x7bff on hardware; `main` writes 0x7c00. - Underflow (f32 and f64): a result whose numerator and denominator exponents differ by less than -150 (f32) or -1075 (f64) is the smallest denormal of its sign when the round mode takes that sign away from zero and bit 1 of the `FLOAT_MODE` denormal field of that precision keeps denormal results, and a zero of its sign otherwise. It takes precedence over the overflow value, and the special cases of the denominator and numerator take precedence over both. The overflow test now checks that src0's exponent field is all ones instead of a NaN src0, so an infinite src0 takes the overflow value too. Before, it gave an infinity of the result's sign, which differs from the hardware where the round mode takes that sign toward zero. At round to nearest even the generated IR is the same as before except for that comparison; the `FP16_OVFL` clamp of `v_div_fixup_f16` moves from a check on the result to the overflow value. `RoundingUseOf` lists the three opcodes as modelling a round mode set at run time (`s_round_mode`, `s_setreg_imm32_b32`), so a shader that sets a directed mode before them no longer throws `does not model directed rounding` (f16, f32) or translates `v_div_fixup_f64` at round to nearest even (f64). `v_div_fixup_f32` in f32 denormal modes 1 and 2 with a directed f32 round mode no longer throws: it flushes its denominator and numerator by bit 0 of the f32 denormal field, as at round to nearest even. `docs/dev/debt/shader.md` keeps that throw for `v_div_scale_f32` only. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 Pro, no title. Hardware reference: RDNA2 (Ryzen 5 7600X integrated graphics, gfx1036, AMD Windows driver 32.0.21045.16010) through `tools/hw-oracle`, wave32, one `v_div_fixup_*` per lane: - `v_div_fixup_f16` over 2,366 triples (14 quotients x 13 denominators x 13 numerators: ±0, a denormal, 2^-14, 2^-12, ±1, 2^15, ±0x7bff, ±infinity, quiet and signaling NaNs) in all 64 combinations of round16 0-3, f16 denormal mode 0-3, `FP16_OVFL` 0/1 and IEEE 0/1; - `v_div_fixup_f32` and `v_div_fixup_f64` over 2,197 triples each (13 x 13 x 13 of the same classes plus the f32/f64 limits, 2^±100) and 343 underflow-boundary triples each (numerator and denominator exponents 150/151 and 1075/1076 apart, both signs) in all 32 combinations of the round mode, the denormal mode of that precision and IEEE 0/1. Every one of the 313,984 results equals the rule above. `main` differs in 12,832 of them (3,400 f16, 4,716 f32, 4,716 f64; for f32 in denormal modes 1 and 2 it throws instead), all with a directed round mode. The PS5's gfx1013 is not measured. `agc_driver_div_fixup_directed_rounding` (new, `DivFixupDirectedRounding.cpp`) runs the three instructions on 28 rows (9 f16, 9 f32, 10 f64: infinite and NaN quotients of both result signs, x/0 and inf/y infinities, NaN denominators, 0/0, the f32/f64 underflow threshold and the largest finite values) in 18 float modes (round 0-3 x denormal fields 0x00/0xf0 x `FP16_OVFL` 0/1, and round 1 with f32 denormal modes 1 and 2), then once more after `s_round_mode 0xf` in a shader declared at round to nearest even. Its expected values are the hardware results of the same instruction words; on hardware the `s_round_mode` run equals the declared round-toward-zero run. `ctest -V` with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ANYPS5_NO_SHADER_CACHE=1` and `ANYPS5_REQUIRE_VULKAN=1`, on the Radeon Graphics iGPU and on the RX 9070 XT. Neither log reports `was not found but was requested by env var VK_INSTANCE_LAYERS`, and none has a `VUID-` line: | run | Radeon Graphics (RDNA2) | RX 9070 XT (RDNA4) | |---|---|---| | `agc_driver_div_fixup_directed_rounding`, this PR | passed | passed | | same test on `main` | fails: `v_div_fixup_f16 FLOAT_MODE 0x5 FP16_OVFL 0 lane 1 is 0xfc00, expected 0xfbff` | fails: same | | same test, this PR with the round-toward-+infinity select of `roundedLimit` swapped | fails: `v_div_fixup_f16 FLOAT_MODE 0x5 ... lane 0 is 0x7bff, expected 0x7c00` | fails: same | | same test, this PR with `keepsDenormalResults` always false | fails: `v_div_fixup_f32 FLOAT_MODE 0xf5 ... lane 13 is 0x0, expected 0x1` | fails: same | | 16 neighbours (`agc_shader_mode_register`, `vector_half_double_output_modifiers` and every execution test running a `v_div_fixup_*`: `div_fixup_f16`, `div_fixup_f16_denormal_modes`, `div_fixup_f64_denormal_modes`, `div_scale_fixup_f32_denormal_modes`, `division`, `division_output_modifiers`, `division_result_modifiers`, `f32_denormal_flush`, `f64_division`, `f64_division_modifiers`, `float16_modifiers`, `float16_ternary_clamp`, `fp16_overflow`, `nan_quieting`), this PR | 16/16 passed | 14/16 passed | | same 16 on `main` | 16/16 passed | 14/16 passed | The two failures on the RX 9070 XT are `agc_driver_float16_modifiers` and `agc_driver_fp16_overflow`, both on `v_ldexp_f16` (`... v_ldexp_f16 ... is 0x00000000, expected 0x00008000`), the same on `main`; `v_ldexp_f16` takes no path this PR changes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) - [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 Graphics (Ryzen 5 7600X iGPU, RDNA2), AMD Windows driver 32.0.21045.16010; AMD Radeon RX 9070 XT (RDNA4), AMD Windows driver 32.0.32015.7029; Windows 11 Pro - [x] AI-assisted: yes
MarcZX10 commented 2026-11-21 04:06:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T04:06:33Z.

Review: COMMENTED


Reviewed — comments

The targeted code inspection found no verified blocking defect, but the review is incomplete and no checks were run.

What I ran

Inspected the prepared diff and targeted source ranges at head 3a7978b16130e66288218dc8c931fb10e83dbd73; no tests or build checks were run. The review bundle listed seven changed files, all included. I read selected ranges from the new execution test, TranslationContext.hpp, Float64Instructions.cpp, FloatInstructions.cpp, RoundingMode.cpp, and VectorInstructions.cpp, plus the changed documentation lines. Other portions of those files were not read.

Results

Check Result Note
Diff inspection passed Targeted changed code inspected
Tests and build not run No local check performed
Full file review not run Only selected ranges were read

Findings

Blocking

None verified in the inspected ranges.

Non-blocking

Nothing to report from the inspected ranges.

Not verified

The uninspected portions of the seven changed files, test discovery/registration, and runtime behavior were not verified. The hardware and test results described in the PR body are author-reported; I did not reproduce them.

To get this to a good state

Nothing is required. Optional: complete inspection of the remaining changed-file contents and run the reported checks.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6002722251 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19563#pullrequestreview-6002722251). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T04:06:33Z. **Review:** COMMENTED --- # Reviewed — comments The targeted code inspection found no verified blocking defect, but the review is incomplete and no checks were run. ## What I ran Inspected the prepared diff and targeted source ranges at head `3a7978b16130e66288218dc8c931fb10e83dbd73`; no tests or build checks were run. The review bundle listed seven changed files, all included. I read selected ranges from the new execution test, `TranslationContext.hpp`, `Float64Instructions.cpp`, `FloatInstructions.cpp`, `RoundingMode.cpp`, and `VectorInstructions.cpp`, plus the changed documentation lines. Other portions of those files were not read. ## Results | Check | Result | Note | |---|---|---| | Diff inspection | passed | Targeted changed code inspected | | Tests and build | not run | No local check performed | | Full file review | not run | Only selected ranges were read | ## Findings ### Blocking None verified in the inspected ranges. ### Non-blocking Nothing to report from the inspected ranges. ## Not verified The uninspected portions of the seven changed files, test discovery/registration, and runtime behavior were not verified. The hardware and test results described in the PR body are author-reported; I did not reproduce them. ## To get this to a good state Nothing is required. Optional: complete inspection of the remaining changed-file contents and run the reported checks.
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!19563
No description provided.