Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/math/MaxMinOptimizeTest.java
41153 views
/*1* Copyright (c) 2021, Huawei Technologies Co. Ltd. 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* @bug 826300626* @summary Test the result of 8263006's optimization27*28* @run main/othervm -Xcomp -XX:-TieredCompilation29* compiler.intrinsics.math.MaxMinOptimizeTest30*/3132package compiler.intrinsics.math;3334import java.util.Arrays;3536public class MaxMinOptimizeTest {3738private static final float fPos = 15280.0f;39private static final float fNeg = -55555.5f;40private static final float fPosZero = 0.0f;41private static final float fNegZero = -0.0f;42private static final float fPosInf = Float.POSITIVE_INFINITY;43private static final float fNegInf = Float.NEGATIVE_INFINITY;44private static final float fNaN = Float.NaN;4546private static final float fPosPosAdd = Float.intBitsToFloat(1190051840);47private static final float fNegNegAdd = Float.intBitsToFloat(-942079104);48private static final float fPosNegAdd = Float.intBitsToFloat(-954379392);49private static final float fPosPosMul = Float.intBitsToFloat(1298049424);50private static final float fNegNegMul = Float.intBitsToFloat(1329067759);51private static final float fPosNegMul = Float.intBitsToFloat(-833985532);5253private static final double dPos = 482390926662501720.0;54private static final double dNeg = -333333333333333333.3;55private static final double dPosZero = 0.0;56private static final double dNegZero = -0.0;57private static final double dPosInf = Double.POSITIVE_INFINITY;58private static final double dNegInf = Double.NEGATIVE_INFINITY;59private static final double dNaN = Double.NaN;6061private static final double dPosPosAdd = Double.longBitsToDouble(4875928555416607765L);62private static final double dNegNegAdd = Double.longBitsToDouble(-4349772506333936299L);63private static final double dPosNegAdd = Double.longBitsToDouble(4864042047724301696L);64private static final double dPosPosMul = Double.longBitsToDouble(5135907348984537565L);65private static final double dNegNegMul = Double.longBitsToDouble(5131119721350321694L);66private static final double dPosNegMul = Double.longBitsToDouble(-4089558839395905027L);6768private static final float[][] f_cases = {69// a b min max add mul70{ fPos, fPos, fPos, fPos, fPosPosAdd, fPosPosMul},71{ fNeg, fNeg, fNeg, fNeg, fNegNegAdd, fNegNegMul},72{ fPos, fNeg, fNeg, fPos, fPosNegAdd, fPosNegMul},73{ fNeg, fPos, fNeg, fPos, fPosNegAdd, fPosNegMul},7475{ fPosZero, fNegZero, fNegZero, fPosZero, fPosZero, fNegZero},76{ fNegZero, fPosZero, fNegZero, fPosZero, fPosZero, fNegZero},77{ fNegZero, fNegZero, fNegZero, fNegZero, fNegZero, fPosZero},7879{ fPos, fPosInf, fPos, fPosInf, fPosInf, fPosInf},80{ fNeg, fNegInf, fNegInf, fNeg, fNegInf, fPosInf},8182{ fPos, fNaN, fNaN, fNaN, fNaN, fNaN},83{ fNaN, fPos, fNaN, fNaN, fNaN, fNaN},84{ fNeg, fNaN, fNaN, fNaN, fNaN, fNaN},85{ fNaN, fNeg, fNaN, fNaN, fNaN, fNaN},8687{ fPosInf, fNaN, fNaN, fNaN, fNaN, fNaN},88{ fNaN, fPosInf, fNaN, fNaN, fNaN, fNaN},89{ fNegInf, fNaN, fNaN, fNaN, fNaN, fNaN},90{ fNaN, fNegInf, fNaN, fNaN, fNaN, fNaN}91};9293private static final double[][] d_cases = {94// a b min max add mul95{ dPos, dPos, dPos, dPos, dPosPosAdd, dPosPosMul},96{ dNeg, dNeg, dNeg, dNeg, dNegNegAdd, dNegNegMul},97{ dPos, dNeg, dNeg, dPos, dPosNegAdd, dPosNegMul},98{ dNeg, dPos, dNeg, dPos, dPosNegAdd, dPosNegMul},99100{ dPosZero, dNegZero, dNegZero, dPosZero, dPosZero, dNegZero},101{ dNegZero, dPosZero, dNegZero, dPosZero, dPosZero, dNegZero},102{ dNegZero, dNegZero, dNegZero, dNegZero, dNegZero, dPosZero},103104{ dPos, dPosInf, dPos, dPosInf, dPosInf, dPosInf},105{ dNeg, dNegInf, dNegInf, dNeg, dNegInf, dPosInf},106107{ dPos, dNaN, dNaN, dNaN, dNaN, dNaN},108{ dNaN, dPos, dNaN, dNaN, dNaN, dNaN},109{ dNeg, dNaN, dNaN, dNaN, dNaN, dNaN},110{ dNaN, dNeg, dNaN, dNaN, dNaN, dNaN},111112{ dPosInf, dNaN, dNaN, dNaN, dNaN, dNaN},113{ dNaN, dPosInf, dNaN, dNaN, dNaN, dNaN},114{ dNegInf, dNaN, dNaN, dNaN, dNaN, dNaN},115{ dNaN, dNegInf, dNaN, dNaN, dNaN, dNaN}116};117118static float add_opt_float(float a, float b) {119return Math.max(a, b) + Math.min(a, b);120}121122static float mul_opt_float(float a, float b) {123return Math.max(a, b) * Math.min(a, b);124}125126static float max_opt_float(float a, float b) {127return Math.max(Math.max(a, b), Math.min(a, b));128}129130static float min_opt_float(float a, float b) {131return Math.min(Math.max(a, b), Math.min(a, b));132}133134static double add_opt_double(double a, double b) {135return Math.max(a, b) + Math.min(a, b);136}137138static double mul_opt_double(double a, double b) {139return Math.max(a, b) * Math.min(a, b);140}141142static double max_opt_double(double a, double b) {143return Math.max(Math.max(a, b), Math.min(a, b));144}145146static double min_opt_double(double a, double b) {147return Math.min(Math.max(a, b), Math.min(a, b));148}149150private static void fTest(float[] row) {151fCheck(row[0], row[1],152min_opt_float(row[0], row[1]),153max_opt_float(row[0], row[1]),154add_opt_float(row[0], row[1]),155mul_opt_float(row[0], row[1]),156row[2], row[3], row[4], row[5]);157}158159private static void fCheck(float a, float b, float fmin, float fmax, float fadd, float fmul, float efmin, float efmax, float efadd, float efmul) {160int min = Float.floatToRawIntBits(fmin);161int max = Float.floatToRawIntBits(fmax);162int add = Float.floatToRawIntBits(fadd);163int mul = Float.floatToRawIntBits(fmul);164int emin = Float.floatToRawIntBits(efmin);165int emax = Float.floatToRawIntBits(efmax);166int eadd = Float.floatToRawIntBits(efadd);167int emul = Float.floatToRawIntBits(efmul);168169if (min != emin || max != emax || add != eadd || mul != emul) {170throw new AssertionError("Unexpected result of float test: " +171"a = " + a + ", b = " + b + ", " +172"result = (" + fmin + ", " + fmax + ", " + fadd + ", " + fmul + "), " +173"expected = (" + efmin + ", " + efmax + ", " + efadd + ", " + efmul + ")");174}175}176177private static void dTest(double[] row) {178dCheck(row[0], row[1],179min_opt_double(row[0], row[1]),180max_opt_double(row[0], row[1]),181add_opt_double(row[0], row[1]),182mul_opt_double(row[0], row[1]),183row[2], row[3], row[4], row[5]);184}185186private static void dCheck(double a, double b, double dmin, double dmax, double dadd, double dmul, double edmin, double edmax, double edadd, double edmul) {187long min = Double.doubleToRawLongBits(dmin);188long max = Double.doubleToRawLongBits(dmax);189long add = Double.doubleToRawLongBits(dadd);190long mul = Double.doubleToRawLongBits(dmul);191long emin = Double.doubleToRawLongBits(edmin);192long emax = Double.doubleToRawLongBits(edmax);193long eadd = Double.doubleToRawLongBits(edadd);194long emul = Double.doubleToRawLongBits(edmul);195196if (min != emin || max != emax || add != eadd || mul != emul) {197throw new AssertionError("Unexpected result of double test: " +198"a = " + a + ", b = " + b + ", " +199"result = (" + dmin + ", " + dmax + ", " + dadd + ", " + dmul + "), " +200"expected = (" + edmin + ", " + edmax + ", " + edadd + ", " + edmul + ")");201}202}203204public static void main(String[] args) {205Arrays.stream(f_cases).forEach(MaxMinOptimizeTest::fTest);206Arrays.stream(d_cases).forEach(MaxMinOptimizeTest::dTest);207}208}209210211