Reputation: 19451
We are writing an emulator and want to know that for a given FPU state, is the state of the the FPU deterministic after running an FPU instruction eg. FDIV?
There are many Intel compatible CPU architectures, that implement x87 instruction set. Should we rely on the resulting CPU state when testing the correctness of our FPU emulation?
Are different implementations round/compute differently?
Upvotes: 3
Views: 992
Reputation: 61
As others have said, the basic five operations will give identical results on all x87 FPUs. However...
The x87 FPU is peculiar. It's 80-bit registers mean that it may get results that are different than on any other FPU (such as on PowerPC or ARM with have 64-bit floating-point registers). These results may be better or worse. Even setting the x87 rounding mode to 32-bit or 64-bit will not make it the same as PowerPC/ARM because the exponent remains unbounded.
For writing an emulator this shouldn't matter, except that it means that you cannot necessarily use the host FPU to emulate the x87 FPU. I covered many of these issues in these two articles:
http://randomascii.wordpress.com/2013/07/16/floating-point-determinism http://randomascii.wordpress.com/2012/03/21/intermediate-floating-point-precision/
Upvotes: 1
Reputation: 80325
Yes, the x87 FPU is deterministic for the basic operations +
, -
, *
, /
, sqrt
. For instance, in its default state (full-width significand and round-to-nearest-even mode), the result of x + y
is exactly the nearest representable 80-bit floating-point value to the result of the mathematical sum of x
and y
.
Makers of compatible chips have implemented the exact same definition for the operations above.
The x87's instruction set, in pure CISC tradition, also contains high-level instructions to compute mathematical functions, say, an approximation of the sine (FSIN
). The results of these instructions have varied over brands and models of processors. In your emulator, you probably need only to provide something as good as one of the worst implementations of these instructions still in use. No-one should complain if you use the result of the sinl()
function from the math library of your host language, which is typically better than FSIN
(by virtue of being implemented with explicit argument reduction and polynomial approximation, and not a call to FSIN
).
Upvotes: 5
Reputation: 2125
It should be, if you stick to the basic FPU instructions, and use the same compiler for your FPU algorithms if you want them to produce consistent results. That's not counting the fact you need to properly initialize your FPU library.
Here is some good reading on the subject if you're interested: https://randomascii.wordpress.com/2013/07/16/floating-point-determinism.
The links to other articles inside this one are exceptionally useful.
Upvotes: 2