Image

Imagesparkymark wrote in Imagejava_dev

Java floating point operations corrupting SSE registers?



I have a Java native method (in C++) which does the following (a little logging is also present to make sure the variables are not optimised away)

1) Assign a floating point number to a variable: it can be anything but the same value has to be used here and in 3). Let's say it is 1.0f.
2) Calls back into Java and perform some floating point calculations, discarding the result.
3) Assign 1.0f to another variable.

On a 64 bit AMD chip, when I compile this native method with VS2005 in Release mode with full optimisation, *and* the Java method in 2) is JIT-compiled (ensured by the -Xcomp VM option) it does not behave as expected. The assignment in 3 assigns 0.0 instead of 1.0f.

(In almost any other configuration: interpreted Java, Debug mode, 32-bit... no problem!)

What seems to be happening, from disassembling the native method, is that the compiler notices that the same floating point number will be used twice and store it in a SSE register called xmm6. The first assignment happens OK, but the Java method called next also seems to make use of this register for its own floating point arithmetic. The second assignment takes place and the contents of xmm6 are copied into the second variable but they are no longer what the native code wrote in there.

For performance reasons I can't specify -"Xint" for the Java, and I can't disable SSE in the C++. Can I keep the Java method away from the registers, or is there some way to cache and restore their state as I move between native and Java code?