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

Merged
Repla09 merged 3 commits from upstream-pr/51 into main 2026-11-26 12:08:52 +00:00
Repla09 commented 2026-11-26 11:31:04 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Repla09 on 2026-11-26T11:31:04Z.


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.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6625722482 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/51). Originally posted by [Repla09](https://github.com/Repla09) on 2026-11-26T11:31:04Z. --- `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.
LakshanSivakumar commented 2026-11-26 11:42:43 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T11:42:43Z.

Review: CHANGES_REQUESTED


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6073546257 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/51#pullrequestreview-6073546257). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T11:42:43Z. **Review:** CHANGES_REQUESTED --- 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.
Repla09 commented 2026-11-26 11:50:13 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Repla09 on 2026-11-26T11:50:13Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7191227356 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/51#issuecomment-7191227356). Originally posted by [Repla09](https://github.com/Repla09) on 2026-11-26T11:50:13Z. --- 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.
LakshanSivakumar commented 2026-11-26 12:01:57 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T12:01:57Z.

Review: APPROVED


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

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6073790967 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/51#pullrequestreview-6073790967). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T12:01:57Z. **Review:** APPROVED --- Thanks for addressing the earlier requests; the regression passes under Rosetta and fails against main. Approved.
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
LakshanSivakumar/Native-Victory-macOS!51
No description provided.