Path: blob/master/test/jdk/java/lang/Math/FusedMultiplyAddTests.java
41149 views
/*1* Copyright (c) 2016, 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* @bug 4851642 825340926* @summary Tests for Math.fusedMac and StrictMath.fusedMac.27* @build Tests28* @build FusedMultiplyAddTests29* @run main FusedMultiplyAddTests30* @run main/othervm -XX:-UseFMA FusedMultiplyAddTests31*/3233/**34* The specifications for both Math.fusedMac and StrictMath.fusedMac35* are the same and both are exactly specified. Therefore, both36* methods are tested in this file.37*/3839public class FusedMultiplyAddTests {40private FusedMultiplyAddTests(){}4142private static final double Infinity = Double.POSITIVE_INFINITY;43private static final float InfinityF = Float.POSITIVE_INFINITY;44private static final double NaN = Double.NaN;45private static final float NaNf = Float.NaN;4647public static void main(String... args) {48int failures = 0;4950failures += testNonFiniteD();51failures += testZeroesD();52failures += testSimpleD();5354failures += testNonFiniteF();55failures += testZeroesF();56failures += testSimpleF();5758if (failures > 0) {59System.err.println("Testing fma incurred "60+ failures + " failures.");61throw new RuntimeException();62}63}6465private static int testNonFiniteD() {66int failures = 0;6768double [][] testCases = {69{Infinity, Infinity, Infinity,70Infinity,71},7273{-Infinity, Infinity, -Infinity,74-Infinity,75},7677{-Infinity, Infinity, Infinity,78NaN,79},8081{Infinity, Infinity, -Infinity,82NaN,83},8485{1.0, Infinity, 2.0,86Infinity,87},8889{1.0, 2.0, Infinity,90Infinity,91},9293{Infinity, 1.0, Infinity,94Infinity,95},9697{Double.MAX_VALUE, 2.0, -Infinity,98-Infinity},99100{Infinity, 1.0, -Infinity,101NaN,102},103104{-Infinity, 1.0, Infinity,105NaN,106},107108{1.0, NaN, 2.0,109NaN,110},111112{1.0, 2.0, NaN,113NaN,114},115116{Infinity, 2.0, NaN,117NaN,118},119120{NaN, 2.0, Infinity,121NaN,122},123};124125for (double[] testCase: testCases)126failures += testFusedMacCase(testCase[0], testCase[1], testCase[2], testCase[3]);127128return failures;129}130131private static int testZeroesD() {132int failures = 0;133134double [][] testCases = {135{+0.0, +0.0, +0.0,136+0.0,137},138139{-0.0, +0.0, +0.0,140+0.0,141},142143{+0.0, +0.0, -0.0,144+0.0,145},146147{+0.0, +0.0, -0.0,148+0.0,149},150151{-0.0, +0.0, -0.0,152-0.0,153},154155{-0.0, -0.0, -0.0,156+0.0,157},158159{-1.0, +0.0, -0.0,160-0.0,161},162163{-1.0, +0.0, +0.0,164+0.0,165},166167{-2.0, +0.0, -0.0,168-0.0,169},170171{-2.0, +0.0, +0.0,172+0.0,173},174};175176for (double[] testCase: testCases)177failures += testFusedMacCase(testCase[0], testCase[1], testCase[2], testCase[3]);178179return failures;180}181182private static int testSimpleD() {183int failures = 0;184185double [][] testCases = {186{1.0, 2.0, 3.0,1875.0,},188189{1.0, 2.0, -2.0,1900.0,},191192{5.0, 5.0, -25.0,1930.0,},194195{Double.MAX_VALUE, 2.0, -Double.MAX_VALUE,196Double.MAX_VALUE},197198{Double.MAX_VALUE, 2.0, 1.0,199Infinity},200201{Double.MIN_VALUE, -Double.MIN_VALUE, +0.0,202-0.0},203204{Double.MIN_VALUE, -Double.MIN_VALUE, -0.0,205-0.0},206207{Double.MIN_VALUE, Double.MIN_VALUE, +0.0,208+0.0},209210{Double.MIN_VALUE, Double.MIN_VALUE, -0.0,211+0.0},212213{Double.MIN_VALUE, +0.0, -0.0,214+0.0},215216{Double.MIN_VALUE, -0.0, -0.0,217-0.0},218219{Double.MIN_VALUE, +0.0, +0.0,220+0.0},221222{Double.MIN_VALUE, -0.0, +0.0,223+0.0},224225{1.0+Math.ulp(1.0), 1.0+Math.ulp(1.0), -1.0-2.0*Math.ulp(1.0),226Math.ulp(1.0)*Math.ulp(1.0)},227};228229for (double[] testCase: testCases)230failures += testFusedMacCase(testCase[0], testCase[1], testCase[2], testCase[3]);231232return failures;233}234235private static int testNonFiniteF() {236int failures = 0;237238float [][] testCases = {239{1.0f, InfinityF, 2.0f,240InfinityF,241},242243{1.0f, 2.0f, InfinityF,244InfinityF,245},246247{InfinityF, 1.0f, InfinityF,248InfinityF,249},250251{Float.MAX_VALUE, 2.0f, -InfinityF,252-InfinityF},253254{InfinityF, 1.0f, -InfinityF,255NaNf,256},257258{-InfinityF, 1.0f, InfinityF,259NaNf,260},261262{1.0f, NaNf, 2.0f,263NaNf,264},265266{1.0f, 2.0f, NaNf,267NaNf,268},269270{InfinityF, 2.0f, NaNf,271NaNf,272},273274{NaNf, 2.0f, InfinityF,275NaNf,276},277};278279for (float[] testCase: testCases)280failures += testFusedMacCase(testCase[0], testCase[1], testCase[2], testCase[3]);281282return failures;283}284285private static int testZeroesF() {286int failures = 0;287288float [][] testCases = {289{+0.0f, +0.0f, +0.0f,290+0.0f,291},292293{-0.0f, +0.0f, +0.0f,294+0.0f,295},296297{+0.0f, +0.0f, -0.0f,298+0.0f,299},300301{+0.0f, +0.0f, -0.0f,302+0.0f,303},304305{-0.0f, +0.0f, -0.0f,306-0.0f,307},308309{-0.0f, -0.0f, -0.0f,310+0.0f,311},312313{-1.0f, +0.0f, -0.0f,314-0.0f,315},316317{-1.0f, +0.0f, +0.0f,318+0.0f,319},320321{-2.0f, +0.0f, -0.0f,322-0.0f,323},324};325326for (float[] testCase: testCases)327failures += testFusedMacCase(testCase[0], testCase[1], testCase[2], testCase[3]);328329return failures;330}331332private static int testSimpleF() {333int failures = 0;334335float [][] testCases = {336{1.0f, 2.0f, 3.0f,3375.0f,},338339{1.0f, 2.0f, -2.0f,3400.0f,},341342{5.0f, 5.0f, -25.0f,3430.0f,},344345{Float.MAX_VALUE, 2.0f, -Float.MAX_VALUE,346Float.MAX_VALUE},347348{Float.MAX_VALUE, 2.0f, 1.0f,349InfinityF},350351{1.0f+Math.ulp(1.0f), 1.0f+Math.ulp(1.0f), -1.0f-2.0f*Math.ulp(1.0f),352Math.ulp(1.0f)*Math.ulp(1.0f)},353354// Double-rounding if done in double precision355{0x1.fffffep23f, 0x1.000004p28f, 0x1.fep5f, 0x1.000002p52f}356};357358for (float[] testCase: testCases)359failures += testFusedMacCase(testCase[0], testCase[1], testCase[2], testCase[3]);360361return failures;362}363364365private static int testFusedMacCase(double input1, double input2, double input3, double expected) {366int failures = 0;367failures += Tests.test("Math.fma(double)", input1, input2, input3,368Math.fma(input1, input2, input3), expected);369failures += Tests.test("StrictMath.fma(double)", input1, input2, input3,370StrictMath.fma(input1, input2, input3), expected);371372// Permute first two inputs373failures += Tests.test("Math.fma(double)", input2, input1, input3,374Math.fma(input2, input1, input3), expected);375failures += Tests.test("StrictMath.fma(double)", input2, input1, input3,376StrictMath.fma(input2, input1, input3), expected);377return failures;378}379380private static int testFusedMacCase(float input1, float input2, float input3, float expected) {381int failures = 0;382failures += Tests.test("Math.fma(float)", input1, input2, input3,383Math.fma(input1, input2, input3), expected);384failures += Tests.test("StrictMath.fma(float)", input1, input2, input3,385StrictMath.fma(input1, input2, input3), expected);386387// Permute first two inputs388failures += Tests.test("Math.fma(float)", input2, input1, input3,389Math.fma(input2, input1, input3), expected);390failures += Tests.test("StrictMath.fma(float)", input2, input1, input3,391StrictMath.fma(input2, input1, input3), expected);392return failures;393}394}395396397