src/share/classes/sun/misc/FloatingDecimal.java

Print this page
rev 6572 : 8024356: Double.parseDouble() is slow for long Strings
Summary: Clamp nDigits to MAX_NDIGITS.
Reviewed-by: bpb, drchase, shade
Contributed-by: Dmitry Nadezhin <dmitry.nadezhin@oracle.com>


  53 
  54     /*
  55      * Constants of the implementation
  56      * Most are IEEE-754 related.
  57      * (There are more really boring constants at the end.)
  58      */
  59     static final long   signMask = 0x8000000000000000L;
  60     static final long   expMask  = 0x7ff0000000000000L;
  61     static final long   fractMask= ~(signMask|expMask);
  62     static final int    expShift = 52;
  63     static final int    expBias  = 1023;
  64     static final long   fractHOB = ( 1L<<expShift ); // assumed High-Order bit
  65     static final long   expOne   = ((long)expBias)<<expShift; // exponent of 1.0
  66     static final int    maxSmallBinExp = 62;
  67     static final int    minSmallBinExp = -( 63 / 3 );
  68     static final int    maxDecimalDigits = 15;
  69     static final int    maxDecimalExponent = 308;
  70     static final int    minDecimalExponent = -324;
  71     static final int    bigDecimalExponent = 324; // i.e. abs(minDecimalExponent)
  72 









  73     static final long   highbyte = 0xff00000000000000L;
  74     static final long   highbit  = 0x8000000000000000L;
  75     static final long   lowbytes = ~highbyte;
  76 
  77     static final int    singleSignMask =    0x80000000;
  78     static final int    singleExpMask  =    0x7f800000;
  79     static final int    singleFractMask =   ~(singleSignMask|singleExpMask);
  80     static final int    singleExpShift  =   23;
  81     static final int    singleFractHOB  =   1<<singleExpShift;
  82     static final int    singleExpBias   =   127;
  83     static final int    singleMaxDecimalDigits = 7;
  84     static final int    singleMaxDecimalExponent = 38;
  85     static final int    singleMinDecimalExponent = -45;
  86 
  87     static final int    intDecimalDigits = 9;
  88 
  89 
  90     /*
  91      * count number of bits from high-order 1 bit to low-order 1 bit,
  92      * inclusive.


1451                          * Double.MIN_VALUE ).
1452                          */
1453                         t = dValue * 2.0;
1454                         t *= tiny10pow[j];
1455                         if ( t == 0.0 ){
1456                             return (isNegative)? -0.0 : 0.0;
1457                         }
1458                         t = Double.MIN_VALUE;
1459                     }
1460                     dValue = t;
1461                 }
1462             }
1463 
1464             /*
1465              * dValue is now approximately the result.
1466              * The hard part is adjusting it, by comparison
1467              * with FDBigInt arithmetic.
1468              * Formulate the EXACT big-number result as
1469              * bigD0 * 10^exp
1470              */




1471             FDBigInt bigD0 = new FDBigInt( lValue, digits, kDigits, nDigits );
1472             exp   = decExponent - nDigits;
1473 
1474             correctionLoop:
1475             while(true){
1476                 /* AS A SIDE EFFECT, THIS METHOD WILL SET THE INSTANCE VARIABLES
1477                  * bigIntExp and bigIntNBits
1478                  */
1479                 FDBigInt bigB = doubleToBigInt( dValue );
1480 
1481                 /*
1482                  * Scale bigD, bigB appropriately for
1483                  * big-integer operations.
1484                  * Naively, we multiply by powers of ten
1485                  * and powers of two. What we actually do
1486                  * is keep track of the powers of 5 and
1487                  * powers of 2 we would use, then factor out
1488                  * common divisors before doing the work.
1489                  */
1490                 int B2, B5; // powers of 2, 5 in bigB




  53 
  54     /*
  55      * Constants of the implementation
  56      * Most are IEEE-754 related.
  57      * (There are more really boring constants at the end.)
  58      */
  59     static final long   signMask = 0x8000000000000000L;
  60     static final long   expMask  = 0x7ff0000000000000L;
  61     static final long   fractMask= ~(signMask|expMask);
  62     static final int    expShift = 52;
  63     static final int    expBias  = 1023;
  64     static final long   fractHOB = ( 1L<<expShift ); // assumed High-Order bit
  65     static final long   expOne   = ((long)expBias)<<expShift; // exponent of 1.0
  66     static final int    maxSmallBinExp = 62;
  67     static final int    minSmallBinExp = -( 63 / 3 );
  68     static final int    maxDecimalDigits = 15;
  69     static final int    maxDecimalExponent = 308;
  70     static final int    minDecimalExponent = -324;
  71     static final int    bigDecimalExponent = 324; // i.e. abs(minDecimalExponent)
  72 
  73     //
  74     // The value below is chosen as a conservative threshold. It
  75     // can be demonstrated that a decimal ulp less than 10^(-1075)
  76     // is enough to guarantee correctness. Compensation is also made
  77     // for the binary mantissa which takes 53 binary digits, or
  78     // 17 decimal ones. Hence 1075 + 17 =~ 1100.
  79     //
  80     static final int    MAX_NDIGITS = 1100;
  81 
  82     static final long   highbyte = 0xff00000000000000L;
  83     static final long   highbit  = 0x8000000000000000L;
  84     static final long   lowbytes = ~highbyte;
  85 
  86     static final int    singleSignMask =    0x80000000;
  87     static final int    singleExpMask  =    0x7f800000;
  88     static final int    singleFractMask =   ~(singleSignMask|singleExpMask);
  89     static final int    singleExpShift  =   23;
  90     static final int    singleFractHOB  =   1<<singleExpShift;
  91     static final int    singleExpBias   =   127;
  92     static final int    singleMaxDecimalDigits = 7;
  93     static final int    singleMaxDecimalExponent = 38;
  94     static final int    singleMinDecimalExponent = -45;
  95 
  96     static final int    intDecimalDigits = 9;
  97 
  98 
  99     /*
 100      * count number of bits from high-order 1 bit to low-order 1 bit,
 101      * inclusive.


1460                          * Double.MIN_VALUE ).
1461                          */
1462                         t = dValue * 2.0;
1463                         t *= tiny10pow[j];
1464                         if ( t == 0.0 ){
1465                             return (isNegative)? -0.0 : 0.0;
1466                         }
1467                         t = Double.MIN_VALUE;
1468                     }
1469                     dValue = t;
1470                 }
1471             }
1472 
1473             /*
1474              * dValue is now approximately the result.
1475              * The hard part is adjusting it, by comparison
1476              * with FDBigInt arithmetic.
1477              * Formulate the EXACT big-number result as
1478              * bigD0 * 10^exp
1479              */
1480             if (nDigits > MAX_NDIGITS) {
1481                 nDigits = MAX_NDIGITS + 1;
1482                 digits[MAX_NDIGITS] = '1';
1483             }
1484             FDBigInt bigD0 = new FDBigInt( lValue, digits, kDigits, nDigits );
1485             exp   = decExponent - nDigits;
1486 
1487             correctionLoop:
1488             while(true){
1489                 /* AS A SIDE EFFECT, THIS METHOD WILL SET THE INSTANCE VARIABLES
1490                  * bigIntExp and bigIntNBits
1491                  */
1492                 FDBigInt bigB = doubleToBigInt( dValue );
1493 
1494                 /*
1495                  * Scale bigD, bigB appropriately for
1496                  * big-integer operations.
1497                  * Naively, we multiply by powers of ten
1498                  * and powers of two. What we actually do
1499                  * is keep track of the powers of 5 and
1500                  * powers of 2 we would use, then factor out
1501                  * common divisors before doing the work.
1502                  */
1503                 int B2, B5; // powers of 2, 5 in bigB