From 403a67747fb834abab18fe649f9e884aa7edcf4d Mon Sep 17 00:00:00 2001 From: sahib453 <117431595+sahib453@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:25:07 +0530 Subject: [PATCH] loader: set only the overflow flag for an exact unmasked overflow (#128) With the overflow exception unmasked, an overflow whose result is exact before the exponent is bounded set the MXCSR precision flag as well as the overflow flag. The Intel SDM (Vol. 1, D.4.2.3, Table D-16) reports precision there only if rounding with an unbounded exponent is inexact. - softfloat.rs: Control reads the overflow mask, and round() reports precision on an unmasked overflow only when the result was rounded. The masked response is unchanged. - cpu_known_answers.rs: the five exact-overflow cases are no longer an ignored defect, three more cover the other rounding modes, and the scalar and conversion sweeps now compare the precision flag here. - docs/cpu/instruction-registry.md: the behaviour is no longer listed as unasserted. Signed-off-by: sahib453 <117431595+sahib453@users.noreply.github.com> --- CHANGELOG.md | 1 + crates/loader/src/softfloat.rs | 10 ++++++++++ crates/loader/tests/cpu_known_answers.rs | 22 ++++++++-------------- docs/cpu/instruction-registry.md | 5 +++-- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e686c9c..5426660 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -188,6 +188,7 @@ remains unsupported. ### Fixed - Made the static probe and the binder share one bounded import parser, after the truncated-name fix for #270. Truncated, unterminated, empty or non-ASCII names, reserved bits and endless tables are reported by the probe and refused by the binder. +- Fixed the MXCSR flags of an unmasked floating-point overflow whose result is exact before the exponent is bounded, such as the largest binary32 value times two (#128). Only the overflow flag is set; the precision flag used to be set as well although nothing was rounded. A masked overflow still sets both flags and delivers infinity or the largest finite value, and an unmasked overflow that is also inexact still sets both. Five Intel SDM cases in `cpu_known_answers.rs` moved from open defect to verified on both engines, with three more for the other rounding modes, and the scalar and conversion sweeps now compare the precision flag in this case as well. Not compared with x86 hardware; awaiting independent review. - Fixed import binding writing some slots before finding an unresolved import, and service calls taking effect before an unreadable return address was noticed. A service that changes a nonvolatile register, including XMM6 to XMM15, now stops the guest (part of #201). - Fixed older import-generator templates that reported success for unimplemented wrappers, used a nonexistent exit reason, and read floating-point arguments from integer registers. - Fixed guest flags in the native DBT (#64, #70). They lived only in host NZCV, so they were lost across helper calls and block boundaries and register ADD, SUB and XOR set none. JB, JAE, JBE and JA used ARM carry polarity, which is inverted for subtraction. Flags are now stored in the guest state. diff --git a/crates/loader/src/softfloat.rs b/crates/loader/src/softfloat.rs index 1c75877..ac9dee0 100644 --- a/crates/loader/src/softfloat.rs +++ b/crates/loader/src/softfloat.rs @@ -35,6 +35,7 @@ pub struct Control { pub rounding: Rounding, pub denormals_are_zero: bool, pub flush_to_zero: bool, + pub overflow_masked: bool, pub underflow_masked: bool, } @@ -49,6 +50,7 @@ impl Control { }, denormals_are_zero: mxcsr & 1 << 6 != 0, flush_to_zero: mxcsr & 1 << 15 != 0, + overflow_masked: mxcsr & 1 << 10 != 0, underflow_masked: mxcsr & 1 << 11 != 0, } } @@ -263,6 +265,14 @@ fn round( flags |= PRECISION; } if leading > format.bias() { + if !control.overflow_masked { + // No result is delivered. Precision is reported only if rounding + // with an unbounded exponent was inexact. + return Outcome { + bits: 0, + flags: OVERFLOW | if inexact { PRECISION } else { 0 }, + }; + } let to_infinity = match control.rounding { Rounding::Nearest => true, Rounding::Down => sign, diff --git a/crates/loader/tests/cpu_known_answers.rs b/crates/loader/tests/cpu_known_answers.rs index 7a96f2b..3ea8c5a 100644 --- a/crates/loader/tests/cpu_known_answers.rs +++ b/crates/loader/tests/cpu_known_answers.rs @@ -2596,12 +2596,15 @@ add_memory_crosses_contiguous_pages: rdi=D+1000 mffd=ffffffffffffffff => mffd=00 push_pop_cross_contiguous_pages: rsp=D+1004 rax=1122334455667788 => rbx=1122334455667788 mffc=8877665544332211 f=* "; -const DEFECT_EXACT_UNMASKED_OVERFLOW: &str = r" +const EXACT_UNMASKED_OVERFLOW: &str = r" @ mulss xmm1, xmm2 !f30f59ca SDM 1 D.4.2.3 Table D-16 (PE only if inexact); APM 1 4.10.2.8 unmasked_overflow_exact_sets_only_oe: x1=s:7f7fffff x2=s:40000000 mx=1b80 => mx=1b88 f=* ! XM unmasked_negative_overflow_exact_sets_only_oe: x1=s:ff7fffff x2=s:40000000 mx=1b80 => mx=1b88 f=* ! XM +unmasked_overflow_exact_sets_only_oe_down: x1=s:7f7fffff x2=s:40000000 mx=3b80 => mx=3b88 f=* ! XM +unmasked_overflow_exact_sets_only_oe_up: x1=s:7f7fffff x2=s:40000000 mx=5b80 => mx=5b88 f=* ! XM +unmasked_overflow_exact_sets_only_oe_zero: x1=s:7f7fffff x2=s:40000000 mx=7b80 => mx=7b88 f=* ! XM @ addsd xmm1, xmm2 !f20f58ca SDM 1 D.4.2.3 Table D-16 (PE only if inexact); APM 1 4.10.2.8 @@ -2649,10 +2652,7 @@ const GROUPS: &[(&str, &str)] = &[ ("fp_mxcsr_and_unmasked", FP_MXCSR_AND_UNMASKED), ("fp_beyond_sse2", FP_BEYOND_SSE2), ("defect_overlong_instruction", DEFECT_OVERLONG_INSTRUCTION), - ( - "defect_exact_unmasked_overflow", - DEFECT_EXACT_UNMASKED_OVERFLOW, - ), + ("exact_unmasked_overflow", EXACT_UNMASKED_OVERFLOW), ("page_crossing", PAGE_CROSSING), ]; @@ -3513,9 +3513,8 @@ fn defect_overlong_instruction() { } #[test] -#[ignore = "known defect (#155): an exact unmasked overflow sets PE as well as OE"] -fn defect_exact_unmasked_overflow() { - check_group(DEFECT_EXACT_UNMASKED_OVERFLOW); +fn exact_unmasked_overflow() { + check_group(EXACT_UNMASKED_OVERFLOW); } #[test] @@ -4738,12 +4737,7 @@ fn deliver( } if lowest + precision - 1 > format.max_exponent() { if mxcsr & OM == 0 { - // PE of an exact overflow is `defect_exact_unmasked_overflow`; this - // sweep compares every other bit. - return Outcome { - open: if unbounded_exact { PE } else { 0 }, - ..outcome(0, OE | unbounded_inexact) - }; + return outcome(0, OE | unbounded_inexact); } let to_infinity = match mode { 0 => true, diff --git a/docs/cpu/instruction-registry.md b/docs/cpu/instruction-registry.md index 30868c6..1ed7084 100644 --- a/docs/cpu/instruction-registry.md +++ b/docs/cpu/instruction-registry.md @@ -40,8 +40,9 @@ XMM registers live in `CpuState::xmm`, lane 0 in the low bits, and MXCSR in `Cpu - a denormal dividend over zero raises divide-by-zero only, not also denormal-operand; - flush-to-zero also flushes a tiny result that is exact, raising underflow and precision; -- DAZ applies before conversion to an integer; -- an unmasked overflow also records precision. +- DAZ applies before conversion to an integer. + +An unmasked overflow records precision only when rounding with an unbounded exponent is inexact (Intel SDM Vol. 1, D.4.2.3, Table D-16). The known-answer cases in `tests/cpu_known_answers.rs` assert this. None of it has been compared with x86 hardware.