author | darcy |
Mon, 03 Feb 2014 09:52:36 -0800 | |
changeset 22634 | 9c18aebe9229 |
parent 10608 | 7cfca36fc79b |
child 26197 | 1bb6b68b87cd |
permissions | -rw-r--r-- |
1826 | 1 |
/* |
22634 | 2 |
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. |
1826 | 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 |
* |
|
5506 | 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. |
|
1826 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4860891 4826732 4780454 4939441 4826652 |
|
27 |
* @summary Tests for IEEE 754[R] recommended functions and similar methods |
|
28 |
* @author Joseph D. Darcy |
|
29 |
*/ |
|
30 |
||
31 |
import sun.misc.DoubleConsts; |
|
32 |
import sun.misc.FloatConsts; |
|
33 |
||
34 |
public class IeeeRecommendedTests { |
|
35 |
private IeeeRecommendedTests(){} |
|
36 |
||
37 |
static final float NaNf = Float.NaN; |
|
38 |
static final double NaNd = Double.NaN; |
|
39 |
static final float infinityF = Float.POSITIVE_INFINITY; |
|
40 |
static final double infinityD = Double.POSITIVE_INFINITY; |
|
41 |
||
42 |
static final float Float_MAX_VALUEmm = 0x1.fffffcP+127f; |
|
43 |
static final float Float_MAX_SUBNORMAL = 0x0.fffffeP-126f; |
|
44 |
static final float Float_MAX_SUBNORMALmm = 0x0.fffffcP-126f; |
|
45 |
||
46 |
static final double Double_MAX_VALUEmm = 0x1.ffffffffffffeP+1023; |
|
47 |
static final double Double_MAX_SUBNORMAL = 0x0.fffffffffffffP-1022; |
|
48 |
static final double Double_MAX_SUBNORMALmm = 0x0.ffffffffffffeP-1022; |
|
49 |
||
50 |
// Initialize shared random number generator |
|
51 |
static java.util.Random rand = new java.util.Random(); |
|
52 |
||
53 |
/** |
|
54 |
* Returns a floating-point power of two in the normal range. |
|
55 |
*/ |
|
56 |
static double powerOfTwoD(int n) { |
|
57 |
return Double.longBitsToDouble((((long)n + (long)DoubleConsts.MAX_EXPONENT) << |
|
58 |
(DoubleConsts.SIGNIFICAND_WIDTH-1)) |
|
59 |
& DoubleConsts.EXP_BIT_MASK); |
|
60 |
} |
|
61 |
||
62 |
/** |
|
63 |
* Returns a floating-point power of two in the normal range. |
|
64 |
*/ |
|
65 |
static float powerOfTwoF(int n) { |
|
66 |
return Float.intBitsToFloat(((n + FloatConsts.MAX_EXPONENT) << |
|
67 |
(FloatConsts.SIGNIFICAND_WIDTH-1)) |
|
68 |
& FloatConsts.EXP_BIT_MASK); |
|
69 |
} |
|
70 |
||
71 |
/* ******************** getExponent tests ****************************** */ |
|
72 |
||
73 |
/* |
|
74 |
* The tests for getExponent should test the special values (NaN, +/- |
|
75 |
* infinity, etc.), test the endpoints of each binade (set of |
|
76 |
* floating-point values with the same exponent), and for good |
|
77 |
* measure, test some random values within each binade. Testing |
|
78 |
* the endpoints of each binade includes testing both positive and |
|
79 |
* negative numbers. Subnormal values with different normalized |
|
80 |
* exponents should be tested too. Both Math and StrictMath |
|
81 |
* methods should return the same results. |
|
82 |
*/ |
|
83 |
||
84 |
/* |
|
85 |
* Test Math.getExponent and StrictMath.getExponent with +d and -d. |
|
86 |
*/ |
|
87 |
static int testGetExponentCase(float f, int expected) { |
|
88 |
float minus_f = -f; |
|
89 |
int failures=0; |
|
90 |
||
91 |
failures+=Tests.test("Math.getExponent(float)", f, |
|
92 |
Math.getExponent(f), expected); |
|
93 |
failures+=Tests.test("Math.getExponent(float)", minus_f, |
|
94 |
Math.getExponent(minus_f), expected); |
|
95 |
||
96 |
failures+=Tests.test("StrictMath.getExponent(float)", f, |
|
97 |
StrictMath.getExponent(f), expected); |
|
98 |
failures+=Tests.test("StrictMath.getExponent(float)", minus_f, |
|
99 |
StrictMath.getExponent(minus_f), expected); |
|
100 |
return failures; |
|
101 |
} |
|
102 |
||
103 |
/* |
|
104 |
* Test Math.getExponent and StrictMath.getExponent with +d and -d. |
|
105 |
*/ |
|
106 |
static int testGetExponentCase(double d, int expected) { |
|
107 |
double minus_d = -d; |
|
108 |
int failures=0; |
|
109 |
||
110 |
failures+=Tests.test("Math.getExponent(double)", d, |
|
111 |
Math.getExponent(d), expected); |
|
112 |
failures+=Tests.test("Math.getExponent(double)", minus_d, |
|
113 |
Math.getExponent(minus_d), expected); |
|
114 |
||
115 |
failures+=Tests.test("StrictMath.getExponent(double)", d, |
|
116 |
StrictMath.getExponent(d), expected); |
|
117 |
failures+=Tests.test("StrictMath.getExponent(double)", minus_d, |
|
118 |
StrictMath.getExponent(minus_d), expected); |
|
119 |
return failures; |
|
120 |
} |
|
121 |
||
122 |
public static int testFloatGetExponent() { |
|
123 |
int failures = 0; |
|
124 |
float [] specialValues = {NaNf, |
|
125 |
Float.POSITIVE_INFINITY, |
|
126 |
+0.0f, |
|
127 |
+1.0f, |
|
128 |
+2.0f, |
|
129 |
+16.0f, |
|
130 |
+Float.MIN_VALUE, |
|
131 |
+Float_MAX_SUBNORMAL, |
|
132 |
+FloatConsts.MIN_NORMAL, |
|
133 |
+Float.MAX_VALUE |
|
134 |
}; |
|
135 |
||
136 |
int [] specialResults = {Float.MAX_EXPONENT + 1, // NaN results |
|
137 |
Float.MAX_EXPONENT + 1, // Infinite results |
|
138 |
Float.MIN_EXPONENT - 1, // Zero results |
|
139 |
0, |
|
140 |
1, |
|
141 |
4, |
|
142 |
FloatConsts.MIN_EXPONENT - 1, |
|
143 |
-FloatConsts.MAX_EXPONENT, |
|
144 |
FloatConsts.MIN_EXPONENT, |
|
145 |
FloatConsts.MAX_EXPONENT |
|
146 |
}; |
|
147 |
||
148 |
// Special value tests |
|
149 |
for(int i = 0; i < specialValues.length; i++) { |
|
150 |
failures += testGetExponentCase(specialValues[i], specialResults[i]); |
|
151 |
} |
|
152 |
||
153 |
||
154 |
// Normal exponent tests |
|
155 |
for(int i = FloatConsts.MIN_EXPONENT; i <= FloatConsts.MAX_EXPONENT; i++) { |
|
156 |
int result; |
|
157 |
||
158 |
// Create power of two |
|
159 |
float po2 = powerOfTwoF(i); |
|
160 |
||
161 |
failures += testGetExponentCase(po2, i); |
|
162 |
||
163 |
// Generate some random bit patterns for the significand |
|
164 |
for(int j = 0; j < 10; j++) { |
|
165 |
int randSignif = rand.nextInt(); |
|
166 |
float randFloat; |
|
167 |
||
168 |
randFloat = Float.intBitsToFloat( // Exponent |
|
169 |
(Float.floatToIntBits(po2)& |
|
170 |
(~FloatConsts.SIGNIF_BIT_MASK)) | |
|
171 |
// Significand |
|
172 |
(randSignif & |
|
173 |
FloatConsts.SIGNIF_BIT_MASK) ); |
|
174 |
||
175 |
failures += testGetExponentCase(randFloat, i); |
|
176 |
} |
|
177 |
||
178 |
if (i > FloatConsts.MIN_EXPONENT) { |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
179 |
float po2minus = Math.nextAfter(po2, |
1826 | 180 |
Float.NEGATIVE_INFINITY); |
181 |
failures += testGetExponentCase(po2minus, i-1); |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
// Subnormal exponent tests |
|
186 |
||
187 |
/* |
|
188 |
* Start with MIN_VALUE, left shift, test high value, low |
|
189 |
* values, and random in between. |
|
190 |
* |
|
191 |
* Use nextAfter to calculate, high value of previous binade, |
|
192 |
* loop count i will indicate how many random bits, if any are |
|
193 |
* needed. |
|
194 |
*/ |
|
195 |
||
196 |
float top=Float.MIN_VALUE; |
|
197 |
for( int i = 1; |
|
198 |
i < FloatConsts.SIGNIFICAND_WIDTH; |
|
199 |
i++, top *= 2.0f) { |
|
200 |
||
201 |
failures += testGetExponentCase(top, |
|
202 |
FloatConsts.MIN_EXPONENT - 1); |
|
203 |
||
204 |
// Test largest value in next smaller binade |
|
205 |
if (i >= 3) {// (i == 1) would test 0.0; |
|
206 |
// (i == 2) would just retest MIN_VALUE |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
207 |
testGetExponentCase(Math.nextAfter(top, 0.0f), |
1826 | 208 |
FloatConsts.MIN_EXPONENT - 1); |
209 |
||
210 |
if( i >= 10) { |
|
211 |
// create a bit mask with (i-1) 1's in the low order |
|
212 |
// bits |
|
213 |
int mask = ~((~0)<<(i-1)); |
|
214 |
float randFloat = Float.intBitsToFloat( // Exponent |
|
215 |
Float.floatToIntBits(top) | |
|
216 |
// Significand |
|
217 |
(rand.nextInt() & mask ) ) ; |
|
218 |
||
219 |
failures += testGetExponentCase(randFloat, |
|
220 |
FloatConsts.MIN_EXPONENT - 1); |
|
221 |
} |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
return failures; |
|
226 |
} |
|
227 |
||
228 |
||
229 |
public static int testDoubleGetExponent() { |
|
230 |
int failures = 0; |
|
231 |
double [] specialValues = {NaNd, |
|
232 |
infinityD, |
|
233 |
+0.0, |
|
234 |
+1.0, |
|
235 |
+2.0, |
|
236 |
+16.0, |
|
237 |
+Double.MIN_VALUE, |
|
238 |
+Double_MAX_SUBNORMAL, |
|
239 |
+DoubleConsts.MIN_NORMAL, |
|
240 |
+Double.MAX_VALUE |
|
241 |
}; |
|
242 |
||
243 |
int [] specialResults = {Double.MAX_EXPONENT + 1, // NaN results |
|
244 |
Double.MAX_EXPONENT + 1, // Infinite results |
|
245 |
Double.MIN_EXPONENT - 1, // Zero results |
|
246 |
0, |
|
247 |
1, |
|
248 |
4, |
|
249 |
DoubleConsts.MIN_EXPONENT - 1, |
|
250 |
-DoubleConsts.MAX_EXPONENT, |
|
251 |
DoubleConsts.MIN_EXPONENT, |
|
252 |
DoubleConsts.MAX_EXPONENT |
|
253 |
}; |
|
254 |
||
255 |
// Special value tests |
|
256 |
for(int i = 0; i < specialValues.length; i++) { |
|
257 |
failures += testGetExponentCase(specialValues[i], specialResults[i]); |
|
258 |
} |
|
259 |
||
260 |
||
261 |
// Normal exponent tests |
|
262 |
for(int i = DoubleConsts.MIN_EXPONENT; i <= DoubleConsts.MAX_EXPONENT; i++) { |
|
263 |
int result; |
|
264 |
||
265 |
// Create power of two |
|
266 |
double po2 = powerOfTwoD(i); |
|
267 |
||
268 |
failures += testGetExponentCase(po2, i); |
|
269 |
||
270 |
// Generate some random bit patterns for the significand |
|
271 |
for(int j = 0; j < 10; j++) { |
|
272 |
long randSignif = rand.nextLong(); |
|
273 |
double randFloat; |
|
274 |
||
275 |
randFloat = Double.longBitsToDouble( // Exponent |
|
276 |
(Double.doubleToLongBits(po2)& |
|
277 |
(~DoubleConsts.SIGNIF_BIT_MASK)) | |
|
278 |
// Significand |
|
279 |
(randSignif & |
|
280 |
DoubleConsts.SIGNIF_BIT_MASK) ); |
|
281 |
||
282 |
failures += testGetExponentCase(randFloat, i); |
|
283 |
} |
|
284 |
||
285 |
if (i > DoubleConsts.MIN_EXPONENT) { |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
286 |
double po2minus = Math.nextAfter(po2, |
1826 | 287 |
Double.NEGATIVE_INFINITY); |
288 |
failures += testGetExponentCase(po2minus, i-1); |
|
289 |
} |
|
290 |
} |
|
291 |
||
292 |
// Subnormal exponent tests |
|
293 |
||
294 |
/* |
|
295 |
* Start with MIN_VALUE, left shift, test high value, low |
|
296 |
* values, and random in between. |
|
297 |
* |
|
298 |
* Use nextAfter to calculate, high value of previous binade; |
|
299 |
* loop count i will indicate how many random bits, if any are |
|
300 |
* needed. |
|
301 |
*/ |
|
302 |
||
303 |
double top=Double.MIN_VALUE; |
|
304 |
for( int i = 1; |
|
305 |
i < DoubleConsts.SIGNIFICAND_WIDTH; |
|
306 |
i++, top *= 2.0f) { |
|
307 |
||
308 |
failures += testGetExponentCase(top, |
|
309 |
DoubleConsts.MIN_EXPONENT - 1); |
|
310 |
||
311 |
// Test largest value in next smaller binade |
|
312 |
if (i >= 3) {// (i == 1) would test 0.0; |
|
313 |
// (i == 2) would just retest MIN_VALUE |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
314 |
testGetExponentCase(Math.nextAfter(top, 0.0), |
1826 | 315 |
DoubleConsts.MIN_EXPONENT - 1); |
316 |
||
317 |
if( i >= 10) { |
|
318 |
// create a bit mask with (i-1) 1's in the low order |
|
319 |
// bits |
|
320 |
long mask = ~((~0L)<<(i-1)); |
|
321 |
double randFloat = Double.longBitsToDouble( // Exponent |
|
322 |
Double.doubleToLongBits(top) | |
|
323 |
// Significand |
|
324 |
(rand.nextLong() & mask ) ) ; |
|
325 |
||
326 |
failures += testGetExponentCase(randFloat, |
|
327 |
DoubleConsts.MIN_EXPONENT - 1); |
|
328 |
} |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
return failures; |
|
333 |
} |
|
334 |
||
335 |
||
336 |
/* ******************** nextAfter tests ****************************** */ |
|
337 |
||
338 |
static int testNextAfterCase(float start, double direction, float expected) { |
|
339 |
int failures=0; |
|
340 |
float minus_start = -start; |
|
341 |
double minus_direction = -direction; |
|
342 |
float minus_expected = -expected; |
|
343 |
||
344 |
failures+=Tests.test("Math.nextAfter(float,double)", start, direction, |
|
345 |
Math.nextAfter(start, direction), expected); |
|
346 |
failures+=Tests.test("Math.nextAfter(float,double)", minus_start, minus_direction, |
|
347 |
Math.nextAfter(minus_start, minus_direction), minus_expected); |
|
348 |
||
349 |
failures+=Tests.test("StrictMath.nextAfter(float,double)", start, direction, |
|
350 |
StrictMath.nextAfter(start, direction), expected); |
|
351 |
failures+=Tests.test("StrictMath.nextAfter(float,double)", minus_start, minus_direction, |
|
352 |
StrictMath.nextAfter(minus_start, minus_direction), minus_expected); |
|
353 |
return failures; |
|
354 |
} |
|
355 |
||
356 |
static int testNextAfterCase(double start, double direction, double expected) { |
|
357 |
int failures=0; |
|
358 |
||
359 |
double minus_start = -start; |
|
360 |
double minus_direction = -direction; |
|
361 |
double minus_expected = -expected; |
|
362 |
||
363 |
failures+=Tests.test("Math.nextAfter(double,double)", start, direction, |
|
364 |
Math.nextAfter(start, direction), expected); |
|
365 |
failures+=Tests.test("Math.nextAfter(double,double)", minus_start, minus_direction, |
|
366 |
Math.nextAfter(minus_start, minus_direction), minus_expected); |
|
367 |
||
368 |
failures+=Tests.test("StrictMath.nextAfter(double,double)", start, direction, |
|
369 |
StrictMath.nextAfter(start, direction), expected); |
|
370 |
failures+=Tests.test("StrictMath.nextAfter(double,double)", minus_start, minus_direction, |
|
371 |
StrictMath.nextAfter(minus_start, minus_direction), minus_expected); |
|
372 |
return failures; |
|
373 |
} |
|
374 |
||
375 |
public static int testFloatNextAfter() { |
|
376 |
int failures=0; |
|
377 |
||
378 |
/* |
|
379 |
* Each row of the testCases matrix represents one test case |
|
380 |
* for nexAfter; given the input of the first two columns, the |
|
381 |
* result in the last column is expected. |
|
382 |
*/ |
|
383 |
float [][] testCases = { |
|
384 |
{NaNf, NaNf, NaNf}, |
|
385 |
{NaNf, 0.0f, NaNf}, |
|
386 |
{0.0f, NaNf, NaNf}, |
|
387 |
{NaNf, infinityF, NaNf}, |
|
388 |
{infinityF, NaNf, NaNf}, |
|
389 |
||
390 |
{infinityF, infinityF, infinityF}, |
|
391 |
{infinityF, -infinityF, Float.MAX_VALUE}, |
|
392 |
{infinityF, 0.0f, Float.MAX_VALUE}, |
|
393 |
||
394 |
{Float.MAX_VALUE, infinityF, infinityF}, |
|
395 |
{Float.MAX_VALUE, -infinityF, Float_MAX_VALUEmm}, |
|
396 |
{Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE}, |
|
397 |
{Float.MAX_VALUE, 0.0f, Float_MAX_VALUEmm}, |
|
398 |
||
399 |
{Float_MAX_VALUEmm, Float.MAX_VALUE, Float.MAX_VALUE}, |
|
400 |
{Float_MAX_VALUEmm, infinityF, Float.MAX_VALUE}, |
|
401 |
{Float_MAX_VALUEmm, Float_MAX_VALUEmm, Float_MAX_VALUEmm}, |
|
402 |
||
403 |
{FloatConsts.MIN_NORMAL, infinityF, FloatConsts.MIN_NORMAL+ |
|
404 |
Float.MIN_VALUE}, |
|
405 |
{FloatConsts.MIN_NORMAL, -infinityF, Float_MAX_SUBNORMAL}, |
|
406 |
{FloatConsts.MIN_NORMAL, 1.0f, FloatConsts.MIN_NORMAL+ |
|
407 |
Float.MIN_VALUE}, |
|
408 |
{FloatConsts.MIN_NORMAL, -1.0f, Float_MAX_SUBNORMAL}, |
|
409 |
{FloatConsts.MIN_NORMAL, FloatConsts.MIN_NORMAL, FloatConsts.MIN_NORMAL}, |
|
410 |
||
411 |
{Float_MAX_SUBNORMAL, FloatConsts.MIN_NORMAL, FloatConsts.MIN_NORMAL}, |
|
412 |
{Float_MAX_SUBNORMAL, Float_MAX_SUBNORMAL, Float_MAX_SUBNORMAL}, |
|
413 |
{Float_MAX_SUBNORMAL, 0.0f, Float_MAX_SUBNORMALmm}, |
|
414 |
||
415 |
{Float_MAX_SUBNORMALmm, Float_MAX_SUBNORMAL, Float_MAX_SUBNORMAL}, |
|
416 |
{Float_MAX_SUBNORMALmm, 0.0f, Float_MAX_SUBNORMALmm-Float.MIN_VALUE}, |
|
417 |
{Float_MAX_SUBNORMALmm, Float_MAX_SUBNORMALmm, Float_MAX_SUBNORMALmm}, |
|
418 |
||
419 |
{Float.MIN_VALUE, 0.0f, 0.0f}, |
|
420 |
{-Float.MIN_VALUE, 0.0f, -0.0f}, |
|
421 |
{Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE}, |
|
422 |
{Float.MIN_VALUE, 1.0f, 2*Float.MIN_VALUE}, |
|
423 |
||
424 |
// Make sure zero behavior is tested |
|
425 |
{0.0f, 0.0f, 0.0f}, |
|
426 |
{0.0f, -0.0f, -0.0f}, |
|
427 |
{-0.0f, 0.0f, 0.0f}, |
|
428 |
{-0.0f, -0.0f, -0.0f}, |
|
429 |
{0.0f, infinityF, Float.MIN_VALUE}, |
|
430 |
{0.0f, -infinityF, -Float.MIN_VALUE}, |
|
431 |
{-0.0f, infinityF, Float.MIN_VALUE}, |
|
432 |
{-0.0f, -infinityF, -Float.MIN_VALUE}, |
|
433 |
{0.0f, Float.MIN_VALUE, Float.MIN_VALUE}, |
|
434 |
{0.0f, -Float.MIN_VALUE, -Float.MIN_VALUE}, |
|
435 |
{-0.0f, Float.MIN_VALUE, Float.MIN_VALUE}, |
|
436 |
{-0.0f, -Float.MIN_VALUE, -Float.MIN_VALUE} |
|
437 |
}; |
|
438 |
||
439 |
for(int i = 0; i < testCases.length; i++) { |
|
440 |
failures += testNextAfterCase(testCases[i][0], testCases[i][1], |
|
441 |
testCases[i][2]); |
|
442 |
} |
|
443 |
||
444 |
return failures; |
|
445 |
} |
|
446 |
||
447 |
public static int testDoubleNextAfter() { |
|
448 |
int failures =0; |
|
449 |
||
450 |
/* |
|
451 |
* Each row of the testCases matrix represents one test case |
|
452 |
* for nexAfter; given the input of the first two columns, the |
|
453 |
* result in the last column is expected. |
|
454 |
*/ |
|
455 |
double [][] testCases = { |
|
456 |
{NaNd, NaNd, NaNd}, |
|
457 |
{NaNd, 0.0d, NaNd}, |
|
458 |
{0.0d, NaNd, NaNd}, |
|
459 |
{NaNd, infinityD, NaNd}, |
|
460 |
{infinityD, NaNd, NaNd}, |
|
461 |
||
462 |
{infinityD, infinityD, infinityD}, |
|
463 |
{infinityD, -infinityD, Double.MAX_VALUE}, |
|
464 |
{infinityD, 0.0d, Double.MAX_VALUE}, |
|
465 |
||
466 |
{Double.MAX_VALUE, infinityD, infinityD}, |
|
467 |
{Double.MAX_VALUE, -infinityD, Double_MAX_VALUEmm}, |
|
468 |
{Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE}, |
|
469 |
{Double.MAX_VALUE, 0.0d, Double_MAX_VALUEmm}, |
|
470 |
||
471 |
{Double_MAX_VALUEmm, Double.MAX_VALUE, Double.MAX_VALUE}, |
|
472 |
{Double_MAX_VALUEmm, infinityD, Double.MAX_VALUE}, |
|
473 |
{Double_MAX_VALUEmm, Double_MAX_VALUEmm, Double_MAX_VALUEmm}, |
|
474 |
||
475 |
{DoubleConsts.MIN_NORMAL, infinityD, DoubleConsts.MIN_NORMAL+ |
|
476 |
Double.MIN_VALUE}, |
|
477 |
{DoubleConsts.MIN_NORMAL, -infinityD, Double_MAX_SUBNORMAL}, |
|
478 |
{DoubleConsts.MIN_NORMAL, 1.0f, DoubleConsts.MIN_NORMAL+ |
|
479 |
Double.MIN_VALUE}, |
|
480 |
{DoubleConsts.MIN_NORMAL, -1.0f, Double_MAX_SUBNORMAL}, |
|
481 |
{DoubleConsts.MIN_NORMAL, DoubleConsts.MIN_NORMAL,DoubleConsts.MIN_NORMAL}, |
|
482 |
||
483 |
{Double_MAX_SUBNORMAL, DoubleConsts.MIN_NORMAL,DoubleConsts.MIN_NORMAL}, |
|
484 |
{Double_MAX_SUBNORMAL, Double_MAX_SUBNORMAL, Double_MAX_SUBNORMAL}, |
|
485 |
{Double_MAX_SUBNORMAL, 0.0d, Double_MAX_SUBNORMALmm}, |
|
486 |
||
487 |
{Double_MAX_SUBNORMALmm, Double_MAX_SUBNORMAL, Double_MAX_SUBNORMAL}, |
|
488 |
{Double_MAX_SUBNORMALmm, 0.0d, Double_MAX_SUBNORMALmm-Double.MIN_VALUE}, |
|
489 |
{Double_MAX_SUBNORMALmm, Double_MAX_SUBNORMALmm, Double_MAX_SUBNORMALmm}, |
|
490 |
||
491 |
{Double.MIN_VALUE, 0.0d, 0.0d}, |
|
492 |
{-Double.MIN_VALUE, 0.0d, -0.0d}, |
|
493 |
{Double.MIN_VALUE, Double.MIN_VALUE, Double.MIN_VALUE}, |
|
494 |
{Double.MIN_VALUE, 1.0f, 2*Double.MIN_VALUE}, |
|
495 |
||
496 |
// Make sure zero behavior is tested |
|
497 |
{0.0d, 0.0d, 0.0d}, |
|
498 |
{0.0d, -0.0d, -0.0d}, |
|
499 |
{-0.0d, 0.0d, 0.0d}, |
|
500 |
{-0.0d, -0.0d, -0.0d}, |
|
501 |
{0.0d, infinityD, Double.MIN_VALUE}, |
|
502 |
{0.0d, -infinityD, -Double.MIN_VALUE}, |
|
503 |
{-0.0d, infinityD, Double.MIN_VALUE}, |
|
504 |
{-0.0d, -infinityD, -Double.MIN_VALUE}, |
|
505 |
{0.0d, Double.MIN_VALUE, Double.MIN_VALUE}, |
|
506 |
{0.0d, -Double.MIN_VALUE, -Double.MIN_VALUE}, |
|
507 |
{-0.0d, Double.MIN_VALUE, Double.MIN_VALUE}, |
|
508 |
{-0.0d, -Double.MIN_VALUE, -Double.MIN_VALUE} |
|
509 |
}; |
|
510 |
||
511 |
for(int i = 0; i < testCases.length; i++) { |
|
512 |
failures += testNextAfterCase(testCases[i][0], testCases[i][1], |
|
513 |
testCases[i][2]); |
|
514 |
} |
|
515 |
return failures; |
|
516 |
} |
|
517 |
||
518 |
/* ******************** nextUp tests ********************************* */ |
|
519 |
||
520 |
public static int testFloatNextUp() { |
|
521 |
int failures=0; |
|
522 |
||
523 |
/* |
|
524 |
* Each row of testCases represents one test case for nextUp; |
|
525 |
* the first column is the input and the second column is the |
|
526 |
* expected result. |
|
527 |
*/ |
|
528 |
float testCases [][] = { |
|
529 |
{NaNf, NaNf}, |
|
530 |
{-infinityF, -Float.MAX_VALUE}, |
|
531 |
{-Float.MAX_VALUE, -Float_MAX_VALUEmm}, |
|
532 |
{-FloatConsts.MIN_NORMAL, -Float_MAX_SUBNORMAL}, |
|
533 |
{-Float_MAX_SUBNORMAL, -Float_MAX_SUBNORMALmm}, |
|
534 |
{-Float.MIN_VALUE, -0.0f}, |
|
535 |
{-0.0f, Float.MIN_VALUE}, |
|
536 |
{+0.0f, Float.MIN_VALUE}, |
|
537 |
{Float.MIN_VALUE, Float.MIN_VALUE*2}, |
|
538 |
{Float_MAX_SUBNORMALmm, Float_MAX_SUBNORMAL}, |
|
539 |
{Float_MAX_SUBNORMAL, FloatConsts.MIN_NORMAL}, |
|
540 |
{FloatConsts.MIN_NORMAL, FloatConsts.MIN_NORMAL+Float.MIN_VALUE}, |
|
541 |
{Float_MAX_VALUEmm, Float.MAX_VALUE}, |
|
542 |
{Float.MAX_VALUE, infinityF}, |
|
543 |
{infinityF, infinityF} |
|
544 |
}; |
|
545 |
||
546 |
for(int i = 0; i < testCases.length; i++) { |
|
547 |
failures+=Tests.test("Math.nextUp(float)", |
|
548 |
testCases[i][0], Math.nextUp(testCases[i][0]), testCases[i][1]); |
|
549 |
||
550 |
failures+=Tests.test("StrictMath.nextUp(float)", |
|
551 |
testCases[i][0], StrictMath.nextUp(testCases[i][0]), testCases[i][1]); |
|
552 |
} |
|
553 |
||
554 |
return failures; |
|
555 |
} |
|
556 |
||
557 |
||
558 |
public static int testDoubleNextUp() { |
|
559 |
int failures=0; |
|
560 |
||
561 |
/* |
|
562 |
* Each row of testCases represents one test case for nextUp; |
|
563 |
* the first column is the input and the second column is the |
|
564 |
* expected result. |
|
565 |
*/ |
|
566 |
double testCases [][] = { |
|
567 |
{NaNd, NaNd}, |
|
568 |
{-infinityD, -Double.MAX_VALUE}, |
|
569 |
{-Double.MAX_VALUE, -Double_MAX_VALUEmm}, |
|
570 |
{-DoubleConsts.MIN_NORMAL, -Double_MAX_SUBNORMAL}, |
|
571 |
{-Double_MAX_SUBNORMAL, -Double_MAX_SUBNORMALmm}, |
|
572 |
{-Double.MIN_VALUE, -0.0d}, |
|
573 |
{-0.0d, Double.MIN_VALUE}, |
|
574 |
{+0.0d, Double.MIN_VALUE}, |
|
575 |
{Double.MIN_VALUE, Double.MIN_VALUE*2}, |
|
576 |
{Double_MAX_SUBNORMALmm, Double_MAX_SUBNORMAL}, |
|
577 |
{Double_MAX_SUBNORMAL, DoubleConsts.MIN_NORMAL}, |
|
578 |
{DoubleConsts.MIN_NORMAL, DoubleConsts.MIN_NORMAL+Double.MIN_VALUE}, |
|
579 |
{Double_MAX_VALUEmm, Double.MAX_VALUE}, |
|
580 |
{Double.MAX_VALUE, infinityD}, |
|
581 |
{infinityD, infinityD} |
|
582 |
}; |
|
583 |
||
584 |
for(int i = 0; i < testCases.length; i++) { |
|
585 |
failures+=Tests.test("Math.nextUp(double)", |
|
586 |
testCases[i][0], Math.nextUp(testCases[i][0]), testCases[i][1]); |
|
587 |
||
588 |
failures+=Tests.test("StrictMath.nextUp(double)", |
|
589 |
testCases[i][0], StrictMath.nextUp(testCases[i][0]), testCases[i][1]); |
|
590 |
} |
|
591 |
||
592 |
return failures; |
|
593 |
} |
|
594 |
||
595 |
/* ******************** nextDown tests ********************************* */ |
|
596 |
||
597 |
public static int testFloatNextDown() { |
|
598 |
int failures=0; |
|
599 |
||
600 |
/* |
|
601 |
* Each row of testCases represents one test case for nextDown; |
|
602 |
* the first column is the input and the second column is the |
|
603 |
* expected result. |
|
604 |
*/ |
|
605 |
float testCases [][] = { |
|
606 |
{NaNf, NaNf}, |
|
607 |
{-infinityF, -infinityF}, |
|
608 |
{-Float.MAX_VALUE, -infinityF}, |
|
609 |
{-Float_MAX_VALUEmm, -Float.MAX_VALUE}, |
|
610 |
{-Float_MAX_SUBNORMAL, -FloatConsts.MIN_NORMAL}, |
|
611 |
{-Float_MAX_SUBNORMALmm, -Float_MAX_SUBNORMAL}, |
|
612 |
{-0.0f, -Float.MIN_VALUE}, |
|
613 |
{+0.0f, -Float.MIN_VALUE}, |
|
614 |
{Float.MIN_VALUE, 0.0f}, |
|
615 |
{Float.MIN_VALUE*2, Float.MIN_VALUE}, |
|
616 |
{Float_MAX_SUBNORMAL, Float_MAX_SUBNORMALmm}, |
|
617 |
{FloatConsts.MIN_NORMAL, Float_MAX_SUBNORMAL}, |
|
618 |
{FloatConsts.MIN_NORMAL+ |
|
619 |
Float.MIN_VALUE, FloatConsts.MIN_NORMAL}, |
|
620 |
{Float.MAX_VALUE, Float_MAX_VALUEmm}, |
|
621 |
{infinityF, Float.MAX_VALUE}, |
|
622 |
}; |
|
623 |
||
624 |
for(int i = 0; i < testCases.length; i++) { |
|
10608 | 625 |
failures+=Tests.test("Math.nextDown(float)", |
626 |
testCases[i][0], Math.nextDown(testCases[i][0]), testCases[i][1]); |
|
627 |
||
628 |
failures+=Tests.test("StrictMath.nextDown(float)", |
|
629 |
testCases[i][0], StrictMath.nextDown(testCases[i][0]), testCases[i][1]); |
|
1826 | 630 |
} |
631 |
||
632 |
return failures; |
|
633 |
} |
|
634 |
||
635 |
||
636 |
public static int testDoubleNextDown() { |
|
637 |
int failures=0; |
|
638 |
||
639 |
/* |
|
640 |
* Each row of testCases represents one test case for nextDown; |
|
641 |
* the first column is the input and the second column is the |
|
642 |
* expected result. |
|
643 |
*/ |
|
644 |
double testCases [][] = { |
|
645 |
{NaNd, NaNd}, |
|
646 |
{-infinityD, -infinityD}, |
|
647 |
{-Double.MAX_VALUE, -infinityD}, |
|
648 |
{-Double_MAX_VALUEmm, -Double.MAX_VALUE}, |
|
649 |
{-Double_MAX_SUBNORMAL, -DoubleConsts.MIN_NORMAL}, |
|
650 |
{-Double_MAX_SUBNORMALmm, -Double_MAX_SUBNORMAL}, |
|
651 |
{-0.0d, -Double.MIN_VALUE}, |
|
652 |
{+0.0d, -Double.MIN_VALUE}, |
|
653 |
{Double.MIN_VALUE, 0.0d}, |
|
654 |
{Double.MIN_VALUE*2, Double.MIN_VALUE}, |
|
655 |
{Double_MAX_SUBNORMAL, Double_MAX_SUBNORMALmm}, |
|
656 |
{DoubleConsts.MIN_NORMAL, Double_MAX_SUBNORMAL}, |
|
657 |
{DoubleConsts.MIN_NORMAL+ |
|
658 |
Double.MIN_VALUE, DoubleConsts.MIN_NORMAL}, |
|
659 |
{Double.MAX_VALUE, Double_MAX_VALUEmm}, |
|
660 |
{infinityD, Double.MAX_VALUE}, |
|
661 |
}; |
|
662 |
||
663 |
for(int i = 0; i < testCases.length; i++) { |
|
10608 | 664 |
failures+=Tests.test("Math.nextDown(double)", |
665 |
testCases[i][0], Math.nextDown(testCases[i][0]), testCases[i][1]); |
|
666 |
||
667 |
failures+=Tests.test("StrictMath.nextDown(double)", |
|
668 |
testCases[i][0], StrictMath.nextDown(testCases[i][0]), testCases[i][1]); |
|
1826 | 669 |
} |
670 |
||
671 |
return failures; |
|
672 |
} |
|
673 |
||
674 |
||
675 |
/* ********************** boolean tests ****************************** */ |
|
676 |
||
677 |
/* |
|
678 |
* Combined tests for boolean functions, isFinite, isInfinite, |
|
679 |
* isNaN, isUnordered. |
|
680 |
*/ |
|
681 |
||
682 |
public static int testFloatBooleanMethods() { |
|
683 |
int failures = 0; |
|
684 |
||
685 |
float testCases [] = { |
|
686 |
NaNf, |
|
687 |
-infinityF, |
|
688 |
infinityF, |
|
689 |
-Float.MAX_VALUE, |
|
690 |
-3.0f, |
|
691 |
-1.0f, |
|
692 |
-FloatConsts.MIN_NORMAL, |
|
693 |
-Float_MAX_SUBNORMALmm, |
|
694 |
-Float_MAX_SUBNORMAL, |
|
695 |
-Float.MIN_VALUE, |
|
696 |
-0.0f, |
|
697 |
+0.0f, |
|
698 |
Float.MIN_VALUE, |
|
699 |
Float_MAX_SUBNORMALmm, |
|
700 |
Float_MAX_SUBNORMAL, |
|
701 |
FloatConsts.MIN_NORMAL, |
|
702 |
1.0f, |
|
703 |
3.0f, |
|
704 |
Float_MAX_VALUEmm, |
|
705 |
Float.MAX_VALUE |
|
706 |
}; |
|
707 |
||
708 |
for(int i = 0; i < testCases.length; i++) { |
|
709 |
// isNaN |
|
22634 | 710 |
failures+=Tests.test("Float.isNaN(float)", testCases[i], |
711 |
Float.isNaN(testCases[i]), (i ==0)); |
|
1826 | 712 |
|
713 |
// isFinite |
|
10608 | 714 |
failures+=Tests.test("Float.isFinite(float)", testCases[i], |
715 |
Float.isFinite(testCases[i]), (i >= 3)); |
|
1826 | 716 |
|
717 |
// isInfinite |
|
22634 | 718 |
failures+=Tests.test("Float.isInfinite(float)", testCases[i], |
719 |
Float.isInfinite(testCases[i]), (i==1 || i==2)); |
|
1826 | 720 |
|
721 |
// isUnorderd |
|
722 |
for(int j = 0; j < testCases.length; j++) { |
|
22634 | 723 |
failures+=Tests.test("Tests.isUnordered(float, float)", testCases[i],testCases[j], |
724 |
Tests.isUnordered(testCases[i],testCases[j]), (i==0 || j==0)); |
|
1826 | 725 |
} |
726 |
} |
|
727 |
||
728 |
return failures; |
|
729 |
} |
|
730 |
||
731 |
public static int testDoubleBooleanMethods() { |
|
732 |
int failures = 0; |
|
733 |
boolean result = false; |
|
734 |
||
735 |
double testCases [] = { |
|
736 |
NaNd, |
|
737 |
-infinityD, |
|
738 |
infinityD, |
|
739 |
-Double.MAX_VALUE, |
|
740 |
-3.0d, |
|
741 |
-1.0d, |
|
742 |
-DoubleConsts.MIN_NORMAL, |
|
743 |
-Double_MAX_SUBNORMALmm, |
|
744 |
-Double_MAX_SUBNORMAL, |
|
745 |
-Double.MIN_VALUE, |
|
746 |
-0.0d, |
|
747 |
+0.0d, |
|
748 |
Double.MIN_VALUE, |
|
749 |
Double_MAX_SUBNORMALmm, |
|
750 |
Double_MAX_SUBNORMAL, |
|
751 |
DoubleConsts.MIN_NORMAL, |
|
752 |
1.0d, |
|
753 |
3.0d, |
|
754 |
Double_MAX_VALUEmm, |
|
755 |
Double.MAX_VALUE |
|
756 |
}; |
|
757 |
||
758 |
for(int i = 0; i < testCases.length; i++) { |
|
759 |
// isNaN |
|
22634 | 760 |
failures+=Tests.test("Double.isNaN(double)", testCases[i], |
761 |
Double.isNaN(testCases[i]), (i ==0)); |
|
1826 | 762 |
|
763 |
// isFinite |
|
10608 | 764 |
failures+=Tests.test("Double.isFinite(double)", testCases[i], |
765 |
Double.isFinite(testCases[i]), (i >= 3)); |
|
1826 | 766 |
|
767 |
// isInfinite |
|
22634 | 768 |
failures+=Tests.test("Double.isInfinite(double)", testCases[i], |
769 |
Double.isInfinite(testCases[i]), (i==1 || i==2)); |
|
1826 | 770 |
|
771 |
// isUnorderd |
|
772 |
for(int j = 0; j < testCases.length; j++) { |
|
22634 | 773 |
failures+=Tests.test("Tests.isUnordered(double, double)", testCases[i],testCases[j], |
774 |
Tests.isUnordered(testCases[i],testCases[j]), (i==0 || j==0)); |
|
1826 | 775 |
} |
776 |
} |
|
777 |
||
778 |
return failures; |
|
779 |
} |
|
780 |
||
781 |
/* ******************** copySign tests******************************** */ |
|
782 |
||
783 |
public static int testFloatCopySign() { |
|
784 |
int failures = 0; |
|
785 |
||
786 |
// testCases[0] are logically positive numbers; |
|
787 |
// testCases[1] are negative numbers. |
|
788 |
float testCases [][] = { |
|
789 |
{+0.0f, |
|
790 |
Float.MIN_VALUE, |
|
791 |
Float_MAX_SUBNORMALmm, |
|
792 |
Float_MAX_SUBNORMAL, |
|
793 |
FloatConsts.MIN_NORMAL, |
|
794 |
1.0f, |
|
795 |
3.0f, |
|
796 |
Float_MAX_VALUEmm, |
|
797 |
Float.MAX_VALUE, |
|
798 |
infinityF, |
|
799 |
}, |
|
800 |
{-infinityF, |
|
801 |
-Float.MAX_VALUE, |
|
802 |
-3.0f, |
|
803 |
-1.0f, |
|
804 |
-FloatConsts.MIN_NORMAL, |
|
805 |
-Float_MAX_SUBNORMALmm, |
|
806 |
-Float_MAX_SUBNORMAL, |
|
807 |
-Float.MIN_VALUE, |
|
808 |
-0.0f} |
|
809 |
}; |
|
810 |
||
811 |
float NaNs[] = {Float.intBitsToFloat(0x7fc00000), // "positive" NaN |
|
812 |
Float.intBitsToFloat(0xFfc00000)}; // "negative" NaN |
|
813 |
||
814 |
// Tests shared between raw and non-raw versions |
|
815 |
for(int i = 0; i < 2; i++) { |
|
816 |
for(int j = 0; j < 2; j++) { |
|
817 |
for(int m = 0; m < testCases[i].length; m++) { |
|
818 |
for(int n = 0; n < testCases[j].length; n++) { |
|
819 |
// copySign(magnitude, sign) |
|
820 |
failures+=Tests.test("Math.copySign(float,float)", |
|
821 |
testCases[i][m],testCases[j][n], |
|
822 |
Math.copySign(testCases[i][m], testCases[j][n]), |
|
823 |
(j==0?1.0f:-1.0f)*Math.abs(testCases[i][m]) ); |
|
824 |
||
825 |
failures+=Tests.test("StrictMath.copySign(float,float)", |
|
826 |
testCases[i][m],testCases[j][n], |
|
827 |
StrictMath.copySign(testCases[i][m], testCases[j][n]), |
|
828 |
(j==0?1.0f:-1.0f)*Math.abs(testCases[i][m]) ); |
|
829 |
} |
|
830 |
} |
|
831 |
} |
|
832 |
} |
|
833 |
||
834 |
// For rawCopySign, NaN may effectively have either sign bit |
|
835 |
// while for copySign NaNs are treated as if they always have |
|
836 |
// a zero sign bit (i.e. as positive numbers) |
|
837 |
for(int i = 0; i < 2; i++) { |
|
838 |
for(int j = 0; j < NaNs.length; j++) { |
|
839 |
for(int m = 0; m < testCases[i].length; m++) { |
|
840 |
// copySign(magnitude, sign) |
|
841 |
||
842 |
failures += (Math.abs(Math.copySign(testCases[i][m], NaNs[j])) == |
|
843 |
Math.abs(testCases[i][m])) ? 0:1; |
|
844 |
||
845 |
||
846 |
failures+=Tests.test("StrictMath.copySign(float,float)", |
|
847 |
testCases[i][m], NaNs[j], |
|
848 |
StrictMath.copySign(testCases[i][m], NaNs[j]), |
|
849 |
Math.abs(testCases[i][m]) ); |
|
850 |
} |
|
851 |
} |
|
852 |
} |
|
853 |
||
854 |
return failures; |
|
855 |
} |
|
856 |
||
857 |
public static int testDoubleCopySign() { |
|
858 |
int failures = 0; |
|
859 |
||
860 |
// testCases[0] are logically positive numbers; |
|
861 |
// testCases[1] are negative numbers. |
|
862 |
double testCases [][] = { |
|
863 |
{+0.0d, |
|
864 |
Double.MIN_VALUE, |
|
865 |
Double_MAX_SUBNORMALmm, |
|
866 |
Double_MAX_SUBNORMAL, |
|
867 |
DoubleConsts.MIN_NORMAL, |
|
868 |
1.0d, |
|
869 |
3.0d, |
|
870 |
Double_MAX_VALUEmm, |
|
871 |
Double.MAX_VALUE, |
|
872 |
infinityD, |
|
873 |
}, |
|
874 |
{-infinityD, |
|
875 |
-Double.MAX_VALUE, |
|
876 |
-3.0d, |
|
877 |
-1.0d, |
|
878 |
-DoubleConsts.MIN_NORMAL, |
|
879 |
-Double_MAX_SUBNORMALmm, |
|
880 |
-Double_MAX_SUBNORMAL, |
|
881 |
-Double.MIN_VALUE, |
|
882 |
-0.0d} |
|
883 |
}; |
|
884 |
||
885 |
double NaNs[] = {Double.longBitsToDouble(0x7ff8000000000000L), // "positive" NaN |
|
886 |
Double.longBitsToDouble(0xfff8000000000000L), // "negative" NaN |
|
887 |
Double.longBitsToDouble(0x7FF0000000000001L), |
|
888 |
Double.longBitsToDouble(0xFFF0000000000001L), |
|
889 |
Double.longBitsToDouble(0x7FF8555555555555L), |
|
890 |
Double.longBitsToDouble(0xFFF8555555555555L), |
|
891 |
Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), |
|
892 |
Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), |
|
893 |
Double.longBitsToDouble(0x7FFDeadBeef00000L), |
|
894 |
Double.longBitsToDouble(0xFFFDeadBeef00000L), |
|
895 |
Double.longBitsToDouble(0x7FFCafeBabe00000L), |
|
896 |
Double.longBitsToDouble(0xFFFCafeBabe00000L)}; |
|
897 |
||
898 |
// Tests shared between Math and StrictMath versions |
|
899 |
for(int i = 0; i < 2; i++) { |
|
900 |
for(int j = 0; j < 2; j++) { |
|
901 |
for(int m = 0; m < testCases[i].length; m++) { |
|
902 |
for(int n = 0; n < testCases[j].length; n++) { |
|
903 |
// copySign(magnitude, sign) |
|
904 |
failures+=Tests.test("MathcopySign(double,double)", |
|
905 |
testCases[i][m],testCases[j][n], |
|
906 |
Math.copySign(testCases[i][m], testCases[j][n]), |
|
907 |
(j==0?1.0f:-1.0f)*Math.abs(testCases[i][m]) ); |
|
908 |
||
909 |
failures+=Tests.test("StrictMath.copySign(double,double)", |
|
910 |
testCases[i][m],testCases[j][n], |
|
911 |
StrictMath.copySign(testCases[i][m], testCases[j][n]), |
|
912 |
(j==0?1.0f:-1.0f)*Math.abs(testCases[i][m]) ); |
|
913 |
} |
|
914 |
} |
|
915 |
} |
|
916 |
} |
|
917 |
||
918 |
// For Math.copySign, NaN may effectively have either sign bit |
|
919 |
// while for StrictMath.copySign NaNs are treated as if they |
|
920 |
// always have a zero sign bit (i.e. as positive numbers) |
|
921 |
for(int i = 0; i < 2; i++) { |
|
922 |
for(int j = 0; j < NaNs.length; j++) { |
|
923 |
for(int m = 0; m < testCases[i].length; m++) { |
|
924 |
// copySign(magnitude, sign) |
|
925 |
||
926 |
failures += (Math.abs(Math.copySign(testCases[i][m], NaNs[j])) == |
|
927 |
Math.abs(testCases[i][m])) ? 0:1; |
|
928 |
||
929 |
||
930 |
failures+=Tests.test("StrictMath.copySign(double,double)", |
|
931 |
testCases[i][m], NaNs[j], |
|
932 |
StrictMath.copySign(testCases[i][m], NaNs[j]), |
|
933 |
Math.abs(testCases[i][m]) ); |
|
934 |
} |
|
935 |
} |
|
936 |
} |
|
937 |
||
938 |
||
939 |
return failures; |
|
940 |
} |
|
941 |
||
942 |
/* ************************ scalb tests ******************************* */ |
|
943 |
||
944 |
static int testScalbCase(float value, int scale_factor, float expected) { |
|
945 |
int failures=0; |
|
946 |
||
947 |
failures+=Tests.test("Math.scalb(float,int)", |
|
948 |
value, scale_factor, |
|
949 |
Math.scalb(value, scale_factor), expected); |
|
950 |
||
951 |
failures+=Tests.test("Math.scalb(float,int)", |
|
952 |
-value, scale_factor, |
|
953 |
Math.scalb(-value, scale_factor), -expected); |
|
954 |
||
955 |
failures+=Tests.test("StrictMath.scalb(float,int)", |
|
956 |
value, scale_factor, |
|
957 |
StrictMath.scalb(value, scale_factor), expected); |
|
958 |
||
959 |
failures+=Tests.test("StrictMath.scalb(float,int)", |
|
960 |
-value, scale_factor, |
|
961 |
StrictMath.scalb(-value, scale_factor), -expected); |
|
962 |
return failures; |
|
963 |
} |
|
964 |
||
965 |
public static int testFloatScalb() { |
|
966 |
int failures=0; |
|
967 |
int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT + |
|
968 |
FloatConsts.SIGNIFICAND_WIDTH + 1; |
|
969 |
||
970 |
||
971 |
// Arguments x, where scalb(x,n) is x for any n. |
|
972 |
float [] identityTestCases = {NaNf, |
|
973 |
-0.0f, |
|
974 |
+0.0f, |
|
975 |
infinityF, |
|
976 |
-infinityF |
|
977 |
}; |
|
978 |
||
979 |
float [] subnormalTestCases = { |
|
980 |
Float.MIN_VALUE, |
|
981 |
3.0f*Float.MIN_VALUE, |
|
982 |
Float_MAX_SUBNORMALmm, |
|
983 |
Float_MAX_SUBNORMAL |
|
984 |
}; |
|
985 |
||
986 |
float [] someTestCases = { |
|
987 |
Float.MIN_VALUE, |
|
988 |
3.0f*Float.MIN_VALUE, |
|
989 |
Float_MAX_SUBNORMALmm, |
|
990 |
Float_MAX_SUBNORMAL, |
|
991 |
FloatConsts.MIN_NORMAL, |
|
992 |
1.0f, |
|
993 |
2.0f, |
|
994 |
3.0f, |
|
995 |
(float)Math.PI, |
|
996 |
Float_MAX_VALUEmm, |
|
997 |
Float.MAX_VALUE |
|
998 |
}; |
|
999 |
||
1000 |
int [] oneMultiplyScalingFactors = { |
|
1001 |
FloatConsts.MIN_EXPONENT, |
|
1002 |
FloatConsts.MIN_EXPONENT+1, |
|
1003 |
-3, |
|
1004 |
-2, |
|
1005 |
-1, |
|
1006 |
0, |
|
1007 |
1, |
|
1008 |
2, |
|
1009 |
3, |
|
1010 |
FloatConsts.MAX_EXPONENT-1, |
|
1011 |
FloatConsts.MAX_EXPONENT |
|
1012 |
}; |
|
1013 |
||
1014 |
int [] manyScalingFactors = { |
|
1015 |
Integer.MIN_VALUE, |
|
1016 |
Integer.MIN_VALUE+1, |
|
1017 |
-MAX_SCALE -1, |
|
1018 |
-MAX_SCALE, |
|
1019 |
-MAX_SCALE+1, |
|
1020 |
||
1021 |
2*FloatConsts.MIN_EXPONENT-1, // -253 |
|
1022 |
2*FloatConsts.MIN_EXPONENT, // -252 |
|
1023 |
2*FloatConsts.MIN_EXPONENT+1, // -251 |
|
1024 |
||
22634 | 1025 |
FloatConsts.MIN_EXPONENT - FloatConsts.SIGNIFICAND_WIDTH, |
1026 |
FloatConsts.MIN_SUB_EXPONENT, |
|
1826 | 1027 |
-FloatConsts.MAX_EXPONENT, // -127 |
1028 |
FloatConsts.MIN_EXPONENT, // -126 |
|
1029 |
||
1030 |
-2, |
|
1031 |
-1, |
|
1032 |
0, |
|
1033 |
1, |
|
1034 |
2, |
|
1035 |
||
1036 |
FloatConsts.MAX_EXPONENT-1, // 126 |
|
1037 |
FloatConsts.MAX_EXPONENT, // 127 |
|
1038 |
FloatConsts.MAX_EXPONENT+1, // 128 |
|
1039 |
||
1040 |
2*FloatConsts.MAX_EXPONENT-1, // 253 |
|
1041 |
2*FloatConsts.MAX_EXPONENT, // 254 |
|
1042 |
2*FloatConsts.MAX_EXPONENT+1, // 255 |
|
1043 |
||
1044 |
MAX_SCALE-1, |
|
1045 |
MAX_SCALE, |
|
1046 |
MAX_SCALE+1, |
|
1047 |
Integer.MAX_VALUE-1, |
|
1048 |
Integer.MAX_VALUE |
|
1049 |
}; |
|
1050 |
||
1051 |
// Test cases where scaling is always a no-op |
|
1052 |
for(int i=0; i < identityTestCases.length; i++) { |
|
1053 |
for(int j=0; j < manyScalingFactors.length; j++) { |
|
1054 |
failures += testScalbCase(identityTestCases[i], |
|
1055 |
manyScalingFactors[j], |
|
1056 |
identityTestCases[i]); |
|
1057 |
} |
|
1058 |
} |
|
1059 |
||
1060 |
// Test cases where result is 0.0 or infinity due to magnitude |
|
1061 |
// of the scaling factor |
|
1062 |
for(int i=0; i < someTestCases.length; i++) { |
|
1063 |
for(int j=0; j < manyScalingFactors.length; j++) { |
|
1064 |
int scaleFactor = manyScalingFactors[j]; |
|
1065 |
if (Math.abs(scaleFactor) >= MAX_SCALE) { |
|
1066 |
float value = someTestCases[i]; |
|
1067 |
failures+=testScalbCase(value, |
|
1068 |
scaleFactor, |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
1069 |
Math.copySign( (scaleFactor>0?infinityF:0.0f), value) ); |
1826 | 1070 |
} |
1071 |
} |
|
1072 |
} |
|
1073 |
||
1074 |
// Test cases that could be done with one floating-point |
|
1075 |
// multiply. |
|
1076 |
for(int i=0; i < someTestCases.length; i++) { |
|
1077 |
for(int j=0; j < oneMultiplyScalingFactors.length; j++) { |
|
1078 |
int scaleFactor = oneMultiplyScalingFactors[j]; |
|
1079 |
float value = someTestCases[i]; |
|
1080 |
||
1081 |
failures+=testScalbCase(value, |
|
1082 |
scaleFactor, |
|
1083 |
value*powerOfTwoF(scaleFactor)); |
|
1084 |
} |
|
1085 |
} |
|
1086 |
||
1087 |
// Create 2^MAX_EXPONENT |
|
1088 |
float twoToTheMaxExp = 1.0f; // 2^0 |
|
1089 |
for(int i = 0; i < FloatConsts.MAX_EXPONENT; i++) |
|
1090 |
twoToTheMaxExp *=2.0f; |
|
1091 |
||
1092 |
// Scale-up subnormal values until they all overflow |
|
1093 |
for(int i=0; i < subnormalTestCases.length; i++) { |
|
1094 |
float scale = 1.0f; // 2^j |
|
1095 |
float value = subnormalTestCases[i]; |
|
1096 |
||
1097 |
for(int j=FloatConsts.MAX_EXPONENT*2; j < MAX_SCALE; j++) { // MAX_SCALE -1 should cause overflow |
|
1098 |
int scaleFactor = j; |
|
1099 |
||
1100 |
failures+=testScalbCase(value, |
|
1101 |
scaleFactor, |
|
22634 | 1102 |
(Tests.ilogb(value) +j > FloatConsts.MAX_EXPONENT ) ? |
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
1103 |
Math.copySign(infinityF, value) : // overflow |
1826 | 1104 |
// calculate right answer |
1105 |
twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) ); |
|
1106 |
scale*=2.0f; |
|
1107 |
} |
|
1108 |
} |
|
1109 |
||
1110 |
// Scale down a large number until it underflows. By scaling |
|
1111 |
// down MAX_NORMALmm, the first subnormal result will be exact |
|
1112 |
// but the next one will round -- all those results can be |
|
1113 |
// checked by halving a separate value in the loop. Actually, |
|
1114 |
// we can keep halving and checking until the product is zero |
|
1115 |
// since: |
|
1116 |
// |
|
1117 |
// 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact |
|
1118 |
// it will round *up* |
|
1119 |
// |
|
1120 |
// 2. When rounding first occurs in the expected product, it |
|
1121 |
// too rounds up, to 2^-MAX_EXPONENT. |
|
1122 |
// |
|
1123 |
// Halving expected after rounding happends to give the same |
|
1124 |
// result as the scalb operation. |
|
1125 |
float expected = Float_MAX_VALUEmm *0.5f; |
|
1126 |
for(int i = -1; i > -MAX_SCALE; i--) { |
|
1127 |
failures+=testScalbCase(Float_MAX_VALUEmm, i, expected); |
|
1128 |
||
1129 |
expected *= 0.5f; |
|
1130 |
} |
|
1131 |
||
1132 |
// Tricky rounding tests: |
|
1133 |
// Scale down a large number into subnormal range such that if |
|
1134 |
// scalb is being implemented with multiple floating-point |
|
1135 |
// multiplies, the value would round twice if the multiplies |
|
1136 |
// were done in the wrong order. |
|
1137 |
||
1138 |
float value = 0x8.0000bP-5f; |
|
1139 |
expected = 0x1.00001p-129f; |
|
1140 |
||
1141 |
for(int i = 0; i < 129; i++) { |
|
1142 |
failures+=testScalbCase(value, |
|
1143 |
-127-i, |
|
1144 |
expected); |
|
1145 |
value *=2.0f; |
|
1146 |
} |
|
1147 |
||
1148 |
return failures; |
|
1149 |
} |
|
1150 |
||
1151 |
static int testScalbCase(double value, int scale_factor, double expected) { |
|
1152 |
int failures=0; |
|
1153 |
||
1154 |
failures+=Tests.test("Math.scalb(double,int)", |
|
1155 |
value, scale_factor, |
|
1156 |
Math.scalb(value, scale_factor), expected); |
|
1157 |
||
1158 |
failures+=Tests.test("Math.scalb(double,int)", |
|
1159 |
-value, scale_factor, |
|
1160 |
Math.scalb(-value, scale_factor), -expected); |
|
1161 |
||
1162 |
failures+=Tests.test("StrictMath.scalb(double,int)", |
|
1163 |
value, scale_factor, |
|
1164 |
StrictMath.scalb(value, scale_factor), expected); |
|
1165 |
||
1166 |
failures+=Tests.test("StrictMath.scalb(double,int)", |
|
1167 |
-value, scale_factor, |
|
1168 |
StrictMath.scalb(-value, scale_factor), -expected); |
|
1169 |
||
1170 |
return failures; |
|
1171 |
} |
|
1172 |
||
1173 |
public static int testDoubleScalb() { |
|
1174 |
int failures=0; |
|
1175 |
int MAX_SCALE = DoubleConsts.MAX_EXPONENT + -DoubleConsts.MIN_EXPONENT + |
|
1176 |
DoubleConsts.SIGNIFICAND_WIDTH + 1; |
|
1177 |
||
1178 |
||
1179 |
// Arguments x, where scalb(x,n) is x for any n. |
|
1180 |
double [] identityTestCases = {NaNd, |
|
1181 |
-0.0, |
|
1182 |
+0.0, |
|
1183 |
infinityD, |
|
1184 |
}; |
|
1185 |
||
1186 |
double [] subnormalTestCases = { |
|
1187 |
Double.MIN_VALUE, |
|
1188 |
3.0d*Double.MIN_VALUE, |
|
1189 |
Double_MAX_SUBNORMALmm, |
|
1190 |
Double_MAX_SUBNORMAL |
|
1191 |
}; |
|
1192 |
||
1193 |
double [] someTestCases = { |
|
1194 |
Double.MIN_VALUE, |
|
1195 |
3.0d*Double.MIN_VALUE, |
|
1196 |
Double_MAX_SUBNORMALmm, |
|
1197 |
Double_MAX_SUBNORMAL, |
|
1198 |
DoubleConsts.MIN_NORMAL, |
|
1199 |
1.0d, |
|
1200 |
2.0d, |
|
1201 |
3.0d, |
|
1202 |
Math.PI, |
|
1203 |
Double_MAX_VALUEmm, |
|
1204 |
Double.MAX_VALUE |
|
1205 |
}; |
|
1206 |
||
1207 |
int [] oneMultiplyScalingFactors = { |
|
1208 |
DoubleConsts.MIN_EXPONENT, |
|
1209 |
DoubleConsts.MIN_EXPONENT+1, |
|
1210 |
-3, |
|
1211 |
-2, |
|
1212 |
-1, |
|
1213 |
0, |
|
1214 |
1, |
|
1215 |
2, |
|
1216 |
3, |
|
1217 |
DoubleConsts.MAX_EXPONENT-1, |
|
1218 |
DoubleConsts.MAX_EXPONENT |
|
1219 |
}; |
|
1220 |
||
1221 |
int [] manyScalingFactors = { |
|
1222 |
Integer.MIN_VALUE, |
|
1223 |
Integer.MIN_VALUE+1, |
|
1224 |
-MAX_SCALE -1, |
|
1225 |
-MAX_SCALE, |
|
1226 |
-MAX_SCALE+1, |
|
1227 |
||
1228 |
2*DoubleConsts.MIN_EXPONENT-1, // -2045 |
|
1229 |
2*DoubleConsts.MIN_EXPONENT, // -2044 |
|
1230 |
2*DoubleConsts.MIN_EXPONENT+1, // -2043 |
|
1231 |
||
22634 | 1232 |
DoubleConsts.MIN_EXPONENT, // -1022 |
1233 |
DoubleConsts.MIN_EXPONENT - DoubleConsts.SIGNIFICAND_WIDTH, |
|
1234 |
DoubleConsts.MIN_SUB_EXPONENT, |
|
1826 | 1235 |
-DoubleConsts.MAX_EXPONENT, // -1023 |
1236 |
DoubleConsts.MIN_EXPONENT, // -1022 |
|
1237 |
||
1238 |
-2, |
|
1239 |
-1, |
|
1240 |
0, |
|
1241 |
1, |
|
1242 |
2, |
|
1243 |
||
1244 |
DoubleConsts.MAX_EXPONENT-1, // 1022 |
|
1245 |
DoubleConsts.MAX_EXPONENT, // 1023 |
|
1246 |
DoubleConsts.MAX_EXPONENT+1, // 1024 |
|
1247 |
||
1248 |
2*DoubleConsts.MAX_EXPONENT-1, // 2045 |
|
1249 |
2*DoubleConsts.MAX_EXPONENT, // 2046 |
|
1250 |
2*DoubleConsts.MAX_EXPONENT+1, // 2047 |
|
1251 |
||
1252 |
MAX_SCALE-1, |
|
1253 |
MAX_SCALE, |
|
1254 |
MAX_SCALE+1, |
|
1255 |
Integer.MAX_VALUE-1, |
|
1256 |
Integer.MAX_VALUE |
|
1257 |
}; |
|
1258 |
||
1259 |
// Test cases where scaling is always a no-op |
|
1260 |
for(int i=0; i < identityTestCases.length; i++) { |
|
1261 |
for(int j=0; j < manyScalingFactors.length; j++) { |
|
1262 |
failures += testScalbCase(identityTestCases[i], |
|
1263 |
manyScalingFactors[j], |
|
1264 |
identityTestCases[i]); |
|
1265 |
} |
|
1266 |
} |
|
1267 |
||
1268 |
// Test cases where result is 0.0 or infinity due to magnitude |
|
1269 |
// of the scaling factor |
|
1270 |
for(int i=0; i < someTestCases.length; i++) { |
|
1271 |
for(int j=0; j < manyScalingFactors.length; j++) { |
|
1272 |
int scaleFactor = manyScalingFactors[j]; |
|
1273 |
if (Math.abs(scaleFactor) >= MAX_SCALE) { |
|
1274 |
double value = someTestCases[i]; |
|
1275 |
failures+=testScalbCase(value, |
|
1276 |
scaleFactor, |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
1277 |
Math.copySign( (scaleFactor>0?infinityD:0.0), value) ); |
1826 | 1278 |
} |
1279 |
} |
|
1280 |
} |
|
1281 |
||
1282 |
// Test cases that could be done with one floating-point |
|
1283 |
// multiply. |
|
1284 |
for(int i=0; i < someTestCases.length; i++) { |
|
1285 |
for(int j=0; j < oneMultiplyScalingFactors.length; j++) { |
|
1286 |
int scaleFactor = oneMultiplyScalingFactors[j]; |
|
1287 |
double value = someTestCases[i]; |
|
1288 |
||
1289 |
failures+=testScalbCase(value, |
|
1290 |
scaleFactor, |
|
1291 |
value*powerOfTwoD(scaleFactor)); |
|
1292 |
} |
|
1293 |
} |
|
1294 |
||
1295 |
// Create 2^MAX_EXPONENT |
|
1296 |
double twoToTheMaxExp = 1.0; // 2^0 |
|
1297 |
for(int i = 0; i < DoubleConsts.MAX_EXPONENT; i++) |
|
1298 |
twoToTheMaxExp *=2.0; |
|
1299 |
||
1300 |
// Scale-up subnormal values until they all overflow |
|
1301 |
for(int i=0; i < subnormalTestCases.length; i++) { |
|
1302 |
double scale = 1.0; // 2^j |
|
1303 |
double value = subnormalTestCases[i]; |
|
1304 |
||
1305 |
for(int j=DoubleConsts.MAX_EXPONENT*2; j < MAX_SCALE; j++) { // MAX_SCALE -1 should cause overflow |
|
1306 |
int scaleFactor = j; |
|
1307 |
||
1308 |
failures+=testScalbCase(value, |
|
1309 |
scaleFactor, |
|
22634 | 1310 |
(Tests.ilogb(value) +j > DoubleConsts.MAX_EXPONENT ) ? |
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
1311 |
Math.copySign(infinityD, value) : // overflow |
1826 | 1312 |
// calculate right answer |
1313 |
twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) ); |
|
1314 |
scale*=2.0; |
|
1315 |
} |
|
1316 |
} |
|
1317 |
||
1318 |
// Scale down a large number until it underflows. By scaling |
|
1319 |
// down MAX_NORMALmm, the first subnormal result will be exact |
|
1320 |
// but the next one will round -- all those results can be |
|
1321 |
// checked by halving a separate value in the loop. Actually, |
|
1322 |
// we can keep halving and checking until the product is zero |
|
1323 |
// since: |
|
1324 |
// |
|
1325 |
// 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact |
|
1326 |
// it will round *up* |
|
1327 |
// |
|
1328 |
// 2. When rounding first occurs in the expected product, it |
|
1329 |
// too rounds up, to 2^-MAX_EXPONENT. |
|
1330 |
// |
|
1331 |
// Halving expected after rounding happends to give the same |
|
1332 |
// result as the scalb operation. |
|
1333 |
double expected = Double_MAX_VALUEmm *0.5f; |
|
1334 |
for(int i = -1; i > -MAX_SCALE; i--) { |
|
1335 |
failures+=testScalbCase(Double_MAX_VALUEmm, i, expected); |
|
1336 |
||
1337 |
expected *= 0.5; |
|
1338 |
} |
|
1339 |
||
1340 |
// Tricky rounding tests: |
|
1341 |
// Scale down a large number into subnormal range such that if |
|
1342 |
// scalb is being implemented with multiple floating-point |
|
1343 |
// multiplies, the value would round twice if the multiplies |
|
1344 |
// were done in the wrong order. |
|
1345 |
||
1346 |
double value = 0x1.000000000000bP-1; |
|
1347 |
expected = 0x0.2000000000001P-1022; |
|
1348 |
for(int i = 0; i < DoubleConsts.MAX_EXPONENT+2; i++) { |
|
1349 |
failures+=testScalbCase(value, |
|
1350 |
-1024-i, |
|
1351 |
expected); |
|
1352 |
value *=2.0; |
|
1353 |
} |
|
1354 |
||
1355 |
return failures; |
|
1356 |
} |
|
1357 |
||
1358 |
/* ************************* ulp tests ******************************* */ |
|
1359 |
||
1360 |
||
1361 |
/* |
|
1362 |
* Test Math.ulp and StrictMath.ulp with +d and -d. |
|
1363 |
*/ |
|
1364 |
static int testUlpCase(float f, float expected) { |
|
1365 |
float minus_f = -f; |
|
1366 |
int failures=0; |
|
1367 |
||
1368 |
failures+=Tests.test("Math.ulp(float)", f, |
|
1369 |
Math.ulp(f), expected); |
|
1370 |
failures+=Tests.test("Math.ulp(float)", minus_f, |
|
1371 |
Math.ulp(minus_f), expected); |
|
1372 |
failures+=Tests.test("StrictMath.ulp(float)", f, |
|
1373 |
StrictMath.ulp(f), expected); |
|
1374 |
failures+=Tests.test("StrictMath.ulp(float)", minus_f, |
|
1375 |
StrictMath.ulp(minus_f), expected); |
|
1376 |
return failures; |
|
1377 |
} |
|
1378 |
||
1379 |
static int testUlpCase(double d, double expected) { |
|
1380 |
double minus_d = -d; |
|
1381 |
int failures=0; |
|
1382 |
||
1383 |
failures+=Tests.test("Math.ulp(double)", d, |
|
1384 |
Math.ulp(d), expected); |
|
1385 |
failures+=Tests.test("Math.ulp(double)", minus_d, |
|
1386 |
Math.ulp(minus_d), expected); |
|
1387 |
failures+=Tests.test("StrictMath.ulp(double)", d, |
|
1388 |
StrictMath.ulp(d), expected); |
|
1389 |
failures+=Tests.test("StrictMath.ulp(double)", minus_d, |
|
1390 |
StrictMath.ulp(minus_d), expected); |
|
1391 |
return failures; |
|
1392 |
} |
|
1393 |
||
1394 |
public static int testFloatUlp() { |
|
1395 |
int failures = 0; |
|
1396 |
float [] specialValues = {NaNf, |
|
1397 |
Float.POSITIVE_INFINITY, |
|
1398 |
+0.0f, |
|
1399 |
+1.0f, |
|
1400 |
+2.0f, |
|
1401 |
+16.0f, |
|
1402 |
+Float.MIN_VALUE, |
|
1403 |
+Float_MAX_SUBNORMAL, |
|
1404 |
+FloatConsts.MIN_NORMAL, |
|
1405 |
+Float.MAX_VALUE |
|
1406 |
}; |
|
1407 |
||
1408 |
float [] specialResults = {NaNf, |
|
1409 |
Float.POSITIVE_INFINITY, |
|
1410 |
Float.MIN_VALUE, |
|
1411 |
powerOfTwoF(-23), |
|
1412 |
powerOfTwoF(-22), |
|
1413 |
powerOfTwoF(-19), |
|
1414 |
Float.MIN_VALUE, |
|
1415 |
Float.MIN_VALUE, |
|
1416 |
Float.MIN_VALUE, |
|
1417 |
powerOfTwoF(104) |
|
1418 |
}; |
|
1419 |
||
1420 |
// Special value tests |
|
1421 |
for(int i = 0; i < specialValues.length; i++) { |
|
1422 |
failures += testUlpCase(specialValues[i], specialResults[i]); |
|
1423 |
} |
|
1424 |
||
1425 |
||
1426 |
// Normal exponent tests |
|
1427 |
for(int i = FloatConsts.MIN_EXPONENT; i <= FloatConsts.MAX_EXPONENT; i++) { |
|
1428 |
float expected; |
|
1429 |
||
1430 |
// Create power of two |
|
1431 |
float po2 = powerOfTwoF(i); |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
1432 |
expected = Math.scalb(1.0f, i - (FloatConsts.SIGNIFICAND_WIDTH-1)); |
1826 | 1433 |
|
1434 |
failures += testUlpCase(po2, expected); |
|
1435 |
||
1436 |
// Generate some random bit patterns for the significand |
|
1437 |
for(int j = 0; j < 10; j++) { |
|
1438 |
int randSignif = rand.nextInt(); |
|
1439 |
float randFloat; |
|
1440 |
||
1441 |
randFloat = Float.intBitsToFloat( // Exponent |
|
1442 |
(Float.floatToIntBits(po2)& |
|
1443 |
(~FloatConsts.SIGNIF_BIT_MASK)) | |
|
1444 |
// Significand |
|
1445 |
(randSignif & |
|
1446 |
FloatConsts.SIGNIF_BIT_MASK) ); |
|
1447 |
||
1448 |
failures += testUlpCase(randFloat, expected); |
|
1449 |
} |
|
1450 |
||
1451 |
if (i > FloatConsts.MIN_EXPONENT) { |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
1452 |
float po2minus = Math.nextAfter(po2, |
1826 | 1453 |
Float.NEGATIVE_INFINITY); |
1454 |
failures += testUlpCase(po2minus, expected/2.0f); |
|
1455 |
} |
|
1456 |
} |
|
1457 |
||
1458 |
// Subnormal tests |
|
1459 |
||
1460 |
/* |
|
1461 |
* Start with MIN_VALUE, left shift, test high value, low |
|
1462 |
* values, and random in between. |
|
1463 |
* |
|
1464 |
* Use nextAfter to calculate, high value of previous binade, |
|
1465 |
* loop count i will indicate how many random bits, if any are |
|
1466 |
* needed. |
|
1467 |
*/ |
|
1468 |
||
1469 |
float top=Float.MIN_VALUE; |
|
1470 |
for( int i = 1; |
|
1471 |
i < FloatConsts.SIGNIFICAND_WIDTH; |
|
1472 |
i++, top *= 2.0f) { |
|
1473 |
||
1474 |
failures += testUlpCase(top, Float.MIN_VALUE); |
|
1475 |
||
1476 |
// Test largest value in next smaller binade |
|
1477 |
if (i >= 3) {// (i == 1) would test 0.0; |
|
1478 |
// (i == 2) would just retest MIN_VALUE |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
1479 |
testUlpCase(Math.nextAfter(top, 0.0f), |
1826 | 1480 |
Float.MIN_VALUE); |
1481 |
||
1482 |
if( i >= 10) { |
|
1483 |
// create a bit mask with (i-1) 1's in the low order |
|
1484 |
// bits |
|
1485 |
int mask = ~((~0)<<(i-1)); |
|
1486 |
float randFloat = Float.intBitsToFloat( // Exponent |
|
1487 |
Float.floatToIntBits(top) | |
|
1488 |
// Significand |
|
1489 |
(rand.nextInt() & mask ) ) ; |
|
1490 |
||
1491 |
failures += testUlpCase(randFloat, Float.MIN_VALUE); |
|
1492 |
} |
|
1493 |
} |
|
1494 |
} |
|
1495 |
||
1496 |
return failures; |
|
1497 |
} |
|
1498 |
||
1499 |
public static int testDoubleUlp() { |
|
1500 |
int failures = 0; |
|
1501 |
double [] specialValues = {NaNd, |
|
1502 |
Double.POSITIVE_INFINITY, |
|
1503 |
+0.0d, |
|
1504 |
+1.0d, |
|
1505 |
+2.0d, |
|
1506 |
+16.0d, |
|
1507 |
+Double.MIN_VALUE, |
|
1508 |
+Double_MAX_SUBNORMAL, |
|
1509 |
+DoubleConsts.MIN_NORMAL, |
|
1510 |
+Double.MAX_VALUE |
|
1511 |
}; |
|
1512 |
||
1513 |
double [] specialResults = {NaNf, |
|
1514 |
Double.POSITIVE_INFINITY, |
|
1515 |
Double.MIN_VALUE, |
|
1516 |
powerOfTwoD(-52), |
|
1517 |
powerOfTwoD(-51), |
|
1518 |
powerOfTwoD(-48), |
|
1519 |
Double.MIN_VALUE, |
|
1520 |
Double.MIN_VALUE, |
|
1521 |
Double.MIN_VALUE, |
|
1522 |
powerOfTwoD(971) |
|
1523 |
}; |
|
1524 |
||
1525 |
// Special value tests |
|
1526 |
for(int i = 0; i < specialValues.length; i++) { |
|
1527 |
failures += testUlpCase(specialValues[i], specialResults[i]); |
|
1528 |
} |
|
1529 |
||
1530 |
||
1531 |
// Normal exponent tests |
|
1532 |
for(int i = DoubleConsts.MIN_EXPONENT; i <= DoubleConsts.MAX_EXPONENT; i++) { |
|
1533 |
double expected; |
|
1534 |
||
1535 |
// Create power of two |
|
1536 |
double po2 = powerOfTwoD(i); |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
1537 |
expected = Math.scalb(1.0, i - (DoubleConsts.SIGNIFICAND_WIDTH-1)); |
1826 | 1538 |
|
1539 |
failures += testUlpCase(po2, expected); |
|
1540 |
||
1541 |
// Generate some random bit patterns for the significand |
|
1542 |
for(int j = 0; j < 10; j++) { |
|
1543 |
long randSignif = rand.nextLong(); |
|
1544 |
double randDouble; |
|
1545 |
||
1546 |
randDouble = Double.longBitsToDouble( // Exponent |
|
1547 |
(Double.doubleToLongBits(po2)& |
|
1548 |
(~DoubleConsts.SIGNIF_BIT_MASK)) | |
|
1549 |
// Significand |
|
1550 |
(randSignif & |
|
1551 |
DoubleConsts.SIGNIF_BIT_MASK) ); |
|
1552 |
||
1553 |
failures += testUlpCase(randDouble, expected); |
|
1554 |
} |
|
1555 |
||
1556 |
if (i > DoubleConsts.MIN_EXPONENT) { |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
1557 |
double po2minus = Math.nextAfter(po2, |
1826 | 1558 |
Double.NEGATIVE_INFINITY); |
1559 |
failures += testUlpCase(po2minus, expected/2.0f); |
|
1560 |
} |
|
1561 |
} |
|
1562 |
||
1563 |
// Subnormal tests |
|
1564 |
||
1565 |
/* |
|
1566 |
* Start with MIN_VALUE, left shift, test high value, low |
|
1567 |
* values, and random in between. |
|
1568 |
* |
|
1569 |
* Use nextAfter to calculate, high value of previous binade, |
|
1570 |
* loop count i will indicate how many random bits, if any are |
|
1571 |
* needed. |
|
1572 |
*/ |
|
1573 |
||
1574 |
double top=Double.MIN_VALUE; |
|
1575 |
for( int i = 1; |
|
1576 |
i < DoubleConsts.SIGNIFICAND_WIDTH; |
|
1577 |
i++, top *= 2.0f) { |
|
1578 |
||
1579 |
failures += testUlpCase(top, Double.MIN_VALUE); |
|
1580 |
||
1581 |
// Test largest value in next smaller binade |
|
1582 |
if (i >= 3) {// (i == 1) would test 0.0; |
|
1583 |
// (i == 2) would just retest MIN_VALUE |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
1584 |
testUlpCase(Math.nextAfter(top, 0.0f), |
1826 | 1585 |
Double.MIN_VALUE); |
1586 |
||
1587 |
if( i >= 10) { |
|
1588 |
// create a bit mask with (i-1) 1's in the low order |
|
1589 |
// bits |
|
1590 |
int mask = ~((~0)<<(i-1)); |
|
1591 |
double randDouble = Double.longBitsToDouble( // Exponent |
|
1592 |
Double.doubleToLongBits(top) | |
|
1593 |
// Significand |
|
1594 |
(rand.nextLong() & mask ) ) ; |
|
1595 |
||
1596 |
failures += testUlpCase(randDouble, Double.MIN_VALUE); |
|
1597 |
} |
|
1598 |
} |
|
1599 |
} |
|
1600 |
||
1601 |
return failures; |
|
1602 |
} |
|
1603 |
||
1604 |
public static int testFloatSignum() { |
|
1605 |
int failures = 0; |
|
1606 |
float testCases [][] = { |
|
1607 |
{NaNf, NaNf}, |
|
1608 |
{-infinityF, -1.0f}, |
|
1609 |
{-Float.MAX_VALUE, -1.0f}, |
|
1610 |
{-FloatConsts.MIN_NORMAL, -1.0f}, |
|
1611 |
{-1.0f, -1.0f}, |
|
1612 |
{-2.0f, -1.0f}, |
|
1613 |
{-Float_MAX_SUBNORMAL, -1.0f}, |
|
1614 |
{-Float.MIN_VALUE, -1.0f}, |
|
1615 |
{-0.0f, -0.0f}, |
|
1616 |
{+0.0f, +0.0f}, |
|
1617 |
{Float.MIN_VALUE, 1.0f}, |
|
1618 |
{Float_MAX_SUBNORMALmm, 1.0f}, |
|
1619 |
{Float_MAX_SUBNORMAL, 1.0f}, |
|
1620 |
{FloatConsts.MIN_NORMAL, 1.0f}, |
|
1621 |
{1.0f, 1.0f}, |
|
1622 |
{2.0f, 1.0f}, |
|
1623 |
{Float_MAX_VALUEmm, 1.0f}, |
|
1624 |
{Float.MAX_VALUE, 1.0f}, |
|
1625 |
{infinityF, 1.0f} |
|
1626 |
}; |
|
1627 |
||
1628 |
for(int i = 0; i < testCases.length; i++) { |
|
1629 |
failures+=Tests.test("Math.signum(float)", |
|
1630 |
testCases[i][0], Math.signum(testCases[i][0]), testCases[i][1]); |
|
1631 |
failures+=Tests.test("StrictMath.signum(float)", |
|
1632 |
testCases[i][0], StrictMath.signum(testCases[i][0]), testCases[i][1]); |
|
1633 |
} |
|
1634 |
||
1635 |
return failures; |
|
1636 |
} |
|
1637 |
||
1638 |
public static int testDoubleSignum() { |
|
1639 |
int failures = 0; |
|
1640 |
double testCases [][] = { |
|
1641 |
{NaNd, NaNd}, |
|
1642 |
{-infinityD, -1.0}, |
|
1643 |
{-Double.MAX_VALUE, -1.0}, |
|
1644 |
{-DoubleConsts.MIN_NORMAL, -1.0}, |
|
1645 |
{-1.0, -1.0}, |
|
1646 |
{-2.0, -1.0}, |
|
1647 |
{-Double_MAX_SUBNORMAL, -1.0}, |
|
1648 |
{-Double.MIN_VALUE, -1.0d}, |
|
1649 |
{-0.0d, -0.0d}, |
|
1650 |
{+0.0d, +0.0d}, |
|
1651 |
{Double.MIN_VALUE, 1.0}, |
|
1652 |
{Double_MAX_SUBNORMALmm, 1.0}, |
|
1653 |
{Double_MAX_SUBNORMAL, 1.0}, |
|
1654 |
{DoubleConsts.MIN_NORMAL, 1.0}, |
|
1655 |
{1.0, 1.0}, |
|
1656 |
{2.0, 1.0}, |
|
1657 |
{Double_MAX_VALUEmm, 1.0}, |
|
1658 |
{Double.MAX_VALUE, 1.0}, |
|
1659 |
{infinityD, 1.0} |
|
1660 |
}; |
|
1661 |
||
1662 |
for(int i = 0; i < testCases.length; i++) { |
|
1663 |
failures+=Tests.test("Math.signum(double)", |
|
1664 |
testCases[i][0], Math.signum(testCases[i][0]), testCases[i][1]); |
|
1665 |
failures+=Tests.test("StrictMath.signum(double)", |
|
1666 |
testCases[i][0], StrictMath.signum(testCases[i][0]), testCases[i][1]); |
|
1667 |
} |
|
1668 |
||
1669 |
return failures; |
|
1670 |
} |
|
1671 |
||
1672 |
||
1673 |
public static void main(String argv[]) { |
|
1674 |
int failures = 0; |
|
1675 |
||
1676 |
failures += testFloatGetExponent(); |
|
1677 |
failures += testDoubleGetExponent(); |
|
1678 |
||
1679 |
failures += testFloatNextAfter(); |
|
1680 |
failures += testDoubleNextAfter(); |
|
1681 |
||
1682 |
failures += testFloatNextUp(); |
|
1683 |
failures += testDoubleNextUp(); |
|
1684 |
||
1685 |
failures += testFloatNextDown(); |
|
1686 |
failures += testDoubleNextDown(); |
|
1687 |
||
1688 |
failures += testFloatBooleanMethods(); |
|
1689 |
failures += testDoubleBooleanMethods(); |
|
1690 |
||
1691 |
failures += testFloatCopySign(); |
|
1692 |
failures += testDoubleCopySign(); |
|
1693 |
||
1694 |
failures += testFloatScalb(); |
|
1695 |
failures += testDoubleScalb(); |
|
1696 |
||
1697 |
failures += testFloatUlp(); |
|
1698 |
failures += testDoubleUlp(); |
|
1699 |
||
1700 |
failures += testFloatSignum(); |
|
1701 |
failures += testDoubleSignum(); |
|
1702 |
||
1703 |
if (failures > 0) { |
|
1704 |
System.err.println("Testing the recommended functions incurred " |
|
1705 |
+ failures + " failures."); |
|
1706 |
throw new RuntimeException(); |
|
1707 |
} |
|
1708 |
} |
|
1709 |
} |