1 /*
   2  * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  *
  27  * @summary converted from VM Testbase jit/FloatingPoint/FPCompare/TestFPBinop.
  28  * VM Testbase keywords: [jit, quick]
  29  *
  30  * @library /vmTestbase
  31  *          /test/lib
  32  * @run driver jdk.test.lib.FileInstaller . .
  33  * @run main/othervm jit.FloatingPoint.FPCompare.TestFPBinop.TestFPBinop
  34  */
  35 
  36 package jit.FloatingPoint.FPCompare.TestFPBinop;
  37 
  38 import nsk.share.TestFailure;
  39 import nsk.share.GoldChecker;
  40 
  41 /** Test of Floating Point Binary Ops.
  42  ** This is intended to be run on a known-correct system and the
  43  ** answer compared with the golden answer with diff or equivalent.
  44  */
  45 public class TestFPBinop {
  46     public static final GoldChecker goldChecker = new GoldChecker( "TestFPBinop" );
  47 
  48     static float floatValues [] = {
  49         Float.MIN_VALUE, Float.MAX_VALUE,
  50         -Float.MIN_VALUE, -Float.MAX_VALUE,
  51         -1.0f, 1.0f, -0.0f, 0.0f,
  52         Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY,
  53         Float.NaN
  54     };
  55     static double doubleValues [] = {
  56         Double.MIN_VALUE, Double.MAX_VALUE,
  57         -Double.MIN_VALUE, -Double.MAX_VALUE,
  58         -1.0, 1.0, -0.0, 0.0,
  59         Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
  60         Double.NaN
  61     };
  62 
  63     static int nValues = floatValues.length;
  64 
  65     static float fOne, fZero;
  66     static double dOne, dZero;
  67 
  68     /* This is intended to thrwart an optimizing compiler
  69      * from simplifying some of the expressions by using algebraic
  70      * identities. */
  71     static {
  72         fOne = new Integer(1).floatValue();
  73         fZero = new Integer(0).floatValue();
  74         dOne = new Integer(1).doubleValue();
  75         dZero = new Integer(0).doubleValue();
  76     }
  77 
  78     static final boolean DEBUG = false;
  79 
  80     static String operandType = "";
  81 
  82     // static values
  83     static float xs, ys;
  84     static double xS, yS;
  85 
  86     /** Test of Floating Point Binary operators.
  87      ** The following orthogonal variables need to be tested.
  88      ** <ul>
  89      ** <li> Data type: float or double
  90      ** <li> Operator: +, -, *, /
  91      ** <li> Data values: +-normal, +-zero, NaN, +-infinity, +- min, +-max
  92      ** <li> Operand: variable, parameter, static, field, array element,
  93      **      function reference, expression, explicit constant.
  94      ** </ul>
  95      */
  96     public static void main (String [] args) {
  97         testFloats();
  98         testDoubles();
  99         TestFPBinop.goldChecker.check();
 100     }
 101 
 102     static void testFloats() {
 103         for (int i = 0; i < floatValues.length; i++) {
 104             float iVal = floatValues[i];
 105             for (int j = 0; j < floatValues.length; j++) {
 106                 float jVal = floatValues[j];
 107                 testFloat(iVal, jVal);
 108             }
 109         }
 110     }
 111     static void testDoubles() {
 112         for (int i = 0; i < doubleValues.length; i++) {
 113             double iVal = doubleValues[i];
 114             for (int j = 0; j < doubleValues.length; j++) {
 115                 double jVal = doubleValues[j];
 116                 testDouble(iVal, jVal);
 117             }
 118         }
 119     }
 120 
 121     static void testFloat (float x, float y) {
 122 
 123         testFloatP(x, y);
 124         testFloatL(x, y);
 125         testFloatS(x, y);
 126         testFloatF(x, y);
 127         testFloatA(x, y);
 128         testFloatM(x, y);
 129         testFloat1(x, y);
 130         testFloat2(x, y);
 131         testFloat3(x, y);
 132     }
 133 
 134     static void testFloatP(float x, float y) {
 135 
 136         check(x, y, x + y, "param", "+");
 137         check(x, y, x - y, "param", "-");
 138         check(x, y, x * y, "param", "*");
 139         check(x, y, x / y, "param", "/");
 140 
 141     }
 142 
 143     static void testFloatL(float x, float y) {
 144 
 145         float xl = x;
 146         float yl = y;
 147 
 148         check(xl, yl, xl + yl, "local", "+");
 149         check(xl, yl, xl - yl, "local", "-");
 150         check(xl, yl, xl * yl, "local", "*");
 151         check(xl, yl, xl / yl, "local", "/");
 152 
 153     }
 154 
 155     static void testFloatS(float x, float y) {
 156 
 157         xs = x;
 158         ys = y;
 159 
 160         check(xs, ys, xs + ys, "static", "+");
 161         check(xs, ys, xs - ys, "static", "-");
 162         check(xs, ys, xs * ys, "static", "*");
 163         check(xs, ys, xs / ys, "static", "/");
 164 
 165     }
 166 
 167     static void testFloatF(float x, float y) {
 168 
 169         FloatObject xo = new FloatObject(x);
 170         FloatObject yo = new FloatObject(y);
 171 
 172         check(xo.f, yo.f, xo.f + yo.f, "field", "+");
 173         check(xo.f, yo.f, xo.f - yo.f, "field", "-");
 174         check(xo.f, yo.f, xo.f * yo.f, "field", "*");
 175         check(xo.f, yo.f, xo.f / yo.f, "field", "/");
 176 
 177     }
 178 
 179     static void testFloatA(float x, float y) {
 180 
 181         int i = index(x);
 182         int j = index(y);
 183         float a [] = floatValues;
 184 
 185         check(a[i], a[j], a[i] + a[j], "a[i]", "+");
 186         check(a[i], a[j], a[i] - a[j], "a[i]", "-");
 187         check(a[i], a[j], a[i] * a[j], "a[i]", "*");
 188         check(a[i], a[j], a[i] / a[j], "a[i]", "/");
 189 
 190     }
 191 
 192     static void testFloatM(float x, float y) {
 193 
 194         check(i(x), i(y), i(x) + i(y), "f(x)", "+");
 195         check(i(x), i(y), i(x) - i(y), "f(x)", "-");
 196         check(i(x), i(y), i(x) * i(y), "f(x)", "*");
 197         check(i(x), i(y), i(x) / i(y), "f(x)", "/");
 198 
 199     }
 200 
 201     static void testFloat1(float x, float y) {
 202 
 203         float zero = fZero;
 204         float one = fOne;
 205 
 206         check(((x + zero) * one), y, ((x + zero) * one) + y, "lExpr", "+");
 207         check(((x + zero) * one), y, ((x + zero) * one) - y, "lExpr", "-");
 208         check(((x + zero) * one), y, ((x + zero) * one) * y, "lExpr", "*");
 209         check(((x + zero) * one), y, ((x + zero) * one) / y, "lExpr", "/");
 210 
 211     }
 212 
 213     static void testFloat3(float x, float y) {
 214 
 215         float zero = fZero;
 216         float one = fOne;
 217 
 218         check(((x + zero) * one), (zero + one * y), ((x + zero) * one) + (zero + one * y), "exprs", "+");
 219         check(((x + zero) * one), (zero + one * y), ((x + zero) * one) - (zero + one * y), "exprs", "-");
 220         check(((x + zero) * one), (zero + one * y), ((x + zero) * one) * (zero + one * y), "exprs", "*");
 221         check(((x + zero) * one), (zero + one * y), ((x + zero) * one) / (zero + one * y), "exprs", "/");
 222 
 223     }
 224 
 225     static void testFloat2(float x, float y) {
 226 
 227         float zero = fZero;
 228         float one = fOne;
 229 
 230         operandType = "rExpr";
 231 
 232         check(x, (zero + one * y), x + (zero + one * y), "rExpr", "+");
 233         check(x, (zero + one * y), x - (zero + one * y), "rExpr", "-");
 234         check(x, (zero + one * y), x * (zero + one * y), "rExpr", "*");
 235         check(x, (zero + one * y), x / (zero + one * y), "rExpr", "/");
 236 
 237     }
 238 
 239     static void testDouble (double x, double y) {
 240 
 241         testDoubleP(x, y);
 242         testDoubleL(x, y);
 243         testDoubleS(x, y);
 244         testDoubleF(x, y);
 245         testDoubleA(x, y);
 246         testDoubleM(x, y);
 247         testDouble1(x, y);
 248         testDouble2(x, y);
 249         testDouble3(x, y);
 250     }
 251 
 252     static void testDoubleP (double x, double y) {
 253 
 254         check(x, y, x + y, "param", "+");
 255         check(x, y, x - y, "param", "-");
 256         check(x, y, x * y, "param", "*");
 257         check(x, y, x / y, "param", "/");
 258 
 259     }
 260 
 261     static void testDoubleL (double x, double y) {
 262 
 263         double xl = x;
 264         double yl = y;
 265 
 266         check(xl, yl, xl + yl, "local", "+");
 267         check(xl, yl, xl - yl, "local", "-");
 268         check(xl, yl, xl * yl, "local", "*");
 269         check(xl, yl, xl / yl, "local", "/");
 270 
 271     }
 272 
 273     static void testDoubleS (double x, double y) {
 274 
 275         xS = x;
 276         yS = y;
 277 
 278         check(xS, yS, xS + yS, "static", "+");
 279         check(xS, yS, xS - yS, "static", "-");
 280         check(xS, yS, xS * yS, "static", "*");
 281         check(xS, yS, xS / yS, "static", "/");
 282 
 283     }
 284 
 285     static void testDoubleF (double x, double y) {
 286 
 287         DoubleObject xo = new DoubleObject(x);
 288         DoubleObject yo = new DoubleObject(y);
 289 
 290         check(xo.f, yo.f, xo.f + yo.f, "field", "+");
 291         check(xo.f, yo.f, xo.f - yo.f, "field", "-");
 292         check(xo.f, yo.f, xo.f * yo.f, "field", "*");
 293         check(xo.f, yo.f, xo.f / yo.f, "field", "/");
 294 
 295     }
 296 
 297     static void testDoubleA (double x, double y) {
 298 
 299         int i = index(x);
 300         int j = index(y);
 301         double a [] = doubleValues;
 302 
 303         check(a[i], a[j], a[i] + a[j], "a[i]", "+");
 304         check(a[i], a[j], a[i] - a[j], "a[i]", "-");
 305         check(a[i], a[j], a[i] * a[j], "a[i]", "*");
 306         check(a[i], a[j], a[i] / a[j], "a[i]", "/");
 307 
 308     }
 309 
 310     static void testDoubleM (double x, double y) {
 311 
 312         check(i(x), i(y), i(x) + i(y), "f(x)", "+");
 313         check(i(x), i(y), i(x) - i(y), "f(x)", "-");
 314         check(i(x), i(y), i(x) * i(y), "f(x)", "*");
 315         check(i(x), i(y), i(x) / i(y), "f(x)", "/");
 316 
 317     }
 318 
 319     static void testDouble1 (double x, double y) {
 320 
 321         double zero = dZero;
 322         double one = dOne;
 323 
 324         check(((x + zero) * one), y, ((x + zero) * one) + y, "lExpr", "+");
 325         check(((x + zero) * one), y, ((x + zero) * one) - y, "lExpr", "-");
 326         check(((x + zero) * one), y, ((x + zero) * one) * y, "lExpr", "*");
 327         check(((x + zero) * one), y, ((x + zero) * one) / y, "lExpr", "/");
 328 
 329     }
 330 
 331     static void testDouble3 (double x, double y) {
 332 
 333         double zero = dZero;
 334         double one = dOne;
 335 
 336         check(((x + zero) * one), (zero + one * y), ((x + zero) * one) + (zero + one * y), "exprs", "+");
 337         check(((x + zero) * one), (zero + one * y), ((x + zero) * one) - (zero + one * y), "exprs", "-");
 338         check(((x + zero) * one), (zero + one * y), ((x + zero) * one) * (zero + one * y), "exprs", "*");
 339         check(((x + zero) * one), (zero + one * y), ((x + zero) * one) / (zero + one * y), "exprs", "/");
 340 
 341     }
 342 
 343     static void testDouble2 (double x, double y) {
 344 
 345         double zero = dZero;
 346         double one = dOne;
 347 
 348         check(x, (zero + one * y), x + (zero + one * y), "rExpr", "+");
 349         check(x, (zero + one * y), x - (zero + one * y), "rExpr", "-");
 350         check(x, (zero + one * y), x * (zero + one * y), "rExpr", "*");
 351         check(x, (zero + one * y), x / (zero + one * y), "rExpr", "/");
 352 
 353     }
 354 
 355 
 356     /* The convoluted coding is intended to prevent inlining */
 357     static float i(float x) {
 358         while (Float.isNaN(x) && Float.floatToIntBits(x) == 0) {
 359             x = 0.0f;
 360         }
 361         return x;
 362     }
 363     static double i(double x) {
 364         while (Double.isNaN(x) && Double.doubleToLongBits(x) == 0L) {
 365             x = 0.0;
 366         }
 367         return x;
 368     }
 369 
 370     static int index(float x) {
 371         for (int i = 0; i < floatValues.length; i++) {
 372             if (new Float(x).equals(new Float(floatValues[i])))
 373                 return i;
 374         }
 375         throw new TestFailure("ERROR: can't find " + x + " in floatValues.");
 376     }
 377 
 378     static int index(double x) {
 379         for (int i = 0; i < doubleValues.length; i++) {
 380             if (new Double(x).equals(new Double(doubleValues[i])))
 381                 return i;
 382         }
 383         throw new TestFailure("ERROR: can't find " + x + " in doubleValues.");
 384     }
 385 
 386     static void check (float x, float y, float result,
 387                        String operands, String operator) {
 388       TestFPBinop.goldChecker.println(x + " " + operator + " " + y +
 389                          " = " + result + ", with float " +
 390                          operands + " operands");
 391     }
 392 
 393     static void check (double x, double y, double result,
 394                        String operands, String operator) {
 395       TestFPBinop.goldChecker.println(x + " " + operator + " " + y +
 396                          " = " + result + ", with double " +
 397                          operands + " operands");
 398     }
 399 
 400 }
 401 
 402 class FloatObject {
 403     public float f;
 404 
 405     public FloatObject(float x) {
 406         f = x;
 407     }
 408 }
 409 
 410 class DoubleObject {
 411     public double f;
 412 
 413     public DoubleObject(double x) {
 414         f = x;
 415     }
 416 }