test/java/lang/Math/IeeeRecommendedTests.java

Print this page




 160             float po2 = powerOfTwoF(i);
 161 
 162             failures += testGetExponentCase(po2, i);
 163 
 164             // Generate some random bit patterns for the significand
 165             for(int j = 0; j < 10; j++) {
 166                 int randSignif = rand.nextInt();
 167                 float randFloat;
 168 
 169                 randFloat = Float.intBitsToFloat( // Exponent
 170                                                  (Float.floatToIntBits(po2)&
 171                                                   (~FloatConsts.SIGNIF_BIT_MASK)) |
 172                                                  // Significand
 173                                                  (randSignif &
 174                                                   FloatConsts.SIGNIF_BIT_MASK) );
 175 
 176                 failures += testGetExponentCase(randFloat, i);
 177             }
 178 
 179             if (i > FloatConsts.MIN_EXPONENT) {
 180                 float po2minus = FpUtils.nextAfter(po2,
 181                                                  Float.NEGATIVE_INFINITY);
 182                 failures += testGetExponentCase(po2minus, i-1);
 183             }
 184         }
 185 
 186         // Subnormal exponent tests
 187 
 188         /*
 189          * Start with MIN_VALUE, left shift, test high value, low
 190          * values, and random in between.
 191          *
 192          * Use nextAfter to calculate, high value of previous binade,
 193          * loop count i will indicate how many random bits, if any are
 194          * needed.
 195          */
 196 
 197         float top=Float.MIN_VALUE;
 198         for( int i = 1;
 199             i < FloatConsts.SIGNIFICAND_WIDTH;
 200             i++, top *= 2.0f) {
 201 
 202             failures += testGetExponentCase(top,
 203                                             FloatConsts.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(FpUtils.nextAfter(top, 0.0f),
 209                                     FloatConsts.MIN_EXPONENT - 1);
 210 
 211                 if( i >= 10) {
 212                     // create a bit mask with (i-1) 1's in the low order
 213                     // bits
 214                     int mask = ~((~0)<<(i-1));
 215                     float randFloat = Float.intBitsToFloat( // Exponent
 216                                                  Float.floatToIntBits(top) |
 217                                                  // Significand
 218                                                  (rand.nextInt() & mask ) ) ;
 219 
 220                     failures += testGetExponentCase(randFloat,
 221                                                     FloatConsts.MIN_EXPONENT - 1);
 222                 }
 223             }
 224         }
 225 
 226         return failures;
 227     }
 228 


 267             double po2 = powerOfTwoD(i);
 268 
 269             failures += testGetExponentCase(po2, i);
 270 
 271             // Generate some random bit patterns for the significand
 272             for(int j = 0; j < 10; j++) {
 273                 long randSignif = rand.nextLong();
 274                 double randFloat;
 275 
 276                 randFloat = Double.longBitsToDouble( // Exponent
 277                                                  (Double.doubleToLongBits(po2)&
 278                                                   (~DoubleConsts.SIGNIF_BIT_MASK)) |
 279                                                  // Significand
 280                                                  (randSignif &
 281                                                   DoubleConsts.SIGNIF_BIT_MASK) );
 282 
 283                 failures += testGetExponentCase(randFloat, i);
 284             }
 285 
 286             if (i > DoubleConsts.MIN_EXPONENT) {
 287                 double po2minus = FpUtils.nextAfter(po2,
 288                                                     Double.NEGATIVE_INFINITY);
 289                 failures += testGetExponentCase(po2minus, i-1);
 290             }
 291         }
 292 
 293         // Subnormal exponent tests
 294 
 295         /*
 296          * Start with MIN_VALUE, left shift, test high value, low
 297          * values, and random in between.
 298          *
 299          * Use nextAfter to calculate, high value of previous binade;
 300          * loop count i will indicate how many random bits, if any are
 301          * needed.
 302          */
 303 
 304         double top=Double.MIN_VALUE;
 305         for( int i = 1;
 306             i < DoubleConsts.SIGNIFICAND_WIDTH;
 307             i++, top *= 2.0f) {
 308 
 309             failures += testGetExponentCase(top,
 310                                             DoubleConsts.MIN_EXPONENT - 1);
 311 
 312             // Test largest value in next smaller binade
 313             if (i >= 3) {// (i == 1) would test 0.0;
 314                          // (i == 2) would just retest MIN_VALUE
 315                 testGetExponentCase(FpUtils.nextAfter(top, 0.0),
 316                                     DoubleConsts.MIN_EXPONENT - 1);
 317 
 318                 if( i >= 10) {
 319                     // create a bit mask with (i-1) 1's in the low order
 320                     // bits
 321                     long mask = ~((~0L)<<(i-1));
 322                     double randFloat = Double.longBitsToDouble( // Exponent
 323                                                  Double.doubleToLongBits(top) |
 324                                                  // Significand
 325                                                  (rand.nextLong() & mask ) ) ;
 326 
 327                     failures += testGetExponentCase(randFloat,
 328                                                     DoubleConsts.MIN_EXPONENT - 1);
 329                 }
 330             }
 331         }
 332 
 333         return failures;
 334     }
 335 


