Interpreter and compiler result mismatch for floatToFloat16 conversion

XMLWordPrintable

    • x86_64
    • generic

      The following test shows different results with the Interpreter and the C1/C2 compiler.

      public class bug_fp32_fp16_conv {
         public static int POS_NAN = 0x7fc00000;
         public static int NEG_NAN = 0xffc00000;

         public static short GOLDEN = Float.floatToFloat16(Float.intBitsToFloat(NEG_NAN) + Float.intBitsToFloat(POS_NAN));

         public static short micro() {
             return Float.floatToFloat16(Float.intBitsToFloat(NEG_NAN) + Float.intBitsToFloat(POS_NAN));
         }

         public static void main(String [] args) {
             short [] res = new short[1024];
             for (int i = 0; i < 10000; i++) {
                 for (int j = 0; j < 1024; j++) {
                     res[j] = micro();
                 }
             }

             for (int i = 0; i < 1024; i++) {
                 if (GOLDEN != res[i]) {
                     throw new AssertionError("FAIL " + " GOLDEN = " + GOLDEN + " != ACTUAL = " + res[i]);
                 }
             }
             System.out.println("PASS");
         }
      }

      EMR>javac bug_fp32_fp16_conv.java
      EMR>java -Xbatch -Xint -cp . bug_fp32_fp16_conv
      PASS
      EMR>
      EMR>
      EMR>java -Xbatch -XX:TieredStopAtLevel=3 -cp . bug_fp32_fp16_conv
      Exception in thread "main" java.lang.AssertionError: FAIL GOLDEN = 32256 != ACTUAL = -512
              at bug_fp32_fp16_conv.main(bug_fp32_fp16_conv.java:25)

      The problem can also be reproduced with JDK-21.

            Assignee:
            Jatin Bhateja
            Reporter:
            Jatin Bhateja
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: