< prev index next >

src/java.base/share/classes/jdk/internal/math/FloatingDecimal.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


2360                     }
2361 
2362                     // Round is set; sticky might be set.
2363 
2364                     // For the sticky bit, it suffices to check the
2365                     // current digit and test for any nonzero digits in
2366                     // the remaining unprocessed input.
2367                     i++;
2368                     while (i < signifLength && !sticky) {
2369                         currentDigit = getHexDigit(significandString, i);
2370                         sticky = sticky || (currentDigit != 0);
2371                         i++;
2372                     }
2373 
2374                 }
2375                 // else all of string was seen, round and sticky are
2376                 // correct as false.
2377 
2378                 // Float calculations
2379                 int floatBits = isNegative ? FloatConsts.SIGN_BIT_MASK : 0;
2380                 if (exponent >= FloatConsts.MIN_EXPONENT) {
2381                     if (exponent > FloatConsts.MAX_EXPONENT) {
2382                         // Float.POSITIVE_INFINITY
2383                         floatBits |= FloatConsts.EXP_BIT_MASK;
2384                     } else {
2385                         int threshShift = DoubleConsts.SIGNIFICAND_WIDTH - FloatConsts.SIGNIFICAND_WIDTH - 1;
2386                         boolean floatSticky = (significand & ((1L << threshShift) - 1)) != 0 || round || sticky;
2387                         int iValue = (int) (significand >>> threshShift);
2388                         if ((iValue & 3) != 1 || floatSticky) {
2389                             iValue++;
2390                         }
2391                         floatBits |= (((((int) exponent) + (FloatConsts.EXP_BIAS - 1))) << SINGLE_EXP_SHIFT) + (iValue >> 1);
2392                     }
2393                 } else {
2394                     if (exponent < FloatConsts.MIN_SUB_EXPONENT - 1) {
2395                         // 0
2396                     } else {
2397                         // exponent == -127 ==> threshShift = 53 - 2 + (-149) - (-127) = 53 - 24
2398                         int threshShift = (int) ((DoubleConsts.SIGNIFICAND_WIDTH - 2 + FloatConsts.MIN_SUB_EXPONENT) - exponent);
2399                         assert threshShift >= DoubleConsts.SIGNIFICAND_WIDTH - FloatConsts.SIGNIFICAND_WIDTH;
2400                         assert threshShift < DoubleConsts.SIGNIFICAND_WIDTH;
2401                         boolean floatSticky = (significand & ((1L << threshShift) - 1)) != 0 || round || sticky;
2402                         int iValue = (int) (significand >>> threshShift);
2403                         if ((iValue & 3) != 1 || floatSticky) {
2404                             iValue++;
2405                         }
2406                         floatBits |= iValue >> 1;
2407                     }
2408                 }
2409                 float fValue = Float.intBitsToFloat(floatBits);
2410 
2411                 // Check for overflow and update exponent accordingly.
2412                 if (exponent > DoubleConsts.MAX_EXPONENT) {         // Infinite result
2413                     // overflow to properly signed infinity
2414                     return isNegative ? A2BC_NEGATIVE_INFINITY : A2BC_POSITIVE_INFINITY;
2415                 } else {  // Finite return value
2416                     if (exponent <= DoubleConsts.MAX_EXPONENT && // (Usually) normal result
2417                             exponent >= DoubleConsts.MIN_EXPONENT) {
2418 
2419                         // The result returned in this block cannot be a
2420                         // zero or subnormal; however after the
2421                         // significand is adjusted from rounding, we could
2422                         // still overflow in infinity.
2423 
2424                         // AND exponent bits into significand; if the
2425                         // significand is incremented and overflows from
2426                         // rounding, this combination will update the
2427                         // exponent correctly, even in the case of
2428                         // Double.MAX_VALUE overflowing to infinity.
2429 
2430                         significand = ((( exponent +
2431                                 (long) DoubleConsts.EXP_BIAS) <<
2432                                 (DoubleConsts.SIGNIFICAND_WIDTH - 1))
2433                                 & DoubleConsts.EXP_BIT_MASK) |
2434                                 (DoubleConsts.SIGNIF_BIT_MASK & significand);
2435 
2436                     } else {  // Subnormal or zero
2437                         // (exponent < DoubleConsts.MIN_EXPONENT)
2438 
2439                         if (exponent < (DoubleConsts.MIN_SUB_EXPONENT - 1)) {
2440                             // No way to round back to nonzero value
2441                             // regardless of significand if the exponent is
2442                             // less than -1075.
2443                             return isNegative ? A2BC_NEGATIVE_ZERO : A2BC_POSITIVE_ZERO;
2444                         } else { //  -1075 <= exponent <= MIN_EXPONENT -1 = -1023
2445                             //
2446                             // Find bit position to round to; recompute
2447                             // round and sticky bits, and shift
2448                             // significand right appropriately.
2449                             //
2450 
2451                             sticky = sticky || round;
2452                             round = false;
2453 
2454                             // Number of bits of significand to preserve is
2455                             // exponent - abs_min_exp +1
2456                             // check:
2457                             // -1075 +1074 + 1 = 0
2458                             // -1023 +1074 + 1 = 52
2459 
2460                             int bitsDiscarded = 53 -
2461                                     ((int) exponent - DoubleConsts.MIN_SUB_EXPONENT + 1);
2462                             assert bitsDiscarded >= 1 && bitsDiscarded <= 53;
2463 
2464                             // What to do here:
2465                             // First, isolate the new round bit
2466                             round = (significand & (1L << (bitsDiscarded - 1))) != 0L;
2467                             if (bitsDiscarded > 1) {
2468                                 // create mask to update sticky bits; low
2469                                 // order bitsDiscarded bits should be 1
2470                                 long mask = ~((~0L) << (bitsDiscarded - 1));
2471                                 sticky = sticky || ((significand & mask) != 0L);
2472                             }
2473 
2474                             // Now, discard the bits
2475                             significand = significand >> bitsDiscarded;
2476 
2477                             significand = ((((long) (DoubleConsts.MIN_EXPONENT - 1) + // subnorm exp.
2478                                     (long) DoubleConsts.EXP_BIAS) <<
2479                                     (DoubleConsts.SIGNIFICAND_WIDTH - 1))
2480                                     & DoubleConsts.EXP_BIT_MASK) |
2481                                     (DoubleConsts.SIGNIF_BIT_MASK & significand);
2482                         }
2483                     }
2484 
2485                     // The significand variable now contains the currently
2486                     // appropriate exponent bits too.
2487 
2488                     //
2489                     // Determine if significand should be incremented;
2490                     // making this determination depends on the least
2491                     // significant bit and the round and sticky bits.
2492                     //
2493                     // Round to nearest even rounding table, adapted from
2494                     // table 4.7 in "Computer Arithmetic" by IsraelKoren.
2495                     // The digit to the left of the "decimal" point is the
2496                     // least significant bit, the digits to the right of
2497                     // the point are the round and sticky bits


   1 /*
   2  * Copyright (c) 1996, 2016, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


2360                     }
2361 
2362                     // Round is set; sticky might be set.
2363 
2364                     // For the sticky bit, it suffices to check the
2365                     // current digit and test for any nonzero digits in
2366                     // the remaining unprocessed input.
2367                     i++;
2368                     while (i < signifLength && !sticky) {
2369                         currentDigit = getHexDigit(significandString, i);
2370                         sticky = sticky || (currentDigit != 0);
2371                         i++;
2372                     }
2373 
2374                 }
2375                 // else all of string was seen, round and sticky are
2376                 // correct as false.
2377 
2378                 // Float calculations
2379                 int floatBits = isNegative ? FloatConsts.SIGN_BIT_MASK : 0;
2380                 if (exponent >= Float.MIN_EXPONENT) {
2381                     if (exponent > Float.MAX_EXPONENT) {
2382                         // Float.POSITIVE_INFINITY
2383                         floatBits |= FloatConsts.EXP_BIT_MASK;
2384                     } else {
2385                         int threshShift = DoubleConsts.SIGNIFICAND_WIDTH - FloatConsts.SIGNIFICAND_WIDTH - 1;
2386                         boolean floatSticky = (significand & ((1L << threshShift) - 1)) != 0 || round || sticky;
2387                         int iValue = (int) (significand >>> threshShift);
2388                         if ((iValue & 3) != 1 || floatSticky) {
2389                             iValue++;
2390                         }
2391                         floatBits |= (((((int) exponent) + (FloatConsts.EXP_BIAS - 1))) << SINGLE_EXP_SHIFT) + (iValue >> 1);
2392                     }
2393                 } else {
2394                     if (exponent < FloatConsts.MIN_SUB_EXPONENT - 1) {
2395                         // 0
2396                     } else {
2397                         // exponent == -127 ==> threshShift = 53 - 2 + (-149) - (-127) = 53 - 24
2398                         int threshShift = (int) ((DoubleConsts.SIGNIFICAND_WIDTH - 2 + FloatConsts.MIN_SUB_EXPONENT) - exponent);
2399                         assert threshShift >= DoubleConsts.SIGNIFICAND_WIDTH - FloatConsts.SIGNIFICAND_WIDTH;
2400                         assert threshShift < DoubleConsts.SIGNIFICAND_WIDTH;
2401                         boolean floatSticky = (significand & ((1L << threshShift) - 1)) != 0 || round || sticky;
2402                         int iValue = (int) (significand >>> threshShift);
2403                         if ((iValue & 3) != 1 || floatSticky) {
2404                             iValue++;
2405                         }
2406                         floatBits |= iValue >> 1;
2407                     }
2408                 }
2409                 float fValue = Float.intBitsToFloat(floatBits);
2410 
2411                 // Check for overflow and update exponent accordingly.
2412                 if (exponent > Double.MAX_EXPONENT) {         // Infinite result
2413                     // overflow to properly signed infinity
2414                     return isNegative ? A2BC_NEGATIVE_INFINITY : A2BC_POSITIVE_INFINITY;
2415                 } else {  // Finite return value
2416                     if (exponent <= Double.MAX_EXPONENT && // (Usually) normal result
2417                             exponent >= Double.MIN_EXPONENT) {
2418 
2419                         // The result returned in this block cannot be a
2420                         // zero or subnormal; however after the
2421                         // significand is adjusted from rounding, we could
2422                         // still overflow in infinity.
2423 
2424                         // AND exponent bits into significand; if the
2425                         // significand is incremented and overflows from
2426                         // rounding, this combination will update the
2427                         // exponent correctly, even in the case of
2428                         // Double.MAX_VALUE overflowing to infinity.
2429 
2430                         significand = ((( exponent +
2431                                 (long) DoubleConsts.EXP_BIAS) <<
2432                                 (DoubleConsts.SIGNIFICAND_WIDTH - 1))
2433                                 & DoubleConsts.EXP_BIT_MASK) |
2434                                 (DoubleConsts.SIGNIF_BIT_MASK & significand);
2435 
2436                     } else {  // Subnormal or zero
2437                         // (exponent < Double.MIN_EXPONENT)
2438 
2439                         if (exponent < (DoubleConsts.MIN_SUB_EXPONENT - 1)) {
2440                             // No way to round back to nonzero value
2441                             // regardless of significand if the exponent is
2442                             // less than -1075.
2443                             return isNegative ? A2BC_NEGATIVE_ZERO : A2BC_POSITIVE_ZERO;
2444                         } else { //  -1075 <= exponent <= MIN_EXPONENT -1 = -1023
2445                             //
2446                             // Find bit position to round to; recompute
2447                             // round and sticky bits, and shift
2448                             // significand right appropriately.
2449                             //
2450 
2451                             sticky = sticky || round;
2452                             round = false;
2453 
2454                             // Number of bits of significand to preserve is
2455                             // exponent - abs_min_exp +1
2456                             // check:
2457                             // -1075 +1074 + 1 = 0
2458                             // -1023 +1074 + 1 = 52
2459 
2460                             int bitsDiscarded = 53 -
2461                                     ((int) exponent - DoubleConsts.MIN_SUB_EXPONENT + 1);
2462                             assert bitsDiscarded >= 1 && bitsDiscarded <= 53;
2463 
2464                             // What to do here:
2465                             // First, isolate the new round bit
2466                             round = (significand & (1L << (bitsDiscarded - 1))) != 0L;
2467                             if (bitsDiscarded > 1) {
2468                                 // create mask to update sticky bits; low
2469                                 // order bitsDiscarded bits should be 1
2470                                 long mask = ~((~0L) << (bitsDiscarded - 1));
2471                                 sticky = sticky || ((significand & mask) != 0L);
2472                             }
2473 
2474                             // Now, discard the bits
2475                             significand = significand >> bitsDiscarded;
2476 
2477                             significand = ((((long) (Double.MIN_EXPONENT - 1) + // subnorm exp.
2478                                     (long) DoubleConsts.EXP_BIAS) <<
2479                                     (DoubleConsts.SIGNIFICAND_WIDTH - 1))
2480                                     & DoubleConsts.EXP_BIT_MASK) |
2481                                     (DoubleConsts.SIGNIF_BIT_MASK & significand);
2482                         }
2483                     }
2484 
2485                     // The significand variable now contains the currently
2486                     // appropriate exponent bits too.
2487 
2488                     //
2489                     // Determine if significand should be incremented;
2490                     // making this determination depends on the least
2491                     // significant bit and the round and sticky bits.
2492                     //
2493                     // Round to nearest even rounding table, adapted from
2494                     // table 4.7 in "Computer Arithmetic" by IsraelKoren.
2495                     // The digit to the left of the "decimal" point is the
2496                     // least significant bit, the digits to the right of
2497                     // the point are the round and sticky bits


< prev index next >