[PR #247] rdna2spirv: support directed MODE.FP_ROUND[3:2] (+inf/-inf) and dynamic s_setreg_b32 #299

Open
opened 2026-11-26 20:17:25 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/247
Author: @Andretuta
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: old-pr/9982


📝 Commits (1)

  • 5f3dea6 rdna2spirv: support directed MODE.FP_ROUND[3:2] (+inf/-inf) and dynamic s_setreg_b32

📊 Changes

3 files changed (+521 additions, -49 deletions)

View changed files

➕ tests/semantic/test_mode_fp_round_directed.py (+386 -0)
📝 tests/semantic/test_scalar_noop_waitcnt.py (+4 -5)
📝 tools/rdna2spirv.py (+131 -44)

📄 Description

Carried over from old PR 9982 by Lewandox in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9982, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
Lewandox: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

Adds full IEEE-754 directed rounding support (RTP / +inf = mode 1 and RTN / -inf = mode 2) and dynamic SGPR s_setreg_b32 writes for MODE.FP_ROUND[3:2] (f16 and f64 rounding) in tools/rdna2spirv.py.

What changed

  • Added Gen._round_mode_flags(is_neg, mode_val) to map all four MODE.FP_ROUND[3:2] encodings (0=RNE, 1=RTP, 2=RTN, 3=RTZ) into sign-aware is_trunc (toward zero) and is_away (away from zero) predicates.
  • Gen.f16_bits / Gen.f16_res: extended integer-only f32-to-f16 narrowing to handle all four rounding modes across normals (bias 0x1FFF when is_away), subnormals & tiny non-zero underflow (all_ones guarded on e != 0 so ±0.0 stays ±0.0 while 2^-126 <= |x| < 2^-24 rounds away to ±0x0001), and overflow boundaries (> 65504.0 overflows to ±inf in is_away, clamps to ±65504 in is_trunc).
  • Gen.f16_omod: finite mul:2 / mul:4 overflow now uses sign-aware is_trunc (±65504 vs ±inf).
  • Gen.sum_toward_zero & Gen.f64_add_mode: extended TwoSum / FastTwoSum error adjustment to step 1 ulp away from zero when is_away (including DBL_MAX + 1.0 -> +inf under RTP) and produce -0.0 on opposite-sign exact cancellation under RTN (mode == 2), per IEEE-754.
  • Gen.mode_write & Gen.write_half_round_mode: accepted modes 1 and 2 in s_round_mode and s_setreg_imm32_b32, and lowered dynamic SGPR s_setreg_b32 hwreg(HW_REG_MODE, 2, 2), sX (sX & 3 -> %mode_r16). Writes targeting other HW_REG_MODE bit ranges remain Unsupported.
  • Added exhaustive bit-exact semantic tests (against a Python fractions.Fraction IEEE-754 oracle + spirv_asm.assemble()) in tests/semantic/test_mode_fp_round_directed.py and updated tests/semantic/test_scalar_noop_waitcnt.py to move modes 1/2 and s_round_mode 4/8 into the modelled set.

Verification

  • python -I -m unittest discover -s tests/semantic -p "test_mode_fp_round_directed.py" -v (PASS)
  • python -I -m unittest discover -s tests/semantic -p "test_scalar_noop_waitcnt.py" -v (PASS)
  • python -I -m unittest discover -s tests/semantic -p "test_valu_add_f64.py" -v (PASS)
  • python -I -m unittest discover -s tests/semantic -p "test_f16_omod_rdna2.py" -v (PASS)
  • python -I -m unittest discover -s tests/semantic -p "test_f16_sum_narrowing.py" -v (PASS)
  • python -I -m unittest discover -s tests/semantic -p "test_valu_mul_f16_mode_round.py" -v (PASS)
  • python -m compileall -q tools/rdna2spirv.py tests/semantic/test_mode_fp_round_directed.py tests/semantic/test_scalar_noop_waitcnt.py (PASS)
  • python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/semantic/test_mode_fp_round_directed.py tests/semantic/test_scalar_noop_waitcnt.py (PASS)

Note: AI-assisted implementation, verified locally against the semantic/assembler test suites above (no game data used).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/247 **Author:** [@Andretuta](https://github.com/Andretuta) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `old-pr/9982` --- ### 📝 Commits (1) - [`5f3dea6`](https://github.com/ubiquitarianism/leonida/commit/5f3dea643a588319c670f570f86cccf40f13675e) rdna2spirv: support directed MODE.FP_ROUND[3:2] (+inf/-inf) and dynamic s_setreg_b32 ### 📊 Changes **3 files changed** (+521 additions, -49 deletions) <details> <summary>View changed files</summary> ➕ `tests/semantic/test_mode_fp_round_directed.py` (+386 -0) 📝 `tests/semantic/test_scalar_noop_waitcnt.py` (+4 -5) 📝 `tools/rdna2spirv.py` (+131 -44) </details> ### 📄 Description > Carried over from **old PR 9982** by `Lewandox` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9982`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `Lewandox`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): Adds full IEEE-754 directed rounding support (`RTP` / `+inf` = mode 1 and `RTN` / `-inf` = mode 2) and dynamic SGPR `s_setreg_b32` writes for `MODE.FP_ROUND[3:2]` (f16 and f64 rounding) in `tools/rdna2spirv.py`. ### What changed - Added `Gen._round_mode_flags(is_neg, mode_val)` to map all four `MODE.FP_ROUND[3:2]` encodings (`0=RNE`, `1=RTP`, `2=RTN`, `3=RTZ`) into sign-aware `is_trunc` (toward zero) and `is_away` (away from zero) predicates. - **`Gen.f16_bits` / `Gen.f16_res`**: extended integer-only f32-to-f16 narrowing to handle all four rounding modes across normals (bias `0x1FFF` when `is_away`), subnormals & tiny non-zero underflow (`all_ones` guarded on `e != 0` so `±0.0` stays `±0.0` while `2^-126 <= |x| < 2^-24` rounds away to `±0x0001`), and overflow boundaries (`> 65504.0` overflows to `±inf` in `is_away`, clamps to `±65504` in `is_trunc`). - **`Gen.f16_omod`**: finite `mul:2` / `mul:4` overflow now uses sign-aware `is_trunc` (`±65504` vs `±inf`). - **`Gen.sum_toward_zero` & `Gen.f64_add_mode`**: extended TwoSum / FastTwoSum error adjustment to step 1 ulp away from zero when `is_away` (including `DBL_MAX + 1.0 -> +inf` under `RTP`) and produce `-0.0` on opposite-sign exact cancellation under `RTN` (`mode == 2`), per IEEE-754. - **`Gen.mode_write` & `Gen.write_half_round_mode`**: accepted modes `1` and `2` in `s_round_mode` and `s_setreg_imm32_b32`, and lowered dynamic SGPR `s_setreg_b32 hwreg(HW_REG_MODE, 2, 2), sX` (`sX & 3 -> %mode_r16`). Writes targeting other `HW_REG_MODE` bit ranges remain `Unsupported`. - Added exhaustive bit-exact semantic tests (against a Python `fractions.Fraction` IEEE-754 oracle + `spirv_asm.assemble()`) in `tests/semantic/test_mode_fp_round_directed.py` and updated `tests/semantic/test_scalar_noop_waitcnt.py` to move modes `1`/`2` and `s_round_mode 4`/`8` into the modelled set. ### Verification - `python -I -m unittest discover -s tests/semantic -p "test_mode_fp_round_directed.py" -v` (PASS) - `python -I -m unittest discover -s tests/semantic -p "test_scalar_noop_waitcnt.py" -v` (PASS) - `python -I -m unittest discover -s tests/semantic -p "test_valu_add_f64.py" -v` (PASS) - `python -I -m unittest discover -s tests/semantic -p "test_f16_omod_rdna2.py" -v` (PASS) - `python -I -m unittest discover -s tests/semantic -p "test_f16_sum_narrowing.py" -v` (PASS) - `python -I -m unittest discover -s tests/semantic -p "test_valu_mul_f16_mode_round.py" -v` (PASS) - `python -m compileall -q tools/rdna2spirv.py tests/semantic/test_mode_fp_round_directed.py tests/semantic/test_scalar_noop_waitcnt.py` (PASS) - `python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/semantic/test_mode_fp_round_directed.py tests/semantic/test_scalar_noop_waitcnt.py` (PASS) Note: AI-assisted implementation, verified locally against the semantic/assembler test suites above (no game data used). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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
starred/leonida#299
No description provided.