1 /* 2 * Copyright (c) 2003, 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 package test.java.lang.Double; 25 26 /* 27 * @test 28 * @library /test/lib 29 * @build jdk.test.lib.RandomFactory 30 * @run main ParseHexFloatingPoint 31 * @bug 4826774 8078672 32 * @summary Numerical tests for hexadecimal inputs to parse{Double, Float} (use -Dseed=X to set PRNG seed) 33 * @author Joseph D. Darcy 34 * @key randomness 35 */ 36 37 import jdk.test.lib.RandomFactory; 38 39 public class ParseHexFloatingPoint { 40 // Android-changed: JUnit expects exactly one public constructor. 41 // private ParseHexFloatingPoint(){} 42 43 public static final double infinityD = Double.POSITIVE_INFINITY; 44 public static final double NaND = Double.NaN; 45 test(String testName, String input, double result, double expected)46 static int test(String testName, String input, 47 double result, double expected) { 48 int failures =0; 49 50 if (Double.compare(result, expected) != 0 ) { 51 System.err.println("Failure for " + testName + 52 ": For input " + input + 53 " expected " + expected + 54 " got " + result + "."); 55 } 56 57 return failures; 58 } 59 testCase(String input, double expected)60 static int testCase(String input, double expected) { 61 int failures =0; 62 63 64 // Try different combination of letter components 65 input = input.toLowerCase(java.util.Locale.US); 66 67 String [] suffices = {"", "f", "F", "d", "D"}; 68 String [] signs = {"", "-", "+"}; 69 70 for(int i = 0; i < 2; i++) { 71 String s1 = input; 72 if(i == 1) 73 s1 = s1.replace('x', 'X'); 74 75 for(int j = 0; j < 2; j++) { 76 String s2 = s1; 77 if(j == 1) 78 s2 = s2.replace('p', 'P'); 79 80 for(int k = 0; k < 2; k++) { 81 String s3 = s2; 82 if(k == 1) 83 s3 = upperCaseHex(s3); 84 85 86 for(int m = 0; m < suffices.length; m++) { 87 String s4 = s3 + suffices[m]; 88 89 90 for(int n = 0; n < signs.length; n++) { 91 String s5 = signs[n] + s4; 92 93 double result = Double.parseDouble(s5); 94 failures += test("Double.parseDouble", 95 s5, result, (signs[n].equals("-") ? 96 -expected: 97 expected)); 98 } 99 } 100 } 101 } 102 } 103 104 return failures; 105 } 106 upperCaseHex(String s)107 static String upperCaseHex(String s) { 108 return s.replace('a', 'A').replace('b', 'B').replace('c', 'C'). 109 replace('d', 'D').replace('e','E').replace('f', 'F'); 110 } 111 112 /* 113 * Test easy and tricky double rounding cases. 114 */ doubleTests()115 static int doubleTests() { 116 117 /* 118 * A String, double pair 119 */ 120 class PairSD { 121 public String s; 122 public double d; 123 PairSD(String s, double d) { 124 this.s = s; 125 this.d = d; 126 } 127 } 128 int failures = 0; 129 130 131 132 // Hex strings that convert to three; test basic functionality 133 // of significand and exponent shift adjusts along with the 134 // no-op of adding leading zeros. These cases don't exercise 135 // the rounding code. 136 String leadingZeros = "0x0000000000000000000"; 137 String [] threeTests = { 138 "0x.003p12", 139 "0x.006p11", 140 "0x.00cp10", 141 "0x.018p9", 142 143 "0x.3p4", 144 "0x.6p3", 145 "0x.cp2", 146 "0x1.8p1", 147 148 "0x3p0", 149 "0x6.0p-1", 150 "0xc.0p-2", 151 "0x18.0p-3", 152 153 "0x3000000p-24", 154 "0x3.0p0", 155 "0x3.000000p0", 156 }; 157 for(int i=0; i < threeTests.length; i++) { 158 String input = threeTests[i]; 159 failures += testCase(input, 3.0); 160 161 input.replaceFirst("^0x", leadingZeros); 162 failures += testCase(input, 3.0); 163 } 164 165 long bigExponents [] = { 166 2*Double.MAX_EXPONENT, 167 2*Double.MIN_EXPONENT, 168 169 (long)Integer.MAX_VALUE-1, 170 (long)Integer.MAX_VALUE, 171 (long)Integer.MAX_VALUE+1, 172 173 (long)Integer.MIN_VALUE-1, 174 (long)Integer.MIN_VALUE, 175 (long)Integer.MIN_VALUE+1, 176 177 Long.MAX_VALUE-1, 178 Long.MAX_VALUE, 179 180 Long.MIN_VALUE+1, 181 Long.MIN_VALUE, 182 }; 183 184 // Test zero significand with large exponents. 185 for(int i = 0; i < bigExponents.length; i++) { 186 failures += testCase("0x0.0p"+Long.toString(bigExponents[i]) , 0.0); 187 } 188 189 // Test nonzero significand with large exponents. 190 for(int i = 0; i < bigExponents.length; i++) { 191 long exponent = bigExponents[i]; 192 failures += testCase("0x10000.0p"+Long.toString(exponent) , 193 (exponent <0?0.0:infinityD)); 194 } 195 196 // Test significands with different lengths and bit patterns. 197 { 198 long signif = 0; 199 for(int i = 1; i <= 0xe; i++) { 200 signif = (signif <<4) | (long)i; 201 failures += testCase("0x"+Long.toHexString(signif)+"p0", signif); 202 } 203 } 204 205 PairSD [] testCases = { 206 new PairSD("0x0.0p0", 0.0/16.0), 207 new PairSD("0x0.1p0", 1.0/16.0), 208 new PairSD("0x0.2p0", 2.0/16.0), 209 new PairSD("0x0.3p0", 3.0/16.0), 210 new PairSD("0x0.4p0", 4.0/16.0), 211 new PairSD("0x0.5p0", 5.0/16.0), 212 new PairSD("0x0.6p0", 6.0/16.0), 213 new PairSD("0x0.7p0", 7.0/16.0), 214 new PairSD("0x0.8p0", 8.0/16.0), 215 new PairSD("0x0.9p0", 9.0/16.0), 216 new PairSD("0x0.ap0", 10.0/16.0), 217 new PairSD("0x0.bp0", 11.0/16.0), 218 new PairSD("0x0.cp0", 12.0/16.0), 219 new PairSD("0x0.dp0", 13.0/16.0), 220 new PairSD("0x0.ep0", 14.0/16.0), 221 new PairSD("0x0.fp0", 15.0/16.0), 222 223 // Half-way case between zero and MIN_VALUE rounds down to 224 // zero 225 new PairSD("0x1.0p-1075", 0.0), 226 227 // Slighly more than half-way case between zero and 228 // MIN_VALUES rounds up to zero. 229 new PairSD("0x1.1p-1075", Double.MIN_VALUE), 230 new PairSD("0x1.000000000001p-1075", Double.MIN_VALUE), 231 new PairSD("0x1.000000000000001p-1075", Double.MIN_VALUE), 232 233 // More subnormal rounding tests 234 new PairSD("0x0.fffffffffffff7fffffp-1022", Math.nextDown(Double.MIN_NORMAL)), 235 new PairSD("0x0.fffffffffffff8p-1022", Double.MIN_NORMAL), 236 new PairSD("0x0.fffffffffffff800000001p-1022",Double.MIN_NORMAL), 237 new PairSD("0x0.fffffffffffff80000000000000001p-1022",Double.MIN_NORMAL), 238 new PairSD("0x1.0p-1022", Double.MIN_NORMAL), 239 240 241 // Large value and overflow rounding tests 242 new PairSD("0x1.fffffffffffffp1023", Double.MAX_VALUE), 243 new PairSD("0x1.fffffffffffff0000000p1023", Double.MAX_VALUE), 244 new PairSD("0x1.fffffffffffff4p1023", Double.MAX_VALUE), 245 new PairSD("0x1.fffffffffffff7fffffp1023", Double.MAX_VALUE), 246 new PairSD("0x1.fffffffffffff8p1023", infinityD), 247 new PairSD("0x1.fffffffffffff8000001p1023", infinityD), 248 249 new PairSD("0x1.ffffffffffffep1023", Math.nextDown(Double.MAX_VALUE)), 250 new PairSD("0x1.ffffffffffffe0000p1023", Math.nextDown(Double.MAX_VALUE)), 251 new PairSD("0x1.ffffffffffffe8p1023", Math.nextDown(Double.MAX_VALUE)), 252 new PairSD("0x1.ffffffffffffe7p1023", Math.nextDown(Double.MAX_VALUE)), 253 new PairSD("0x1.ffffffffffffeffffffp1023", Double.MAX_VALUE), 254 new PairSD("0x1.ffffffffffffe8000001p1023", Double.MAX_VALUE), 255 }; 256 257 for (int i = 0; i < testCases.length; i++) { 258 failures += testCase(testCases[i].s,testCases[i].d); 259 } 260 261 failures += significandAlignmentTests(); 262 263 { 264 java.util.Random rand = RandomFactory.getRandom(); 265 // Consistency check; double => hexadecimal => double 266 // preserves the original value. 267 for(int i = 0; i < 1000; i++) { 268 double d = rand.nextDouble(); 269 failures += testCase(Double.toHexString(d), d); 270 } 271 } 272 273 return failures; 274 } 275 276 /* 277 * Verify rounding works the same regardless of how the 278 * significand is aligned on input. A useful extension could be 279 * to have this sort of test for strings near the overflow 280 * threshold. 281 */ significandAlignmentTests()282 static int significandAlignmentTests() { 283 int failures = 0; 284 // baseSignif * 2^baseExp = nextDown(2.0) 285 long [] baseSignifs = { 286 0x1ffffffffffffe00L, 287 0x1fffffffffffff00L 288 }; 289 290 double [] answers = { 291 Math.nextDown(Math.nextDown(2.0)), 292 Math.nextDown(2.0), 293 2.0 294 }; 295 296 int baseExp = -60; 297 int count = 0; 298 for(int i = 0; i < 2; i++) { 299 for(long j = 0; j <= 0xfL; j++) { 300 for(long k = 0; k <= 8; k+= 4) { // k = {0, 4, 8} 301 long base = baseSignifs[i]; 302 long testValue = base | (j<<4) | k; 303 304 int offset = 0; 305 // Calculate when significand should be incremented 306 // see table 4.7 in Koren book 307 308 if ((base & 0x100L) == 0L ) { // lsb is 0 309 if ( (j >= 8L) && // round is 1 310 ((j & 0x7L) != 0 || k != 0 ) ) // sticky is 1 311 offset = 1; 312 } 313 else { // lsb is 1 314 if (j >= 8L) // round is 1 315 offset = 1; 316 } 317 318 double expected = answers[i+offset]; 319 320 for(int m = -2; m <= 3; m++) { 321 count ++; 322 323 // Form equal value string and evaluate it 324 String s = "0x" + 325 Long.toHexString((m >=0) ?(testValue<<m):(testValue>>(-m))) + 326 "p" + (baseExp - m); 327 328 failures += testCase(s, expected); 329 } 330 } 331 } 332 } 333 334 return failures; 335 } 336 337 338 /* 339 * Test tricky float rounding cases. The code which 340 * reads in a hex string converts the string to a double value. 341 * If a float value is needed, the double value is cast to float. 342 * However, the cast be itself not always guaranteed to return the 343 * right result since: 344 * 345 * 1. hex string => double can discard a sticky bit which would 346 * influence a direct hex string => float conversion. 347 * 348 * 2. hex string => double => float can have a rounding to double 349 * precision which results in a larger float value while a direct 350 * hex string => float conversion would not round up. 351 * 352 * This method includes tests of the latter two possibilities. 353 */ floatTests()354 static int floatTests(){ 355 int failures = 0; 356 357 /* 358 * A String, float pair 359 */ 360 class PairSD { 361 public String s; 362 public float f; 363 PairSD(String s, float f) { 364 this.s = s; 365 this.f = f; 366 } 367 } 368 369 String [][] roundingTestCases = { 370 // Target float value hard rouding version 371 372 {"0x1.000000p0", "0x1.0000000000001p0"}, 373 374 // Try some values that should round up to nextUp(1.0f) 375 {"0x1.000002p0", "0x1.0000010000001p0"}, 376 {"0x1.000002p0", "0x1.00000100000008p0"}, 377 {"0x1.000002p0", "0x1.0000010000000fp0"}, 378 {"0x1.000002p0", "0x1.00000100000001p0"}, 379 {"0x1.000002p0", "0x1.00000100000000000000000000000000000000001p0"}, 380 {"0x1.000002p0", "0x1.0000010000000fp0"}, 381 382 // Potential double rounding cases 383 {"0x1.000002p0", "0x1.000002fffffffp0"}, 384 {"0x1.000002p0", "0x1.000002fffffff8p0"}, 385 {"0x1.000002p0", "0x1.000002ffffffffp0"}, 386 387 {"0x1.000002p0", "0x1.000002ffff0ffp0"}, 388 {"0x1.000002p0", "0x1.000002ffff0ff8p0"}, 389 {"0x1.000002p0", "0x1.000002ffff0fffp0"}, 390 391 392 {"0x1.000000p0", "0x1.000000fffffffp0"}, 393 {"0x1.000000p0", "0x1.000000fffffff8p0"}, 394 {"0x1.000000p0", "0x1.000000ffffffffp0"}, 395 396 {"0x1.000000p0", "0x1.000000ffffffep0"}, 397 {"0x1.000000p0", "0x1.000000ffffffe8p0"}, 398 {"0x1.000000p0", "0x1.000000ffffffefp0"}, 399 400 // Float subnormal cases 401 {"0x0.000002p-126", "0x0.0000010000001p-126"}, 402 {"0x0.000002p-126", "0x0.00000100000000000001p-126"}, 403 404 {"0x0.000006p-126", "0x0.0000050000001p-126"}, 405 {"0x0.000006p-126", "0x0.00000500000000000001p-126"}, 406 407 {"0x0.0p-149", "0x0.7ffffffffffffffp-149"}, 408 {"0x1.0p-148", "0x1.3ffffffffffffffp-148"}, 409 {"0x1.cp-147", "0x1.bffffffffffffffp-147"}, 410 411 {"0x1.fffffcp-127", "0x1.fffffdffffffffp-127"}, 412 }; 413 414 String [] signs = {"", "-"}; 415 416 for(int i = 0; i < roundingTestCases.length; i++) { 417 for(int j = 0; j < signs.length; j++) { 418 String expectedIn = signs[j]+roundingTestCases[i][0]; 419 String resultIn = signs[j]+roundingTestCases[i][1]; 420 421 float expected = Float.parseFloat(expectedIn); 422 float result = Float.parseFloat(resultIn); 423 424 if( Float.compare(expected, result) != 0) { 425 failures += 1; 426 System.err.println("" + (i+1)); 427 System.err.println("Expected = " + Float.toHexString(expected)); 428 System.err.println("Rounded = " + Float.toHexString(result)); 429 System.err.println("Double = " + Double.toHexString(Double.parseDouble(resultIn))); 430 System.err.println("Input = " + resultIn); 431 System.err.println(""); 432 } 433 } 434 } 435 436 return failures; 437 } 438 main(String argv[])439 public static void main(String argv[]) { 440 int failures = 0; 441 442 failures += doubleTests(); 443 failures += floatTests(); 444 445 if (failures != 0) { 446 throw new RuntimeException("" + failures + " failures while " + 447 "testing hexadecimal floating-point " + 448 "parsing."); 449 } 450 } 451 452 } 453