Numerical error on native float access

Ulf Zibis Ulf.Zibis at gmx.de
Sat Apr 17 15:37:43 PDT 2010


There seems to be a bug in Float.intBitsToFloat(int) and/or 
Float.floatToRawIntBits(float) and/or sun.misc.Unsafe.getInt/Float.

If I replace
         return 
Float.intBitsToFloat(Integer.reverseBytes(unsafe.getInt(offset)));
with
         return reverseBytes(unsafe.getFloat(offset));
     ...
     private static float reverseBytes(float x) {
         // intBitsToFloat, floatToRawIntBits should become NOP if 
intrinified
         return 
Float.intBitsToFloat(Integer.reverseBytes(Float.floatToRawIntBits(x)));
     }

... I get jtreg (jtr nioBuffer) failure with slightly different result:

   java.nio.DirectFloatBufferS[pos=0 lim=1792 cap=1792] 2f712001
java.lang.RuntimeException: java.nio.DirectFloatBufferS[pos=11 lim=11 
cap=1792]:
  Expected ' '=0x0.91697687, got ' '=0x0.91795343
         at Basic.fail(Basic.java:94)
         at Basic.ck(Basic.java:119)
         at BasicFloat.test(BasicFloat.java:494)
         at BasicByte.testViews(BasicByte.java:251)
         at BasicByte.test(BasicByte.java:618)
         at BasicByte.test(BasicByte.java:917)
         at Basic.main(Basic.java:132)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
         at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
         at java.lang.reflect.Method.invoke(Method.java:613)
         at 
com.sun.javatest.regtest.MainWrapper$MainThread.run(MainWrapper.java:96)
         at java.lang.Thread.run(Thread.java:717)

JavaTest Message: Test threw exception: java.lang.RuntimeException: 
java.nio.DirectFloatBufferS[pos=11 lim=11 cap=1792]: Expected ' 
'=0x0.91697687, got ' '=0x0.91795343

Equivalent error for equivalent methods in java.lang.Double.

Any idea, which of the mentioned methods is wrong?

-Ulf




More information about the nio-dev mailing list