1044         };
1045 
1046         // Test cases where scaling is always a no-op
1047         for(int i=0; i < identityTestCases.length; i++) {
1048             for(int j=0; j < manyScalingFactors.length; j++) {
1049                 failures += testScalbCase(identityTestCases[i],
1050                                           manyScalingFactors[j],
1051                                           identityTestCases[i]);
1052             }
1053         }
1054 
1055         // Test cases where result is 0.0 or infinity due to magnitude
1056         // of the scaling factor
1057         for(int i=0; i < someTestCases.length; i++) {
1058             for(int j=0; j < manyScalingFactors.length; j++) {
1059                 int scaleFactor = manyScalingFactors[j];
1060                 if (Math.abs(scaleFactor) >= MAX_SCALE) {
1061                     float value = someTestCases[i];
1062                     failures+=testScalbCase(value,
1063                                             scaleFactor,
1064                                             FpUtils.copySign( (scaleFactor>0?infinityF:0.0f), value) );
1065                 }
1066             }
1067         }
1068 
1069         // Test cases that could be done with one floating-point
1070         // multiply.
1071         for(int i=0; i < someTestCases.length; i++) {
1072             for(int j=0; j < oneMultiplyScalingFactors.length; j++) {
1073                 int scaleFactor = oneMultiplyScalingFactors[j];
1074                     float value = someTestCases[i];
1075 
1076                     failures+=testScalbCase(value,
1077                                             scaleFactor,
1078                                             value*powerOfTwoF(scaleFactor));
1079             }
1080         }
1081 
1082         // Create 2^MAX_EXPONENT
1083         float twoToTheMaxExp = 1.0f; // 2^0
1084         for(int i = 0; i < FloatConsts.MAX_EXPONENT; i++)
1085             twoToTheMaxExp *=2.0f;
1086 
1087         // Scale-up subnormal values until they all overflow
1088         for(int i=0; i < subnormalTestCases.length; i++) {
1089             float scale = 1.0f; // 2^j
1090             float value = subnormalTestCases[i];
1091 
1092             for(int j=FloatConsts.MAX_EXPONENT*2; j < MAX_SCALE; j++) { // MAX_SCALE -1 should cause overflow
1093                 int scaleFactor = j;
1094 
1095                 failures+=testScalbCase(value,
1096                                         scaleFactor,
1097                                         (FpUtils.ilogb(value) +j > FloatConsts.MAX_EXPONENT ) ?
1098                                         FpUtils.copySign(infinityF, value) : // overflow
1099                                         // calculate right answer
1100                                         twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) );
1101                 scale*=2.0f;
1102             }
1103         }
1104 
1105         // Scale down a large number until it underflows.  By scaling
1106         // down MAX_NORMALmm, the first subnormal result will be exact
1107         // but the next one will round -- all those results can be
1108         // checked by halving a separate value in the loop.  Actually,
1109         // we can keep halving and checking until the product is zero
1110         // since:
1111         //
1112         // 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact
1113         // it will round *up*
1114         //
1115         // 2. When rounding first occurs in the expected product, it
1116         // too rounds up, to 2^-MAX_EXPONENT.
1117         //
1118         // Halving expected after rounding happends to give the same


