1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, Arm Limited. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * @test
  27  * @bug 8212043
  28  * @summary Test compiler intrinsics of floating-point Math.min/max
  29  *
  30  * @run main/othervm -Xint compiler.intrinsics.math.TestFpMinMaxIntrinsics
  31  * @run main/othervm -XX:+UnlockDiagnosticVMOptions
  32  *                   -Xcomp -XX:TieredStopAtLevel=1
  33  *                   -XX:CompileOnly=java/lang/Math
  34  *                   compiler.intrinsics.math.TestFpMinMaxIntrinsics
  35  * @run main/othervm -XX:+UnlockDiagnosticVMOptions
  36  *                   -Xcomp -XX:-TieredCompilation
  37  *                   -XX:CompileOnly=java/lang/Math
  38  *                   compiler.intrinsics.math.TestFpMinMaxIntrinsics
  39  */
  40 
  41 package compiler.intrinsics.math;
  42 
  43 import java.util.Arrays;
  44 
  45 public class TestFpMinMaxIntrinsics {
  46 
  47     private static final float fPos     =  15280.0f;
  48     private static final float fNeg     = -55555.5f;
  49     private static final float fPosZero =      0.0f;
  50     private static final float fNegZero =     -0.0f;
  51     private static final float fPosInf  = Float.POSITIVE_INFINITY;
  52     private static final float fNegInf  = Float.NEGATIVE_INFINITY;
  53     private static final float fNaN     = Float.NaN;
  54 
  55     private static final double dPos     =  482390926662501720.0;
  56     private static final double dNeg     = -333333333333333333.3;
  57     private static final double dPosZero =                   0.0;
  58     private static final double dNegZero =                  -0.0;
  59     private static final double dPosInf  = Double.POSITIVE_INFINITY;
  60     private static final double dNegInf  = Double.NEGATIVE_INFINITY;
  61     private static final double dNaN     = Double.NaN;
  62 
  63     private static final float[][] f_cases = {
  64         //     a         b         min       max
  65         {     fPos,     fPos,     fPos,     fPos },
  66         {     fPos,     fNeg,     fNeg,     fPos },
  67         { fPosZero, fNegZero, fNegZero, fPosZero },
  68         { fNegZero, fNegZero, fNegZero, fNegZero },
  69         {     fPos,  fPosInf,     fPos,  fPosInf },
  70         {     fNeg,  fNegInf,  fNegInf,     fNeg },
  71         {     fPos,     fNaN,     fNaN,     fNaN },
  72         {  fNegInf,     fNaN,     fNaN,     fNaN },
  73     };
  74 
  75     private static final double[][] d_cases = {
  76         //     a         b         min       max
  77         {     dPos,     dPos,     dPos,     dPos },
  78         {     dPos,     dNeg,     dNeg,     dPos },
  79         { dPosZero, dNegZero, dNegZero, dPosZero },
  80         { dNegZero, dNegZero, dNegZero, dNegZero },
  81         {     dPos,  dPosInf,     dPos,  dPosInf },
  82         {     dNeg,  dNegInf,  dNegInf,     dNeg },
  83         {     dPos,     dNaN,     dNaN,     dNaN },
  84         {  dNegInf,     dNaN,     dNaN,     dNaN },
  85     };
  86 
  87     private static void fTest(float[] row) {
  88         float min = Math.min(row[0], row[1]);
  89         float max = Math.max(row[0], row[1]);
  90         if (Float.isNaN(min) && Float.isNaN(max)
  91                 && Float.isNaN(row[2]) && Float.isNaN(row[3])) {
  92             // Return if all of them are NaN
  93             return;
  94         }
  95         if (min != row[2] || max != row[3]) {
  96             throw new AssertionError("Unexpected result of float min/max: " +
  97                     "a = " + row[0] + ", b = " + row[1] + ", " +
  98                     "result = (" + min + ", " + max + "), " +
  99                     "expected = (" + row[2] + ", " + row[3] + ")");
 100         }
 101     }
 102 
 103     private static void dTest(double[] row) {
 104         double min = Math.min(row[0], row[1]);
 105         double max = Math.max(row[0], row[1]);
 106         if (Double.isNaN(min) && Double.isNaN(max)
 107                 && Double.isNaN(row[2]) && Double.isNaN(row[3])) {
 108             // Return if all of them are NaN
 109             return;
 110         }
 111         if (min != row[2] || max != row[3]) {
 112             throw new AssertionError("Unexpected result of double min/max" +
 113                     "a = " + row[0] + ", b = " + row[1] + ", " +
 114                     "result = (" + min + ", " + max + "), " +
 115                     "expected = (" + row[2] + ", " + row[3] + ")");
 116         }
 117     }
 118 
 119     public static void main(String[] args) {
 120         Arrays.stream(f_cases).forEach(TestFpMinMaxIntrinsics::fTest);
 121         Arrays.stream(d_cases).forEach(TestFpMinMaxIntrinsics::dTest);
 122         System.out.println("PASS");
 123     }
 124 }
 125