1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2020, BELLSOFT. 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  * @summary Test compiler intrinsics for signum
  28  * @requires os.arch=="aarch64"
  29  * @library /test/lib
  30  *
  31  * @run main/othervm
  32  *      -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
  33  *      -XX:+UseSignumIntrinsic
  34  *      compiler.intrinsics.math.TestSignumIntrinsic
  35  * @run main/othervm
  36  *      -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
  37  *      -XX:-UseSignumIntrinsic -XX:+UseCopySignIntrinsic
  38  *      compiler.intrinsics.math.TestSignumIntrinsic
  39  */
  40 
  41 package compiler.intrinsics.math;
  42 
  43 import jdk.test.lib.Asserts;
  44 
  45 public class TestSignumIntrinsic {
  46 
  47     private static final float[][] float_cases = {
  48         {123.4f,                       1.0f},
  49         {-56.7f,                      -1.0f},
  50         {7e30f,                        1.0f},
  51         {-0.3e30f,                    -1.0f},
  52         {Float.MAX_VALUE,              1.0f},
  53         {-Float.MAX_VALUE,            -1.0f},
  54         {Float.MIN_VALUE,              1.0f},
  55         {-Float.MIN_VALUE,            -1.0f},
  56         {0.0f,                         0.0f},
  57         {-0.0f,                       -0.0f},
  58         {Float.POSITIVE_INFINITY,      1.0f},
  59         {Float.NEGATIVE_INFINITY,     -1.0f},
  60         {Float.NaN,               Float.NaN},
  61         {Float.MIN_NORMAL,             1.0f},
  62         {-Float.MIN_NORMAL,           -1.0f},
  63         {0x0.0002P-126f,               1.0f},
  64         {-0x0.0002P-126f,             -1.0f}
  65     };
  66 
  67     private static final double[][] double_cases = {
  68         {123.4d,                         1.0d},
  69         {-56.7d,                        -1.0d},
  70         {7e30d,                          1.0d},
  71         {-0.3e30d,                      -1.0d},
  72         {Double.MAX_VALUE,               1.0d},
  73         {-Double.MAX_VALUE,             -1.0d},
  74         {Double.MIN_VALUE,               1.0d},
  75         {-Double.MIN_VALUE,             -1.0d},
  76         {0.0d,                           0.0d},
  77         {-0.0d,                         -0.0d},
  78         {Double.POSITIVE_INFINITY,       1.0d},
  79         {Double.NEGATIVE_INFINITY,      -1.0d},
  80         {Double.NaN,               Double.NaN},
  81         {Double.MIN_NORMAL,              1.0d},
  82         {-Double.MIN_NORMAL,            -1.0d},
  83         {0x0.00000001P-1022,             1.0d},
  84         {-0x0.00000001P-1022,           -1.0d}
  85     };
  86 
  87     public static void main(String[] args) throws Exception {
  88         float fAccum = 0.0f;
  89         double dAccum = 0.0d;
  90         for (int i = 0; i < 100_000; i++) {
  91             fAccum += floatTest();
  92             dAccum += doubleTest();
  93         }
  94         System.out.println("SUCCESS. Accum values: " + fAccum + " and " + dAccum);
  95     }
  96 
  97     private static float floatTest() {
  98         float accum = 0.0f;
  99         for (float[] fcase : float_cases) {
 100             float arg = fcase[0];
 101             float expected = fcase[1];
 102             float calculated = Math.signum(arg);
 103             Asserts.assertEQ(expected, calculated, "Unexpected float result");
 104             accum += calculated;
 105         }
 106         return accum;
 107     }
 108 
 109     private static double doubleTest() {
 110         double accum = 0.0d;
 111         for (double[] dcase : double_cases) {
 112             double arg = dcase[0];
 113             double expected = dcase[1];
 114             double calculated = Math.signum(arg);
 115             Asserts.assertEQ(expected, calculated, "Unexpected double result");
 116             accum += calculated;
 117         }
 118         return accum;
 119     }
 120 }