1251         };
1252 
1253         // Test cases where scaling is always a no-op
1254         for(int i=0; i < identityTestCases.length; i++) {
1255             for(int j=0; j < manyScalingFactors.length; j++) {
1256                 failures += testScalbCase(identityTestCases[i],
1257                                           manyScalingFactors[j],
1258                                           identityTestCases[i]);
1259             }
1260         }
1261 
1262         // Test cases where result is 0.0 or infinity due to magnitude
1263         // of the scaling factor
1264         for(int i=0; i < someTestCases.length; i++) {
1265             for(int j=0; j < manyScalingFactors.length; j++) {
1266                 int scaleFactor = manyScalingFactors[j];
1267                 if (Math.abs(scaleFactor) >= MAX_SCALE) {
1268                     double value = someTestCases[i];
1269                     failures+=testScalbCase(value,
1270                                             scaleFactor,
1271                                             FpUtils.copySign( (scaleFactor>0?infinityD:0.0), value) );
1272                 }
1273             }
1274         }
1275 
1276         // Test cases that could be done with one floating-point
1277         // multiply.
1278         for(int i=0; i < someTestCases.length; i++) {
1279             for(int j=0; j < oneMultiplyScalingFactors.length; j++) {
1280                 int scaleFactor = oneMultiplyScalingFactors[j];
1281                     double value = someTestCases[i];
1282 
1283                     failures+=testScalbCase(value,
1284                                             scaleFactor,
1285                                             value*powerOfTwoD(scaleFactor));
1286             }
1287         }
1288 
1289         // Create 2^MAX_EXPONENT
1290         double twoToTheMaxExp = 1.0; // 2^0
1291         for(int i = 0; i < DoubleConsts.MAX_EXPONENT; i++)
1292             twoToTheMaxExp *=2.0;
1293 
1294         // Scale-up subnormal values until they all overflow
1295         for(int i=0; i < subnormalTestCases.length; i++) {
1296             double scale = 1.0; // 2^j
1297             double value = subnormalTestCases[i];
1298 
1299             for(int j=DoubleConsts.MAX_EXPONENT*2; j < MAX_SCALE; j++) { // MAX_SCALE -1 should cause overflow
1300                 int scaleFactor = j;
1301 
1302                 failures+=testScalbCase(value,
1303                                         scaleFactor,
1304                                         (FpUtils.ilogb(value) +j > DoubleConsts.MAX_EXPONENT ) ?
1305                                         FpUtils.copySign(infinityD, value) : // overflow
1306                                         // calculate right answer
1307                                         twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) );
1308                 scale*=2.0;
1309             }
1310         }
1311 
1312         // Scale down a large number until it underflows.  By scaling
1313         // down MAX_NORMALmm, the first subnormal result will be exact
1314         // but the next one will round -- all those results can be
1315         // checked by halving a separate value in the loop.  Actually,
1316         // we can keep halving and checking until the product is zero
1317         // since:
1318         //
1319         // 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact
1320         // it will round *up*
1321         //
1322         // 2. When rounding first occurs in the expected product, it
1323         // too rounds up, to 2^-MAX_EXPONENT.
1324         //
1325         // Halving expected after rounding happends to give the same


