jdk/src/java.base/share/classes/sun/misc/FloatingDecimal.java
author ihse
Fri, 12 Jun 2015 08:31:01 +0200
changeset 33957 39113ae98993
parent 30655 d83f50188ca9
child 32649 2ee9017c7597
permissions -rw-r--r--
8085822: JEP 223: New Version-String Scheme (initial integration) Reviewed-by: erikj, dcubed, dholmes, alanb Contributed-by: Magnus Ihse Bursie <magnus.ihse.bursie@oracle.com>, Alejandro E Murillo <alejandro.murillo@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
     2
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4052
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4052
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4052
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4052
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4052
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    28
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    31
/**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    32
 * A class for converting between ASCII and decimal representations of a single
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    33
 * or double precision floating point number. Most conversions are provided via
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    34
 * static convenience methods, although a <code>BinaryToASCIIConverter</code>
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    35
 * instance may be obtained and reused.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    36
 */
9521
e18f95eadb3c 7039369: Limit range of strictfp in FloatingDecimal
darcy
parents: 9276
diff changeset
    37
public class FloatingDecimal{
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    38
    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    39
    // Constants of the implementation;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    40
    // most are IEEE-754 related.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    41
    // (There are more really boring constants at the end.)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    42
    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    43
    static final int    EXP_SHIFT = DoubleConsts.SIGNIFICAND_WIDTH - 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    44
    static final long   FRACT_HOB = ( 1L<<EXP_SHIFT ); // assumed High-Order bit
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    45
    static final long   EXP_ONE   = ((long)DoubleConsts.EXP_BIAS)<<EXP_SHIFT; // exponent of 1.0
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    46
    static final int    MAX_SMALL_BIN_EXP = 62;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    47
    static final int    MIN_SMALL_BIN_EXP = -( 63 / 3 );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    48
    static final int    MAX_DECIMAL_DIGITS = 15;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    49
    static final int    MAX_DECIMAL_EXPONENT = 308;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    50
    static final int    MIN_DECIMAL_EXPONENT = -324;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    51
    static final int    BIG_DECIMAL_EXPONENT = 324; // i.e. abs(MIN_DECIMAL_EXPONENT)
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
    52
    static final int    MAX_NDIGITS = 1100;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    53
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    54
    static final int    SINGLE_EXP_SHIFT  =   FloatConsts.SIGNIFICAND_WIDTH - 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    55
    static final int    SINGLE_FRACT_HOB  =   1<<SINGLE_EXP_SHIFT;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    56
    static final int    SINGLE_MAX_DECIMAL_DIGITS = 7;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    57
    static final int    SINGLE_MAX_DECIMAL_EXPONENT = 38;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    58
    static final int    SINGLE_MIN_DECIMAL_EXPONENT = -45;
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
    59
    static final int    SINGLE_MAX_NDIGITS = 200;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    60
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    61
    static final int    INT_DECIMAL_DIGITS = 9;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    62
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    63
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    64
     * Converts a double precision floating point value to a <code>String</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    65
     *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    66
     * @param d The double precision value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    67
     * @return The value converted to a <code>String</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    68
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    69
    public static String toJavaFormatString(double d) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    70
        return getBinaryToASCIIConverter(d).toJavaFormatString();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    71
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    73
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    74
     * Converts a single precision floating point value to a <code>String</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    75
     *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    76
     * @param f The single precision value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    77
     * @return The value converted to a <code>String</code>.
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    78
     */
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    79
    public static String toJavaFormatString(float f) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    80
        return getBinaryToASCIIConverter(f).toJavaFormatString();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    81
    }
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    82
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    83
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    84
     * Appends a double precision floating point value to an <code>Appendable</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    85
     * @param d The double precision value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    86
     * @param buf The <code>Appendable</code> with the value appended.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    87
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    88
    public static void appendTo(double d, Appendable buf) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    89
        getBinaryToASCIIConverter(d).appendTo(buf);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    90
    }
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
    91
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    92
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    93
     * Appends a single precision floating point value to an <code>Appendable</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    94
     * @param f The single precision value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    95
     * @param buf The <code>Appendable</code> with the value appended.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    96
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    97
    public static void appendTo(float f, Appendable buf) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    98
        getBinaryToASCIIConverter(f).appendTo(buf);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
    99
    }
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   100
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   101
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   102
     * Converts a <code>String</code> to a double precision floating point value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   103
     *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   104
     * @param s The <code>String</code> to convert.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   105
     * @return The double precision value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   106
     * @throws NumberFormatException If the <code>String</code> does not
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   107
     * represent a properly formatted double precision value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   108
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   109
    public static double parseDouble(String s) throws NumberFormatException {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   110
        return readJavaFormatString(s).doubleValue();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   111
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   112
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   113
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   114
     * Converts a <code>String</code> to a single precision floating point value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   115
     *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   116
     * @param s The <code>String</code> to convert.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   117
     * @return The single precision value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   118
     * @throws NumberFormatException If the <code>String</code> does not
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   119
     * represent a properly formatted single precision value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   120
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   121
    public static float parseFloat(String s) throws NumberFormatException {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   122
        return readJavaFormatString(s).floatValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   125
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   126
     * A converter which can process single or double precision floating point
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   127
     * values into an ASCII <code>String</code> representation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   129
    public interface BinaryToASCIIConverter {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   130
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   131
         * Converts a floating point value into an ASCII <code>String</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   132
         * @return The value converted to a <code>String</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   133
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   134
        public String toJavaFormatString();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   135
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   136
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   137
         * Appends a floating point value to an <code>Appendable</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   138
         * @param buf The <code>Appendable</code> to receive the value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   139
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   140
        public void appendTo(Appendable buf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   142
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   143
         * Retrieves the decimal exponent most closely corresponding to this value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   144
         * @return The decimal exponent.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   145
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   146
        public int getDecimalExponent();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   147
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   148
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   149
         * Retrieves the value as an array of digits.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   150
         * @param digits The digit array.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   151
         * @return The number of valid digits copied into the array.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   152
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   153
        public int getDigits(char[] digits);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   154
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   155
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   156
         * Indicates the sign of the value.
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 26722
diff changeset
   157
         * @return {@code value < 0.0}.
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   158
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   159
        public boolean isNegative();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   161
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   162
         * Indicates whether the value is either infinite or not a number.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   163
         *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   164
         * @return <code>true</code> if and only if the value is <code>NaN</code>
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   165
         * or infinite.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   166
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   167
        public boolean isExceptional();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   169
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   170
         * Indicates whether the value was rounded up during the binary to ASCII
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   171
         * conversion.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   172
         *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   173
         * @return <code>true</code> if and only if the value was rounded up.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   174
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   175
        public boolean digitsRoundedUp();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   177
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   178
         * Indicates whether the binary to ASCII conversion was exact.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   179
         *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   180
         * @return <code>true</code> if any only if the conversion was exact.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   181
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   182
        public boolean decimalDigitsExact();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   183
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   184
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   185
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   186
     * A <code>BinaryToASCIIConverter</code> which represents <code>NaN</code>
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   187
     * and infinite values.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   189
    private static class ExceptionalBinaryToASCIIBuffer implements BinaryToASCIIConverter {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   190
        final private String image;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   191
        private boolean isNegative;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   193
        public ExceptionalBinaryToASCIIBuffer(String image, boolean isNegative) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   194
            this.image = image;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   195
            this.isNegative = isNegative;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   198
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   199
        public String toJavaFormatString() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   200
            return image;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   203
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   204
        public void appendTo(Appendable buf) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   205
            if (buf instanceof StringBuilder) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   206
                ((StringBuilder) buf).append(image);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   207
            } else if (buf instanceof StringBuffer) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   208
                ((StringBuffer) buf).append(image);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   209
            } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   210
                assert false;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   211
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   212
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   214
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   215
        public int getDecimalExponent() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   216
            throw new IllegalArgumentException("Exceptional value does not have an exponent");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   218
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   219
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   220
        public int getDigits(char[] digits) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   221
            throw new IllegalArgumentException("Exceptional value does not have digits");
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   222
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   223
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   224
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   225
        public boolean isNegative() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   226
            return isNegative;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   227
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   228
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   229
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   230
        public boolean isExceptional() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   231
            return true;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   232
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   233
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   234
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   235
        public boolean digitsRoundedUp() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   236
            throw new IllegalArgumentException("Exceptional value is not rounded");
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   237
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   238
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   239
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   240
        public boolean decimalDigitsExact() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   241
            throw new IllegalArgumentException("Exceptional value is not exact");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   245
    private static final String INFINITY_REP = "Infinity";
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   246
    private static final int INFINITY_LENGTH = INFINITY_REP.length();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   247
    private static final String NAN_REP = "NaN";
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   248
    private static final int NAN_LENGTH = NAN_REP.length();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   249
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   250
    private static final BinaryToASCIIConverter B2AC_POSITIVE_INFINITY = new ExceptionalBinaryToASCIIBuffer(INFINITY_REP, false);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   251
    private static final BinaryToASCIIConverter B2AC_NEGATIVE_INFINITY = new ExceptionalBinaryToASCIIBuffer("-" + INFINITY_REP, true);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   252
    private static final BinaryToASCIIConverter B2AC_NOT_A_NUMBER = new ExceptionalBinaryToASCIIBuffer(NAN_REP, false);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   253
    private static final BinaryToASCIIConverter B2AC_POSITIVE_ZERO = new BinaryToASCIIBuffer(false, new char[]{'0'});
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   254
    private static final BinaryToASCIIConverter B2AC_NEGATIVE_ZERO = new BinaryToASCIIBuffer(true,  new char[]{'0'});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   256
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   257
     * A buffered implementation of <code>BinaryToASCIIConverter</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   258
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   259
    static class BinaryToASCIIBuffer implements BinaryToASCIIConverter {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   260
        private boolean isNegative;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   261
        private int decExponent;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   262
        private int firstDigitIndex;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   263
        private int nDigits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   264
        private final char[] digits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   265
        private final char[] buffer = new char[26];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   266
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   267
        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   268
        // The fields below provide additional information about the result of
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   269
        // the binary to decimal digits conversion done in dtoa() and roundup()
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   270
        // methods. They are changed if needed by those two methods.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   271
        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   272
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   273
        // True if the dtoa() binary to decimal conversion was exact.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   274
        private boolean exactDecimalConversion = false;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   275
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   276
        // True if the result of the binary to decimal conversion was rounded-up
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   277
        // at the end of the conversion process, i.e. roundUp() method was called.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   278
        private boolean decimalDigitsRoundedUp = false;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   279
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   280
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   281
         * Default constructor; used for non-zero values,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   282
         * <code>BinaryToASCIIBuffer</code> may be thread-local and reused
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   283
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   284
        BinaryToASCIIBuffer(){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   285
            this.digits = new char[20];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   287
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   288
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   289
         * Creates a specialized value (positive and negative zeros).
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   290
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   291
        BinaryToASCIIBuffer(boolean isNegative, char[] digits){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   292
            this.isNegative = isNegative;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   293
            this.decExponent  = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   294
            this.digits = digits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   295
            this.firstDigitIndex = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   296
            this.nDigits = digits.length;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   297
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   299
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   300
        public String toJavaFormatString() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   301
            int len = getChars(buffer);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   302
            return new String(buffer, 0, len);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   303
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   304
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   305
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   306
        public void appendTo(Appendable buf) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   307
            int len = getChars(buffer);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   308
            if (buf instanceof StringBuilder) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   309
                ((StringBuilder) buf).append(buffer, 0, len);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   310
            } else if (buf instanceof StringBuffer) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   311
                ((StringBuffer) buf).append(buffer, 0, len);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   312
            } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   313
                assert false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   316
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   317
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   318
        public int getDecimalExponent() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   319
            return decExponent;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   320
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   322
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   323
        public int getDigits(char[] digits) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   324
            System.arraycopy(this.digits,firstDigitIndex,digits,0,this.nDigits);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   325
            return this.nDigits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   326
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   327
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   328
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   329
        public boolean isNegative() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   330
            return isNegative;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   331
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   333
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   334
        public boolean isExceptional() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   335
            return false;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   336
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   337
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   338
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   339
        public boolean digitsRoundedUp() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   340
            return decimalDigitsRoundedUp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   342
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   343
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   344
        public boolean decimalDigitsExact() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   345
            return exactDecimalConversion;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   348
        private void setSign(boolean isNegative) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   349
            this.isNegative = isNegative;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   350
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   352
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   353
         * This is the easy subcase --
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   354
         * all the significant bits, after scaling, are held in lvalue.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   355
         * negSign and decExponent tell us what processing and scaling
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   356
         * has already been done. Exceptional cases have already been
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   357
         * stripped out.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   358
         * In particular:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   359
         * lvalue is a finite number (not Inf, nor NaN)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   360
         * lvalue > 0L (not zero, nor negative).
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   361
         *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   362
         * The only reason that we develop the digits here, rather than
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   363
         * calling on Long.toString() is that we can do it a little faster,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   364
         * and besides want to treat trailing 0s specially. If Long.toString
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   365
         * changes, we should re-evaluate this strategy!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   366
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   367
        private void developLongDigits( int decExponent, long lvalue, int insignificantDigits ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   368
            if ( insignificantDigits != 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   369
                // Discard non-significant low-order bits, while rounding,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   370
                // up to insignificant value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   371
                long pow10 = FDBigInteger.LONG_5_POW[insignificantDigits] << insignificantDigits; // 10^i == 5^i * 2^i;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   372
                long residue = lvalue % pow10;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   373
                lvalue /= pow10;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   374
                decExponent += insignificantDigits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   375
                if ( residue >= (pow10>>1) ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   376
                    // round up based on the low-order bits we're discarding
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   377
                    lvalue++;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   378
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   380
            int  digitno = digits.length -1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   381
            int  c;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   382
            if ( lvalue <= Integer.MAX_VALUE ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   383
                assert lvalue > 0L : lvalue; // lvalue <= 0
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   384
                // even easier subcase!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   385
                // can do int arithmetic rather than long!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   386
                int  ivalue = (int)lvalue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                c = ivalue%10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                ivalue /= 10;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   389
                while ( c == 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   390
                    decExponent++;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   391
                    c = ivalue%10;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   392
                    ivalue /= 10;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   393
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   394
                while ( ivalue != 0){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   395
                    digits[digitno--] = (char)(c+'0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   396
                    decExponent++;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   397
                    c = ivalue%10;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   398
                    ivalue /= 10;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   399
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   400
                digits[digitno] = (char)(c+'0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   401
            } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   402
                // same algorithm as above (same bugs, too )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   403
                // but using long arithmetic.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                c = (int)(lvalue%10L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                lvalue /= 10L;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   406
                while ( c == 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   407
                    decExponent++;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   408
                    c = (int)(lvalue%10L);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   409
                    lvalue /= 10L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   410
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   411
                while ( lvalue != 0L ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   412
                    digits[digitno--] = (char)(c+'0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   413
                    decExponent++;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   414
                    c = (int)(lvalue%10L);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   415
                    lvalue /= 10;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   416
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   417
                digits[digitno] = (char)(c+'0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   419
            this.decExponent = decExponent+1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   420
            this.firstDigitIndex = digitno;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   421
            this.nDigits = this.digits.length - digitno;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   424
        private void dtoa( int binExp, long fractBits, int nSignificantBits, boolean isCompatibleFormat)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   425
        {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   426
            assert fractBits > 0 ; // fractBits here can't be zero or negative
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   427
            assert (fractBits & FRACT_HOB)!=0  ; // Hi-order bit should be set
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   428
            // Examine number. Determine if it is an easy case,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   429
            // which we can do pretty trivially using float/long conversion,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   430
            // or whether we must do real work.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   431
            final int tailZeros = Long.numberOfTrailingZeros(fractBits);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   432
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   433
            // number of significant bits of fractBits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   434
            final int nFractBits = EXP_SHIFT+1-tailZeros;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   435
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   436
            // reset flags to default values as dtoa() does not always set these
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   437
            // flags and a prior call to dtoa() might have set them to incorrect
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   438
            // values with respect to the current state.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   439
            decimalDigitsRoundedUp = false;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   440
            exactDecimalConversion = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   442
            // number of significant bits to the right of the point.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   443
            int nTinyBits = Math.max( 0, nFractBits - binExp - 1 );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   444
            if ( binExp <= MAX_SMALL_BIN_EXP && binExp >= MIN_SMALL_BIN_EXP ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   445
                // Look more closely at the number to decide if,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   446
                // with scaling by 10^nTinyBits, the result will fit in
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   447
                // a long.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   448
                if ( (nTinyBits < FDBigInteger.LONG_5_POW.length) && ((nFractBits + N_5_BITS[nTinyBits]) < 64 ) ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   449
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   450
                    // We can do this:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   451
                    // take the fraction bits, which are normalized.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   452
                    // (a) nTinyBits == 0: Shift left or right appropriately
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   453
                    //     to align the binary point at the extreme right, i.e.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   454
                    //     where a long int point is expected to be. The integer
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   455
                    //     result is easily converted to a string.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   456
                    // (b) nTinyBits > 0: Shift right by EXP_SHIFT-nFractBits,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   457
                    //     which effectively converts to long and scales by
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   458
                    //     2^nTinyBits. Then multiply by 5^nTinyBits to
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   459
                    //     complete the scaling. We know this won't overflow
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   460
                    //     because we just counted the number of bits necessary
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   461
                    //     in the result. The integer you get from this can
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   462
                    //     then be converted to a string pretty easily.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   463
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   464
                    if ( nTinyBits == 0 ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   465
                        int insignificant;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   466
                        if ( binExp > nSignificantBits ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   467
                            insignificant = insignificantDigitsForPow2(binExp-nSignificantBits-1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   468
                        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   469
                            insignificant = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   470
                        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   471
                        if ( binExp >= EXP_SHIFT ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   472
                            fractBits <<= (binExp-EXP_SHIFT);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   473
                        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   474
                            fractBits >>>= (EXP_SHIFT-binExp) ;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   475
                        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   476
                        developLongDigits( 0, fractBits, insignificant );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   477
                        return;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   478
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   479
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   480
                    // The following causes excess digits to be printed
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   481
                    // out in the single-float case. Our manipulation of
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   482
                    // halfULP here is apparently not correct. If we
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   483
                    // better understand how this works, perhaps we can
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   484
                    // use this special case again. But for the time being,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   485
                    // we do not.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   486
                    // else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   487
                    //     fractBits >>>= EXP_SHIFT+1-nFractBits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   488
                    //     fractBits//= long5pow[ nTinyBits ];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   489
                    //     halfULP = long5pow[ nTinyBits ] >> (1+nSignificantBits-nFractBits);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   490
                    //     developLongDigits( -nTinyBits, fractBits, insignificantDigits(halfULP) );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   491
                    //     return;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   492
                    // }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   493
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   494
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   496
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   497
            // This is the hard case. We are going to compute large positive
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   498
            // integers B and S and integer decExp, s.t.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   499
            //      d = ( B / S )// 10^decExp
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   500
            //      1 <= B / S < 10
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   501
            // Obvious choices are:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   502
            //      decExp = floor( log10(d) )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   503
            //      B      = d// 2^nTinyBits// 10^max( 0, -decExp )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   504
            //      S      = 10^max( 0, decExp)// 2^nTinyBits
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   505
            // (noting that nTinyBits has already been forced to non-negative)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   506
            // I am also going to compute a large positive integer
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   507
            //      M      = (1/2^nSignificantBits)// 2^nTinyBits// 10^max( 0, -decExp )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   508
            // i.e. M is (1/2) of the ULP of d, scaled like B.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   509
            // When we iterate through dividing B/S and picking off the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   510
            // quotient bits, we will know when to stop when the remainder
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   511
            // is <= M.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   512
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   513
            // We keep track of powers of 2 and powers of 5.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   514
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   515
            int decExp = estimateDecExp(fractBits,binExp);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   516
            int B2, B5; // powers of 2 and powers of 5, respectively, in B
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   517
            int S2, S5; // powers of 2 and powers of 5, respectively, in S
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   518
            int M2, M5; // powers of 2 and powers of 5, respectively, in M
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   519
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   520
            B5 = Math.max( 0, -decExp );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   521
            B2 = B5 + nTinyBits + binExp;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   522
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   523
            S5 = Math.max( 0, decExp );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   524
            S2 = S5 + nTinyBits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   525
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   526
            M5 = B5;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   527
            M2 = B2 - nSignificantBits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   528
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   529
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   530
            // the long integer fractBits contains the (nFractBits) interesting
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   531
            // bits from the mantissa of d ( hidden 1 added if necessary) followed
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   532
            // by (EXP_SHIFT+1-nFractBits) zeros. In the interest of compactness,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   533
            // I will shift out those zeros before turning fractBits into a
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   534
            // FDBigInteger. The resulting whole number will be
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   535
            //      d * 2^(nFractBits-1-binExp).
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   536
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   537
            fractBits >>>= tailZeros;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   538
            B2 -= nFractBits-1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   539
            int common2factor = Math.min( B2, S2 );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   540
            B2 -= common2factor;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   541
            S2 -= common2factor;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   542
            M2 -= common2factor;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   543
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   544
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   545
            // HACK!! For exact powers of two, the next smallest number
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   546
            // is only half as far away as we think (because the meaning of
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   547
            // ULP changes at power-of-two bounds) for this reason, we
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   548
            // hack M2. Hope this works.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   549
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   550
            if ( nFractBits == 1 ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   551
                M2 -= 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   553
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   554
            if ( M2 < 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   555
                // oops.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   556
                // since we cannot scale M down far enough,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   557
                // we must scale the other values up.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   558
                B2 -= M2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   559
                S2 -= M2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   560
                M2 =  0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   562
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   563
            // Construct, Scale, iterate.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   564
            // Some day, we'll write a stopping test that takes
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   565
            // account of the asymmetry of the spacing of floating-point
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   566
            // numbers below perfect powers of 2
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   567
            // 26 Sept 96 is not that day.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   568
            // So we use a symmetric test.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   569
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   570
            int ndigit = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   571
            boolean low, high;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   572
            long lowDigitDifference;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   573
            int  q;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   575
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   576
            // Detect the special cases where all the numbers we are about
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   577
            // to compute will fit in int or long integers.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   578
            // In these cases, we will avoid doing FDBigInteger arithmetic.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   579
            // We use the same algorithms, except that we "normalize"
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   580
            // our FDBigIntegers before iterating. This is to make division easier,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   581
            // as it makes our fist guess (quotient of high-order words)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   582
            // more accurate!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   583
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   584
            // Some day, we'll write a stopping test that takes
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   585
            // account of the asymmetry of the spacing of floating-point
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   586
            // numbers below perfect powers of 2
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   587
            // 26 Sept 96 is not that day.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   588
            // So we use a symmetric test.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   589
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   590
            // binary digits needed to represent B, approx.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   591
            int Bbits = nFractBits + B2 + (( B5 < N_5_BITS.length )? N_5_BITS[B5] : ( B5*3 ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   593
            // binary digits needed to represent 10*S, approx.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   594
            int tenSbits = S2+1 + (( (S5+1) < N_5_BITS.length )? N_5_BITS[(S5+1)] : ( (S5+1)*3 ));
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   595
            if ( Bbits < 64 && tenSbits < 64){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   596
                if ( Bbits < 32 && tenSbits < 32){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   597
                    // wa-hoo! They're all ints!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   598
                    int b = ((int)fractBits * FDBigInteger.SMALL_5_POW[B5] ) << B2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   599
                    int s = FDBigInteger.SMALL_5_POW[S5] << S2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   600
                    int m = FDBigInteger.SMALL_5_POW[M5] << M2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   601
                    int tens = s * 10;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   602
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   603
                    // Unroll the first iteration. If our decExp estimate
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   604
                    // was too high, our first quotient will be zero. In this
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   605
                    // case, we discard it and decrement decExp.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   606
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   607
                    ndigit = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   608
                    q = b / s;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   609
                    b = 10 * ( b % s );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   610
                    m *= 10;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   611
                    low  = (b <  m );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   612
                    high = (b+m > tens );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   613
                    assert q < 10 : q; // excessively large digit
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   614
                    if ( (q == 0) && ! high ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   615
                        // oops. Usually ignore leading zero.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   616
                        decExp--;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   617
                    } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   618
                        digits[ndigit++] = (char)('0' + q);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   619
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   620
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   621
                    // HACK! Java spec sez that we always have at least
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   622
                    // one digit after the . in either F- or E-form output.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   623
                    // Thus we will need more than one digit if we're using
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   624
                    // E-form
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   625
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   626
                    if ( !isCompatibleFormat ||decExp < -3 || decExp >= 8 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   627
                        high = low = false;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   628
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   629
                    while( ! low && ! high ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   630
                        q = b / s;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   631
                        b = 10 * ( b % s );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   632
                        m *= 10;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   633
                        assert q < 10 : q; // excessively large digit
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   634
                        if ( m > 0L ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   635
                            low  = (b <  m );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   636
                            high = (b+m > tens );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   637
                        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   638
                            // hack -- m might overflow!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   639
                            // in this case, it is certainly > b,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   640
                            // which won't
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   641
                            // and b+m > tens, too, since that has overflowed
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   642
                            // either!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   643
                            low = true;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   644
                            high = true;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   645
                        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   646
                        digits[ndigit++] = (char)('0' + q);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   647
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   648
                    lowDigitDifference = (b<<1) - tens;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   649
                    exactDecimalConversion  = (b == 0);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   650
                } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   651
                    // still good! they're all longs!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   652
                    long b = (fractBits * FDBigInteger.LONG_5_POW[B5] ) << B2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   653
                    long s = FDBigInteger.LONG_5_POW[S5] << S2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   654
                    long m = FDBigInteger.LONG_5_POW[M5] << M2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   655
                    long tens = s * 10L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   656
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   657
                    // Unroll the first iteration. If our decExp estimate
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   658
                    // was too high, our first quotient will be zero. In this
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   659
                    // case, we discard it and decrement decExp.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   660
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   661
                    ndigit = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   662
                    q = (int) ( b / s );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   663
                    b = 10L * ( b % s );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   664
                    m *= 10L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   665
                    low  = (b <  m );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   666
                    high = (b+m > tens );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   667
                    assert q < 10 : q; // excessively large digit
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   668
                    if ( (q == 0) && ! high ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   669
                        // oops. Usually ignore leading zero.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   670
                        decExp--;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    } else {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   672
                        digits[ndigit++] = (char)('0' + q);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   673
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   674
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   675
                    // HACK! Java spec sez that we always have at least
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   676
                    // one digit after the . in either F- or E-form output.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   677
                    // Thus we will need more than one digit if we're using
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   678
                    // E-form
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   679
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   680
                    if ( !isCompatibleFormat || decExp < -3 || decExp >= 8 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   681
                        high = low = false;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   682
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   683
                    while( ! low && ! high ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   684
                        q = (int) ( b / s );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   685
                        b = 10 * ( b % s );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   686
                        m *= 10;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   687
                        assert q < 10 : q;  // excessively large digit
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   688
                        if ( m > 0L ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   689
                            low  = (b <  m );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   690
                            high = (b+m > tens );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   691
                        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   692
                            // hack -- m might overflow!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   693
                            // in this case, it is certainly > b,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   694
                            // which won't
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   695
                            // and b+m > tens, too, since that has overflowed
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   696
                            // either!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   697
                            low = true;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   698
                            high = true;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   699
                        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   700
                        digits[ndigit++] = (char)('0' + q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                    }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   702
                    lowDigitDifference = (b<<1) - tens;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   703
                    exactDecimalConversion  = (b == 0);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   704
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   705
            } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   706
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   707
                // We really must do FDBigInteger arithmetic.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   708
                // Fist, construct our FDBigInteger initial values.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   709
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   710
                FDBigInteger Sval = FDBigInteger.valueOfPow52(S5, S2);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   711
                int shiftBias = Sval.getNormalizationBias();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   712
                Sval = Sval.leftShift(shiftBias); // normalize so that division works better
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   713
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   714
                FDBigInteger Bval = FDBigInteger.valueOfMulPow52(fractBits, B5, B2 + shiftBias);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   715
                FDBigInteger Mval = FDBigInteger.valueOfPow52(M5 + 1, M2 + shiftBias + 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   716
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   717
                FDBigInteger tenSval = FDBigInteger.valueOfPow52(S5 + 1, S2 + shiftBias + 1); //Sval.mult( 10 );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   718
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   719
                // Unroll the first iteration. If our decExp estimate
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   720
                // was too high, our first quotient will be zero. In this
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   721
                // case, we discard it and decrement decExp.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   722
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   723
                ndigit = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   724
                q = Bval.quoRemIteration( Sval );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   725
                low  = (Bval.cmp( Mval ) < 0);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   726
                high = tenSval.addAndCmp(Bval,Mval)<=0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   727
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   728
                assert q < 10 : q; // excessively large digit
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   729
                if ( (q == 0) && ! high ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   730
                    // oops. Usually ignore leading zero.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   731
                    decExp--;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   732
                } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   733
                    digits[ndigit++] = (char)('0' + q);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   734
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   735
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   736
                // HACK! Java spec sez that we always have at least
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   737
                // one digit after the . in either F- or E-form output.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   738
                // Thus we will need more than one digit if we're using
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   739
                // E-form
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   740
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   741
                if (!isCompatibleFormat || decExp < -3 || decExp >= 8 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   742
                    high = low = false;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   743
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   744
                while( ! low && ! high ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   745
                    q = Bval.quoRemIteration( Sval );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   746
                    assert q < 10 : q;  // excessively large digit
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   747
                    Mval = Mval.multBy10(); //Mval = Mval.mult( 10 );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   748
                    low  = (Bval.cmp( Mval ) < 0);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   749
                    high = tenSval.addAndCmp(Bval,Mval)<=0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   750
                    digits[ndigit++] = (char)('0' + q);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   751
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   752
                if ( high && low ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   753
                    Bval = Bval.leftShift(1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   754
                    lowDigitDifference = Bval.cmp(tenSval);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   755
                } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   756
                    lowDigitDifference = 0L; // this here only for flow analysis!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   757
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   758
                exactDecimalConversion  = (Bval.cmp( FDBigInteger.ZERO ) == 0);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   759
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   760
            this.decExponent = decExp+1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   761
            this.firstDigitIndex = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   762
            this.nDigits = ndigit;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   763
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   764
            // Last digit gets rounded based on stopping condition.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   765
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   766
            if ( high ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   767
                if ( low ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   768
                    if ( lowDigitDifference == 0L ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   769
                        // it's a tie!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   770
                        // choose based on which digits we like.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   771
                        if ( (digits[firstDigitIndex+nDigits-1]&1) != 0 ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   772
                            roundup();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   773
                        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   774
                    } else if ( lowDigitDifference > 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   775
                        roundup();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   777
                } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   778
                    roundup();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   779
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   780
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   781
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   782
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   783
        // add one to the least significant digit.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   784
        // in the unlikely event there is a carry out, deal with it.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   785
        // assert that this will only happen where there
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   786
        // is only one digit, e.g. (float)1e-44 seems to do it.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   787
        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   788
        private void roundup() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   789
            int i = (firstDigitIndex + nDigits - 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   790
            int q = digits[i];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   791
            if (q == '9') {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   792
                while (q == '9' && i > firstDigitIndex) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   793
                    digits[i] = '0';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   794
                    q = digits[--i];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   795
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   796
                if (q == '9') {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   797
                    // carryout! High-order 1, rest 0s, larger exp.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   798
                    decExponent += 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   799
                    digits[firstDigitIndex] = '1';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   802
                // else fall through.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   804
            digits[i] = (char) (q + 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   805
            decimalDigitsRoundedUp = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   808
        /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
         * Estimate decimal exponent. (If it is small-ish,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
         * we could double-check.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
         * First, scale the mantissa bits such that 1 <= d2 < 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
         * We are then going to estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
         *          log10(d2) ~=~  (d2-1.5)/1.5 + log(1.5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
         * and so we can estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
         *      log10(d) ~=~ log10(d2) + binExp * log10(2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         * take the floor and call it decExp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
         */
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   819
        static int estimateDecExp(long fractBits, int binExp) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   820
            double d2 = Double.longBitsToDouble( EXP_ONE | ( fractBits & DoubleConsts.SIGNIF_BIT_MASK ) );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   821
            double d = (d2-1.5D)*0.289529654D + 0.176091259 + (double)binExp * 0.301029995663981;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   822
            long dBits = Double.doubleToRawLongBits(d);  //can't be NaN here so use raw
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   823
            int exponent = (int)((dBits & DoubleConsts.EXP_BIT_MASK) >> EXP_SHIFT) - DoubleConsts.EXP_BIAS;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   824
            boolean isNegative = (dBits & DoubleConsts.SIGN_BIT_MASK) != 0; // discover sign
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   825
            if(exponent>=0 && exponent<52) { // hot path
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   826
                long mask   = DoubleConsts.SIGNIF_BIT_MASK >> exponent;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   827
                int r = (int)(( (dBits&DoubleConsts.SIGNIF_BIT_MASK) | FRACT_HOB )>>(EXP_SHIFT-exponent));
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   828
                return isNegative ? (((mask & dBits) == 0L ) ? -r : -r-1 ) : r;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   829
            } else if (exponent < 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   830
                return (((dBits&~DoubleConsts.SIGN_BIT_MASK) == 0) ? 0 :
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   831
                        ( (isNegative) ? -1 : 0) );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   832
            } else { //if (exponent >= 52)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   833
                return (int)d;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   836
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   837
        private static int insignificantDigits(int insignificant) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   838
            int i;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   839
            for ( i = 0; insignificant >= 10L; i++ ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   840
                insignificant /= 10L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   841
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   842
            return i;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   843
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   845
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   846
         * Calculates
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   847
         * <pre>
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   848
         * insignificantDigitsForPow2(v) == insignificantDigits(1L<<v)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   849
         * </pre>
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   850
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   851
        private static int insignificantDigitsForPow2(int p2) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   852
            if(p2>1 && p2 < insignificantDigitsNumber.length) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   853
                return insignificantDigitsNumber[p2];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   854
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   855
            return 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   856
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   857
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   858
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   859
         *  If insignificant==(1L << ixd)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   860
         *  i = insignificantDigitsNumber[idx] is the same as:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   861
         *  int i;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   862
         *  for ( i = 0; insignificant >= 10L; i++ )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   863
         *         insignificant /= 10L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   864
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   865
        private static int[] insignificantDigitsNumber = {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   866
            0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   867
            4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   868
            8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 11,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   869
            12, 12, 12, 12, 13, 13, 13, 14, 14, 14,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   870
            15, 15, 15, 15, 16, 16, 16, 17, 17, 17,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   871
            18, 18, 18, 19
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   872
        };
15255
0b53a05e0b07 7131459: [Fmt-De] DecimalFormat produces wrong format() results when close to a tie
darcy
parents: 14342
diff changeset
   873
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   874
        // approximately ceil( log2( long5pow[i] ) )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   875
        private static final int[] N_5_BITS = {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   876
                0,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   877
                3,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   878
                5,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   879
                7,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   880
                10,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   881
                12,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   882
                14,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   883
                17,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   884
                19,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   885
                21,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   886
                24,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   887
                26,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   888
                28,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   889
                31,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   890
                33,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   891
                35,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   892
                38,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   893
                40,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   894
                42,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   895
                45,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   896
                47,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   897
                49,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   898
                52,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   899
                54,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   900
                56,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   901
                59,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   902
                61,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   903
        };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   905
        private int getChars(char[] result) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   906
            assert nDigits <= 19 : nDigits; // generous bound on size of nDigits
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   907
            int i = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   908
            if (isNegative) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   909
                result[0] = '-';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   910
                i = 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   911
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            if (decExponent > 0 && decExponent < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                // print digits.digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                int charLength = Math.min(nDigits, decExponent);
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   915
                System.arraycopy(digits, firstDigitIndex, result, i, charLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                i += charLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                if (charLength < decExponent) {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   918
                    charLength = decExponent - charLength;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   919
                    Arrays.fill(result,i,i+charLength,'0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                    i += charLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                    result[i++] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                    result[i++] = '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    result[i++] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                    if (charLength < nDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                        int t = nDigits - charLength;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   927
                        System.arraycopy(digits, firstDigitIndex+charLength, result, i, t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                        i += t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                        result[i++] = '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   933
            } else if (decExponent <= 0 && decExponent > -3) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                result[i++] = '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                result[i++] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                if (decExponent != 0) {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   937
                    Arrays.fill(result, i, i-decExponent, '0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    i -= decExponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   940
                System.arraycopy(digits, firstDigitIndex, result, i, nDigits);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                i += nDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            } else {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   943
                result[i++] = digits[firstDigitIndex];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                result[i++] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                if (nDigits > 1) {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   946
                    System.arraycopy(digits, firstDigitIndex+1, result, i, nDigits - 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   947
                    i += nDigits - 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                    result[i++] = '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                result[i++] = 'E';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                int e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                if (decExponent <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                    result[i++] = '-';
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   955
                    e = -decExponent + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                } else {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   957
                    e = decExponent - 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                // decExponent has 1, 2, or 3, digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                if (e <= 9) {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   961
                    result[i++] = (char) (e + '0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                } else if (e <= 99) {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   963
                    result[i++] = (char) (e / 10 + '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   964
                    result[i++] = (char) (e % 10 + '0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                } else {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   966
                    result[i++] = (char) (e / 100 + '0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                    e %= 100;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   968
                    result[i++] = (char) (e / 10 + '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   969
                    result[i++] = (char) (e % 10 + '0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   972
            return i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   974
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   975
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   976
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   977
    private static final ThreadLocal<BinaryToASCIIBuffer> threadLocalBinaryToASCIIBuffer =
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   978
            new ThreadLocal<BinaryToASCIIBuffer>() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   979
                @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   980
                protected BinaryToASCIIBuffer initialValue() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   981
                    return new BinaryToASCIIBuffer();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   982
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   983
            };
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   984
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   985
    private static BinaryToASCIIBuffer getBinaryToASCIIBuffer() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   986
        return threadLocalBinaryToASCIIBuffer.get();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   987
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   988
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   989
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   990
     * A converter which can process an ASCII <code>String</code> representation
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   991
     * of a single or double precision floating point value into a
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   992
     * <code>float</code> or a <code>double</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   993
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   994
    interface ASCIIToBinaryConverter {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   995
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   996
        double doubleValue();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   997
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   998
        float floatValue();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
   999
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1000
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1001
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1002
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1003
     * A <code>ASCIIToBinaryConverter</code> container for a <code>double</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1004
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1005
    static class PreparedASCIIToBinaryBuffer implements ASCIIToBinaryConverter {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1006
        final private double doubleVal;
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1007
        final private float floatVal;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1008
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1009
        public PreparedASCIIToBinaryBuffer(double doubleVal, float floatVal) {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1010
            this.doubleVal = doubleVal;
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1011
            this.floatVal = floatVal;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1012
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1013
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1014
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1015
        public double doubleValue() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1016
            return doubleVal;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1017
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1018
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1019
        @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1020
        public float floatValue() {
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1021
            return floatVal;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1022
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1025
    static final ASCIIToBinaryConverter A2BC_POSITIVE_INFINITY = new PreparedASCIIToBinaryBuffer(Double.POSITIVE_INFINITY, Float.POSITIVE_INFINITY);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1026
    static final ASCIIToBinaryConverter A2BC_NEGATIVE_INFINITY = new PreparedASCIIToBinaryBuffer(Double.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1027
    static final ASCIIToBinaryConverter A2BC_NOT_A_NUMBER  = new PreparedASCIIToBinaryBuffer(Double.NaN, Float.NaN);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1028
    static final ASCIIToBinaryConverter A2BC_POSITIVE_ZERO = new PreparedASCIIToBinaryBuffer(0.0d, 0.0f);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1029
    static final ASCIIToBinaryConverter A2BC_NEGATIVE_ZERO = new PreparedASCIIToBinaryBuffer(-0.0d, -0.0f);
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1030
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1031
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1032
     * A buffered implementation of <code>ASCIIToBinaryConverter</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1033
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1034
    static class ASCIIToBinaryBuffer implements ASCIIToBinaryConverter {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1035
        boolean     isNegative;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1036
        int         decExponent;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1037
        char        digits[];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1038
        int         nDigits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1039
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1040
        ASCIIToBinaryBuffer( boolean negSign, int decExponent, char[] digits, int n)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1041
        {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1042
            this.isNegative = negSign;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1043
            this.decExponent = decExponent;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1044
            this.digits = digits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1045
            this.nDigits = n;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1046
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1047
18545
5f4e734fad1b 6469160: (fmt) general (%g) formatting of zero (0.0) with precision 0 or 1 throws ArrayOutOfBoundsException
bpb
parents: 18537
diff changeset
  1048
        /**
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1049
         * Takes a FloatingDecimal, which we presumably just scanned in,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1050
         * and finds out what its value is, as a double.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1051
         *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1052
         * AS A SIDE EFFECT, SET roundDir TO INDICATE PREFERRED
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1053
         * ROUNDING DIRECTION in case the result is really destined
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1054
         * for a single-precision float.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1055
         */
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1056
        @Override
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1057
        public double doubleValue() {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1058
            int kDigits = Math.min(nDigits, MAX_DECIMAL_DIGITS + 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1059
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1060
            // convert the lead kDigits to a long integer.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1061
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1062
            // (special performance hack: start to do it using int)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1063
            int iValue = (int) digits[0] - (int) '0';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1064
            int iDigits = Math.min(kDigits, INT_DECIMAL_DIGITS);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1065
            for (int i = 1; i < iDigits; i++) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1066
                iValue = iValue * 10 + (int) digits[i] - (int) '0';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1067
            }
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1068
            long lValue = (long) iValue;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1069
            for (int i = iDigits; i < kDigits; i++) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1070
                lValue = lValue * 10L + (long) ((int) digits[i] - (int) '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1071
            }
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1072
            double dValue = (double) lValue;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1073
            int exp = decExponent - kDigits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1074
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1075
            // lValue now contains a long integer with the value of
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1076
            // the first kDigits digits of the number.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1077
            // dValue contains the (double) of the same.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1078
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1079
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1080
            if (nDigits <= MAX_DECIMAL_DIGITS) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1081
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1082
                // possibly an easy case.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1083
                // We know that the digits can be represented
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1084
                // exactly. And if the exponent isn't too outrageous,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1085
                // the whole thing can be done with one operation,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1086
                // thus one rounding error.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1087
                // Note that all our constructors trim all leading and
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1088
                // trailing zeros, so simple values (including zero)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1089
                // will always end up here
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1090
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1091
                if (exp == 0 || dValue == 0.0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1092
                    return (isNegative) ? -dValue : dValue; // small floating integer
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1093
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1094
                else if (exp >= 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1095
                    if (exp <= MAX_SMALL_TEN) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1096
                        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1097
                        // Can get the answer with one operation,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1098
                        // thus one roundoff.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1099
                        //
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1100
                        double rValue = dValue * SMALL_10_POW[exp];
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1101
                        return (isNegative) ? -rValue : rValue;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1102
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1103
                    int slop = MAX_DECIMAL_DIGITS - kDigits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1104
                    if (exp <= MAX_SMALL_TEN + slop) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1105
                        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1106
                        // We can multiply dValue by 10^(slop)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1107
                        // and it is still "small" and exact.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1108
                        // Then we can multiply by 10^(exp-slop)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1109
                        // with one rounding.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1110
                        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1111
                        dValue *= SMALL_10_POW[slop];
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1112
                        double rValue = dValue * SMALL_10_POW[exp - slop];
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1113
                        return (isNegative) ? -rValue : rValue;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1114
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1115
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1116
                    // Else we have a hard case with a positive exp.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1117
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1118
                } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1119
                    if (exp >= -MAX_SMALL_TEN) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1120
                        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1121
                        // Can get the answer in one division.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1122
                        //
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1123
                        double rValue = dValue / SMALL_10_POW[-exp];
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1124
                        return (isNegative) ? -rValue : rValue;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1125
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1126
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1127
                    // Else we have a hard case with a negative exp.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1128
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1129
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1130
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1131
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1132
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1133
            // Harder cases:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1134
            // The sum of digits plus exponent is greater than
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1135
            // what we think we can do with one error.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1136
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1137
            // Start by approximating the right answer by,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1138
            // naively, scaling by powers of 10.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1139
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1140
            if (exp > 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1141
                if (decExponent > MAX_DECIMAL_EXPONENT + 1) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1142
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1143
                    // Lets face it. This is going to be
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1144
                    // Infinity. Cut to the chase.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1145
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1146
                    return (isNegative) ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1147
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1148
                if ((exp & 15) != 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1149
                    dValue *= SMALL_10_POW[exp & 15];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1150
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1151
                if ((exp >>= 4) != 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1152
                    int j;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1153
                    for (j = 0; exp > 1; j++, exp >>= 1) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1154
                        if ((exp & 1) != 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1155
                            dValue *= BIG_10_POW[j];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1156
                        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1157
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1158
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1159
                    // The reason for the weird exp > 1 condition
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1160
                    // in the above loop was so that the last multiply
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1161
                    // would get unrolled. We handle it here.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1162
                    // It could overflow.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1163
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1164
                    double t = dValue * BIG_10_POW[j];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1165
                    if (Double.isInfinite(t)) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1166
                        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1167
                        // It did overflow.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1168
                        // Look more closely at the result.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1169
                        // If the exponent is just one too large,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1170
                        // then use the maximum finite as our estimate
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1171
                        // value. Else call the result infinity
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1172
                        // and punt it.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1173
                        // ( I presume this could happen because
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1174
                        // rounding forces the result here to be
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1175
                        // an ULP or two larger than
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1176
                        // Double.MAX_VALUE ).
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1177
                        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1178
                        t = dValue / 2.0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1179
                        t *= BIG_10_POW[j];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1180
                        if (Double.isInfinite(t)) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1181
                            return (isNegative) ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1182
                        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1183
                        t = Double.MAX_VALUE;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1184
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1185
                    dValue = t;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1186
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1187
            } else if (exp < 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1188
                exp = -exp;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1189
                if (decExponent < MIN_DECIMAL_EXPONENT - 1) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1190
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1191
                    // Lets face it. This is going to be
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1192
                    // zero. Cut to the chase.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1193
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1194
                    return (isNegative) ? -0.0 : 0.0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1195
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1196
                if ((exp & 15) != 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1197
                    dValue /= SMALL_10_POW[exp & 15];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1198
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1199
                if ((exp >>= 4) != 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1200
                    int j;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1201
                    for (j = 0; exp > 1; j++, exp >>= 1) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1202
                        if ((exp & 1) != 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1203
                            dValue *= TINY_10_POW[j];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1204
                        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1205
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1206
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1207
                    // The reason for the weird exp > 1 condition
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1208
                    // in the above loop was so that the last multiply
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1209
                    // would get unrolled. We handle it here.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1210
                    // It could underflow.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1211
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1212
                    double t = dValue * TINY_10_POW[j];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1213
                    if (t == 0.0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1214
                        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1215
                        // It did underflow.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1216
                        // Look more closely at the result.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1217
                        // If the exponent is just one too small,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1218
                        // then use the minimum finite as our estimate
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1219
                        // value. Else call the result 0.0
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1220
                        // and punt it.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1221
                        // ( I presume this could happen because
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1222
                        // rounding forces the result here to be
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1223
                        // an ULP or two less than
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1224
                        // Double.MIN_VALUE ).
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1225
                        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1226
                        t = dValue * 2.0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1227
                        t *= TINY_10_POW[j];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1228
                        if (t == 0.0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1229
                            return (isNegative) ? -0.0 : 0.0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1230
                        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1231
                        t = Double.MIN_VALUE;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1232
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1233
                    dValue = t;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1234
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1236
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1237
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1238
            // dValue is now approximately the result.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1239
            // The hard part is adjusting it, by comparison
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1240
            // with FDBigInteger arithmetic.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1241
            // Formulate the EXACT big-number result as
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1242
            // bigD0 * 10^exp
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1243
            //
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1244
            if (nDigits > MAX_NDIGITS) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1245
                nDigits = MAX_NDIGITS + 1;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1246
                digits[MAX_NDIGITS] = '1';
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1247
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1248
            FDBigInteger bigD0 = new FDBigInteger(lValue, digits, kDigits, nDigits);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1249
            exp = decExponent - nDigits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1250
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1251
            long ieeeBits = Double.doubleToRawLongBits(dValue); // IEEE-754 bits of double candidate
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1252
            final int B5 = Math.max(0, -exp); // powers of 5 in bigB, value is not modified inside correctionLoop
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1253
            final int D5 = Math.max(0, exp); // powers of 5 in bigD, value is not modified inside correctionLoop
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1254
            bigD0 = bigD0.multByPow52(D5, 0);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1255
            bigD0.makeImmutable();   // prevent bigD0 modification inside correctionLoop
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1256
            FDBigInteger bigD = null;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1257
            int prevD2 = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1258
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1259
            correctionLoop:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1260
            while (true) {
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1261
                // here ieeeBits can't be NaN, Infinity or zero
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1262
                int binexp = (int) (ieeeBits >>> EXP_SHIFT);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1263
                long bigBbits = ieeeBits & DoubleConsts.SIGNIF_BIT_MASK;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1264
                if (binexp > 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1265
                    bigBbits |= FRACT_HOB;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1266
                } else { // Normalize denormalized numbers.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1267
                    assert bigBbits != 0L : bigBbits; // doubleToBigInt(0.0)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1268
                    int leadingZeros = Long.numberOfLeadingZeros(bigBbits);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1269
                    int shift = leadingZeros - (63 - EXP_SHIFT);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1270
                    bigBbits <<= shift;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1271
                    binexp = 1 - shift;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1272
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1273
                binexp -= DoubleConsts.EXP_BIAS;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1274
                int lowOrderZeros = Long.numberOfTrailingZeros(bigBbits);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1275
                bigBbits >>>= lowOrderZeros;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1276
                final int bigIntExp = binexp - EXP_SHIFT + lowOrderZeros;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1277
                final int bigIntNBits = EXP_SHIFT + 1 - lowOrderZeros;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1278
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1279
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1280
                // Scale bigD, bigB appropriately for
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1281
                // big-integer operations.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1282
                // Naively, we multiply by powers of ten
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1283
                // and powers of two. What we actually do
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1284
                // is keep track of the powers of 5 and
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1285
                // powers of 2 we would use, then factor out
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1286
                // common divisors before doing the work.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1287
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1288
                int B2 = B5; // powers of 2 in bigB
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1289
                int D2 = D5; // powers of 2 in bigD
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1290
                int Ulp2;   // powers of 2 in halfUlp.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1291
                if (bigIntExp >= 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1292
                    B2 += bigIntExp;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1293
                } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1294
                    D2 -= bigIntExp;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1295
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1296
                Ulp2 = B2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1297
                // shift bigB and bigD left by a number s. t.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1298
                // halfUlp is still an integer.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1299
                int hulpbias;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1300
                if (binexp <= -DoubleConsts.EXP_BIAS) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1301
                    // This is going to be a denormalized number
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1302
                    // (if not actually zero).
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1303
                    // half an ULP is at 2^-(DoubleConsts.EXP_BIAS+EXP_SHIFT+1)
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1304
                    hulpbias = binexp + lowOrderZeros + DoubleConsts.EXP_BIAS;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1305
                } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1306
                    hulpbias = 1 + lowOrderZeros;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1307
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1308
                B2 += hulpbias;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1309
                D2 += hulpbias;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1310
                // if there are common factors of 2, we might just as well
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1311
                // factor them out, as they add nothing useful.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1312
                int common2 = Math.min(B2, Math.min(D2, Ulp2));
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1313
                B2 -= common2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1314
                D2 -= common2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1315
                Ulp2 -= common2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1316
                // do multiplications by powers of 5 and 2
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1317
                FDBigInteger bigB = FDBigInteger.valueOfMulPow52(bigBbits, B5, B2);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1318
                if (bigD == null || prevD2 != D2) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1319
                    bigD = bigD0.leftShift(D2);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1320
                    prevD2 = D2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1321
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1322
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1323
                // to recap:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1324
                // bigB is the scaled-big-int version of our floating-point
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1325
                // candidate.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1326
                // bigD is the scaled-big-int version of the exact value
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1327
                // as we understand it.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1328
                // halfUlp is 1/2 an ulp of bigB, except for special cases
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1329
                // of exact powers of 2
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1330
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1331
                // the plan is to compare bigB with bigD, and if the difference
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1332
                // is less than halfUlp, then we're satisfied. Otherwise,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1333
                // use the ratio of difference to halfUlp to calculate a fudge
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1334
                // factor to add to the floating value, then go 'round again.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1335
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1336
                FDBigInteger diff;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1337
                int cmpResult;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1338
                boolean overvalue;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1339
                if ((cmpResult = bigB.cmp(bigD)) > 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1340
                    overvalue = true; // our candidate is too big.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1341
                    diff = bigB.leftInplaceSub(bigD); // bigB is not user further - reuse
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1342
                    if ((bigIntNBits == 1) && (bigIntExp > -DoubleConsts.EXP_BIAS + 1)) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1343
                        // candidate is a normalized exact power of 2 and
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1344
                        // is too big (larger than Double.MIN_NORMAL). We will be subtracting.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1345
                        // For our purposes, ulp is the ulp of the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1346
                        // next smaller range.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1347
                        Ulp2 -= 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1348
                        if (Ulp2 < 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1349
                            // rats. Cannot de-scale ulp this far.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1350
                            // must scale diff in other direction.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1351
                            Ulp2 = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1352
                            diff = diff.leftShift(1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1353
                        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1354
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1355
                } else if (cmpResult < 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1356
                    overvalue = false; // our candidate is too small.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1357
                    diff = bigD.rightInplaceSub(bigB); // bigB is not user further - reuse
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1358
                } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1359
                    // the candidate is exactly right!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1360
                    // this happens with surprising frequency
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1361
                    break correctionLoop;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1362
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1363
                cmpResult = diff.cmpPow52(B5, Ulp2);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1364
                if ((cmpResult) < 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1365
                    // difference is small.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1366
                    // this is close enough
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1367
                    break correctionLoop;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1368
                } else if (cmpResult == 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1369
                    // difference is exactly half an ULP
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1370
                    // round to some other value maybe, then finish
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1371
                    if ((ieeeBits & 1) != 0) { // half ties to even
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1372
                        ieeeBits += overvalue ? -1 : 1; // nextDown or nextUp
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1373
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1374
                    break correctionLoop;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1375
                } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1376
                    // difference is non-trivial.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1377
                    // could scale addend by ratio of difference to
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1378
                    // halfUlp here, if we bothered to compute that difference.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1379
                    // Most of the time ( I hope ) it is about 1 anyway.
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1380
                    ieeeBits += overvalue ? -1 : 1; // nextDown or nextUp
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1381
                    if (ieeeBits == 0 || ieeeBits == DoubleConsts.EXP_BIT_MASK) { // 0.0 or Double.POSITIVE_INFINITY
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1382
                        break correctionLoop; // oops. Fell off end of range.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1383
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1384
                    continue; // try again.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1385
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1386
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1387
            }
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1388
            if (isNegative) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1389
                ieeeBits |= DoubleConsts.SIGN_BIT_MASK;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1390
            }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1391
            return Double.longBitsToDouble(ieeeBits);
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1392
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1393
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1394
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1395
         * Takes a FloatingDecimal, which we presumably just scanned in,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1396
         * and finds out what its value is, as a float.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1397
         * This is distinct from doubleValue() to avoid the extremely
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1398
         * unlikely case of a double rounding error, wherein the conversion
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1399
         * to double has one rounding error, and the conversion of that double
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1400
         * to a float has another rounding error, IN THE WRONG DIRECTION,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1401
         * ( because of the preference to a zero low-order bit ).
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1402
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1403
        @Override
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1404
        public float floatValue() {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1405
            int kDigits = Math.min(nDigits, SINGLE_MAX_DECIMAL_DIGITS + 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1406
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1407
            // convert the lead kDigits to an integer.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1408
            //
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1409
            int iValue = (int) digits[0] - (int) '0';
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1410
            for (int i = 1; i < kDigits; i++) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1411
                iValue = iValue * 10 + (int) digits[i] - (int) '0';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1412
            }
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1413
            float fValue = (float) iValue;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1414
            int exp = decExponent - kDigits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1415
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1416
            // iValue now contains an integer with the value of
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1417
            // the first kDigits digits of the number.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1418
            // fValue contains the (float) of the same.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1419
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1420
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1421
            if (nDigits <= SINGLE_MAX_DECIMAL_DIGITS) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1422
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1423
                // possibly an easy case.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1424
                // We know that the digits can be represented
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1425
                // exactly. And if the exponent isn't too outrageous,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1426
                // the whole thing can be done with one operation,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1427
                // thus one rounding error.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1428
                // Note that all our constructors trim all leading and
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1429
                // trailing zeros, so simple values (including zero)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1430
                // will always end up here.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1431
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1432
                if (exp == 0 || fValue == 0.0f) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1433
                    return (isNegative) ? -fValue : fValue; // small floating integer
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1434
                } else if (exp >= 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1435
                    if (exp <= SINGLE_MAX_SMALL_TEN) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1436
                        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1437
                        // Can get the answer with one operation,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1438
                        // thus one roundoff.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1439
                        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1440
                        fValue *= SINGLE_SMALL_10_POW[exp];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1441
                        return (isNegative) ? -fValue : fValue;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1442
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1443
                    int slop = SINGLE_MAX_DECIMAL_DIGITS - kDigits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1444
                    if (exp <= SINGLE_MAX_SMALL_TEN + slop) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1445
                        //
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1446
                        // We can multiply fValue by 10^(slop)
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1447
                        // and it is still "small" and exact.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1448
                        // Then we can multiply by 10^(exp-slop)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1449
                        // with one rounding.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1450
                        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1451
                        fValue *= SINGLE_SMALL_10_POW[slop];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1452
                        fValue *= SINGLE_SMALL_10_POW[exp - slop];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1453
                        return (isNegative) ? -fValue : fValue;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1454
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1455
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1456
                    // Else we have a hard case with a positive exp.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1457
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1458
                } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1459
                    if (exp >= -SINGLE_MAX_SMALL_TEN) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1460
                        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1461
                        // Can get the answer in one division.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1462
                        //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1463
                        fValue /= SINGLE_SMALL_10_POW[-exp];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1464
                        return (isNegative) ? -fValue : fValue;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1465
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1466
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1467
                    // Else we have a hard case with a negative exp.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1468
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1469
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1470
            } else if ((decExponent >= nDigits) && (nDigits + decExponent <= MAX_DECIMAL_DIGITS)) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1471
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1472
                // In double-precision, this is an exact floating integer.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1473
                // So we can compute to double, then shorten to float
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1474
                // with one round, and get the right answer.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1475
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1476
                // First, finish accumulating digits.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1477
                // Then convert that integer to a double, multiply
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1478
                // by the appropriate power of ten, and convert to float.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1479
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1480
                long lValue = (long) iValue;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1481
                for (int i = kDigits; i < nDigits; i++) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1482
                    lValue = lValue * 10L + (long) ((int) digits[i] - (int) '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1483
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1484
                double dValue = (double) lValue;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1485
                exp = decExponent - nDigits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1486
                dValue *= SMALL_10_POW[exp];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1487
                fValue = (float) dValue;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1488
                return (isNegative) ? -fValue : fValue;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1489
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1490
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1491
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1492
            // Harder cases:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1493
            // The sum of digits plus exponent is greater than
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1494
            // what we think we can do with one error.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1495
            //
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1496
            // Start by approximating the right answer by,
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1497
            // naively, scaling by powers of 10.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1498
            // Scaling uses doubles to avoid overflow/underflow.
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1499
            //
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1500
            double dValue = fValue;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1501
            if (exp > 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1502
                if (decExponent > SINGLE_MAX_DECIMAL_EXPONENT + 1) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1503
                    //
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1504
                    // Lets face it. This is going to be
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1505
                    // Infinity. Cut to the chase.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1506
                    //
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1507
                    return (isNegative) ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1508
                }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1509
                if ((exp & 15) != 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1510
                    dValue *= SMALL_10_POW[exp & 15];
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1511
                }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1512
                if ((exp >>= 4) != 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1513
                    int j;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1514
                    for (j = 0; exp > 0; j++, exp >>= 1) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1515
                        if ((exp & 1) != 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1516
                            dValue *= BIG_10_POW[j];
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1517
                        }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1518
                    }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1519
                }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1520
            } else if (exp < 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1521
                exp = -exp;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1522
                if (decExponent < SINGLE_MIN_DECIMAL_EXPONENT - 1) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1523
                    //
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1524
                    // Lets face it. This is going to be
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1525
                    // zero. Cut to the chase.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1526
                    //
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1527
                    return (isNegative) ? -0.0f : 0.0f;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1528
                }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1529
                if ((exp & 15) != 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1530
                    dValue /= SMALL_10_POW[exp & 15];
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1531
                }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1532
                if ((exp >>= 4) != 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1533
                    int j;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1534
                    for (j = 0; exp > 0; j++, exp >>= 1) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1535
                        if ((exp & 1) != 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1536
                            dValue *= TINY_10_POW[j];
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1537
                        }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1538
                    }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1539
                }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1540
            }
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1541
            fValue = Math.max(Float.MIN_VALUE, Math.min(Float.MAX_VALUE, (float) dValue));
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1542
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1543
            //
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1544
            // fValue is now approximately the result.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1545
            // The hard part is adjusting it, by comparison
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1546
            // with FDBigInteger arithmetic.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1547
            // Formulate the EXACT big-number result as
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1548
            // bigD0 * 10^exp
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1549
            //
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1550
            if (nDigits > SINGLE_MAX_NDIGITS) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1551
                nDigits = SINGLE_MAX_NDIGITS + 1;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1552
                digits[SINGLE_MAX_NDIGITS] = '1';
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1553
            }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1554
            FDBigInteger bigD0 = new FDBigInteger(iValue, digits, kDigits, nDigits);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1555
            exp = decExponent - nDigits;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1556
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1557
            int ieeeBits = Float.floatToRawIntBits(fValue); // IEEE-754 bits of float candidate
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1558
            final int B5 = Math.max(0, -exp); // powers of 5 in bigB, value is not modified inside correctionLoop
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1559
            final int D5 = Math.max(0, exp); // powers of 5 in bigD, value is not modified inside correctionLoop
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1560
            bigD0 = bigD0.multByPow52(D5, 0);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1561
            bigD0.makeImmutable();   // prevent bigD0 modification inside correctionLoop
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1562
            FDBigInteger bigD = null;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1563
            int prevD2 = 0;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1564
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1565
            correctionLoop:
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1566
            while (true) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1567
                // here ieeeBits can't be NaN, Infinity or zero
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1568
                int binexp = ieeeBits >>> SINGLE_EXP_SHIFT;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1569
                int bigBbits = ieeeBits & FloatConsts.SIGNIF_BIT_MASK;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1570
                if (binexp > 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1571
                    bigBbits |= SINGLE_FRACT_HOB;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1572
                } else { // Normalize denormalized numbers.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1573
                    assert bigBbits != 0 : bigBbits; // floatToBigInt(0.0)
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1574
                    int leadingZeros = Integer.numberOfLeadingZeros(bigBbits);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1575
                    int shift = leadingZeros - (31 - SINGLE_EXP_SHIFT);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1576
                    bigBbits <<= shift;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1577
                    binexp = 1 - shift;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1578
                }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1579
                binexp -= FloatConsts.EXP_BIAS;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1580
                int lowOrderZeros = Integer.numberOfTrailingZeros(bigBbits);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1581
                bigBbits >>>= lowOrderZeros;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1582
                final int bigIntExp = binexp - SINGLE_EXP_SHIFT + lowOrderZeros;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1583
                final int bigIntNBits = SINGLE_EXP_SHIFT + 1 - lowOrderZeros;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1584
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1585
                //
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1586
                // Scale bigD, bigB appropriately for
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1587
                // big-integer operations.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1588
                // Naively, we multiply by powers of ten
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1589
                // and powers of two. What we actually do
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1590
                // is keep track of the powers of 5 and
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1591
                // powers of 2 we would use, then factor out
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1592
                // common divisors before doing the work.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1593
                //
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1594
                int B2 = B5; // powers of 2 in bigB
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1595
                int D2 = D5; // powers of 2 in bigD
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1596
                int Ulp2;   // powers of 2 in halfUlp.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1597
                if (bigIntExp >= 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1598
                    B2 += bigIntExp;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1599
                } else {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1600
                    D2 -= bigIntExp;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1601
                }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1602
                Ulp2 = B2;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1603
                // shift bigB and bigD left by a number s. t.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1604
                // halfUlp is still an integer.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1605
                int hulpbias;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1606
                if (binexp <= -FloatConsts.EXP_BIAS) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1607
                    // This is going to be a denormalized number
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1608
                    // (if not actually zero).
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1609
                    // half an ULP is at 2^-(FloatConsts.EXP_BIAS+SINGLE_EXP_SHIFT+1)
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1610
                    hulpbias = binexp + lowOrderZeros + FloatConsts.EXP_BIAS;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1611
                } else {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1612
                    hulpbias = 1 + lowOrderZeros;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1613
                }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1614
                B2 += hulpbias;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1615
                D2 += hulpbias;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1616
                // if there are common factors of 2, we might just as well
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1617
                // factor them out, as they add nothing useful.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1618
                int common2 = Math.min(B2, Math.min(D2, Ulp2));
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1619
                B2 -= common2;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1620
                D2 -= common2;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1621
                Ulp2 -= common2;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1622
                // do multiplications by powers of 5 and 2
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1623
                FDBigInteger bigB = FDBigInteger.valueOfMulPow52(bigBbits, B5, B2);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1624
                if (bigD == null || prevD2 != D2) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1625
                    bigD = bigD0.leftShift(D2);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1626
                    prevD2 = D2;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1627
                }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1628
                //
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1629
                // to recap:
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1630
                // bigB is the scaled-big-int version of our floating-point
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1631
                // candidate.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1632
                // bigD is the scaled-big-int version of the exact value
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1633
                // as we understand it.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1634
                // halfUlp is 1/2 an ulp of bigB, except for special cases
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1635
                // of exact powers of 2
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1636
                //
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1637
                // the plan is to compare bigB with bigD, and if the difference
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1638
                // is less than halfUlp, then we're satisfied. Otherwise,
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1639
                // use the ratio of difference to halfUlp to calculate a fudge
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1640
                // factor to add to the floating value, then go 'round again.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1641
                //
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1642
                FDBigInteger diff;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1643
                int cmpResult;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1644
                boolean overvalue;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1645
                if ((cmpResult = bigB.cmp(bigD)) > 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1646
                    overvalue = true; // our candidate is too big.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1647
                    diff = bigB.leftInplaceSub(bigD); // bigB is not user further - reuse
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1648
                    if ((bigIntNBits == 1) && (bigIntExp > -FloatConsts.EXP_BIAS + 1)) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1649
                        // candidate is a normalized exact power of 2 and
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1650
                        // is too big (larger than Float.MIN_NORMAL). We will be subtracting.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1651
                        // For our purposes, ulp is the ulp of the
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1652
                        // next smaller range.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1653
                        Ulp2 -= 1;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1654
                        if (Ulp2 < 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1655
                            // rats. Cannot de-scale ulp this far.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1656
                            // must scale diff in other direction.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1657
                            Ulp2 = 0;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1658
                            diff = diff.leftShift(1);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1659
                        }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1660
                    }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1661
                } else if (cmpResult < 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1662
                    overvalue = false; // our candidate is too small.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1663
                    diff = bigD.rightInplaceSub(bigB); // bigB is not user further - reuse
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1664
                } else {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1665
                    // the candidate is exactly right!
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1666
                    // this happens with surprising frequency
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1667
                    break correctionLoop;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1668
                }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1669
                cmpResult = diff.cmpPow52(B5, Ulp2);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1670
                if ((cmpResult) < 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1671
                    // difference is small.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1672
                    // this is close enough
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1673
                    break correctionLoop;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1674
                } else if (cmpResult == 0) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1675
                    // difference is exactly half an ULP
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1676
                    // round to some other value maybe, then finish
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1677
                    if ((ieeeBits & 1) != 0) { // half ties to even
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1678
                        ieeeBits += overvalue ? -1 : 1; // nextDown or nextUp
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1679
                    }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1680
                    break correctionLoop;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1681
                } else {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1682
                    // difference is non-trivial.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1683
                    // could scale addend by ratio of difference to
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1684
                    // halfUlp here, if we bothered to compute that difference.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1685
                    // Most of the time ( I hope ) it is about 1 anyway.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1686
                    ieeeBits += overvalue ? -1 : 1; // nextDown or nextUp
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1687
                    if (ieeeBits == 0 || ieeeBits == FloatConsts.EXP_BIT_MASK) { // 0.0 or Float.POSITIVE_INFINITY
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1688
                        break correctionLoop; // oops. Fell off end of range.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1689
                    }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1690
                    continue; // try again.
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1691
                }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1692
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1693
            }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1694
            if (isNegative) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1695
                ieeeBits |= FloatConsts.SIGN_BIT_MASK;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1696
            }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  1697
            return Float.intBitsToFloat(ieeeBits);
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1698
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1699
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1700
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1701
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1702
         * All the positive powers of 10 that can be
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1703
         * represented exactly in double/float.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1704
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1705
        private static final double[] SMALL_10_POW = {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1706
            1.0e0,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1707
            1.0e1, 1.0e2, 1.0e3, 1.0e4, 1.0e5,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1708
            1.0e6, 1.0e7, 1.0e8, 1.0e9, 1.0e10,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1709
            1.0e11, 1.0e12, 1.0e13, 1.0e14, 1.0e15,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1710
            1.0e16, 1.0e17, 1.0e18, 1.0e19, 1.0e20,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1711
            1.0e21, 1.0e22
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1714
        private static final float[] SINGLE_SMALL_10_POW = {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1715
            1.0e0f,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1716
            1.0e1f, 1.0e2f, 1.0e3f, 1.0e4f, 1.0e5f,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1717
            1.0e6f, 1.0e7f, 1.0e8f, 1.0e9f, 1.0e10f
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1718
        };
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1719
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1720
        private static final double[] BIG_10_POW = {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1721
            1e16, 1e32, 1e64, 1e128, 1e256 };
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1722
        private static final double[] TINY_10_POW = {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1723
            1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1724
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1725
        private static final int MAX_SMALL_TEN = SMALL_10_POW.length-1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1726
        private static final int SINGLE_MAX_SMALL_TEN = SINGLE_SMALL_10_POW.length-1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1727
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1728
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1729
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1730
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1731
     * Returns a <code>BinaryToASCIIConverter</code> for a <code>double</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1732
     * The returned object is a <code>ThreadLocal</code> variable of this class.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1733
     *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1734
     * @param d The double precision value to convert.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1735
     * @return The converter.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1736
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1737
    public static BinaryToASCIIConverter getBinaryToASCIIConverter(double d) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1738
        return getBinaryToASCIIConverter(d, true);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1739
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1740
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1741
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1742
     * Returns a <code>BinaryToASCIIConverter</code> for a <code>double</code>.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1743
     * The returned object is a <code>ThreadLocal</code> variable of this class.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1744
     *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1745
     * @param d The double precision value to convert.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1746
     * @param isCompatibleFormat
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1747
     * @return The converter.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1748
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1749
    static BinaryToASCIIConverter getBinaryToASCIIConverter(double d, boolean isCompatibleFormat) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1750
        long dBits = Double.doubleToRawLongBits(d);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1751
        boolean isNegative = (dBits&DoubleConsts.SIGN_BIT_MASK) != 0; // discover sign
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1752
        long fractBits = dBits & DoubleConsts.SIGNIF_BIT_MASK;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1753
        int  binExp = (int)( (dBits&DoubleConsts.EXP_BIT_MASK) >> EXP_SHIFT );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1754
        // Discover obvious special cases of NaN and Infinity.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1755
        if ( binExp == (int)(DoubleConsts.EXP_BIT_MASK>>EXP_SHIFT) ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1756
            if ( fractBits == 0L ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1757
                return isNegative ? B2AC_NEGATIVE_INFINITY : B2AC_POSITIVE_INFINITY;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1758
            } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1759
                return B2AC_NOT_A_NUMBER;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1760
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1761
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1762
        // Finish unpacking
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1763
        // Normalize denormalized numbers.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1764
        // Insert assumed high-order bit for normalized numbers.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1765
        // Subtract exponent bias.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1766
        int  nSignificantBits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1767
        if ( binExp == 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1768
            if ( fractBits == 0L ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1769
                // not a denorm, just a 0!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1770
                return isNegative ? B2AC_NEGATIVE_ZERO : B2AC_POSITIVE_ZERO;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1771
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1772
            int leadingZeros = Long.numberOfLeadingZeros(fractBits);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1773
            int shift = leadingZeros-(63-EXP_SHIFT);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1774
            fractBits <<= shift;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1775
            binExp = 1 - shift;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1776
            nSignificantBits =  64-leadingZeros; // recall binExp is  - shift count.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1777
        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1778
            fractBits |= FRACT_HOB;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1779
            nSignificantBits = EXP_SHIFT+1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1780
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1781
        binExp -= DoubleConsts.EXP_BIAS;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1782
        BinaryToASCIIBuffer buf = getBinaryToASCIIBuffer();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1783
        buf.setSign(isNegative);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1784
        // call the routine that actually does all the hard work.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1785
        buf.dtoa(binExp, fractBits, nSignificantBits, isCompatibleFormat);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1786
        return buf;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1787
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1788
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1789
    private static BinaryToASCIIConverter getBinaryToASCIIConverter(float f) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1790
        int fBits = Float.floatToRawIntBits( f );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1791
        boolean isNegative = (fBits&FloatConsts.SIGN_BIT_MASK) != 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1792
        int fractBits = fBits&FloatConsts.SIGNIF_BIT_MASK;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1793
        int binExp = (fBits&FloatConsts.EXP_BIT_MASK) >> SINGLE_EXP_SHIFT;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1794
        // Discover obvious special cases of NaN and Infinity.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1795
        if ( binExp == (FloatConsts.EXP_BIT_MASK>>SINGLE_EXP_SHIFT) ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1796
            if ( fractBits == 0L ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1797
                return isNegative ? B2AC_NEGATIVE_INFINITY : B2AC_POSITIVE_INFINITY;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1798
            } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1799
                return B2AC_NOT_A_NUMBER;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1800
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1801
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1802
        // Finish unpacking
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1803
        // Normalize denormalized numbers.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1804
        // Insert assumed high-order bit for normalized numbers.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1805
        // Subtract exponent bias.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1806
        int  nSignificantBits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1807
        if ( binExp == 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1808
            if ( fractBits == 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1809
                // not a denorm, just a 0!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1810
                return isNegative ? B2AC_NEGATIVE_ZERO : B2AC_POSITIVE_ZERO;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1811
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1812
            int leadingZeros = Integer.numberOfLeadingZeros(fractBits);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1813
            int shift = leadingZeros-(31-SINGLE_EXP_SHIFT);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1814
            fractBits <<= shift;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1815
            binExp = 1 - shift;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1816
            nSignificantBits =  32 - leadingZeros; // recall binExp is  - shift count.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1817
        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1818
            fractBits |= SINGLE_FRACT_HOB;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1819
            nSignificantBits = SINGLE_EXP_SHIFT+1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1820
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1821
        binExp -= FloatConsts.EXP_BIAS;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1822
        BinaryToASCIIBuffer buf = getBinaryToASCIIBuffer();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1823
        buf.setSign(isNegative);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1824
        // call the routine that actually does all the hard work.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1825
        buf.dtoa(binExp, ((long)fractBits)<<(EXP_SHIFT-SINGLE_EXP_SHIFT), nSignificantBits, true);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1826
        return buf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10598
diff changeset
  1829
    @SuppressWarnings("fallthrough")
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1830
    static ASCIIToBinaryConverter readJavaFormatString( String in ) throws NumberFormatException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        boolean isNegative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        boolean signSeen   = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        int     decExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        char    c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
    parseNumber:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            in = in.trim(); // don't fool around with white space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                            // throws NullPointerException if null
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1840
            int len = in.length();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1841
            if ( len == 0 ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1842
                throw new NumberFormatException("empty String");
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1843
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            int i = 0;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1845
            switch (in.charAt(i)){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            case '-':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                isNegative = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                //FALLTHROUGH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                signSeen = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            c = in.charAt(i);
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1854
            if(c == 'N') { // Check for NaN
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1855
                if((len-i)==NAN_LENGTH && in.indexOf(NAN_REP,i)==i) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1856
                    return A2BC_NOT_A_NUMBER;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1858
                // something went wrong, throw exception
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1859
                break parseNumber;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1860
            } else if(c == 'I') { // Check for Infinity strings
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1861
                if((len-i)==INFINITY_LENGTH && in.indexOf(INFINITY_REP,i)==i) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1862
                    return isNegative? A2BC_NEGATIVE_INFINITY : A2BC_POSITIVE_INFINITY;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1863
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1864
                // something went wrong, throw exception
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1865
                break parseNumber;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1866
            } else if (c == '0')  { // check for hexadecimal floating-point number
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1867
                if (len > i+1 ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1868
                    char ch = in.charAt(i+1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1869
                    if (ch == 'x' || ch == 'X' ) { // possible hex string
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1870
                        return parseHexString(in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            }  // look for and process decimal floating-point string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1875
            char[] digits = new char[ len ];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            int    nDigits= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            boolean decSeen = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
            int decPt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
            int nLeadZero = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            int nTrailZero= 0;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1881
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1882
        skipLeadingZerosLoop:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1883
            while (i < len) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1884
                c = in.charAt(i);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1885
                if (c == '0') {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1886
                    nLeadZero++;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1887
                } else if (c == '.') {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1888
                    if (decSeen) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                        // already saw one ., this is the 2nd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                        throw new NumberFormatException("multiple points");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                    decPt = i;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1893
                    if (signSeen) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                        decPt -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                    decSeen = true;
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1897
                } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1898
                    break skipLeadingZerosLoop;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1899
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1900
                i++;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1901
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1902
        digitLoop:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1903
            while (i < len) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1904
                c = in.charAt(i);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1905
                if (c >= '1' && c <= '9') {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1906
                    digits[nDigits++] = c;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1907
                    nTrailZero = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1908
                } else if (c == '0') {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1909
                    digits[nDigits++] = c;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1910
                    nTrailZero++;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1911
                } else if (c == '.') {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1912
                    if (decSeen) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1913
                        // already saw one ., this is the 2nd.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1914
                        throw new NumberFormatException("multiple points");
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1915
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1916
                    decPt = i;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1917
                    if (signSeen) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1918
                        decPt -= 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1919
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1920
                    decSeen = true;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1921
                } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                    break digitLoop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1926
            nDigits -=nTrailZero;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1927
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1928
            // At this point, we've scanned all the digits and decimal
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1929
            // point we're going to see. Trim off leading and trailing
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1930
            // zeros, which will just confuse us later, and adjust
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1931
            // our initial decimal exponent accordingly.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1932
            // To review:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1933
            // we have seen i total characters.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1934
            // nLeadZero of them were zeros before any other digits.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1935
            // nTrailZero of them were zeros after any other digits.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1936
            // if ( decSeen ), then a . was seen after decPt characters
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1937
            // ( including leading zeros which have been discarded )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1938
            // nDigits characters were neither lead nor trailing
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1939
            // zeros, nor point
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1940
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1941
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1942
            // special hack: if we saw no non-zero digits, then the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1943
            // answer is zero!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1944
            // Unfortunately, we feel honor-bound to keep parsing!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1945
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1946
            boolean isZero = (nDigits == 0);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1947
            if ( isZero &&  nLeadZero == 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1948
                // we saw NO DIGITS AT ALL,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1949
                // not even a crummy 0!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1950
                // this is not allowed.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1951
                break parseNumber; // go throw exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1953
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1954
            // Our initial exponent is decPt, adjusted by the number of
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1955
            // discarded zeros. Or, if there was no decPt,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1956
            // then its just nDigits adjusted by discarded trailing zeros.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1957
            //
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            if ( decSeen ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                decExp = decPt - nLeadZero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            } else {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1961
                decExp = nDigits + nTrailZero;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1964
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1965
            // Look for 'e' or 'E' and an optionally signed integer.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1966
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1967
            if ( (i < len) &&  (((c = in.charAt(i) )=='e') || (c == 'E') ) ){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                int expSign = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                int expVal  = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                int reallyBig = Integer.MAX_VALUE / 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                boolean expOverflow = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                switch( in.charAt(++i) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                case '-':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                    expSign = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                    //FALLTHROUGH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                int expAt = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            expLoop:
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1981
                while ( i < len  ){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                    if ( expVal >= reallyBig ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                        // the next character will cause integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                        // overflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                        expOverflow = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                    }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1987
                    c = in.charAt(i++);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1988
                    if(c>='0' && c<='9') {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                        expVal = expVal*10 + ( (int)c - (int)'0' );
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  1990
                    } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                        i--;           // back up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                        break expLoop; // stop parsing exponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                }
26722
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  1995
                int expLimit = BIG_DECIMAL_EXPONENT + nDigits + nTrailZero;
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  1996
                if (expOverflow || (expVal > expLimit)) {
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  1997
                    // There is still a chance that the exponent will be safe to
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  1998
                    // use: if it would eventually decrease due to a negative
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  1999
                    // decExp, and that number is below the limit.  We check for
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2000
                    // that here.
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2001
                    if (!expOverflow && (expSign == 1 && decExp < 0)
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2002
                            && (expVal + decExp) < expLimit) {
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2003
                        // Cannot overflow: adding a positive and negative number.
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2004
                        decExp += expVal;
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2005
                    } else {
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2006
                        //
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2007
                        // The intent here is to end up with
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2008
                        // infinity or zero, as appropriate.
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2009
                        // The reason for yielding such a small decExponent,
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2010
                        // rather than something intuitive such as
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2011
                        // expSign*Integer.MAX_VALUE, is that this value
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2012
                        // is subject to further manipulation in
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2013
                        // doubleValue() and floatValue(), and I don't want
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2014
                        // it to be able to cause overflow there!
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2015
                        // (The only way we can get into trouble here is for
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2016
                        // really outrageous nDigits+nTrailZero, such as 2
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2017
                        // billion.)
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2018
                        //
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2019
                        decExp = expSign * expLimit;
eb30ed2a0bfe 8043740: Doubles with large exponents overflow to Infinity incorrectly
bpb
parents: 25859
diff changeset
  2020
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                    // this should not overflow, since we tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                    // for expVal > (MAX+N), where N >= abs(decExp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                    decExp = decExp + expSign*expVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                // if we saw something not a digit ( or end of string )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                // after the [Ee][+-], without seeing any digits at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                // this is certainly an error. If we saw some digits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                // but then some trailing garbage, that might be ok.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                // so we just fall through in that case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                // HUMBUG
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2033
                if ( i == expAt ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                    break parseNumber; // certainly bad
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2035
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2037
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2038
            // We parsed everything we could.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2039
            // If there are leftovers, then this is not good input!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2040
            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2041
            if ( i < len &&
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2042
                ((i != len - 1) ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                (in.charAt(i) != 'f' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                 in.charAt(i) != 'F' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                 in.charAt(i) != 'd' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
                 in.charAt(i) != 'D'))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                break parseNumber; // go throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2049
            if(isZero) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2050
                return isNegative ? A2BC_NEGATIVE_ZERO : A2BC_POSITIVE_ZERO;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2051
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2052
            return new ASCIIToBinaryBuffer(isNegative, decExp, digits, nDigits);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        } catch ( StringIndexOutOfBoundsException e ){ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        throw new NumberFormatException("For input string: \"" + in + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2057
    private static class HexFloatPattern {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2058
        /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2059
         * Grammar is compatible with hexadecimal floating-point constants
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2060
         * described in section 6.4.4.2 of the C99 specification.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2061
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2062
        private static final Pattern VALUE = Pattern.compile(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                   //1           234                   56                7                   8      9
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                    "([-+])?0[xX](((\\p{XDigit}+)\\.?)|((\\p{XDigit}*)\\.(\\p{XDigit}+)))[pP]([-+])?(\\p{Digit}+)[fFdD]?"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                    );
2170
f4454a8d22de 6799689: Make sun.misc.FloatingDecimal.hexFloatPattern static field initialized lazily
mchung
parents: 2
diff changeset
  2066
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2068
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2069
     * Converts string s to a suitable floating decimal; uses the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2070
     * double constructor and sets the roundDir variable appropriately
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * in case the value is later converted to a float.
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2072
     *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2073
     * @param s The <code>String</code> to parse.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     */
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2075
   static ASCIIToBinaryConverter parseHexString(String s) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2076
            // Verify string is a member of the hexadecimal floating-point
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2077
            // string language.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2078
            Matcher m = HexFloatPattern.VALUE.matcher(s);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2079
            boolean validInput = m.matches();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2080
            if (!validInput) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2081
                // Input does not match pattern
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2082
                throw new NumberFormatException("For input string: \"" + s + "\"");
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2083
            } else { // validInput
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2084
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2085
                // We must isolate the sign, significand, and exponent
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2086
                // fields.  The sign value is straightforward.  Since
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2087
                // floating-point numbers are stored with a normalized
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2088
                // representation, the significand and exponent are
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2089
                // interrelated.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                //
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2091
                // After extracting the sign, we normalized the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2092
                // significand as a hexadecimal value, calculating an
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2093
                // exponent adjust for any shifts made during
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2094
                // normalization.  If the significand is zero, the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2095
                // exponent doesn't need to be examined since the output
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2096
                // will be zero.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2097
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2098
                // Next the exponent in the input string is extracted.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2099
                // Afterwards, the significand is normalized as a *binary*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2100
                // value and the input value's normalized exponent can be
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2101
                // computed.  The significand bits are copied into a
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2102
                // double significand; if the string has more logical bits
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2103
                // than can fit in a double, the extra bits affect the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2104
                // round and sticky bits which are used to round the final
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2105
                // value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2106
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2107
                //  Extract significand sign
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2108
                String group1 = m.group(1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2109
                boolean isNegative = ((group1 != null) && group1.equals("-"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2111
                //  Extract Significand magnitude
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2112
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2113
                // Based on the form of the significand, calculate how the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2114
                // binary exponent needs to be adjusted to create a
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2115
                // normalized//hexadecimal* floating-point number; that
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2116
                // is, a number where there is one nonzero hex digit to
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2117
                // the left of the (hexa)decimal point.  Since we are
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2118
                // adjusting a binary, not hexadecimal exponent, the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2119
                // exponent is adjusted by a multiple of 4.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2120
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2121
                // There are a number of significand scenarios to consider;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2122
                // letters are used in indicate nonzero digits:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2123
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2124
                // 1. 000xxxx       =>      x.xxx   normalized
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2125
                //    increase exponent by (number of x's - 1)*4
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2126
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2127
                // 2. 000xxx.yyyy =>        x.xxyyyy        normalized
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2128
                //    increase exponent by (number of x's - 1)*4
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2129
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2130
                // 3. .000yyy  =>   y.yy    normalized
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2131
                //    decrease exponent by (number of zeros + 1)*4
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2132
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2133
                // 4. 000.00000yyy => y.yy normalized
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2134
                //    decrease exponent by (number of zeros to right of point + 1)*4
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2135
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2136
                // If the significand is exactly zero, return a properly
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2137
                // signed zero.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2138
                //
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2140
                String significandString = null;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2141
                int signifLength = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2142
                int exponentAdjust = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2143
                {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2144
                    int leftDigits = 0; // number of meaningful digits to
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2145
                    // left of "decimal" point
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2146
                    // (leading zeros stripped)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2147
                    int rightDigits = 0; // number of digits to right of
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2148
                    // "decimal" point; leading zeros
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2149
                    // must always be accounted for
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2150
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2151
                    // The significand is made up of either
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2152
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2153
                    // 1. group 4 entirely (integer portion only)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2154
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2155
                    // OR
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2156
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2157
                    // 2. the fractional portion from group 7 plus any
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2158
                    // (optional) integer portions from group 6.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2159
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2160
                    String group4;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2161
                    if ((group4 = m.group(4)) != null) {  // Integer-only significand
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2162
                        // Leading zeros never matter on the integer portion
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2163
                        significandString = stripLeadingZeros(group4);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2164
                        leftDigits = significandString.length();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2165
                    } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2166
                        // Group 6 is the optional integer; leading zeros
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2167
                        // never matter on the integer portion
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2168
                        String group6 = stripLeadingZeros(m.group(6));
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2169
                        leftDigits = group6.length();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2171
                        // fraction
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2172
                        String group7 = m.group(7);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2173
                        rightDigits = group7.length();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2175
                        // Turn "integer.fraction" into "integer"+"fraction"
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2176
                        significandString =
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2177
                                ((group6 == null) ? "" : group6) + // is the null
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2178
                                        // check necessary?
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2179
                                        group7;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2180
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2182
                    significandString = stripLeadingZeros(significandString);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2183
                    signifLength = significandString.length();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2185
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2186
                    // Adjust exponent as described above
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2187
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2188
                    if (leftDigits >= 1) {  // Cases 1 and 2
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2189
                        exponentAdjust = 4 * (leftDigits - 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2190
                    } else {                // Cases 3 and 4
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2191
                        exponentAdjust = -4 * (rightDigits - signifLength + 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2192
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2194
                    // If the significand is zero, the exponent doesn't
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2195
                    // matter; return a properly signed zero.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2197
                    if (signifLength == 0) { // Only zeros in input
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2198
                        return isNegative ? A2BC_NEGATIVE_ZERO : A2BC_POSITIVE_ZERO;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2202
                //  Extract Exponent
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2203
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2204
                // Use an int to read in the exponent value; this should
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2205
                // provide more than sufficient range for non-contrived
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2206
                // inputs.  If reading the exponent in as an int does
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2207
                // overflow, examine the sign of the exponent and
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2208
                // significand to determine what to do.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2209
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2210
                String group8 = m.group(8);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2211
                boolean positiveExponent = (group8 == null) || group8.equals("+");
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2212
                long unsignedRawExponent;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2213
                try {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2214
                    unsignedRawExponent = Integer.parseInt(m.group(9));
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2215
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2216
                catch (NumberFormatException e) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2217
                    // At this point, we know the exponent is
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2218
                    // syntactically well-formed as a sequence of
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2219
                    // digits.  Therefore, if an NumberFormatException
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2220
                    // is thrown, it must be due to overflowing int's
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2221
                    // range.  Also, at this point, we have already
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2222
                    // checked for a zero significand.  Thus the signs
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2223
                    // of the exponent and significand determine the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2224
                    // final result:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2225
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2226
                    //                      significand
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2227
                    //                      +               -
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2228
                    // exponent     +       +infinity       -infinity
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2229
                    //              -       +0.0            -0.0
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2230
                    return isNegative ?
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2231
                              (positiveExponent ? A2BC_NEGATIVE_INFINITY : A2BC_NEGATIVE_ZERO)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2232
                            : (positiveExponent ? A2BC_POSITIVE_INFINITY : A2BC_POSITIVE_ZERO);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2233
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2234
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2235
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2236
                long rawExponent =
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2237
                        (positiveExponent ? 1L : -1L) * // exponent sign
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2238
                                unsignedRawExponent;            // exponent magnitude
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2239
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2240
                // Calculate partially adjusted exponent
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2241
                long exponent = rawExponent + exponentAdjust;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2242
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2243
                // Starting copying non-zero bits into proper position in
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2244
                // a long; copy explicit bit too; this will be masked
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2245
                // later for normal values.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2246
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2247
                boolean round = false;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2248
                boolean sticky = false;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2249
                int nextShift = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2250
                long significand = 0L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2251
                // First iteration is different, since we only copy
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2252
                // from the leading significand bit; one more exponent
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2253
                // adjust will be needed...
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2255
                // IMPORTANT: make leadingDigit a long to avoid
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2256
                // surprising shift semantics!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2257
                long leadingDigit = getHexDigit(significandString, 0);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2258
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2259
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2260
                // Left shift the leading digit (53 - (bit position of
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2261
                // leading 1 in digit)); this sets the top bit of the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2262
                // significand to 1.  The nextShift value is adjusted
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2263
                // to take into account the number of bit positions of
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2264
                // the leadingDigit actually used.  Finally, the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2265
                // exponent is adjusted to normalize the significand
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2266
                // as a binary value, not just a hex value.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2267
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2268
                if (leadingDigit == 1) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2269
                    significand |= leadingDigit << 52;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2270
                    nextShift = 52 - 4;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2271
                    // exponent += 0
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2272
                } else if (leadingDigit <= 3) { // [2, 3]
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2273
                    significand |= leadingDigit << 51;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2274
                    nextShift = 52 - 5;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2275
                    exponent += 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2276
                } else if (leadingDigit <= 7) { // [4, 7]
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2277
                    significand |= leadingDigit << 50;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2278
                    nextShift = 52 - 6;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2279
                    exponent += 2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2280
                } else if (leadingDigit <= 15) { // [8, f]
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2281
                    significand |= leadingDigit << 49;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2282
                    nextShift = 52 - 7;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2283
                    exponent += 3;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2284
                } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2285
                    throw new AssertionError("Result from digit conversion too large!");
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2286
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2287
                // The preceding if-else could be replaced by a single
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2288
                // code block based on the high-order bit set in
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2289
                // leadingDigit.  Given leadingOnePosition,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2290
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2291
                // significand |= leadingDigit << (SIGNIFICAND_WIDTH - leadingOnePosition);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2292
                // nextShift = 52 - (3 + leadingOnePosition);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2293
                // exponent += (leadingOnePosition-1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2294
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2295
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2296
                // Now the exponent variable is equal to the normalized
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2297
                // binary exponent.  Code below will make representation
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2298
                // adjustments if the exponent is incremented after
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2299
                // rounding (includes overflows to infinity) or if the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2300
                // result is subnormal.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2301
                //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2302
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2303
                // Copy digit into significand until the significand can't
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2304
                // hold another full hex digit or there are no more input
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2305
                // hex digits.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2306
                int i = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2307
                for (i = 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2308
                     i < signifLength && nextShift >= 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2309
                     i++) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2310
                    long currentDigit = getHexDigit(significandString, i);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2311
                    significand |= (currentDigit << nextShift);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2312
                    nextShift -= 4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2315
                // After the above loop, the bulk of the string is copied.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2316
                // Now, we must copy any partial hex digits into the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2317
                // significand AND compute the round bit and start computing
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2318
                // sticky bit.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2319
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2320
                if (i < signifLength) { // at least one hex input digit exists
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2321
                    long currentDigit = getHexDigit(significandString, i);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2322
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2323
                    // from nextShift, figure out how many bits need
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2324
                    // to be copied, if any
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2325
                    switch (nextShift) { // must be negative
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2326
                        case -1:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2327
                            // three bits need to be copied in; can
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2328
                            // set round bit
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2329
                            significand |= ((currentDigit & 0xEL) >> 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2330
                            round = (currentDigit & 0x1L) != 0L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2331
                            break;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2332
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2333
                        case -2:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2334
                            // two bits need to be copied in; can
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2335
                            // set round and start sticky
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2336
                            significand |= ((currentDigit & 0xCL) >> 2);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2337
                            round = (currentDigit & 0x2L) != 0L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2338
                            sticky = (currentDigit & 0x1L) != 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2339
                            break;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2340
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2341
                        case -3:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2342
                            // one bit needs to be copied in
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2343
                            significand |= ((currentDigit & 0x8L) >> 3);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2344
                            // Now set round and start sticky, if possible
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2345
                            round = (currentDigit & 0x4L) != 0L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2346
                            sticky = (currentDigit & 0x3L) != 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2347
                            break;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2348
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2349
                        case -4:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2350
                            // all bits copied into significand; set
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2351
                            // round and start sticky
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2352
                            round = ((currentDigit & 0x8L) != 0);  // is top bit set?
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2353
                            // nonzeros in three low order bits?
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2354
                            sticky = (currentDigit & 0x7L) != 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2355
                            break;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2356
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2357
                        default:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2358
                            throw new AssertionError("Unexpected shift distance remainder.");
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2359
                            // break;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2360
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2361
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2362
                    // Round is set; sticky might be set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2364
                    // For the sticky bit, it suffices to check the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2365
                    // current digit and test for any nonzero digits in
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2366
                    // the remaining unprocessed input.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2367
                    i++;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2368
                    while (i < signifLength && !sticky) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2369
                        currentDigit = getHexDigit(significandString, i);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2370
                        sticky = sticky || (currentDigit != 0);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2371
                        i++;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2372
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2373
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2374
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2375
                // else all of string was seen, round and sticky are
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2376
                // correct as false.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2377
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2378
                // Float calculations
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2379
                int floatBits = isNegative ? FloatConsts.SIGN_BIT_MASK : 0;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2380
                if (exponent >= FloatConsts.MIN_EXPONENT) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2381
                    if (exponent > FloatConsts.MAX_EXPONENT) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2382
                        // Float.POSITIVE_INFINITY
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2383
                        floatBits |= FloatConsts.EXP_BIT_MASK;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2384
                    } else {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2385
                        int threshShift = DoubleConsts.SIGNIFICAND_WIDTH - FloatConsts.SIGNIFICAND_WIDTH - 1;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2386
                        boolean floatSticky = (significand & ((1L << threshShift) - 1)) != 0 || round || sticky;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2387
                        int iValue = (int) (significand >>> threshShift);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2388
                        if ((iValue & 3) != 1 || floatSticky) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2389
                            iValue++;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2390
                        }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2391
                        floatBits |= (((((int) exponent) + (FloatConsts.EXP_BIAS - 1))) << SINGLE_EXP_SHIFT) + (iValue >> 1);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2392
                    }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2393
                } else {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2394
                    if (exponent < FloatConsts.MIN_SUB_EXPONENT - 1) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2395
                        // 0
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2396
                    } else {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2397
                        // exponent == -127 ==> threshShift = 53 - 2 + (-149) - (-127) = 53 - 24
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2398
                        int threshShift = (int) ((DoubleConsts.SIGNIFICAND_WIDTH - 2 + FloatConsts.MIN_SUB_EXPONENT) - exponent);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2399
                        assert threshShift >= DoubleConsts.SIGNIFICAND_WIDTH - FloatConsts.SIGNIFICAND_WIDTH;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2400
                        assert threshShift < DoubleConsts.SIGNIFICAND_WIDTH;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2401
                        boolean floatSticky = (significand & ((1L << threshShift) - 1)) != 0 || round || sticky;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2402
                        int iValue = (int) (significand >>> threshShift);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2403
                        if ((iValue & 3) != 1 || floatSticky) {
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2404
                            iValue++;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2405
                        }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2406
                        floatBits |= iValue >> 1;
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2407
                    }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2408
                }
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2409
                float fValue = Float.intBitsToFloat(floatBits);
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2410
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2411
                // Check for overflow and update exponent accordingly.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2412
                if (exponent > DoubleConsts.MAX_EXPONENT) {         // Infinite result
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2413
                    // overflow to properly signed infinity
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2414
                    return isNegative ? A2BC_NEGATIVE_INFINITY : A2BC_POSITIVE_INFINITY;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2415
                } else {  // Finite return value
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2416
                    if (exponent <= DoubleConsts.MAX_EXPONENT && // (Usually) normal result
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2417
                            exponent >= DoubleConsts.MIN_EXPONENT) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2418
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2419
                        // The result returned in this block cannot be a
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2420
                        // zero or subnormal; however after the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2421
                        // significand is adjusted from rounding, we could
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2422
                        // still overflow in infinity.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2423
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2424
                        // AND exponent bits into significand; if the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2425
                        // significand is incremented and overflows from
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2426
                        // rounding, this combination will update the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2427
                        // exponent correctly, even in the case of
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2428
                        // Double.MAX_VALUE overflowing to infinity.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2429
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2430
                        significand = ((( exponent +
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2431
                                (long) DoubleConsts.EXP_BIAS) <<
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2432
                                (DoubleConsts.SIGNIFICAND_WIDTH - 1))
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2433
                                & DoubleConsts.EXP_BIT_MASK) |
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2434
                                (DoubleConsts.SIGNIF_BIT_MASK & significand);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2435
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2436
                    } else {  // Subnormal or zero
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2437
                        // (exponent < DoubleConsts.MIN_EXPONENT)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2438
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2439
                        if (exponent < (DoubleConsts.MIN_SUB_EXPONENT - 1)) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2440
                            // No way to round back to nonzero value
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2441
                            // regardless of significand if the exponent is
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2442
                            // less than -1075.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2443
                            return isNegative ? A2BC_NEGATIVE_ZERO : A2BC_POSITIVE_ZERO;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2444
                        } else { //  -1075 <= exponent <= MIN_EXPONENT -1 = -1023
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2445
                            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2446
                            // Find bit position to round to; recompute
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2447
                            // round and sticky bits, and shift
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2448
                            // significand right appropriately.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2449
                            //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2450
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2451
                            sticky = sticky || round;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2452
                            round = false;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2453
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2454
                            // Number of bits of significand to preserve is
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2455
                            // exponent - abs_min_exp +1
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2456
                            // check:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2457
                            // -1075 +1074 + 1 = 0
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2458
                            // -1023 +1074 + 1 = 52
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2459
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2460
                            int bitsDiscarded = 53 -
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2461
                                    ((int) exponent - DoubleConsts.MIN_SUB_EXPONENT + 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2462
                            assert bitsDiscarded >= 1 && bitsDiscarded <= 53;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2464
                            // What to do here:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2465
                            // First, isolate the new round bit
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2466
                            round = (significand & (1L << (bitsDiscarded - 1))) != 0L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2467
                            if (bitsDiscarded > 1) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2468
                                // create mask to update sticky bits; low
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2469
                                // order bitsDiscarded bits should be 1
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2470
                                long mask = ~((~0L) << (bitsDiscarded - 1));
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2471
                                sticky = sticky || ((significand & mask) != 0L);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2472
                            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2473
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2474
                            // Now, discard the bits
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2475
                            significand = significand >> bitsDiscarded;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2476
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2477
                            significand = ((((long) (DoubleConsts.MIN_EXPONENT - 1) + // subnorm exp.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2478
                                    (long) DoubleConsts.EXP_BIAS) <<
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2479
                                    (DoubleConsts.SIGNIFICAND_WIDTH - 1))
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2480
                                    & DoubleConsts.EXP_BIT_MASK) |
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2481
                                    (DoubleConsts.SIGNIF_BIT_MASK & significand);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2482
                        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2483
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2484
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2485
                    // The significand variable now contains the currently
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2486
                    // appropriate exponent bits too.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2487
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2488
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2489
                    // Determine if significand should be incremented;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2490
                    // making this determination depends on the least
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2491
                    // significant bit and the round and sticky bits.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2492
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2493
                    // Round to nearest even rounding table, adapted from
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2494
                    // table 4.7 in "Computer Arithmetic" by IsraelKoren.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2495
                    // The digit to the left of the "decimal" point is the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2496
                    // least significant bit, the digits to the right of
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2497
                    // the point are the round and sticky bits
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2498
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2499
                    // Number       Round(x)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2500
                    // x0.00        x0.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2501
                    // x0.01        x0.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2502
                    // x0.10        x0.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2503
                    // x0.11        x1. = x0. +1
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2504
                    // x1.00        x1.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2505
                    // x1.01        x1.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2506
                    // x1.10        x1. + 1
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2507
                    // x1.11        x1. + 1
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2508
                    //
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2509
                    boolean leastZero = ((significand & 1L) == 0L);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2510
                    if ((leastZero && round && sticky) ||
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2511
                            ((!leastZero) && round)) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2512
                        significand++;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2513
                    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2514
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2515
                    double value = isNegative ?
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2516
                            Double.longBitsToDouble(significand | DoubleConsts.SIGN_BIT_MASK) :
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2517
                            Double.longBitsToDouble(significand );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2518
18537
2ae90e54f001 7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents: 18143
diff changeset
  2519
                    return new PreparedASCIIToBinaryBuffer(value, fValue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
    /**
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2525
     * Returns <code>s</code> with any leading zeros removed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
    static String stripLeadingZeros(String s) {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2528
//        return  s.replaceFirst("^0+", "");
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2529
        if(!s.isEmpty() && s.charAt(0)=='0') {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2530
            for(int i=1; i<s.length(); i++) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2531
                if(s.charAt(i)!='0') {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2532
                    return s.substring(i);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2533
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2534
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2535
            return "";
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2536
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2537
        return s;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
    /**
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 15255
diff changeset
  2541
     * Extracts a hexadecimal digit from position <code>position</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
     * of string <code>s</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
    static int getHexDigit(String s, int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
        int value = Character.digit(s.charAt(position), 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
        if (value <= -1 || value >= 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
            throw new AssertionError("Unexpected failure of digit conversion of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
                                     s.charAt(position));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
}