C1 compilation produces different NaN values than the interpreter

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      - Operating system: Linux
      - Compiler/toolchain: GCC 9.5
      - Source commit: 24fe61ea13610d2f5d22f1b6696b871eb0505379 (tag: 17.0.19-internal+0, branch: master)
      - Build type: release
      - Build environment: Linux, compiled from source with GCC 9.5
      - Also failed in 17.0.17+10, release

      A DESCRIPTION OF THE PROBLEM :
      The following small Java program returns different results depending on the VM execution mode. When run with -Xcomp (C2 compiled only), the method Func returns 10L. When run with -Xmixed or -Xint, it returns 20L. The logic hinges on comparisons of Float.floatToRawIntBits of expressions that include:
      - NaN propagation through Math.max/Math.min
      - Use of -0.0f
      - Division and modulo
      - Reassociation-sensitive floating-point operations


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Save the following code as Test.java.
      2. Run the program with different VM modes:
         - java -Xint Test.java
         - java -Xmixed Test.java
         - java -Xcomp Test.java

      ---------- BEGIN SOURCE ----------

      public class Test {

          public static long Func(int param) {
              float varFloat1 = param + 0.5f;
              float varFloat2 = -0.0f;
              if (
                  !(
                          Float.floatToRawIntBits(
                                  100.1f / Math.max(varFloat1, 1) / Math.max(varFloat1, Float.NaN)
                              +
                                  Math.min(Float.NaN, varFloat1)
                          )
                      ==
                          Float.floatToRawIntBits(
                                  100.2f - varFloat2 % varFloat2 / Math.max(varFloat1, 1) * 0.1f
                              +
                                  Math.max(varFloat1, Float.NaN) + Math.min(4.2f, varFloat1)
                          )
                  )
              ) {
                  return 10L;
              }
              return 20L;
          }

          public static void main(String[] strArr) {
              System.out.println("FuncReturn: " + Func(10));
          }
      }

      ---------- END SOURCE ----------

      FREQUENCY :
      ALWAYS

            Assignee:
            Manuel Hässig
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: