f16_ref: preserve signed zero in to_f16/scale and mask 16-bit inputs #2303

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

@⁠kirigayakazima on GitHub · 2026-11-26 13:15 UTC · original
kirigayakazima:fix/f16-ref-safety → main · +66 −1 · 2 files · 1 commits · open

Claimed in the coordination issue as: F16-REF-NEGATIVE-ZERO-SIGN-AND-MASK-SAFETY (#1608)

What and why

  • Preserve the sign bit (0x8000) in to_f16() when passed float(-0.0) so scale(0x0000, -1.0) and scale(0x8000, 1.0) no longer collapse -0.0 through Fraction(-0.0) into +0.0 (0x0000).
  • Mask inputs to 16 bits (& 0xFFFF) consistently across is_nan(), same_f16(), ref_add(), ref_sub(), ref_mul(), ref_fma(), neg(), scale(), and clamp01() in tests/semantic/f16_ref.py (so upper word bits cannot leak through neg(), scale(), clamp01(), or break -0.0/same_f16() equality checks).
  • Add standard-library unit tests in tests/test_f16_ref.py.

Tested

  • python -I tests/test_f16_ref.py -v: 3 passed, 0 failed (detached upstream/main baseline with only the new test file fails with 3 failures).
  • Existing 13 f16_ref-using semantic suites (test_alu_min3_max3_med3_16, test_alu_round, test_interp_guards, test_interp_p1lv_f16, test_valu_add_f16, test_valu_fma_f16, test_valu_mul_f16, test_valu_pk_add_f16, test_valu_pk_fma_f16, test_valu_pk_minmax_f16, test_valu_pk_mul_f16, test_valu_sub_f16, test_valu_subrev_f16): 162 passed, 0 failed.
  • python -I tests/test_ci_registration.py: 16 passed.
  • python -I tests/test_text_write_line_endings.py: 3 passed.
  • python -I scripts/check_no_game_data.py, python -I -m compileall -q tests/semantic/f16_ref.py tests/test_f16_ref.py, and git diff --cached --check: passed.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
**@⁠kirigayakazima** on GitHub · 2026-11-26 13:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2303) `kirigayakazima:fix/f16-ref-safety` → `main` · +66 −1 · 2 files · 1 commits · open Claimed in the coordination issue as: F16-REF-NEGATIVE-ZERO-SIGN-AND-MASK-SAFETY (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### What and why - Preserve the sign bit (`0x8000`) in `to_f16()` when passed `float(-0.0)` so `scale(0x0000, -1.0)` and `scale(0x8000, 1.0)` no longer collapse `-0.0` through `Fraction(-0.0)` into `+0.0` (`0x0000`). - Mask inputs to 16 bits (`& 0xFFFF`) consistently across `is_nan()`, `same_f16()`, `ref_add()`, `ref_sub()`, `ref_mul()`, `ref_fma()`, `neg()`, `scale()`, and `clamp01()` in `tests/semantic/f16_ref.py` (so upper word bits cannot leak through `neg()`, `scale()`, `clamp01()`, or break `-0.0`/`same_f16()` equality checks). - Add standard-library unit tests in `tests/test_f16_ref.py`. ### Tested - `python -I tests/test_f16_ref.py -v`: 3 passed, 0 failed (detached `upstream/main` baseline with only the new test file fails with 3 failures). - Existing 13 `f16_ref`-using semantic suites (`test_alu_min3_max3_med3_16`, `test_alu_round`, `test_interp_guards`, `test_interp_p1lv_f16`, `test_valu_add_f16`, `test_valu_fma_f16`, `test_valu_mul_f16`, `test_valu_pk_add_f16`, `test_valu_pk_fma_f16`, `test_valu_pk_minmax_f16`, `test_valu_pk_mul_f16`, `test_valu_sub_f16`, `test_valu_subrev_f16`): 162 passed, 0 failed. - `python -I tests/test_ci_registration.py`: 16 passed. - `python -I tests/test_text_write_line_endings.py`: 3 passed. - `python -I scripts/check_no_game_data.py`, `python -I -m compileall -q tests/semantic/f16_ref.py tests/test_f16_ref.py`, and `git diff --cached --check`: passed. GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:20:36 +00:00
Author
Owner

@⁠lixonik on GitHub · 2026-11-26 17:57 UTC · original · reviewed

Windows 11: all three f16 reference tests passed, including signed zero and masking. No findings from these checks.

AI-assisted.

**@⁠lixonik** on GitHub · 2026-11-26 17:57 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2303#pullrequestreview-6078689920) · **reviewed** Windows 11: all three f16 reference tests passed, including signed zero and masking. No findings from these checks. AI-assisted.
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-27 04:41 UTC · original · approved these changes

Approving 95b1cb7dc.

f16_ref masks every 16-bit input and to_f16 keeps the sign of zero (-0.0 gives 0x8000). As for #2347, the semantic tests that import f16_ref or f16_unary_cases all pass on this batch: 208 passed (5,662 subtests), so no expected value the translator tests rely on moved.

Merged onto main d1dff7949 in one batch with 15 other tool PRs of this series (no conflicts), on Linux with Python 3.13. tests/test_f16_ref.py: 3 passed (new). AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠tiguan26** on GitHub · 2026-11-27 04:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2303#pullrequestreview-6083570192) · **approved these changes** Approving 95b1cb7dc. `f16_ref` masks every 16-bit input and `to_f16` keeps the sign of zero (-0.0 gives 0x8000). As for #2347, the semantic tests that import `f16_ref` or `f16_unary_cases` all pass on this batch: 208 passed (5,662 subtests), so no expected value the translator tests rely on moved. Merged onto `main` `d1dff7949` in one batch with 15 other tool PRs of this series (no conflicts), on Linux with Python 3.13. `tests/test_f16_ref.py`: 3 passed (new). AI-assisted (Claude Code, Claude Opus 5.5).
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/2303/head:github-bot/gh-2303'
git switch 'github-bot/gh-2303'

Merge

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