48 points · by profquail · 16y ago · 35 comments
This isn't a bug. Both code paths produce results of the highest representable precision of the hardware in question. It's just that there are multiple hardware units capable of giving you the answer, and Apple's toolchain picks a different one than whatever Tom is using elsewhere.
And as has been pointed out in FooBarWidget's comment -- any code that relies on bit-precise results from floating point computation is almost certainly concealing precision bugs anyway. That's not the right way to approach floating point architecture.
x87 does floating point math with 80 bits (in registers). But the variables are stored in memory locations with 64 bits. So the results are rounded.
The problem comes from when to do the rounding. And that can vary depending on if a register is needed for something else, optimizations, the order of code, etc.
To put it another way, it isn't Apple doing something wrong. If your code depends on rounding errors in FP representations, your code is buggy.
That's also why good numerical algorithms are hard to design. You need to worry about losing precision. As a simple example, if you calculate (a-b)c and a is almost equal to b, you're much better off doing ac-b*c. These tricks are important.
For example, on x86, many C / C++ compilers interpret long double as a 64-bit float while others interpret it as 80-bit extended precision.
And extended precision TBYTE (in Intel syntax) does actually take up 10 bytes. There's a speed loss for not aligning, but no correctness loss. In a possibly related note, however, Apple has bizarre and unnecessarily strict alignment policies for Mac OS on x86:
That's a stretch. Just because it's called "GCC" doesn't mean it behaves exactly the same on every platform, even if the CPU architecture is the same.
Apple makes extensive modifications, has their own ABI (see below), etc.
In a possibly related note, however, Apple has bizarre and unnecessarily strict alignment policies for Mac OS on x86:
It's not "bizarre and unnecessarily strict" if you want to be able to rely on SSE2+. Apple had the advantage of being able to define their ABI without regard to most legacy concerns, and so they did.
The reason that it's strictly enforced everywhere is that since Apple's compilers use SSE2+ they must be able to assume that, at function entry, the stack is properly (16 byte) aligned.
I understand your pain -- I've had to update a JIT implementation to deal with this, along with quite a bit of assembly that assumed 4 byte alignment, but Apple's reasoning makes sense.
See also: http://stackoverflow.com/questions/612443/why-does-the-mac-a...
Wait a minute, does that mean that floating point types don't have a stable ABI? If I have a library with function foo(float bar) compiled with compiler A, then it's not safe to compile an app - that uses that library - with compiler B?
For example, the following C statement:
printf("%d\n", sizeof(long double));
gives the following results on my box: Cygwin gcc: 12
MSVC: 8
Embarcadero bcc32: 10
So yes, if you are assuming that foo(long double bar) - that specific header definition - compiled with compiler A will be binary compatible when compiled with compiler B, then you are not necessarily safe.Just `float`, though, is rather more solid.
Trying compiler constants first...
f is 0.12345, rounded to 0.1235, expanded to 0.123450003564357760000000000000
d is 0.12345, rounded to 0.1235, expanded to 0.123450000000000000000000000000
ld is 0.12345, rounded to 0.1235, expanded to 0.123450000000000000000000000000
Now trying derived values...
f is 0.12345, rounded to 0.1235, expanded to 0.123450003564357760000000000000
d is 0.12345, rounded to 0.1235, expanded to 0.123450000000000000000000000000
ld is 0.12345, rounded to 0.1235, expanded to 0.123450000000000000000000000000
So don't blame the architecture