Home > Backend Development > C++ > body text

Why does floating-point arithmetic produce different results between x86 and x64 architectures?

Barbara Streisand
Release: 2024-11-03 03:12:29
Original
901 people have browsed it

Why does floating-point arithmetic produce different results between x86 and x64 architectures?

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;
Copy after login

Discrepancies:

  • x86 Build: Variable bLarger1 is false (both d and b are set to 65.0), while bLarger2 is true.
  • x64 Build: Both bLarger1 and bLarger2 are false.

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);
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!