Home > Backend Development > C++ > body text

Why Do Floating-Point Arithmetic Results Differ Between x86 and x64 Builds in MS VS 2010?

Patricia Arquette
Release: 2024-10-31 19:36:02
Original
642 people have browsed it

Why Do Floating-Point Arithmetic Results Differ Between x86 and x64 Builds in MS VS 2010?

Floating-Point Arithmetic Discrepancies Between x86 and x64 in MS VS 2010

In Microsoft Visual Studio 2010, floating-point arithmetic operations exhibit discrepancies between x86 and x64 builds, even when executed on the same 64-bit machine. This is evident in the following code snippet:

<code class="c++">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;</code>
Copy after login

The boolean variable bLarger1 is consistently false, with d being set to 65.0 in both builds. However, bLarger2 is false for x64 but true for x86.

The Culprit: x87 vs. SSE

The discrepancy arises from the fact that the x86 build uses the x87 floating-point unit (FPU) while the x64 build uses the Streaming SIMD Extensions (SSE) unit. The difference lies in the precision of the calculations.

The x87 FPU performs calculations in double precision by default, even when handling single-precision values. The SSE unit, on the other hand, performs calculations in pure single precision. As a result, x87 calculations are slightly more accurate than SSE calculations.

In this case, the x87 FPU's increased precision leads to a rounding error that causes d to be slightly less than 65.0. This makes bLarger2 false for x86. In contrast, the SSE calculation in x64 produces a more precise value for d, resulting in a true value for bLarger2.

Workaround for x86

To force the x86 build to perform calculations in single precision, the following line can be added:

<code class="c++">_controlfp(_PC_24, _MCW_PC);</code>
Copy after login

This sets the floating-point control word to perform all calculations in single precision. With this change, both bLarger1 and bLarger2 will be set to false for both x86 and x64 builds.

Conclusion

The discrepancy in floating-point arithmetic between x86 and x64 builds in MS VS 2010 stems from the difference between the x87 FPU and the SSE unit. The x87 FPU's higher precision leads to slightly different values compared to the SSE unit's true single-precision calculations. By forcing single-precision calculations on x86, developers can mitigate this issue and ensure consistent results across different platforms.

The above is the detailed content of Why Do Floating-Point Arithmetic Results Differ Between x86 and x64 Builds in MS VS 2010?. 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!