1406                                    powerOfTwoF(-22),
1407                                    powerOfTwoF(-19),
1408                                    Float.MIN_VALUE,
1409                                    Float.MIN_VALUE,
1410                                    Float.MIN_VALUE,
1411                                    powerOfTwoF(104)
1412         };
1413 
1414         // Special value tests
1415         for(int i = 0; i < specialValues.length; i++) {
1416             failures += testUlpCase(specialValues[i], specialResults[i]);
1417         }
1418 
1419 
1420         // Normal exponent tests
1421         for(int i = FloatConsts.MIN_EXPONENT; i <= FloatConsts.MAX_EXPONENT; i++) {
1422             float expected;
1423 
1424             // Create power of two
1425             float po2 = powerOfTwoF(i);
1426             expected = FpUtils.scalb(1.0f, i - (FloatConsts.SIGNIFICAND_WIDTH-1));
1427 
1428             failures += testUlpCase(po2, expected);
1429 
1430             // Generate some random bit patterns for the significand
1431             for(int j = 0; j < 10; j++) {
1432                 int randSignif = rand.nextInt();
1433                 float randFloat;
1434 
1435                 randFloat = Float.intBitsToFloat( // Exponent
1436                                                  (Float.floatToIntBits(po2)&
1437                                                   (~FloatConsts.SIGNIF_BIT_MASK)) |
1438                                                  // Significand
1439                                                  (randSignif &
1440                                                   FloatConsts.SIGNIF_BIT_MASK) );
1441 
1442                 failures += testUlpCase(randFloat, expected);
1443             }
1444 
1445             if (i > FloatConsts.MIN_EXPONENT) {
1446                 float po2minus = FpUtils.nextAfter(po2,
1447                                                    Float.NEGATIVE_INFINITY);
1448                 failures += testUlpCase(po2minus, expected/2.0f);
1449             }
1450         }
1451 
1452         // Subnormal tests
1453 
1454         /*
1455          * Start with MIN_VALUE, left shift, test high value, low
1456          * values, and random in between.
1457          *
1458          * Use nextAfter to calculate, high value of previous binade,
1459          * loop count i will indicate how many random bits, if any are
1460          * needed.
1461          */
1462 
1463         float top=Float.MIN_VALUE;
1464         for( int i = 1;
1465             i < FloatConsts.SIGNIFICAND_WIDTH;
1466             i++, top *= 2.0f) {
1467 
1468             failures += testUlpCase(top, Float.MIN_VALUE);
1469 
1470             // Test largest value in next smaller binade
1471             if (i >= 3) {// (i == 1) would test 0.0;
1472                          // (i == 2) would just retest MIN_VALUE
1473                 testUlpCase(FpUtils.nextAfter(top, 0.0f),
1474                             Float.MIN_VALUE);
1475 
1476                 if( i >= 10) {
1477                     // create a bit mask with (i-1) 1's in the low order
1478                     // bits
1479                     int mask = ~((~0)<<(i-1));
1480                     float randFloat = Float.intBitsToFloat( // Exponent
1481                                                  Float.floatToIntBits(top) |
1482                                                  // Significand
1483                                                  (rand.nextInt() & mask ) ) ;
1484 
1485                     failures += testUlpCase(randFloat, Float.MIN_VALUE);
1486                 }
1487             }
1488         }
1489 
1490         return failures;
1491     }
1492 
1493     public static int testDoubleUlp() {


1511                                    powerOfTwoD(-51),
1512                                    powerOfTwoD(-48),
1513                                    Double.MIN_VALUE,
1514                                    Double.MIN_VALUE,
1515                                    Double.MIN_VALUE,
1516                                    powerOfTwoD(971)
1517         };
1518 
1519         // Special value tests
1520         for(int i = 0; i < specialValues.length; i++) {
1521             failures += testUlpCase(specialValues[i], specialResults[i]);
1522         }
1523 
1524 
1525         // Normal exponent tests
1526         for(int i = DoubleConsts.MIN_EXPONENT; i <= DoubleConsts.MAX_EXPONENT; i++) {
1527             double expected;
1528 
1529             // Create power of two
1530             double po2 = powerOfTwoD(i);
1531             expected = FpUtils.scalb(1.0, i - (DoubleConsts.SIGNIFICAND_WIDTH-1));
1532 
1533             failures += testUlpCase(po2, expected);
1534 
1535             // Generate some random bit patterns for the significand
1536             for(int j = 0; j < 10; j++) {
1537                 long randSignif = rand.nextLong();
1538                 double randDouble;
1539 
1540                 randDouble = Double.longBitsToDouble( // Exponent
1541                                                  (Double.doubleToLongBits(po2)&
1542                                                   (~DoubleConsts.SIGNIF_BIT_MASK)) |
1543                                                  // Significand
1544                                                  (randSignif &
1545                                                   DoubleConsts.SIGNIF_BIT_MASK) );
1546 
1547                 failures += testUlpCase(randDouble, expected);
1548             }
1549 
1550             if (i > DoubleConsts.MIN_EXPONENT) {
1551                 double po2minus = FpUtils.nextAfter(po2,
1552                                                     Double.NEGATIVE_INFINITY);
1553                 failures += testUlpCase(po2minus, expected/2.0f);
1554             }
1555         }
1556 
1557         // Subnormal tests
1558 
1559         /*
1560          * Start with MIN_VALUE, left shift, test high value, low
1561          * values, and random in between.
1562          *
1563          * Use nextAfter to calculate, high value of previous binade,
1564          * loop count i will indicate how many random bits, if any are
1565          * needed.
1566          */
1567 
1568         double top=Double.MIN_VALUE;
1569         for( int i = 1;
1570             i < DoubleConsts.SIGNIFICAND_WIDTH;
1571             i++, top *= 2.0f) {
1572 
1573             failures += testUlpCase(top, Double.MIN_VALUE);
1574 
1575             // Test largest value in next smaller binade
1576             if (i >= 3) {// (i == 1) would test 0.0;
1577                          // (i == 2) would just retest MIN_VALUE
1578                 testUlpCase(FpUtils.nextAfter(top, 0.0f),
1579                             Double.MIN_VALUE);
1580 
1581                 if( i >= 10) {
1582                     // create a bit mask with (i-1) 1's in the low order
1583                     // bits
1584                     int mask = ~((~0)<<(i-1));
1585                     double randDouble = Double.longBitsToDouble( // Exponent
1586                                                  Double.doubleToLongBits(top) |
1587                                                  // Significand
1588                                                  (rand.nextLong() & mask ) ) ;
1589 
1590                     failures += testUlpCase(randDouble, Double.MIN_VALUE);
1591                 }
1592             }
1593         }
1594 
1595         return failures;
1596     }
1597 
1598     public static int testFloatSignum() {




 160             float po2 = powerOfTwoF(i);
 161 
 162             failures += testGetExponentCase(po2, i);
 163 
 164             // Generate some random bit patterns for the significand
 165             for(int j = 0; j < 10; j++) {
 166                 int randSignif = rand.nextInt();
 167                 float randFloat;
 168 
 169                 randFloat = Float.intBitsToFloat( // Exponent
 170                                                  (Float.floatToIntBits(po2)&
 171                                                   (~FloatConsts.SIGNIF_BIT_MASK)) |
 172                                                  // Significand
 173                                                  (randSignif &
 174                                                   FloatConsts.SIGNIF_BIT_MASK) );
 175 
 176                 failures += testGetExponentCase(randFloat, i);
 177             }
 178 
 179             if (i > FloatConsts.MIN_EXPONENT) {
 180                 float po2minus = Math.nextAfter(po2,
 181                                                  Float.NEGATIVE_INFINITY);
 182                 failures += testGetExponentCase(po2minus, i-1);
 183             }
 184         }
 185 
 186         // Subnormal exponent tests
 187 
 188         /*
 189          * Start with MIN_VALUE, left shift, test high value, low
 190          * values, and random in between.
 191          *
 192          * Use nextAfter to calculate, high value of previous binade,
 193          * loop count i will indicate how many random bits, if any are
 194          * needed.
 195          */
 196 
 197         float top=Float.MIN_VALUE;
 198         for( int i = 1;
 199             i < FloatConsts.SIGNIFICAND_WIDTH;
 200             i++, top *= 2.0f) {
 201 
 202             failures += testGetExponentCase(top,
 203                                             FloatConsts.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),
 209                                     FloatConsts.MIN_EXPONENT - 1);
 210 
 211                 if( i >= 10) {
 212                     // create a bit mask with (i-1) 1's in the low order
 213                     // bits
 214                     int mask = ~((~0)<<(i-1));
 215                     float randFloat = Float.intBitsToFloat( // Exponent
 216                                                  Float.floatToIntBits(top) |
 217                                                  // Significand
 218                                                  (rand.nextInt() & mask ) ) ;
 219 
 220                     failures += testGetExponentCase(randFloat,
 221                                                     FloatConsts.MIN_EXPONENT - 1);
 222                 }
 223             }
 224         }
 225 
 226         return failures;
 227     }
 228 


 267             double po2 = powerOfTwoD(i);
 268 
 269             failures += testGetExponentCase(po2, i);
 270 
 271             // Generate some random bit patterns for the significand
 272             for(int j = 0; j < 10; j++) {
 273                 long randSignif = rand.nextLong();
 274                 double randFloat;
 275 
 276                 randFloat = Double.longBitsToDouble( // Exponent
 277                                                  (Double.doubleToLongBits(po2)&
 278                                                   (~DoubleConsts.SIGNIF_BIT_MASK)) |
 279                                                  // Significand
 280                                                  (randSignif &
 281                                                   DoubleConsts.SIGNIF_BIT_MASK) );
 282 
 283                 failures += testGetExponentCase(randFloat, i);
 284             }
 285 
 286             if (i > DoubleConsts.MIN_EXPONENT) {
 287                 double po2minus = Math.nextAfter(po2,
 288                                                     Double.NEGATIVE_INFINITY);
 289                 failures += testGetExponentCase(po2minus, i-1);
 290             }
 291         }
 292 
 293         // Subnormal exponent tests
 294 
 295         /*
 296          * Start with MIN_VALUE, left shift, test high value, low
 297          * values, and random in between.
 298          *
 299          * Use nextAfter to calculate, high value of previous binade;
 300          * loop count i will indicate how many random bits, if any are
 301          * needed.
 302          */
 303 
 304         double top=Double.MIN_VALUE;
 305         for( int i = 1;
 306             i < DoubleConsts.SIGNIFICAND_WIDTH;
 307             i++, top *= 2.0f) {
 308 
 309             failures += testGetExponentCase(top,
 310                                             DoubleConsts.MIN_EXPONENT - 1);
 311 
 312             // Test largest value in next smaller binade
 313             if (i >= 3) {// (i == 1) would test 0.0;
 314                          // (i == 2) would just retest MIN_VALUE
 315                 testGetExponentCase(Math.nextAfter(top, 0.0),
 316                                     DoubleConsts.MIN_EXPONENT - 1);
 317 
 318                 if( i >= 10) {
 319                     // create a bit mask with (i-1) 1's in the low order
 320                     // bits
 321                     long mask = ~((~0L)<<(i-1));
 322                     double randFloat = Double.longBitsToDouble( // Exponent
 323                                                  Double.doubleToLongBits(top) |
 324                                                  // Significand
 325                                                  (rand.nextLong() & mask ) ) ;
 326 
 327                     failures += testGetExponentCase(randFloat,
 328                                                     DoubleConsts.MIN_EXPONENT - 1);
 329                 }
 330             }
 331         }
 332 
 333         return failures;
 334     }
 335 


