Post

Replies

Boosts

Views

Activity

Rosetta 2: wrong scalar SSE results after x87 fsqrt/fdiv in translated x86_64 code
I'm seeing translated x86_64 code compute wrong floating-point results under Rosetta 2, with no fault raised. Filed as FB25702862. Posting here in case others hit it, and to ask whether anyone can check other macOS versions. Environment: macOS 27.2.1 (26B131), Apple M4 Max, Apple clang 21.0.0. Reproduce // clang -arch x86_64 -O1 -o repro repro.c && ./repro #include <math.h> #include <stdio.h> __attribute__((noinline)) static double f(float a, double v) { double d = a; // cvtss2sd xmm, xmm double r = (double)sqrtl((long double)v); // x87: fld / fsqrt / fstp return r - d; // subsd reads d } int main(void) { volatile float a = 24.0f; volatile double v = 7.9466; printf("got %.17g\n", f(a, v)); printf("expected %.17g\n", sqrt(v) - 24.0); return 0; }Output under Rosetta: got 2.8189714436297506 expected -21.181028556370251Built with -arch arm64 it prints the expected value. -O1, -O2 and -Os all fail the same way. What triggers it Three things in one basic block: A scalar conversion with a register source, such as cvtss2sd xmm0, xmm0 (also cvtsd2ss, sqrtsd, sqrtss, roundsd), where the destination's upper bits are not known to be zero. An x87 fsqrt, fdiv, fprem, fprem1, fptan or fpatan. For fsqrt, operands that are zero, a power of two, negative, infinite or NaN are harmless. A scalar use of the converted register: addsd, subsd, mulsd, divsd, comisd, ucomisd, cvtsd2ss and similar. The smallest sequence I have: cvtss2sd xmm0, xmm0 movsd [rsp-8], xmm1 fld qword ptr [rsp-8] fsqrt fstp qword ptr [rsp-8] xorpd xmm1, xmm1 addsd xmm1, xmm0 ; sees a tiny denormal instead of the converted valueA store of xmm0 at the same point sees the correct value. Zeroing the destination before the conversion, or any jump or call between the three parts, avoids the problem. VEX-encoded forms behave the same. What seems to be happening In the translated code, the conversion result is held in a scratch vector register and read from there again after the x87 helper calls. The runtime's software square root and divide get their starting estimate from the hardware reciprocal-estimate instructions using that same register and don't restore it. The value the program then reads is exactly that estimate: for fsqrt of 7.9466 the low 32 bits are 0x3fb5a000, which is frsqrte(7.9466 / 16) with FPCR.AH set. Who is affected Code from Mono's x64 JIT, including Unity games running under Rosetta. It emits Math.Sqrt as x87 fsqrt and widens float call results with cvtss2sd reg, reg, so an expression like GetRange() < vector.magnitude can evaluate wrongly. That is how I found it. C and C++ that mix long double arithmetic with float/double conversions. For runtime-generated code the fault also depends on placement: if a 4 KB page boundary falls between the conversion and the use, the result is correct. A JIT-compiled program therefore fails on most runs but not all, which made this hard to pin down. Questions Can anyone reproduce this on an older macOS release? I could only test 27.2.1, so I don't know whether it is a regression. Is there a recommended way to avoid x87 helper calls in translated code, short of changing the code generator?
0
0
177
1d
Rosetta 2: wrong scalar SSE results after x87 fsqrt/fdiv in translated x86_64 code
I'm seeing translated x86_64 code compute wrong floating-point results under Rosetta 2, with no fault raised. Filed as FB25702862. Posting here in case others hit it, and to ask whether anyone can check other macOS versions. Environment: macOS 27.2.1 (26B131), Apple M4 Max, Apple clang 21.0.0. Reproduce // clang -arch x86_64 -O1 -o repro repro.c && ./repro #include <math.h> #include <stdio.h> __attribute__((noinline)) static double f(float a, double v) { double d = a; // cvtss2sd xmm, xmm double r = (double)sqrtl((long double)v); // x87: fld / fsqrt / fstp return r - d; // subsd reads d } int main(void) { volatile float a = 24.0f; volatile double v = 7.9466; printf("got %.17g\n", f(a, v)); printf("expected %.17g\n", sqrt(v) - 24.0); return 0; }Output under Rosetta: got 2.8189714436297506 expected -21.181028556370251Built with -arch arm64 it prints the expected value. -O1, -O2 and -Os all fail the same way. What triggers it Three things in one basic block: A scalar conversion with a register source, such as cvtss2sd xmm0, xmm0 (also cvtsd2ss, sqrtsd, sqrtss, roundsd), where the destination's upper bits are not known to be zero. An x87 fsqrt, fdiv, fprem, fprem1, fptan or fpatan. For fsqrt, operands that are zero, a power of two, negative, infinite or NaN are harmless. A scalar use of the converted register: addsd, subsd, mulsd, divsd, comisd, ucomisd, cvtsd2ss and similar. The smallest sequence I have: cvtss2sd xmm0, xmm0 movsd [rsp-8], xmm1 fld qword ptr [rsp-8] fsqrt fstp qword ptr [rsp-8] xorpd xmm1, xmm1 addsd xmm1, xmm0 ; sees a tiny denormal instead of the converted valueA store of xmm0 at the same point sees the correct value. Zeroing the destination before the conversion, or any jump or call between the three parts, avoids the problem. VEX-encoded forms behave the same. What seems to be happening In the translated code, the conversion result is held in a scratch vector register and read from there again after the x87 helper calls. The runtime's software square root and divide get their starting estimate from the hardware reciprocal-estimate instructions using that same register and don't restore it. The value the program then reads is exactly that estimate: for fsqrt of 7.9466 the low 32 bits are 0x3fb5a000, which is frsqrte(7.9466 / 16) with FPCR.AH set. Who is affected Code from Mono's x64 JIT, including Unity games running under Rosetta. It emits Math.Sqrt as x87 fsqrt and widens float call results with cvtss2sd reg, reg, so an expression like GetRange() < vector.magnitude can evaluate wrongly. That is how I found it. C and C++ that mix long double arithmetic with float/double conversions. For runtime-generated code the fault also depends on placement: if a 4 KB page boundary falls between the conversion and the use, the result is correct. A JIT-compiled program therefore fails on most runs but not all, which made this hard to pin down. Questions Can anyone reproduce this on an older macOS release? I could only test 27.2.1, so I don't know whether it is a regression. Is there a recommended way to avoid x87 helper calls in translated code, short of changing the code generator?
Replies
0
Boosts
0
Views
177
Activity
1d