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