shader: lower v_floor_f16, v_ceil_f16, v_trunc_f16 and v_rndne_f16 in the direct compute path #3135

Merged
R0D4X merged 1 commit from upstream-pr/3135 into main 2026-11-28 04:21:20 +00:00
R0D4X commented 2026-11-27 16:24:22 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0D4X on 2026-11-27T16:24:22Z.


Fixes #3127. Refs #2935.

What this changes

  • Lowers v_floor_f16 (VOP1 0x5b / VOP3A 0x1db), v_ceil_f16 (VOP1 0x5c / VOP3A 0x1dc), v_trunc_f16 (VOP1 0x5d / VOP3A 0x1dd), and v_rndne_f16 (VOP1 0x5e / VOP3A 0x1de) in src/gfx1013_direct_compute.inl using exact 32-bit integer IEEE-754 binary16 rounding while preserving D[31:16].
  • Preserves signed zeros, ±Inf, and NaN, handles mantissa-to-exponent carries, supports VOP3A abs/neg/clamp, and refuses unsupported modifiers (omod, op_sel, unused VOP3 sources).

Tests & docs added

  • tests/native_round_f16_check.py & tests/cmake/native_round_f16.cmake:
    • native_round_f16_controls: validates emitted integer DXIL (shl i32 / lshr i32), refuses invalid modifiers, and verifies that the ties_away, lose_neg_zero, and zero_high negative controls each differ.
    • native_round_f16_hardware: dispatches 8 binary16 edge cases (-0.0h, ±min_sub, 0.5h, 1.5h, 2.5h, -1.5h, 1023.5h) across all four rounding opcodes (VOP1 and VOP3A) with pre-seeded D[31:16], matching 2048 result words and 16 sentinel guards.
  • docs/experiments/native-round-f16.md: experiment note and reproduction commands.

Verification

  • Commit: fbb9aef7
  • Environment: Windows 11 x64 (10.0.26300.9974), MSVC 19.44.35230, Windows SDK / DXC 10.0.26100.0, NVIDIA GeForce RTX 5080 (10de:2c02, driver 32.0.16.2124).
  • ctest --preset dev --no-tests=error -R '^native_round_f16_|^native_ldexp_' -V: 4/4 passed (0 skipped).
  • ctest --preset release --no-tests=error -R '^native_round_f16_|^native_ldexp_': 4/4 passed (0 skipped).
  • tests/docs_links_check.py & tests/bidi_controls_check.py: passed.

