Path: blob/master/test/jdk/java/lang/StrictMath/ExpTests.java
41149 views
/*1* Copyright (c) 2015, 2017, 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 813968826* @key randomness27* @library /test/lib28* @build jdk.test.lib.RandomFactory29* @build Tests30* @build FdlibmTranslit31* @build ExpTests32* @run main ExpTests33* @summary Tests specifically for StrictMath.exp34*/3536import jdk.test.lib.RandomFactory;3738/**39* The role of this test is to verify that the FDLIBM exp algorithm is40* being used by running golden file style tests on values that may41* vary from one conforming exponential implementation to another.42*/4344public class ExpTests {45private ExpTests(){}4647public static void main(String [] argv) {48int failures = 0;4950failures += testExp();51failures += testAgainstTranslit();5253if (failures > 0) {54System.err.println("Testing the exponential incurred "55+ failures + " failures.");56throw new RuntimeException();57}58}5960// From the fdlibm source, the overflow threshold in hex is:61// 0x4086_2E42_FEFA_39EF.62static final double EXP_OVERFLOW_THRESH = Double.longBitsToDouble(0x4086_2E42_FEFA_39EFL);6364// From the fdlibm source, the underflow threshold in hex is:65// 0xc087_4910_D52D_3051L.66static final double EXP_UNDERFLOW_THRESH = Double.longBitsToDouble(0xc087_4910_D52D_3051L);6768static int testExp() {69int failures = 0;7071double [][] testCases = {72// Some of these could be moved to common Math/StrictMath exp testing.73{Double.NaN, Double.NaN},74{Double.MAX_VALUE, Double.POSITIVE_INFINITY},75{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},76{Double.NEGATIVE_INFINITY, +0.0},77{EXP_OVERFLOW_THRESH, 0x1.ffff_ffff_fff2ap1023},78{Math.nextUp(EXP_OVERFLOW_THRESH), Double.POSITIVE_INFINITY},79{Math.nextDown(EXP_UNDERFLOW_THRESH), +0.0},80{EXP_UNDERFLOW_THRESH, +Double.MIN_VALUE},81};8283for(double[] testCase: testCases)84failures+=testExpCase(testCase[0], testCase[1]);8586return failures;87}8889static int testExpCase(double input, double expected) {90int failures = 0;9192failures+=Tests.test("StrictMath.exp(double)", input,93StrictMath.exp(input), expected);94return failures;95}9697// Initialize shared random number generator98private static java.util.Random random = RandomFactory.getRandom();99100/**101* Test StrictMath.exp against transliteration port of exp.102*/103private static int testAgainstTranslit() {104int failures = 0;105106double[] decisionPoints = {107// Near overflow threshold108EXP_OVERFLOW_THRESH - 512*Math.ulp(EXP_OVERFLOW_THRESH),109110// Near underflow threshold111EXP_UNDERFLOW_THRESH - 512*Math.ulp(EXP_UNDERFLOW_THRESH),112113// Straddle algorithm conditional checks114Double.longBitsToDouble(0x4086_2E42_0000_0000L - 512L),115Double.longBitsToDouble(0x3fd6_2e42_0000_0000L - 512L),116Double.longBitsToDouble(0x3FF0_A2B2_0000_0000L - 512L),117Double.longBitsToDouble(0x3e30_0000_0000_0000L - 512L),118119// Other notable points120Double.MIN_NORMAL - Math.ulp(Double.MIN_NORMAL)*512,121-Double.MIN_VALUE*512,122};123124for (double decisionPoint : decisionPoints) {125double ulp = Math.ulp(decisionPoint);126failures += testRange(decisionPoint - 1024*ulp, ulp, 1_024);127}128129// Try out some random values130for (int i = 0; i < 100; i++) {131double x = Tests.createRandomDouble(random);132failures += testRange(x, Math.ulp(x), 100);133}134135return failures;136}137138private static int testRange(double start, double increment, int count) {139int failures = 0;140double x = start;141for (int i = 0; i < count; i++, x += increment) {142failures += testExpCase(x, FdlibmTranslit.Exp.compute(x));143}144return failures;145}146}147148149