1044         };
1045 
1046         // Test cases where scaling is always a no-op
1047         for(int i=0; i < identityTestCases.length; i++) {
1048             for(int j=0; j < manyScalingFactors.length; j++) {
1049                 failures += testScalbCase(identityTestCases[i],
1050                                           manyScalingFactors[j],
1051                                           identityTestCases[i]);
1052             }
1053         }
1054 
1055         // Test cases where result is 0.0 or infinity due to magnitude
1056         // of the scaling factor
1057         for(int i=0; i < someTestCases.length; i++) {
1058             for(int j=0; j < manyScalingFactors.length; j++) {
1059                 int scaleFactor = manyScalingFactors[j];
1060                 if (Math.abs(scaleFactor) >= MAX_SCALE) {
1061                     float value = someTestCases[i];
1062                     failures+=testScalbCase(value,
1063                                             scaleFactor,
1064                                             Math.copySign( (scaleFactor>0?infinityF:0.0f), value) );
1065                 }
1066             }
1067         }
1068 
1069         // Test cases that could be done with one floating-point
1070         // multiply.
1071         for(int i=0; i < someTestCases.length; i++) {
1072             for(int j=0; j < oneMultiplyScalingFactors.length; j++) {
1073                 int scaleFactor = oneMultiplyScalingFactors[j];
1074                     float value = someTestCases[i];
1075 
1076                     failures+=testScalbCase(value,
1077                                             scaleFactor,
1078                                             value*powerOfTwoF(scaleFactor));
1079             }
1080         }
1081 
1082         // Create 2^MAX_EXPONENT
1083         float twoToTheMaxExp = 1.0f; // 2^0
1084         for(int i = 0; i < FloatConsts.MAX_EXPONENT; i++)
1085             twoToTheMaxExp *=2.0f;
1086 
1087         // Scale-up subnormal values until they all overflow
1088         for(int i=0; i < subnormalTestCases.length; i++) {
1089             float scale = 1.0f; // 2^j
1090             float value = subnormalTestCases[i];
1091 
1092             for(int j=FloatConsts.MAX_EXPONENT*2; j < MAX_SCALE; j++) { // MAX_SCALE -1 should cause overflow
1093                 int scaleFactor = j;
1094 
1095                 failures+=testScalbCase(value,
1096                                         scaleFactor,
1097                                         (FpUtils.ilogb(value) +j > FloatConsts.MAX_EXPONENT ) ?
1098                                         Math.copySign(infinityF, value) : // overflow
1099                                         // calculate right answer
1100                                         twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) );
1101                 scale*=2.0f;
1102             }
1103         }
1104 
1105         // Scale down a large number until it underflows.  By scaling
1106         // down MAX_NORMALmm, the first subnormal result will be exact
1107         // but the next one will round -- all those results can be
1108         // checked by halving a separate value in the loop.  Actually,
1109         // we can keep halving and checking until the product is zero
1110         // since:
1111         //
1112         // 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact
1113         // it will round *up*
1114         //
1115         // 2. When rounding first occurs in the expected product, it
1116         // too rounds up, to 2^-MAX_EXPONENT.
1117         //
1118         // Halving expected after rounding happends to give the same