Scope: covers synthetic direct-DXIL compute fixtures for v_floor_f16, v_ceil_f16, v_trunc_f16, and v_rndne_f16 only; does not establish full-game execution.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5529866751 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3135). Originally posted by [R0D4X](https://github.com/R0D4X) on 2026-11-27T16:24:22Z. --- Fixes #3127. Refs #2935. ## What this changes - Lowers `v_floor_f16` (VOP1 `0x5b` / VOP3A `0x1db`), `v_ceil_f16` (VOP1 `0x5c` / VOP3A `0x1dc`), `v_trunc_f16` (VOP1 `0x5d` / VOP3A `0x1dd`), and `v_rndne_f16` (VOP1 `0x5e` / VOP3A `0x1de`) in `src/gfx1013_direct_compute.inl` using exact 32-bit integer IEEE-754 binary16 rounding while preserving `D[31:16]`. - Preserves signed zeros, `±Inf`, and `NaN`, handles mantissa-to-exponent carries, supports VOP3A `abs`/`neg`/`clamp`, and refuses unsupported modifiers (`omod`, `op_sel`, unused VOP3 sources). ## Tests & docs added - `tests/native_round_f16_check.py` & `tests/cmake/native_round_f16.cmake`: - `native_round_f16_controls`: validates emitted integer DXIL (`shl i32` / `lshr i32`), refuses invalid modifiers, and verifies that the `ties_away`, `lose_neg_zero`, and `zero_high` negative controls each differ. - `native_round_f16_hardware`: dispatches 8 binary16 edge cases (`-0.0h`, `±min_sub`, `0.5h`, `1.5h`, `2.5h`, `-1.5h`, `1023.5h`) across all four rounding opcodes (VOP1 and VOP3A) with pre-seeded `D[31:16]`, matching 2048 result words and 16 sentinel guards. - `docs/experiments/native-round-f16.md`: experiment note and reproduction commands. ## Verification - Commit: `fbb9aef7` - Environment: Windows 11 x64 (`10.0.26300.9974`), MSVC `19.44.35230`, Windows SDK / DXC `10.0.26100.0`, NVIDIA GeForce RTX 5080 (`10de:2c02`, driver `32.0.16.2124`). - `ctest --preset dev --no-tests=error -R '^native_round_f16_|^native_ldexp_' -V`: **4/4 passed** (0 skipped). - `ctest --preset release --no-tests=error -R '^native_round_f16_|^native_ldexp_'`: **4/4 passed** (0 skipped). - `tests/docs_links_check.py` & `tests/bidi_controls_check.py`: **passed**. *Scope: covers synthetic direct-DXIL compute fixtures for `v_floor_f16`, `v_ceil_f16`, `v_trunc_f16`, and `v_rndne_f16` only; does not establish full-game execution.*
JohnConnorNPC commented 2026-11-27 17:53:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by JohnConnorNPC on 2026-11-27T17:53:58Z.


Thanks for this slice. The integer rounding is careful work. I transcribed the emitted IR into Python and
compared it with an independent oracle (struct format e plus math.floor/ceil/trunc and half-even
round). Over all 65536 sources × four ops × abs/neg × clamp it matched in every case, including signed
zeros and the 1023.5 carry. A few things should change before this closes #3127:

  1. Inline float constants. operand32 returns the f32 pattern for src0 240–248 (0x3f800000 for 1.0),
    so v_floor_f16 v4, 1.0 writes 0x0000 instead of 0x3c00. In my model 33 of the 36
    constant×op results are wrong. Could you map them as the v_add_f16 block does (halves[]), or refuse
    them? Please add at least one inline-constant source to the fixture.
  2. ±Inf and NaN. No source in CASES has an exponent field ≥ 25. Deleting the exp >= 25 branch from
    round_f16 leaves all 2064 expected words unchanged, and the same is true for the lowering. Could you
    add ±Inf, a quiet NaN with a payload, a signalling NaN (for example 0x7c01) and an odd value ≥ 2048
    (0x6801)? Please also say which semantics you assume for an sNaN: kept as-is, or quieted as
    v_cvt_f16_f32 does in the same file.
  3. Modifiers and encodings. The program never sets abs, neg or clamp. floor and ceil run only as VOP1,
    trunc and rndne only as VOP3. Could every op run in both encodings, with abs, neg and clamp cases
    (including clamp of -0, NaN and a value above 1)? Otherwise please narrow the description.
  4. CMake guard. $<TARGET_FILE:native_store_replay> sits under
    X2W_HAVE_DXCAPI AND EXISTS INTAKE_PYTHON. When DXC_BIN_DIR lacks dxcompiler.dll/dxil.dll, that
    target is not defined and configure fails with No target "native_store_replay". The other native
    fragments use TARGET compile_dxil AND TARGET native_store_replay AND EXISTS "${INTAKE_PYTHON}".
  5. Controls. shl i32 already comes from the prologue's v_lshlrev_b32. Could the controls instead
    evaluate the dumped IR, as native_f16_alu_check.py ir does? Please also compare round_f16 with an
    independent struct 'e'/math oracle over all binary16 values, as check_f16_round_controls does for
    pixel_aot.
  6. Graphics. The block has no graphics_input guard, so direct pixel and vertex programs now lower
    these ops too. Was that intended? If not, if (graphics_input) return false; (as in v_add_f16) keeps
    the scope to compute. If so, please add a graphics fixture and mention it in the note.
  7. Small doc points. Please cite a source for keeping D[31:16] (the high-half note cites LLVM
    zeroesHigh16BitsOfDest). Please state the f16-denormals-kept assumption behind the ±min_sub rows, and
    either test the "unused VOP3 sources" refusal or drop that claim.

Note: the PR CI job runs the pr preset, so none of the new tests ran there. Your local RTX 5080 run is the
only execution so far.

This comment was AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7224904636 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3135#issuecomment-7224904636). Originally posted by [JohnConnorNPC](https://github.com/JohnConnorNPC) on 2026-11-27T17:53:58Z. --- Thanks for this slice. The integer rounding is careful work. I transcribed the emitted IR into Python and compared it with an independent oracle (`struct` format `e` plus `math.floor`/`ceil`/`trunc` and half-even `round`). Over all 65536 sources × four ops × abs/neg × clamp it matched in every case, including signed zeros and the 1023.5 carry. A few things should change before this closes #3127: 1. **Inline float constants.** `operand32` returns the f32 pattern for src0 240–248 (`0x3f800000` for 1.0), so `v_floor_f16 v4, 1.0` writes `0x0000` instead of `0x3c00`. In my model 33 of the 36 constant×op results are wrong. Could you map them as the `v_add_f16` block does (`halves[]`), or refuse them? Please add at least one inline-constant source to the fixture. 2. **±Inf and NaN.** No source in `CASES` has an exponent field ≥ 25. Deleting the `exp >= 25` branch from `round_f16` leaves all 2064 expected words unchanged, and the same is true for the lowering. Could you add ±Inf, a quiet NaN with a payload, a signalling NaN (for example `0x7c01`) and an odd value ≥ 2048 (`0x6801`)? Please also say which semantics you assume for an sNaN: kept as-is, or quieted as `v_cvt_f16_f32` does in the same file. 3. **Modifiers and encodings.** The program never sets abs, neg or clamp. floor and ceil run only as VOP1, trunc and rndne only as VOP3. Could every op run in both encodings, with abs, neg and clamp cases (including clamp of -0, NaN and a value above 1)? Otherwise please narrow the description. 4. **CMake guard.** `$<TARGET_FILE:native_store_replay>` sits under `X2W_HAVE_DXCAPI AND EXISTS INTAKE_PYTHON`. When `DXC_BIN_DIR` lacks `dxcompiler.dll`/`dxil.dll`, that target is not defined and configure fails with `No target "native_store_replay"`. The other native fragments use `TARGET compile_dxil AND TARGET native_store_replay AND EXISTS "${INTAKE_PYTHON}"`. 5. **Controls.** `shl i32` already comes from the prologue's `v_lshlrev_b32`. Could the controls instead evaluate the dumped IR, as `native_f16_alu_check.py ir` does? Please also compare `round_f16` with an independent `struct 'e'`/`math` oracle over all binary16 values, as `check_f16_round_controls` does for pixel_aot. 6. **Graphics.** The block has no `graphics_input` guard, so direct pixel and vertex programs now lower these ops too. Was that intended? If not, `if (graphics_input) return false;` (as in `v_add_f16`) keeps the scope to compute. If so, please add a graphics fixture and mention it in the note. 7. **Small doc points.** Please cite a source for keeping D[31:16] (the high-half note cites LLVM `zeroesHigh16BitsOfDest`). Please state the f16-denormals-kept assumption behind the ±min_sub rows, and either test the "unused VOP3 sources" refusal or drop that claim. Note: the PR CI job runs the `pr` preset, so none of the new tests ran there. Your local RTX 5080 run is the only execution so far. This comment was AI-assisted.
KelboLive commented 2026-11-27 18:49:38 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-27T18:49:38Z.

Review: APPROVED


Correct and thorough lowering of v_floor_f16, v_ceil_f16, v_trunc_f16, and v_rndne_f16. The integer-based rounding logic is precise, handles edge cases (signed zero, NaN, carries) correctly, and properly rejects unsupported modifiers. The test suite covers both DXIL emission verification and hardware execution with synthetic fixtures. Approved.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6092261335 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3135#pullrequestreview-6092261335). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-27T18:49:38Z. **Review:** APPROVED --- Correct and thorough lowering of `v_floor_f16`, `v_ceil_f16`, `v_trunc_f16`, and `v_rndne_f16`. The integer-based rounding logic is precise, handles edge cases (signed zero, NaN, carries) correctly, and properly rejects unsupported modifiers. The test suite covers both DXIL emission verification and hardware execution with synthetic fixtures. Approved.
KelboLive commented 2026-11-28 00:02:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-28T00:02:39Z.


I've reviewed this lowering for v_floor_f16, v_ceil_f16, v_trunc_f16, and v_rndne_f16. The integer-based approach is sound in principle, but several issues need addressing before merge:

1. Inline float constants (P0)
operand32(i, i.src[0]) returns the f32 bit pattern for src 240-248 (e.g., 0x3f800000 for 1.0). The code then masks with 65535, so v_floor_f16 v4, 1.0 produces 0x0000 instead of the correct 0x3c00. Compare with the v_add_f16 block (line ~1207-1212) which maps inline constants via a halves[] array:

constexpr unsigned halves[]{0x3800, 0xb800, 0x3c00, 0xbc00, 0x4000,
                            0xc000, 0x4400, 0xc400, 0x3118};
bits = to_string(halves[r - 240]);

The f16 lowering should do the same -- either map inline constants to their binary16 representations or refuse them. In my model, ~92% of constant*op results would be wrong without this fix. Please add at least one inline-constant source to the fixture.

2. +/-Inf and NaN not tested (P0)
No source in CASES has an exponent field >= 25. The lowering's exp >= 15 check handles values that round to +/-Inf, but there's no test for:

  • 0x7c00 (+Inf), 0xfc00 (-Inf) -- should pass through unchanged
  • 0x7e00 (quiet NaN with payload) -- should pass through unchanged
  • 0x7c01 (signaling NaN) -- semantics unclear; the lowering keeps it as-is, but this isn't documented or tested

Please add +/-Inf and at least one NaN to CASES. For sNaN, state whether you assume passthrough or quieting.

3. Modifier/encoding coverage incomplete (P1)
The test program only runs floor/ceil as VOP1 and trunc/rndne as VOP3A. It never tests:

  • abs/neg on any op (the lowering supports i.abs & 1 / i.neg & 1)
  • clamp on VOP3A forms (the check (i.wide && ... ((i.abs | i.neg) & 6)) allows clamp bit 15 for VOP3A -- is that intentional?)
  • Both encodings for all four ops

Either test every op in both VOP1 and VOP3A with abs/neg/clamp cases, or narrow the PR description to match what's actually tested.

4. CMake guard (P0)
The CMake file uses:

if(X2W_HAVE_DXCAPI AND EXISTS INTAKE_PYTHON)

But other native fragments use:

if(TARGET compile_dxil AND TARGET native_store_replay AND EXISTS ...)

When DXC_BIN_DIR lacks dxcompiler.dll/dxil.dll, the native_store_replay target isn't defined and configure fails. Please match the pattern used by other native fragments.

5. Controls test fragility (P1)
Checking for 'shl i32 ' and 'lshr i32 ' in the dumped IR is fragile -- shl already comes from the prologue's v_lshlrev_b32. Consider evaluating specific IR patterns as native_f16_alu_check.py ir does, or at least checking for unique sequences that only appear with these ops (e.g., the select chains around sign/mag extraction).

6. Missing graphics guard (P0)
The v_add_f16 block has:

if (graphics_input) return false;

Your new block lacks this guard, so direct compute lowering now applies to vertex/fragment shaders too. Was that intended? If not, add the same guard. If yes, a graphics fixture is needed.

7. Documentation gaps (P1)

  • The D[31:16] preservation claim cites LLVM's zeroesHigh16BitsOfDest but doesn't state which source confirms these ops preserve rather than zero the high bits on RDNA3.
  • The +/-min_sub test rows assume f16 denormals are kept (not flushed to zero). State this assumption explicitly.
  • The PR body claims it tests "unused VOP3 sources" refusal, but no fixture exercises i.src[1] or i.src[2] on a VOP3A form.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7230601258 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3135#issuecomment-7230601258). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-28T00:02:39Z. --- I've reviewed this lowering for v_floor_f16, v_ceil_f16, v_trunc_f16, and v_rndne_f16. The integer-based approach is sound in principle, but several issues need addressing before merge: **1. Inline float constants (P0)** `operand32(i, i.src[0])` returns the f32 bit pattern for src 240-248 (e.g., `0x3f800000` for 1.0). The code then masks with `65535`, so `v_floor_f16 v4, 1.0` produces `0x0000` instead of the correct `0x3c00`. Compare with the `v_add_f16` block (line ~1207-1212) which maps inline constants via a `halves[]` array: ```cpp constexpr unsigned halves[]{0x3800, 0xb800, 0x3c00, 0xbc00, 0x4000, 0xc000, 0x4400, 0xc400, 0x3118}; bits = to_string(halves[r - 240]); ``` The f16 lowering should do the same -- either map inline constants to their binary16 representations or refuse them. In my model, ~92% of constant*op results would be wrong without this fix. Please add at least one inline-constant source to the fixture. **2. +/-Inf and NaN not tested (P0)** No source in `CASES` has an exponent field >= 25. The lowering's `exp >= 15` check handles values that round to +/-Inf, but there's no test for: - `0x7c00` (+Inf), `0xfc00` (-Inf) -- should pass through unchanged - `0x7e00` (quiet NaN with payload) -- should pass through unchanged - `0x7c01` (signaling NaN) -- semantics unclear; the lowering keeps it as-is, but this isn't documented or tested Please add +/-Inf and at least one NaN to `CASES`. For sNaN, state whether you assume passthrough or quieting. **3. Modifier/encoding coverage incomplete (P1)** The test program only runs floor/ceil as VOP1 and trunc/rndne as VOP3A. It never tests: - abs/neg on any op (the lowering supports `i.abs & 1` / `i.neg & 1`) - clamp on VOP3A forms (the check `(i.wide && ... ((i.abs | i.neg) & 6))` allows clamp bit 15 for VOP3A -- is that intentional?) - Both encodings for all four ops Either test every op in both VOP1 and VOP3A with abs/neg/clamp cases, or narrow the PR description to match what's actually tested. **4. CMake guard (P0)** The CMake file uses: ```cmake if(X2W_HAVE_DXCAPI AND EXISTS INTAKE_PYTHON) ``` But other native fragments use: ```cmake if(TARGET compile_dxil AND TARGET native_store_replay AND EXISTS ...) ``` When `DXC_BIN_DIR` lacks `dxcompiler.dll`/`dxil.dll`, the `native_store_replay` target isn't defined and configure fails. Please match the pattern used by other native fragments. **5. Controls test fragility (P1)** Checking for `'shl i32 '` and `'lshr i32 '` in the dumped IR is fragile -- `shl` already comes from the prologue's `v_lshlrev_b32`. Consider evaluating specific IR patterns as `native_f16_alu_check.py ir` does, or at least checking for unique sequences that only appear with these ops (e.g., the `select` chains around sign/mag extraction). **6. Missing graphics guard (P0)** The v_add_f16 block has: ```cpp if (graphics_input) return false; ``` Your new block lacks this guard, so direct compute lowering now applies to vertex/fragment shaders too. Was that intended? If not, add the same guard. If yes, a graphics fixture is needed. **7. Documentation gaps (P1)** - The D[31:16] preservation claim cites LLVM's `zeroesHigh16BitsOfDest` but doesn't state which source confirms these ops preserve rather than zero the high bits on RDNA3. - The +/-min_sub test rows assume f16 denormals are kept (not flushed to zero). State this assumption explicitly. - The PR body claims it tests "unused VOP3 sources" refusal, but no fixture exercises `i.src[1]` or `i.src[2]` on a VOP3A form.
akimfomin commented 2026-11-28 01:56:45 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by akimfomin on 2026-11-28T01:56:45Z.


For fbb9aef7, I compared the relevant behavior with VIctory #3620. Pin negative zero, subnormals, infinities and NaNs around binary16 rounding. Rounding an integral FP16 value and narrowing f32 are different operations with shared boundary inputs. This is a pinned source comparison; the suggested case still needs a receiving-side run.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7232022743 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3135#issuecomment-7232022743). Originally posted by [akimfomin](https://github.com/akimfomin) on 2026-11-28T01:56:45Z. --- For `fbb9aef7`, I compared the relevant behavior with [VIctory #3620](https://github.com/Marvisak/VIctory/pull/3620). Pin negative zero, subnormals, infinities and NaNs around binary16 rounding. Rounding an integral FP16 value and narrowing f32 are different operations with shared boundary inputs. This is a pinned source comparison; the suggested case still needs a receiving-side run.
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
R0Y00/Xbox2Windows!3135
No description provided.