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

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

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2303
Author: @kirigayakazima
Created: 11/26/2026
Status: 🔄 Open

Base: main ← Head: fix/f16-ref-safety


📝 Commits (1)

  • 95b1cb7 f16_ref: preserve signed zero in to_f16/scale and mask 16-bit inputs

📊 Changes

2 files changed (+66 additions, -1 deletions)

View changed files

📝 tests/semantic/f16_ref.py (+17 -1)
➕ tests/test_f16_ref.py (+49 -0)

📄 Description

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.

🔄 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/2303 **Author:** [@kirigayakazima](https://github.com/kirigayakazima) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/f16-ref-safety` --- ### 📝 Commits (1) - [`95b1cb7`](https://github.com/ubiquitarianism/leonida/commit/95b1cb7dc7df10cf0f10b0faabc05933326bcf33) f16_ref: preserve signed zero in to_f16/scale and mask 16-bit inputs ### 📊 Changes **2 files changed** (+66 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `tests/semantic/f16_ref.py` (+17 -1) ➕ `tests/test_f16_ref.py` (+49 -0) </details> ### 📄 Description 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. --- <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#2306
No description provided.