r/AskProgramming • u/Odd-Heron5704 • Aug 29 '26
Can simple mathematical functions give different results in different CPU architectures?
I'm referring to modern CPUs. To make the question more specific, arm64 vs x86
And if so, how do you fix it?
I asked chatGPT and it gave me this example but I'd like to ask it here (since AI can make mistakes). Can you let me know? Thanks
It says this can give different results due to rounding errors. If so, how to you write code so that you don't have this issue?
#include <iostream>
int main() {
double a = 1.0 + 0x1p-27;
double b = 1.0 - 0x1p-27;
double c = -1.0;
std::cout << (a * b + c) << '\n';
}
17
Upvotes
3
u/real_kerim Aug 29 '26
Is this related to the AoE2 cross-play post? The explanation in that post was... questionable.
2
u/khedoros Aug 29 '26
Taking your code above, I compiled it like this, forcing SSE floating-point in one compilation (64-bit internal calculations), and 387 floating-point (80-bit internal calculations) in another.
I could do a similar experiment on my Raspberry Pi (ARM64 architecture, currently running a 32-bit OS though, I think) if I wanted to, and I suspect it would match the SSE answer.
37
u/Avereniect Aug 29 '26 edited Aug 29 '26
So long as both C++ implementations adhere to IEEE-754 and follow strict math, that code should deliver identical results on both architectures because the rounding behavior is a well-defined part of spec. Although rounding may lead to inexact results, that is different than saying that the results will vary across compliant implementations.
For an actual example where results may differ, I'd suggest contrasting an x86 target with only x87 80-bit floats supported compared to a more recent x86 implementation that supports IEEE-754 via SSE or later.
15
u/tgm4mop Aug 29 '26
Modern CPUs follow IEEE standards for basic floating point operations, so the same assembly code will give the same results. (With some caveat about rounding modes)
However C++ introduces some complexity, as it does not guarantee IEEE behavior itself, but you can usually control it with compiler flags. In the example you give, the compiler could choose to emit either an add and a multiply, or a single fused multiply-add. These could give different results. So if you want reproducible results across compilers, you will need to have the compiler flags carefully set to ensure the generated assembly is the same across builds.
13
u/daV1980 Aug 29 '26 •
Other posts here are correct for when CPUs and compilers are running in IEEE 754 strict mode. IEEE 754 strict mode requires the compiler and processor to run exactly the operations you've asked for and the resulting answer must be bitwise the same on any implementation (at the time of the result, any intermediate calculations are allowed to use implementation defined precision, but they are non-observable and so are not required to match).
However, many applications and games run with 'fast math' (on GCC or clang, this is the option -ffast-math). This can be beneficial for performance in many applications for a variety of reasons. One is that many CPUs have an operation to do a "fused multiply add (FMA)," which means that they can execute A * B + C as one instruction. But the result of using FMA is that the results will very frequently not be the same as if you actually multiplied A * B, then added C to the result.
Floating point math doesn't meet three of the four properties of ordinary arithmetic, primarily due to precision but also because of internal implementation details. That is that FP math is not commutative, associative, or distributive.
Changing the order of operations or the grouping of those operations absolutely can change the results.
--
I saw the post about AoE 2, and while I didn't work on AoE 2, I was a programmer on an extremely popular RTS in the early 2000s; if you are a fan of the genre and played in that era you almost definitely played the game I worked on.
RTS games of that era (and maybe now? but I don't work on them nowadays) used a networking architecture called peer to peer. This architecture relies on having clients send commands to each other and executing those commands at some point in the future (that point being decided based on the latency of the players from each other). Rather than there being a single, dedicated server who runs commands and sends back the results, this says "let's just send the commands to each other, and we'll both execute them at the same time and by the inductive property we will agree about the next state of the game (that is "state a + command 1 = state b").
Periodically these games need to verify that they are still in sync, and the fastest way to do this is to simply CRC all of the game state memory and exchange that CRC over the network. But doing a CRC in this way requires that you are bitwise accurate across all state in the game. For integers, this is pretty straightforward.
But because of the fuzziness of floats described above, this can be much harder to achieve across architectures, especially if the game uses fast math. For x86-based processors, even -fast-math is likely to yield identical results (it is in AMD's interests to match what Intel generates and vice versa, and both chips will be driven by the same compiled code). But when you are running on ARM (which is the processor used by modern Apple machines), you are either running in emulation or recompiling. In both cases, ensuring that you will get bit-accurate results in computations becomes more challenging. As a bonus problem, historically (though I haven't looked recently as I don't generally ship things for ARM these days), performance of fp strict mode on ARM was terrible. Like "30-50% slower" terrible, so not really feasible for a game.
Anyways, it's certainly possible, but it's difficult.
10
u/AndrewBorg1126 Aug 29 '26
Not if they all correctly implement the same standard.
If there is an inplementation bug, or if they are not designed to fit the same standard, they could behave differently.
5
u/Mynameismikek Aug 29 '26
Floating point values are usually disallowed in fully deterministic systems because differing order of operations can give rise to errors.
Equivalent hand written assembly should come to the same result, but any non-trivial compiled code can drift.
5
u/Chuu Aug 30 '26
I'm just going to post this at the top level because a bunch of posts are giving an incorrect answer. Even following IEEE-754 strictly you can get different results on different architectures.
The problem is while the IEEE-754 standard is overwhelmingly popular for floating point representation, modern CPUs have expanded the scope of what floating point operators they support which might result in different results on different architectures.
For example, most modern FPUs can do a "fused multiply add" that which computes `a=a+(b*c)` as a single instruction much more efficiently than an add and multiply. This was standardized by IEEE-754 in 2008, and specifically only rounds once.
Which means if your CPU supports it, a=a+(b*c) might be computed with a FMA instruction and be strictly IEEE-754 compliant and produce one result. However for an architecture that does not support FMA, the compiler might compute it as (tmp=(b*c), a=a+tmp) which has *two* rounding instructions -- one after the multiply and one after the add. Which could produce a different result.
Both are IEEE-754 compliant.
3
u/Plus-Painter-2004 Aug 29 '26
On different architectures it shouldn’t assuming they follow the same standards eg ieee 754 floats. What might give different results is if different compilers are used which might optimise code differently, which in turn can cause certain operations to return different values since rounding errors are affected by order of operations (eg a(b+c) could give a slightly different result to ab + ac)
1
u/Whole-Chest90 Aug 29 '26
As others have said here, and what would make sense to me, is that the CPU itself shouldn't give different answers, but different layers (by that I mean compilers, IDEs, platforms) on those cpus might.
1
u/MyTinyHappyPlace Aug 29 '26
IEEE floating point is very strictly standardized. You can have differences on C-level when it comes to numeric ranges of integers, though.
I know, but here is my favorite line: "Don't divide, Intel inside".
1
u/Useful_Calendar_6274 Aug 29 '26
simple? no. that would be an error
1
u/SpiritedInflation835 Aug 29 '26
Well... I can only point out the chipsets in the TI-30 calculators with their famous logarithm bug: http://www.datamath.org/Story/LogarithmBug.htm
1
u/Senior_Care_557 Aug 29 '26
interesting
1
u/Torebbjorn Aug 30 '26
Well yes, since C(++) do not give strict requirements for what an "int" or a "float" is, just that they must be at least of certain sizes and also adhere to some relational rules, you could have different results based on platform.
I would think that modern x86 and arm64 cpus all use the same standards, namely IEEE 754, but I don't know for certain if all do.
1
u/Affectionate-Slice70 Aug 30 '26
If you’re not doing anything funky they will be the same. At some level the hardware differs but that is intentionally abstract away from you and standardised as you go up the stack.
1
u/Traveling-Techie Aug 30 '26
This may or may not be useful, but when I worked running benchmarks on parallel supercomputers we ran into the issue that, strictly speaking, floating point arithmetic is not associative. Order of operations matter, and parallel computations controlled by an optimizing compiler can give differing results. The errors usually occur in low order bits that are not reliable in the first place, but users were very attached to the idea of repeatability, and we had to educate them about why different runs under different loads gave slightly different results.
1
u/SeriousPlankton2000 Aug 31 '26
Yes, there was a Pentium bug. "Intel inside, can't divide".
1
u/PvtRoom Aug 29 '26
Different CPUs can have different optimisations.
1030 + 10-30 - 1030 = 10-30, but that's way smaller than rounding errors
CPUs do things like optimize code for speed, A+B-C, it could recognise as B (as A=C, fastest and most accurate), or it could do (A+B)-C = 0. Then you have what does the compiler do, does the compiler do fancy stuff to preserve accuracy.