Path: blob/master/test/hotspot/jtreg/vmTestbase/jit/FloatingPoint/FPCompare/TestFPBinop/TestFPBinop.java
40951 views
/*1* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25*26* @summary converted from VM Testbase jit/FloatingPoint/FPCompare/TestFPBinop.27* VM Testbase keywords: [jit, quick]28*29* @library /vmTestbase30* /test/lib31* @run main/othervm jit.FloatingPoint.FPCompare.TestFPBinop.TestFPBinop32*/3334package jit.FloatingPoint.FPCompare.TestFPBinop;3536import nsk.share.TestFailure;37import nsk.share.GoldChecker;3839/** Test of Floating Point Binary Ops.40** This is intended to be run on a known-correct system and the41** answer compared with the golden answer with diff or equivalent.42*/43public class TestFPBinop {44public static final GoldChecker goldChecker = new GoldChecker( "TestFPBinop" );4546static float floatValues [] = {47Float.MIN_VALUE, Float.MAX_VALUE,48-Float.MIN_VALUE, -Float.MAX_VALUE,49-1.0f, 1.0f, -0.0f, 0.0f,50Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY,51Float.NaN52};53static double doubleValues [] = {54Double.MIN_VALUE, Double.MAX_VALUE,55-Double.MIN_VALUE, -Double.MAX_VALUE,56-1.0, 1.0, -0.0, 0.0,57Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,58Double.NaN59};6061static int nValues = floatValues.length;6263static float fOne, fZero;64static double dOne, dZero;6566/* This is intended to thrwart an optimizing compiler67* from simplifying some of the expressions by using algebraic68* identities. */69static {70fOne = Integer.valueOf(1).floatValue();71fZero = Integer.valueOf(0).floatValue();72dOne = Integer.valueOf(1).doubleValue();73dZero = Integer.valueOf(0).doubleValue();74}7576static final boolean DEBUG = false;7778static String operandType = "";7980// static values81static float xs, ys;82static double xS, yS;8384/** Test of Floating Point Binary operators.85** The following orthogonal variables need to be tested.86** <ul>87** <li> Data type: float or double88** <li> Operator: +, -, *, /89** <li> Data values: +-normal, +-zero, NaN, +-infinity, +- min, +-max90** <li> Operand: variable, parameter, static, field, array element,91** function reference, expression, explicit constant.92** </ul>93*/94public static void main (String [] args) {95testFloats();96testDoubles();97TestFPBinop.goldChecker.check();98}99100static void testFloats() {101for (int i = 0; i < floatValues.length; i++) {102float iVal = floatValues[i];103for (int j = 0; j < floatValues.length; j++) {104float jVal = floatValues[j];105testFloat(iVal, jVal);106}107}108}109static void testDoubles() {110for (int i = 0; i < doubleValues.length; i++) {111double iVal = doubleValues[i];112for (int j = 0; j < doubleValues.length; j++) {113double jVal = doubleValues[j];114testDouble(iVal, jVal);115}116}117}118119static void testFloat (float x, float y) {120121testFloatP(x, y);122testFloatL(x, y);123testFloatS(x, y);124testFloatF(x, y);125testFloatA(x, y);126testFloatM(x, y);127testFloat1(x, y);128testFloat2(x, y);129testFloat3(x, y);130}131132static void testFloatP(float x, float y) {133134check(x, y, x + y, "param", "+");135check(x, y, x - y, "param", "-");136check(x, y, x * y, "param", "*");137check(x, y, x / y, "param", "/");138139}140141static void testFloatL(float x, float y) {142143float xl = x;144float yl = y;145146check(xl, yl, xl + yl, "local", "+");147check(xl, yl, xl - yl, "local", "-");148check(xl, yl, xl * yl, "local", "*");149check(xl, yl, xl / yl, "local", "/");150151}152153static void testFloatS(float x, float y) {154155xs = x;156ys = y;157158check(xs, ys, xs + ys, "static", "+");159check(xs, ys, xs - ys, "static", "-");160check(xs, ys, xs * ys, "static", "*");161check(xs, ys, xs / ys, "static", "/");162163}164165static void testFloatF(float x, float y) {166167FloatObject xo = new FloatObject(x);168FloatObject yo = new FloatObject(y);169170check(xo.f, yo.f, xo.f + yo.f, "field", "+");171check(xo.f, yo.f, xo.f - yo.f, "field", "-");172check(xo.f, yo.f, xo.f * yo.f, "field", "*");173check(xo.f, yo.f, xo.f / yo.f, "field", "/");174175}176177static void testFloatA(float x, float y) {178179int i = index(x);180int j = index(y);181float a [] = floatValues;182183check(a[i], a[j], a[i] + a[j], "a[i]", "+");184check(a[i], a[j], a[i] - a[j], "a[i]", "-");185check(a[i], a[j], a[i] * a[j], "a[i]", "*");186check(a[i], a[j], a[i] / a[j], "a[i]", "/");187188}189190static void testFloatM(float x, float y) {191192check(i(x), i(y), i(x) + i(y), "f(x)", "+");193check(i(x), i(y), i(x) - i(y), "f(x)", "-");194check(i(x), i(y), i(x) * i(y), "f(x)", "*");195check(i(x), i(y), i(x) / i(y), "f(x)", "/");196197}198199static void testFloat1(float x, float y) {200201float zero = fZero;202float one = fOne;203204check(((x + zero) * one), y, ((x + zero) * one) + y, "lExpr", "+");205check(((x + zero) * one), y, ((x + zero) * one) - y, "lExpr", "-");206check(((x + zero) * one), y, ((x + zero) * one) * y, "lExpr", "*");207check(((x + zero) * one), y, ((x + zero) * one) / y, "lExpr", "/");208209}210211static void testFloat3(float x, float y) {212213float zero = fZero;214float one = fOne;215216check(((x + zero) * one), (zero + one * y), ((x + zero) * one) + (zero + one * y), "exprs", "+");217check(((x + zero) * one), (zero + one * y), ((x + zero) * one) - (zero + one * y), "exprs", "-");218check(((x + zero) * one), (zero + one * y), ((x + zero) * one) * (zero + one * y), "exprs", "*");219check(((x + zero) * one), (zero + one * y), ((x + zero) * one) / (zero + one * y), "exprs", "/");220221}222223static void testFloat2(float x, float y) {224225float zero = fZero;226float one = fOne;227228operandType = "rExpr";229230check(x, (zero + one * y), x + (zero + one * y), "rExpr", "+");231check(x, (zero + one * y), x - (zero + one * y), "rExpr", "-");232check(x, (zero + one * y), x * (zero + one * y), "rExpr", "*");233check(x, (zero + one * y), x / (zero + one * y), "rExpr", "/");234235}236237static void testDouble (double x, double y) {238239testDoubleP(x, y);240testDoubleL(x, y);241testDoubleS(x, y);242testDoubleF(x, y);243testDoubleA(x, y);244testDoubleM(x, y);245testDouble1(x, y);246testDouble2(x, y);247testDouble3(x, y);248}249250static void testDoubleP (double x, double y) {251252check(x, y, x + y, "param", "+");253check(x, y, x - y, "param", "-");254check(x, y, x * y, "param", "*");255check(x, y, x / y, "param", "/");256257}258259static void testDoubleL (double x, double y) {260261double xl = x;262double yl = y;263264check(xl, yl, xl + yl, "local", "+");265check(xl, yl, xl - yl, "local", "-");266check(xl, yl, xl * yl, "local", "*");267check(xl, yl, xl / yl, "local", "/");268269}270271static void testDoubleS (double x, double y) {272273xS = x;274yS = y;275276check(xS, yS, xS + yS, "static", "+");277check(xS, yS, xS - yS, "static", "-");278check(xS, yS, xS * yS, "static", "*");279check(xS, yS, xS / yS, "static", "/");280281}282283static void testDoubleF (double x, double y) {284285DoubleObject xo = new DoubleObject(x);286DoubleObject yo = new DoubleObject(y);287288check(xo.f, yo.f, xo.f + yo.f, "field", "+");289check(xo.f, yo.f, xo.f - yo.f, "field", "-");290check(xo.f, yo.f, xo.f * yo.f, "field", "*");291check(xo.f, yo.f, xo.f / yo.f, "field", "/");292293}294295static void testDoubleA (double x, double y) {296297int i = index(x);298int j = index(y);299double a [] = doubleValues;300301check(a[i], a[j], a[i] + a[j], "a[i]", "+");302check(a[i], a[j], a[i] - a[j], "a[i]", "-");303check(a[i], a[j], a[i] * a[j], "a[i]", "*");304check(a[i], a[j], a[i] / a[j], "a[i]", "/");305306}307308static void testDoubleM (double x, double y) {309310check(i(x), i(y), i(x) + i(y), "f(x)", "+");311check(i(x), i(y), i(x) - i(y), "f(x)", "-");312check(i(x), i(y), i(x) * i(y), "f(x)", "*");313check(i(x), i(y), i(x) / i(y), "f(x)", "/");314315}316317static void testDouble1 (double x, double y) {318319double zero = dZero;320double one = dOne;321322check(((x + zero) * one), y, ((x + zero) * one) + y, "lExpr", "+");323check(((x + zero) * one), y, ((x + zero) * one) - y, "lExpr", "-");324check(((x + zero) * one), y, ((x + zero) * one) * y, "lExpr", "*");325check(((x + zero) * one), y, ((x + zero) * one) / y, "lExpr", "/");326327}328329static void testDouble3 (double x, double y) {330331double zero = dZero;332double one = dOne;333334check(((x + zero) * one), (zero + one * y), ((x + zero) * one) + (zero + one * y), "exprs", "+");335check(((x + zero) * one), (zero + one * y), ((x + zero) * one) - (zero + one * y), "exprs", "-");336check(((x + zero) * one), (zero + one * y), ((x + zero) * one) * (zero + one * y), "exprs", "*");337check(((x + zero) * one), (zero + one * y), ((x + zero) * one) / (zero + one * y), "exprs", "/");338339}340341static void testDouble2 (double x, double y) {342343double zero = dZero;344double one = dOne;345346check(x, (zero + one * y), x + (zero + one * y), "rExpr", "+");347check(x, (zero + one * y), x - (zero + one * y), "rExpr", "-");348check(x, (zero + one * y), x * (zero + one * y), "rExpr", "*");349check(x, (zero + one * y), x / (zero + one * y), "rExpr", "/");350351}352353354/* The convoluted coding is intended to prevent inlining */355static float i(float x) {356while (Float.isNaN(x) && Float.floatToIntBits(x) == 0) {357x = 0.0f;358}359return x;360}361static double i(double x) {362while (Double.isNaN(x) && Double.doubleToLongBits(x) == 0L) {363x = 0.0;364}365return x;366}367368static int index(float x) {369for (int i = 0; i < floatValues.length; i++) {370if (Float.valueOf(x).equals(Float.valueOf(floatValues[i])))371return i;372}373throw new TestFailure("ERROR: can't find " + x + " in floatValues.");374}375376static int index(double x) {377for (int i = 0; i < doubleValues.length; i++) {378if (Double.valueOf(x).equals(Double.valueOf(doubleValues[i])))379return i;380}381throw new TestFailure("ERROR: can't find " + x + " in doubleValues.");382}383384static void check (float x, float y, float result,385String operands, String operator) {386TestFPBinop.goldChecker.println(x + " " + operator + " " + y +387" = " + result + ", with float " +388operands + " operands");389}390391static void check (double x, double y, double result,392String operands, String operator) {393TestFPBinop.goldChecker.println(x + " " + operator + " " + y +394" = " + result + ", with double " +395operands + " operands");396}397398}399400class FloatObject {401public float f;402403public FloatObject(float x) {404f = x;405}406}407408class DoubleObject {409public double f;410411public DoubleObject(double x) {412f = x;413}414}415416417