1251         };
1252 
1253         // Test cases where scaling is always a no-op
1254         for(int i=0; i < identityTestCases.length; i++) {
1255             for(int j=0; j < manyScalingFactors.length; j++) {
1256                 failures += testScalbCase(identityTestCases[i],
1257                                           manyScalingFactors[j],
1258                                           identityTestCases[i]);
1259             }
1260         }
1261 
1262         // Test cases where result is 0.0 or infinity due to magnitude
1263         // of the scaling factor
1264         for(int i=0; i < someTestCases.length; i++) {
1265             for(int j=0; j < manyScalingFactors.length; j++) {
1266                 int scaleFactor = manyScalingFactors[j];
1267                 if (Math.abs(scaleFactor) >= MAX_SCALE) {
1268                     double value = someTestCases[i];
1269                     failures+=testScalbCase(value,
1270                                             scaleFactor,
1271                                             Math.copySign( (scaleFactor>0?infinityD:0.0), value) );
1272                 }
1273             }
1274         }
1275 
1276         // Test cases that could be done with one floating-point
1277         // multiply.
1278         for(int i=0; i < someTestCases.length; i++) {
1279             for(int j=0; j < oneMultiplyScalingFactors.length; j++) {
1280                 int scaleFactor = oneMultiplyScalingFactors[j];
1281                     double value = someTestCases[i];
1282 
1283                     failures+=testScalbCase(value,
1284                                             scaleFactor,
1285                                             value*powerOfTwoD(scaleFactor));
1286             }
1287         }
1288 
1289         // Create 2^MAX_EXPONENT
1290         double twoToTheMaxExp = 1.0; // 2^0
1291         for(int i = 0; i < DoubleConsts.MAX_EXPONENT; i++)
1292             twoToTheMaxExp *=2.0;
1293 
1294         // Scale-up subnormal values until they all overflow
1295         for(int i=0; i < subnormalTestCases.length; i++) {
1296             double scale = 1.0; // 2^j
1297             double value = subnormalTestCases[i];
1298 
1299             for(int j=DoubleConsts.MAX_EXPONENT*2; j < MAX_SCALE; j++) { // MAX_SCALE -1 should cause overflow
1300                 int scaleFactor = j;
1301 
1302                 failures+=testScalbCase(value,
1303                                         scaleFactor,
1304                                         (FpUtils.ilogb(value) +j > DoubleConsts.MAX_EXPONENT ) ?
1305                                         Math.copySign(infinityD, value) : // overflow
1306                                         // calculate right answer
1307                                         twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) );
1308                 scale*=2.0;
1309             }
1310         }
1311 
1312         // Scale down a large number until it underflows.  By scaling
1313         // down MAX_NORMALmm, the first subnormal result will be exact
1314         // but the next one will round -- all those results can be
1315         // checked by halving a separate value in the loop.  Actually,
1316         // we can keep halving and checking until the product is zero
1317         // since:
1318         //
1319         // 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact
1320         // it will round *up*
1321         //
1322         // 2. When rounding first occurs in the expected product, it
1323         // too rounds up, to 2^-MAX_EXPONENT.
1324         //
1325         // Halving expected after rounding happends to give the same


