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

Open
github-bot wants to merge 1 commit from github-bot/gh-247 into main AGit
Owner

@⁠Andretuta on GitHub · 2026-11-24 02:53 UTC · original
ubiquitarianism:old-pr/9982 → main · +521 −49 · 3 files · 1 commits · draft

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).

**@⁠Andretuta** on GitHub · 2026-11-24 02:53 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/247) `ubiquitarianism:old-pr/9982` → `main` · +521 −49 · 3 files · 1 commits · draft > 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).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:50:24 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/247/head:github-bot/gh-247'
git switch 'github-bot/gh-247'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-247'
git switch 'github-bot/gh-247'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-247'
git switch 'github-bot/gh-247'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-247'
git switch 'main'
git merge --squash 'github-bot/gh-247'
git switch 'main'
git merge --ff-only 'github-bot/gh-247'
git switch 'main'
git merge 'github-bot/gh-247'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.