IeeeRecommendedTests.java revision 2362
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * @bug 4860891 4826732 4780454 4939441 4826652 * @summary Tests for IEEE 754[R] recommended functions and similar methods * @author Joseph D. Darcy // Initialize shared random number generator * Returns a floating-point power of two in the normal range. * Returns a floating-point power of two in the normal range. /* ******************** getExponent tests ****************************** */ * The tests for getExponent should test the special values (NaN, +/- * infinity, etc.), test the endpoints of each binade (set of * floating-point values with the same exponent), and for good * measure, test some random values within each binade. Testing * the endpoints of each binade includes testing both positive and * negative numbers. Subnormal values with different normalized * exponents should be tested too. Both Math and StrictMath * methods should return the same results. * Test Math.getExponent and StrictMath.getExponent with +d and -d. * Test Math.getExponent and StrictMath.getExponent with +d and -d. // Generate some random bit patterns for the significand for(
int j =
0; j <
10; j++) {
// Subnormal exponent tests * Start with MIN_VALUE, left shift, test high value, low * values, and random in between. * Use nextAfter to calculate, high value of previous binade, * loop count i will indicate how many random bits, if any are // Test largest value in next smaller binade if (i >=
3) {
// (i == 1) would test 0.0; // (i == 2) would just retest MIN_VALUE // create a bit mask with (i-1) 1's in the low order int mask = ~((~
0)<<(i-
1));
// Generate some random bit patterns for the significand for(
int j =
0; j <
10; j++) {
// Subnormal exponent tests * Start with MIN_VALUE, left shift, test high value, low * values, and random in between. * Use nextAfter to calculate, high value of previous binade; * loop count i will indicate how many random bits, if any are // Test largest value in next smaller binade if (i >=
3) {
// (i == 1) would test 0.0; // (i == 2) would just retest MIN_VALUE // create a bit mask with (i-1) 1's in the low order long mask = ~((~
0L)<<(i-
1));
/* ******************** nextAfter tests ****************************** */ * Each row of the testCases matrix represents one test case * for nexAfter; given the input of the first two columns, the * result in the last column is expected. // Make sure zero behavior is tested * Each row of the testCases matrix represents one test case * for nexAfter; given the input of the first two columns, the * result in the last column is expected. // Make sure zero behavior is tested /* ******************** nextUp tests ********************************* */ * Each row of testCases represents one test case for nextUp; * the first column is the input and the second column is the * Each row of testCases represents one test case for nextUp; * the first column is the input and the second column is the /* ******************** nextDown tests ********************************* */ * Each row of testCases represents one test case for nextDown; * the first column is the input and the second column is the * Each row of testCases represents one test case for nextDown; * the first column is the input and the second column is the /* ********************** boolean tests ****************************** */ * Combined tests for boolean functions, isFinite, isInfinite, /* ******************** copySign tests******************************** */ // testCases[0] are logically positive numbers; // testCases[1] are negative numbers. // Tests shared between raw and non-raw versions for(
int i =
0; i <
2; i++) {
for(
int j =
0; j <
2; j++) {
// copySign(magnitude, sign) // For rawCopySign, NaN may effectively have either sign bit // while for copySign NaNs are treated as if they always have // a zero sign bit (i.e. as positive numbers) for(
int i =
0; i <
2; i++) {
// copySign(magnitude, sign) // testCases[0] are logically positive numbers; // testCases[1] are negative numbers. // Tests shared between Math and StrictMath versions for(
int i =
0; i <
2; i++) {
for(
int j =
0; j <
2; j++) {
// copySign(magnitude, sign) // For Math.copySign, NaN may effectively have either sign bit // while for StrictMath.copySign NaNs are treated as if they // always have a zero sign bit (i.e. as positive numbers) for(
int i =
0; i <
2; i++) {
// copySign(magnitude, sign) /* ************************ scalb tests ******************************* */ // Arguments x, where scalb(x,n) is x for any n. // Test cases where scaling is always a no-op // Test cases where result is 0.0 or infinity due to magnitude // Test cases that could be done with one floating-point // Scale-up subnormal values until they all overflow float scale =
1.0f;
// 2^j // calculate right answer // Scale down a large number until it underflows. By scaling // down MAX_NORMALmm, the first subnormal result will be exact // but the next one will round -- all those results can be // checked by halving a separate value in the loop. Actually, // we can keep halving and checking until the product is zero // 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact // 2. When rounding first occurs in the expected product, it // too rounds up, to 2^-MAX_EXPONENT. // Halving expected after rounding happends to give the same // result as the scalb operation. // Tricky rounding tests: // Scale down a large number into subnormal range such that if // scalb is being implemented with multiple floating-point // multiplies, the value would round twice if the multiplies // were done in the wrong order. for(
int i =
0; i <
129; i++) {
// Arguments x, where scalb(x,n) is x for any n. // Test cases where scaling is always a no-op // Test cases where result is 0.0 or infinity due to magnitude // Test cases that could be done with one floating-point // Scale-up subnormal values until they all overflow double scale =
1.0;
// 2^j // calculate right answer // Scale down a large number until it underflows. By scaling // down MAX_NORMALmm, the first subnormal result will be exact // but the next one will round -- all those results can be // checked by halving a separate value in the loop. Actually, // we can keep halving and checking until the product is zero // 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact // 2. When rounding first occurs in the expected product, it // too rounds up, to 2^-MAX_EXPONENT. // Halving expected after rounding happends to give the same // result as the scalb operation. // Tricky rounding tests: // Scale down a large number into subnormal range such that if // scalb is being implemented with multiple floating-point // multiplies, the value would round twice if the multiplies // were done in the wrong order. double value =
0x1.
000000000000bP-
1;
/* ************************* ulp tests ******************************* */ * Test Math.ulp and StrictMath.ulp with +d and -d. // Generate some random bit patterns for the significand for(
int j =
0; j <
10; j++) {
* Start with MIN_VALUE, left shift, test high value, low * values, and random in between. * Use nextAfter to calculate, high value of previous binade, * loop count i will indicate how many random bits, if any are // Test largest value in next smaller binade if (i >=
3) {
// (i == 1) would test 0.0; // (i == 2) would just retest MIN_VALUE // create a bit mask with (i-1) 1's in the low order int mask = ~((~
0)<<(i-
1));
// Generate some random bit patterns for the significand for(
int j =
0; j <
10; j++) {
* Start with MIN_VALUE, left shift, test high value, low * values, and random in between. * Use nextAfter to calculate, high value of previous binade, * loop count i will indicate how many random bits, if any are // Test largest value in next smaller binade if (i >=
3) {
// (i == 1) would test 0.0; // (i == 2) would just retest MIN_VALUE // create a bit mask with (i-1) 1's in the low order int mask = ~((~
0)<<(i-
1));