1 /* 2 * Copyright (c) 2015, 2017, 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 * @bug 8139688 27 * @key randomness 28 * @library /test/lib 29 * @build Tests 30 * @build FdlibmTranslit 31 * @build ExpTests 32 * @run main ExpTests 33 * @summary Tests specifically for StrictMath.exp 34 */ 35 36 import jdk.test.lib.RandomFactory; 37 38 /** 39 * The role of this test is to verify that the FDLIBM exp algorithm is 40 * being used by running golden file style tests on values that may 41 * vary from one conforming exponential implementation to another. 42 */ 43 44 public class ExpTests { 45 private ExpTests(){} 46 47 public static void main(String [] argv) { 48 int failures = 0; 49 50 failures += testExp(); 51 failures += testAgainstTranslit(); 52 53 if (failures > 0) { 54 System.err.println("Testing the exponential incurred " 55 + failures + " failures."); 56 throw new RuntimeException(); 57 } 58 } 59 60 // From the fdlibm source, the overflow threshold in hex is: 61 // 0x4086_2E42_FEFA_39EF. 62 static final double EXP_OVERFLOW_THRESH = Double.longBitsToDouble(0x4086_2E42_FEFA_39EFL); 63 64 // From the fdlibm source, the underflow threshold in hex is: 65 // 0xc087_4910_D52D_3051L. 66 static final double EXP_UNDERFLOW_THRESH = Double.longBitsToDouble(0xc087_4910_D52D_3051L); 67 68 static int testExp() { 69 int failures = 0; 70 71 double [][] 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 }; 82 83 for(double[] testCase: testCases) 84 failures+=testExpCase(testCase[0], testCase[1]); 85 86 return failures; 87 } 88 89 static int testExpCase(double input, double expected) { 90 int failures = 0; 91 92 failures+=Tests.test("StrictMath.exp(double)", input, 93 StrictMath.exp(input), expected); 94 return failures; 95 } 96 97 // Initialize shared random number generator 98 private static java.util.Random random = RandomFactory.getRandom(); 99 100 /** 101 * Test StrictMath.exp against transliteration port of exp. 102 */ 103 private static int testAgainstTranslit() { 104 int failures = 0; 105 106 double[] decisionPoints = { 107 // Near overflow threshold 108 EXP_OVERFLOW_THRESH - 512*Math.ulp(EXP_OVERFLOW_THRESH), 109 110 // Near underflow threshold 111 EXP_UNDERFLOW_THRESH - 512*Math.ulp(EXP_UNDERFLOW_THRESH), 112 113 // Straddle algorithm conditional checks 114 Double.longBitsToDouble(0x4086_2E42_0000_0000L - 512L), 115 Double.longBitsToDouble(0x3fd6_2e42_0000_0000L - 512L), 116 Double.longBitsToDouble(0x3FF0_A2B2_0000_0000L - 512L), 117 Double.longBitsToDouble(0x3e30_0000_0000_0000L - 512L), 118 119 // Other notable points 120 Double.MIN_NORMAL - Math.ulp(Double.MIN_NORMAL)*512, 121 -Double.MIN_VALUE*512, 122 }; 123 124 for (double decisionPoint : decisionPoints) { 125 double ulp = Math.ulp(decisionPoint); 126 failures += testRange(decisionPoint - 1024*ulp, ulp, 1_024); 127 } 128 129 // Try out some random values 130 for (int i = 0; i < 100; i++) { 131 double x = Tests.createRandomDouble(random); 132 failures += testRange(x, Math.ulp(x), 100); 133 } 134 135 return failures; 136 } 137 138 private static int testRange(double start, double increment, int count) { 139 int failures = 0; 140 double x = start; 141 for (int i = 0; i < count; i++, x += increment) { 142 failures += testExpCase(x, FdlibmTranslit.Exp.compute(x)); 143 } 144 return failures; 145 } 146 }