GNU GCC does not round floating-point divisions to the nearest value
11–20 of 87 posts
Re: GNU GCC does not round floating-point divisions to the nearest value
#12Re: GNU GCC does not round floating-point divisions to the nearest value
#13Re: GNU GCC does not round floating-point divisions to the nearest value
#14That doesn't mean it's necessarily the right thing to do though.
Clang can be made generate the same code as the 'broken' gcc output by declaring the variables as 'long double' instead of 'double'. So I think this is where the issue lies?
Edit: godbolt link requiring large monitor: https://godbolt.org/z/tFiZwW
Re: GNU GCC does not round floating-point divisions to the nearest value
#15https://www.kotaku.com.au/2018/06/the-mario-64-trick-that-ta...
Re: GNU GCC does not round floating-point divisions to the nearest value
#16This seems like a property of the Intel 8087 math coprocessor and its distant descendants found in x86 chips, not anything to do with gcc?
My understanding is that in this case it's GCC's optimizer evaluating the expression at compile time in a way that gives a different result to if it's done at runtime. EDIT: GCC gets it wrong with -O0 (when it's evaluated at runtime) and right with -O2 (when it evaluates it at compile time) Clang appears to get it right when evaluated at compile time or runtime. Clang uses the divsd instruction; GCC uses the fdiv ins…
You can configure gcc to use sse math with the -mfpmath=sse instruction. The author states that gcc still gets it "wrong" but he simply is not correct. gcc will use divsd and gets 0.501782303180000055.
gcc defaults to use the x87 fpu instead of sse because not all 32 bit x86 cpus have SSE instructions. It's a safer default. When compiled in 64 bit mode, it uses up to SSE2 instructions, because all x86_64 CPUs have SSE2.
Re: GNU GCC does not round floating-point divisions to the nearest value
#17If you add '-ffast-math' to the compiler parameters in his godbolt link you get the 'right' answer. Doing that means the division is done with the 'divsd' instruction, which clang uses even without the 'fast-math' flag. That doesn't mean it's necessarily the right thing to do though. Clang can be made generate the same code as the 'broken' gcc output by declaring the variables as 'long double' instead of 'double'. So…
Re: GNU GCC does not round floating-point divisions to the nearest value
#18Perhaps the title should mention that this is 32-bit x86, which is rather obsolete. This should work okay on x86_64.
Re: GNU GCC does not round floating-point divisions to the nearest value
#19This seems like a property of the Intel 8087 math coprocessor and its distant descendants found in x86 chips, not anything to do with gcc?
But then why does python and JavaScript on the same machines get it right?
If you are still writing 32 bit x86 C code, it's probably some embedded or legacy CPU, and it's wrong to assume SSE instructions are available. So GCC correctly defaults to using FPU instructions instead of SSE instructions on 32 bit x86.
Javascript and python will basically never be run in embedded environments, and their legacy environments probably aren't that legacy that SSE instructions aren't available. So it's reasonable to default to SSE.
Re: GNU GCC does not round floating-point divisions to the nearest value
#20Perhaps the title should mention that this is 32-bit x86, which is rather obsolete. This should work okay on x86_64.