5 ms·
Probably a bug report with the most duplicates? > The x87 FPU was originally designed in (or before) 1980. I think that's why it is quite simple: it has only o
by yokohummer7 10y ago
Probably a bug report with the most duplicates?
> The x87 FPU was originally designed in (or before) 1980. I think that's why it is quite simple: it has only one unit for all FP data types. Of course, the precision must be of the widest type, which is the 80-bit long double.
> Consider you have a program, where all the FP variables are of the type double. You perform some FP operations and one of them is e.g. 1e-300/1e300, which results in 1e-600. Despite this value cannot be held by a "double", it is stored in an 80-bit FPU register as the result. Consider you use the variable "x" to hold that result. If the program has been compiled with optimization, the value need not be stored in RAM. So, say, it is still in the register.
> Consider you need x to be nonzero, so you perform the test x != 0. Since 1e-600 is not zero, the test yields true. While you perform some other computations, the value is moved to RAM and converted to 0 because x is of type "double". Now you want to use your certainly nonzero x... Hard luck :-(
> Note that if the result doesn't have its corresponding variable and you perform the test directly on an expression, the problem can come to light even without optimization.
> It could seem that performing all FP operations in extended precision can bring benefits only. But it introduces a serious pitfall: moving a value may change the value!!!
TIL long double is a thing.
- vortico 10y agoSo would turning off denormals fix the problem? That concept has given me 100 problems in the past and had helped me 0 times.
- wtallis 10y agoTurning off denormals won't fix the issue that a long double has a wider exponent field than a double, and can represent smaller magnitudes without relying on denormals.
- kr7 10y agoThe solution is to compile with SSE2 on x86. (flags: -mfpmath=sse -msse -msse2) On x86-64, the compiler should default to SSE2. SSE2 is ~16 years old so compatibility shouldn't be an issue.
- phire 10y agoTechnically, you only actually need the instructions from the original SSE set to do floating point operations. SSE2 adds a bunch of really useful integer floating point instructions. But the only extra cpus that gets you is the Pentium III, AMD Athlon XP, and AMD Duron. SSE2 is supported on every single x86 cpu released after those, such as the Pentium 4, Pentium M, and Athlon 64. It's a real shame that people are still using CPUs that don't support SSE4, such as the AMD Phenom and Phenom II cpus, otherwise everyone would have moved to exclusive SSE4.