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

Merged
kirigayakazima merged 1 commit from upstream-pr/2303 into main 2026-11-28 15:10:54 +00:00
kirigayakazima commented 2026-11-26 13:15:24 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-26T13:15:24Z.


Claimed in the coordination issue as: F16-REF-NEGATIVE-ZERO-SIGN-AND-MASK-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7193007298)

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.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5510703490 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2303). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-26T13:15:24Z. --- Claimed in the coordination issue as: F16-REF-NEGATIVE-ZERO-SIGN-AND-MASK-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7193007298) ### 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.
lixonik commented 2026-11-26 17:57:39 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-26T17:57:39Z.

Review: COMMENTED


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

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6078689920 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2303#pullrequestreview-6078689920). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-26T17:57:39Z. **Review:** COMMENTED --- Windows 11: all three f16 reference tests passed, including signed zero and masking. No findings from these checks. AI-assisted.
tiguan26 commented 2026-11-27 04:41:29 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-27T04:41:29Z.

Review: APPROVED


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

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6083570192 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2303#pullrequestreview-6083570192). Originally posted by [tiguan26](https://github.com/tiguan26) on 2026-11-27T04:41:29Z. **Review:** APPROVED --- 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).
zmasi commented 2026-11-28 10:23:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T10:23:49Z.


#2303 | head 95b1cb7dc7 (merged cleanly with main 901b6c1bf) | python -I tests/test_f16_ref.py && python -I scripts/run_semantic_tests.py -p "test_*f16*.py" tests/semantic | red on main: FAILED (failures=3) (to_f16(-0.0) and scale(0x0000, -1.0) returning +0 0x0000 instead of 0x8000, and is_nan(0x17E00) returning False) | green on PR+main: 3 tests OK and 55 files, 575 tests, 0 files failed | Windows 11, Python 3.12.10 | AI-assisted: yes

Audited to_f16 (math.copysign -0.0 preservation before Fraction(v) drops zero sign), scale, and 16-bit input masking across is_nan, same_f16, ref_add, ref_sub, ref_mul, ref_fma, neg, and clamp01 in tests/semantic/f16_ref.py. Verified both standalone and merged together with #2347 (tests/semantic/f16_unary_cases.py): 0 merge conflicts and all unit, semantic (575/575), tests/test_ci_registration.py (18 OK), tests/test_text_write_line_endings.py (3 OK), tests/test_runner_calls_all_tests.py (3 OK), and scripts/check_no_game_data.py checks pass.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7237869522 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2303#issuecomment-7237869522). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T10:23:49Z. --- #2303 | head 95b1cb7dc7df10cf0f10b0faabc05933326bcf33 (merged cleanly with main 901b6c1bf) | `python -I tests/test_f16_ref.py && python -I scripts/run_semantic_tests.py -p "test_*f16*.py" tests/semantic` | red on main: `FAILED (failures=3)` (`to_f16(-0.0)` and `scale(0x0000, -1.0)` returning `+0` `0x0000` instead of `0x8000`, and `is_nan(0x17E00)` returning `False`) | green on PR+main: `3 tests OK` and `55 files, 575 tests, 0 files failed` | Windows 11, Python 3.12.10 | AI-assisted: yes Audited `to_f16` (`math.copysign` `-0.0` preservation before `Fraction(v)` drops zero sign), `scale`, and 16-bit input masking across `is_nan`, `same_f16`, `ref_add`, `ref_sub`, `ref_mul`, `ref_fma`, `neg`, and `clamp01` in `tests/semantic/f16_ref.py`. Verified both standalone and merged together with #2347 (`tests/semantic/f16_unary_cases.py`): `0` merge conflicts and all unit, semantic (`575/575`), `tests/test_ci_registration.py` (`18 OK`), `tests/test_text_write_line_endings.py` (`3 OK`), `tests/test_runner_calls_all_tests.py` (`3 OK`), and `scripts/check_no_game_data.py` checks pass.
MoonEyes commented 2026-11-28 15:10:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T15:10:51Z.


Red/green: classified READY by oreo-lot3 (red on main b1619e0a3, green on head merged with main); head 95b1cb7dc7 unchanged since 2026-11-26, before that run.
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7241265952 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2303#issuecomment-7241265952). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T15:10:51Z. --- Red/green: classified READY by oreo-lot3 (red on main b1619e0a3, green on head merged with main); head 95b1cb7dc7df unchanged since 2026-11-26, before that run. Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
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
ProjectLeonida/mandy!2303
No description provided.