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