1406                                    powerOfTwoF(-22),
1407                                    powerOfTwoF(-19),
1408                                    Float.MIN_VALUE,
1409                                    Float.MIN_VALUE,
1410                                    Float.MIN_VALUE,
1411                                    powerOfTwoF(104)
1412         };
1413 
1414         // Special value tests
1415         for(int i = 0; i < specialValues.length; i++) {
1416             failures += testUlpCase(specialValues[i], specialResults[i]);
1417         }
1418 
1419 
1420         // Normal exponent tests
1421         for(int i = FloatConsts.MIN_EXPONENT; i <= FloatConsts.MAX_EXPONENT; i++) {
1422             float expected;
1423 
1424             // Create power of two
1425             float po2 = powerOfTwoF(i);
1426             expected = Math.scalb(1.0f, i - (FloatConsts.SIGNIFICAND_WIDTH-1));
1427 
1428             failures += testUlpCase(po2, expected);
1429 
1430             // Generate some random bit patterns for the significand
1431             for(int j = 0; j < 10; j++) {
1432                 int randSignif = rand.nextInt();
1433                 float randFloat;
1434 
1435                 randFloat = Float.intBitsToFloat( // Exponent
1436                                                  (Float.floatToIntBits(po2)&
1437                                                   (~FloatConsts.SIGNIF_BIT_MASK)) |
1438                                                  // Significand
1439                                                  (randSignif &
1440                                                   FloatConsts.SIGNIF_BIT_MASK) );
1441 
1442                 failures += testUlpCase(randFloat, expected);
1443             }
1444 
1445             if (i > FloatConsts.MIN_EXPONENT) {
1446                 float po2minus = Math.nextAfter(po2,
1447                                                    Float.NEGATIVE_INFINITY);
1448                 failures += testUlpCase(po2minus, expected/2.0f);
1449             }
1450         }
1451 
1452         // Subnormal tests
1453 
1454         /*
1455          * Start with MIN_VALUE, left shift, test high value, low
1456          * values, and random in between.
1457          *
1458          * Use nextAfter to calculate, high value of previous binade,
1459          * loop count i will indicate how many random bits, if any are
1460          * needed.
1461          */
1462 
1463         float top=Float.MIN_VALUE;
1464         for( int i = 1;
1465             i < FloatConsts.SIGNIFICAND_WIDTH;
1466             i++, top *= 2.0f) {
1467 
1468             failures += testUlpCase(top, Float.MIN_VALUE);
1469 
1470             // Test largest value in next smaller binade
1471             if (i >= 3) {// (i == 1) would test 0.0;
1472                          // (i == 2) would just retest MIN_VALUE
1473                 testUlpCase(Math.nextAfter(top, 0.0f),
1474                             Float.MIN_VALUE);
1475 
1476                 if( i >= 10) {
1477                     // create a bit mask with (i-1) 1's in the low order
1478                     // bits
1479                     int mask = ~((~0)<<(i-1));
1480                     float randFloat = Float.intBitsToFloat( // Exponent
1481                                                  Float.floatToIntBits(top) |
1482                                                  // Significand
1483                                                  (rand.nextInt() & mask ) ) ;
1484 
1485                     failures += testUlpCase(randFloat, Float.MIN_VALUE);
1486                 }
1487             }
1488         }
1489 
1490         return failures;
1491     }
1492 
1493     public static int testDoubleUlp() {


1511                                    powerOfTwoD(-51),
1512                                    powerOfTwoD(-48),
1513                                    Double.MIN_VALUE,
1514                                    Double.MIN_VALUE,
1515                                    Double.MIN_VALUE,
1516                                    powerOfTwoD(971)
1517         };
1518 
1519         // Special value tests
1520         for(int i = 0; i < specialValues.length; i++) {
1521             failures += testUlpCase(specialValues[i], specialResults[i]);
1522         }
1523 
1524 
1525         // Normal exponent tests
1526         for(int i = DoubleConsts.MIN_EXPONENT; i <= DoubleConsts.MAX_EXPONENT; i++) {
1527             double expected;
1528 
1529             // Create power of two
1530             double po2 = powerOfTwoD(i);
1531             expected = Math.scalb(1.0, i - (DoubleConsts.SIGNIFICAND_WIDTH-1));
1532 
1533             failures += testUlpCase(po2, expected);
1534 
1535             // Generate some random bit patterns for the significand
1536             for(int j = 0; j < 10; j++) {
1537                 long randSignif = rand.nextLong();
1538                 double randDouble;
1539 
1540                 randDouble = Double.longBitsToDouble( // Exponent
1541                                                  (Double.doubleToLongBits(po2)&
1542                                                   (~DoubleConsts.SIGNIF_BIT_MASK)) |
1543                                                  // Significand
1544                                                  (randSignif &
1545                                                   DoubleConsts.SIGNIF_BIT_MASK) );
1546 
1547                 failures += testUlpCase(randDouble, expected);
1548             }
1549 
1550             if (i > DoubleConsts.MIN_EXPONENT) {
1551                 double po2minus = Math.nextAfter(po2,
1552                                                     Double.NEGATIVE_INFINITY);
1553                 failures += testUlpCase(po2minus, expected/2.0f);
1554             }
1555         }
1556 
1557         // Subnormal tests
1558 
1559         /*
1560          * Start with MIN_VALUE, left shift, test high value, low
1561          * values, and random in between.
1562          *
1563          * Use nextAfter to calculate, high value of previous binade,
1564          * loop count i will indicate how many random bits, if any are
1565          * needed.
1566          */
1567 
1568         double top=Double.MIN_VALUE;
1569         for( int i = 1;
1570             i < DoubleConsts.SIGNIFICAND_WIDTH;
1571             i++, top *= 2.0f) {
1572 
1573             failures += testUlpCase(top, Double.MIN_VALUE);
1574 
1575             // Test largest value in next smaller binade
1576             if (i >= 3) {// (i == 1) would test 0.0;
1577                          // (i == 2) would just retest MIN_VALUE
1578                 testUlpCase(Math.nextAfter(top, 0.0f),
1579                             Double.MIN_VALUE);
1580 
1581                 if( i >= 10) {
1582                     // create a bit mask with (i-1) 1's in the low order
1583                     // bits
1584                     int mask = ~((~0)<<(i-1));
1585                     double randDouble = Double.longBitsToDouble( // Exponent
1586                                                  Double.doubleToLongBits(top) |
1587                                                  // Significand
1588                                                  (rand.nextLong() & mask ) ) ;
1589 
1590                     failures += testUlpCase(randDouble, Double.MIN_VALUE);
1591                 }
1592             }
1593         }
1594 
1595         return failures;
1596     }
1597 
1598     public static int testFloatSignum() {