Discrepancies in Floating-Point Arithmetic: x86 vs. x64
In a code snippet involving floating-point arithmetic, inconsistencies arise between MS VS 2010 builds targeting x86 and x64 architectures. The code is as follows:
float a = 50.0f; float b = 65.0f; float c = 1.3f; float d = a*c; bool bLarger1 = d < b; bool bLarger2 = (a*c) < b;
Discrepancies:
Underlying Issue:
The discrepancy stems from the expression bool bLarger2 = (a*c) < b;. While it appears to represent the same comparison as bool bLarger1 = d < b, it actually performs the multiplication and comparison separately.
Difference in Floating-Point Units:
The key difference lies in the floating-point units employed by the two architectures. The x86 architecture uses the x87 floating-point unit, which performs calculations at a higher precision than single-precision (typically double-precision). In contrast, the x64 architecture uses the SSE floating-point unit, which performs pure single-precision calculations.
Impact on the Multiplication:
In the bLarger1 expression, the multiplication of a and c is performed by the hardware multiply instruction. This instruction uses double-precision precision, resulting in d being set to 65.0.
However, in the bLarger2 expression, the multiplication is explicitly performed in single-precision due to the type conversion (a*c). This results in (a*c) being set to 64.999992.
x87 Precision Control:
By default, the x87 unit operates at double-precision. However, it is possible to persuade the unit to perform single-precision calculations using the _controlfp function.
_controlfp(_PC_24, _MCW_PC);
By adding this line to the 32-bit code, both bLarger1 and bLarger2 will be set to false.
Compiler Options:
In more recent versions of Visual Studio, the compiler may emit SSE instructions even for 32-bit targets. This ensures consistency in floating-point arithmetic across different architectures.
The above is the detailed content of Why does floating-point arithmetic produce different results between x86 and x64 architectures?. For more information, please follow other related articles on the PHP Chinese website!