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