ucrt: keep remquo's quotient bits when the quotient is huge #51

Manually merged
github merged 3 commits from gh-pr/51/head into gh-pr/51/base 2026-11-26 14:44:01 +00:00
Owner

@⁠Repla09 on GitHub · 2026-11-26 11:31 UTC · original
Repla09:fix/ucrt-remquo-quotient-bits → main · +66 −2 · 2 files · 3 commits · merged 2026-11-26 12:08 UTC

remquo stores the sign and the low bits (at least three) of the integral quotient x / y. The host took them from (x - r) / y, which is no exact integer once the quotient passes 2^53: remquo(7.32e218, 1.23e62) gave 7 where the low bits are 100, and 83,412 of 199,812 random finite pairs disagreed with the host libm.

Now they come from |x| mod 8|y|, which fmod computes exactly (when 8|y| overflows, |x / y| is below 8 and |x| is used directly).

remquo_keeps_the_low_bits_of_a_huge_quotient fails without the fix (bits 7 instead of 4) and checks the sign and low three bits against the host's remquo over 300,000 pairs across every exponent.

Testing

  • cargo test --target x86_64-apple-darwin -p victory-hle passes (220 unit, 15 native_probe; the D3D12 probe test skipped, no Metal Shader Converter here).
  • Clippy on x86_64-apple-darwin shows no new warnings; rustfmt --check and cargo xtask policy pass.
**@⁠Repla09** on GitHub · 2026-11-26 11:31 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/51) `Repla09:fix/ucrt-remquo-quotient-bits` → `main` · +66 −2 · 2 files · 3 commits · merged 2026-11-26 12:08 UTC `remquo` stores the sign and the low bits (at least three) of the integral quotient `x / y`. The host took them from `(x - r) / y`, which is no exact integer once the quotient passes 2^53: `remquo(7.32e218, 1.23e62)` gave 7 where the low bits are 100, and 83,412 of 199,812 random finite pairs disagreed with the host libm. Now they come from `|x| mod 8|y|`, which `fmod` computes exactly (when `8|y|` overflows, `|x / y|` is below 8 and `|x|` is used directly). `remquo_keeps_the_low_bits_of_a_huge_quotient` fails without the fix (bits 7 instead of 4) and checks the sign and low three bits against the host's `remquo` over 300,000 pairs across every exponent. ## Testing - `cargo test --target x86_64-apple-darwin -p victory-hle` passes (220 unit, 15 native_probe; the D3D12 probe test skipped, no Metal Shader Converter here). - Clippy on `x86_64-apple-darwin` shows no new warnings; `rustfmt --check` and `cargo xtask policy` pass.
remquo stores the sign and the low bits (at least three) of the
integral quotient x / y. The host took them from (x - r) / y, which is
no exact integer once the quotient passes 2^53: remquo(7.32e218,
1.23e62) gave 7 where the low bits are 100, and 83,412 of 199,812
random finite pairs disagreed with the host libm.

Take them from |x| mod 8|y| instead, which fmod computes exactly. The
test checks the sign and low three bits against the host's remquo over
300,000 pairs across every exponent.
m - remainder(m, |y|) overflows near DBL_MAX: remquo(DBL_MAX, 1e308)
stored 7 instead of 2. Divide each by |y| first. The test covers that
case, and native-host.md states the quotient-bit guarantee.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:43:59 +00:00
github manually merged commit 72bf3b0d3f into gh-pr/51/base 2026-11-26 14:44:01 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 11:42 UTC · original · requested changes

The huge-quotient regression catches the original bug, but one boundary case still fails. remquo(f64::MAX, 1.0e308, &q) stores 7 instead of 2, because the intermediate m - remainder(m, ay) overflows before the division (ucrt/math.rs:~305). That bug is on main too. Please add that case, and avoid the overflowing intermediate, or take the quotient bits straight from the host remquo. Please also document the quotient-bit guarantee in the UCRT notes: the low three bits of the magnitude, plus the sign.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 11:42 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/51#pullrequestreview-6073546257) · **requested changes** The huge-quotient regression catches the original bug, but one boundary case still fails. `remquo(f64::MAX, 1.0e308, &q)` stores `7` instead of `2`, because the intermediate `m - remainder(m, ay)` overflows before the division (`ucrt/math.rs:~305`). That bug is on main too. Please add that case, and avoid the overflowing intermediate, or take the quotient bits straight from the host `remquo`. Please also document the quotient-bit guarantee in the UCRT notes: the low three bits of the magnitude, plus the sign.
Author
Owner

@⁠Repla09 on GitHub · 2026-11-26 11:50 UTC · original

Thanks, good catch. In 28f0c54 the quotient bits divide by |y| before subtracting (m / ay - remainder(m, ay) / ay), so nothing overflows near DBL_MAX: remquo(DBL_MAX, 1e308) now stores 2, and that case is in the test. The UCRT notes state the guarantee (sign plus the low three bits of the magnitude). Rebased onto main.

**@⁠Repla09** on GitHub · 2026-11-26 11:50 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/51#issuecomment-7191227356) Thanks, good catch. In 28f0c54 the quotient bits divide by |y| before subtracting (`m / ay - remainder(m, ay) / ay`), so nothing overflows near DBL_MAX: `remquo(DBL_MAX, 1e308)` now stores 2, and that case is in the test. The UCRT notes state the guarantee (sign plus the low three bits of the magnitude). Rebased onto main.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 12:01 UTC · original · approved these changes

Thanks for addressing the earlier requests; the regression passes under Rosetta and fails against main. Approved.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 12:01 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/51#pullrequestreview-6073790967) · **approved these changes** Thanks for addressing the earlier requests; the regression passes under Rosetta and fails against main. Approved.
This discussion has been locked. Commenting is limited to contributors.
No description provided.