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