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