FloatMathPlugin for Croquet
andreas.raab at gmx.de
Mon Mar 27 02:44:15 UTC 2006
Hi Guys -
I've just committed the changes for the FloatMathPlugin which Croquet
uses to get bit-identical cross-platform floating point behavior (with a
big thanks to Sun for developing fdlibm ;-)
Getting FloatMathPlugin to compile should be simple; getting it to work
might be tricky. Because of this there are a bunch of tests with
extensive test data to ensure we truly have cross-platform behavior. For
efficiency, it's important to note that the "common" floating point
operations are not handled by fdlibm (it expects IEEE 754 conforming
behavior for the basic operations like add, subtract, multiply, divide)
but covers pretty much everything else.
What I have found playing with both PPC and Intel FPUs is the following:
Even if your FPU is IEEE754 compliant this means very little, as IEEE754
leaves lots of room for interpretation. Most importantly, make sure you
check the following two issues:
* Internal accuracy. Some FPUs (x87) have an internal accuracy that is
larger than 64bits (80bits on Intel). For compliance the FPU *MUST* be
set to treat all operations as 64bit (on x87 this means setting the
precision to 53 bits, e.g., _PC_53 in the control word).
* Fused multiply-add. Some FPUs (PPC) allow for so-called "fused"
multiply-add operations which compute the result of "a*b+c" with higher
internal accuracy. You must disable fused multiply-add *AT ALL COSTS*
(e.g., by using a global compiler switch if available) as the use of
fused multiply-add is both, subject to choice by the compiler (meaning
that even two different compilers might decide differently where to use
it) and is not generally supported on all FPUs.
The tests that I've provided come in two flavours: One flavour is just a
general compliance test which computes a bunch of numbers hashes them
(using MD5) and compares it with a precomputed value. This test is a
good quick way to check for compliance.
The second flavour uses pre-computed test data so you can actually look
at what is computed differently and where. The test data also comes in
two flavours, a small data set (10k entries) and a large data set (1000k
entries). The small one is a good one early on to find the problematic
cases quickly, the large one is good to check that with 1:1000000 chance
the code actually does compute the correct values. You need to drop the
data sets into the image directory for those tests to be run.
Since (so far) I have only tested this on x87 and PPC, I don't know if
other FPUs have other issues. If you encounter "wrong" results despite
checking for the above two issues you might want to read the manual for
your FPU to find out if there are any settings that may affect IEE754
compliance (like fused-madd, or internal accuracy).
PS. And apologies for the temporary SVN mess - I wasn't aware that my
SVN client wouldn't interpret the directory I had clicked on as being
relative to the URL (and ended up with committing everything to the
Cross/plugins directory instead of Cross/plugins/FloatMathPlugin).
More information about the Vm-dev