jdk/src/java.base/share/classes/sun/misc/FormattedFloatingDecimal.java
author jbachorik
Tue, 26 May 2015 11:57:51 +0200 (2015-05-26)
changeset 30906 1b67cbb0adce
parent 25859 3317bb8137f4
permissions -rw-r--r--
8080663: Use sun.misc.SharedSecrets to allow access from java.management to @ConstructorProperties Reviewed-by: alanb, mchung, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
     2
 * Copyright (c) 2003, 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: 2278
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: 2278
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: 2278
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2278
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2278
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: 11131
diff changeset
    28
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
9521
e18f95eadb3c 7039369: Limit range of strictfp in FloatingDecimal
darcy
parents: 9276
diff changeset
    30
public class FormattedFloatingDecimal{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    public enum Form { SCIENTIFIC, COMPATIBLE, DECIMAL_FLOAT, GENERAL };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    34
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    35
    public static FormattedFloatingDecimal valueOf(double d, int precision, Form form){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    36
        FloatingDecimal.BinaryToASCIIConverter fdConverter =
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    37
                FloatingDecimal.getBinaryToASCIIConverter(d, form == Form.COMPATIBLE);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    38
        return new FormattedFloatingDecimal(precision,form, fdConverter);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    39
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    40
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    41
    private int decExponentRounded;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    42
    private char[] mantissa;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    43
    private char[] exponent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    45
    private static final ThreadLocal<Object> threadLocalCharBuffer =
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    46
            new ThreadLocal<Object>() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    47
                @Override
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    48
                protected Object initialValue() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    49
                    return new char[20];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    50
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    51
            };
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    52
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    53
    private static char[] getBuffer(){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    54
        return (char[]) threadLocalCharBuffer.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    57
    private FormattedFloatingDecimal(int precision, Form form, FloatingDecimal.BinaryToASCIIConverter fdConverter) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    58
        if (fdConverter.isExceptional()) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    59
            this.mantissa = fdConverter.toJavaFormatString().toCharArray();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    60
            this.exponent = null;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    61
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    63
        char[] digits = getBuffer();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    64
        int nDigits = fdConverter.getDigits(digits);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    65
        int decExp = fdConverter.getDecimalExponent();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    66
        int exp;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    67
        boolean isNegative = fdConverter.isNegative();
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    68
        switch (form) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    69
            case COMPATIBLE:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    70
                exp = decExp;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    71
                this.decExponentRounded = exp;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    72
                fillCompatible(precision, digits, nDigits, exp, isNegative);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    73
                break;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    74
            case DECIMAL_FLOAT:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    75
                exp = applyPrecision(decExp, digits, nDigits, decExp + precision);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    76
                fillDecimal(precision, digits, nDigits, exp, isNegative);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    77
                this.decExponentRounded = exp;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    78
                break;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    79
            case SCIENTIFIC:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    80
                exp = applyPrecision(decExp, digits, nDigits, precision + 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    81
                fillScientific(precision, digits, nDigits, exp, isNegative);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    82
                this.decExponentRounded = exp;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    83
                break;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    84
            case GENERAL:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    85
                exp = applyPrecision(decExp, digits, nDigits, precision);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    86
                // adjust precision to be the number of digits to right of decimal
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    87
                // the real exponent to be output is actually exp - 1, not exp
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    88
                if (exp - 1 < -4 || exp - 1 >= precision) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    89
                    // form = Form.SCIENTIFIC;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    90
                    precision--;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    91
                    fillScientific(precision, digits, nDigits, exp, isNegative);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    92
                } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    93
                    // form = Form.DECIMAL_FLOAT;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    94
                    precision = precision - exp;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    95
                    fillDecimal(precision, digits, nDigits, exp, isNegative);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    96
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    97
                this.decExponentRounded = exp;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    98
                break;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
    99
            default:
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   100
                assert false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   104
    // returns the exponent after rounding has been done by applyPrecision
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   105
    public int getExponentRounded() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   106
        return decExponentRounded - 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   109
    public char[] getMantissa(){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   110
        return mantissa;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   113
    public char[] getExponent(){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   114
        return exponent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   117
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   118
     * Returns new decExp in case of overflow.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   119
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   120
    private static int applyPrecision(int decExp, char[] digits, int nDigits, int prec) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   121
        if (prec >= nDigits || prec < 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   122
            // no rounding necessary
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   123
            return decExp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   125
        if (prec == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            // only one digit (0 or 1) is returned because the precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            // excludes all significant digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (digits[0] >= '5') {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   129
                digits[0] = '1';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   130
                Arrays.fill(digits, 1, nDigits, '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   131
                return decExp + 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   132
            } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   133
                Arrays.fill(digits, 0, nDigits, '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   134
                return decExp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   137
        int q = digits[prec];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   138
        if (q >= '5') {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   139
            int i = prec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            q = digits[--i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if ( q == '9' ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                while ( q == '9' && i > 0 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    q = digits[--i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                if ( q == '9' ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    // carryout! High-order 1, rest 0s, larger exp.
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   147
                    digits[0] = '1';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   148
                    Arrays.fill(digits, 1, nDigits, '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   149
                    return decExp+1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   152
            digits[i] = (char)(q + 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   153
            Arrays.fill(digits, i+1, nDigits, '0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } else {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   155
            Arrays.fill(digits, prec, nDigits, '0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   157
        return decExp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   160
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   161
     * Fills mantissa and exponent char arrays for compatible format.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   162
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   163
    private void fillCompatible(int precision, char[] digits, int nDigits, int exp, boolean isNegative) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   164
        int startIndex = isNegative ? 1 : 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   165
        if (exp > 0 && exp < 8) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   166
            // print digits.digits.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   167
            if (nDigits < exp) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   168
                int extraZeros = exp - nDigits;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   169
                mantissa = create(isNegative, nDigits + extraZeros + 2);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   170
                System.arraycopy(digits, 0, mantissa, startIndex, nDigits);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   171
                Arrays.fill(mantissa, startIndex + nDigits, startIndex + nDigits + extraZeros, '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   172
                mantissa[startIndex + nDigits + extraZeros] = '.';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   173
                mantissa[startIndex + nDigits + extraZeros+1] = '0';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   174
            } else if (exp < nDigits) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   175
                int t = Math.min(nDigits - exp, precision);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   176
                mantissa = create(isNegative, exp + 1 + t);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   177
                System.arraycopy(digits, 0, mantissa, startIndex, exp);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   178
                mantissa[startIndex + exp ] = '.';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   179
                System.arraycopy(digits, exp, mantissa, startIndex+exp+1, t);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   180
            } else { // exp == digits.length
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   181
                mantissa = create(isNegative, nDigits + 2);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   182
                System.arraycopy(digits, 0, mantissa, startIndex, nDigits);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   183
                mantissa[startIndex + nDigits ] = '.';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   184
                mantissa[startIndex + nDigits +1] = '0';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   186
        } else if (exp <= 0 && exp > -3) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   187
            int zeros = Math.max(0, Math.min(-exp, precision));
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   188
            int t = Math.max(0, Math.min(nDigits, precision + exp));
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   189
            // write '0' s before the significant digits
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   190
            if (zeros > 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   191
                mantissa = create(isNegative, zeros + 2 + t);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   192
                mantissa[startIndex] = '0';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   193
                mantissa[startIndex+1] = '.';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   194
                Arrays.fill(mantissa, startIndex + 2, startIndex + 2 + zeros, '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   195
                if (t > 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   196
                    // copy only when significant digits are within the precision
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   197
                    System.arraycopy(digits, 0, mantissa, startIndex + 2 + zeros, t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   199
            } else if (t > 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   200
                mantissa = create(isNegative, zeros + 2 + t);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   201
                mantissa[startIndex] = '0';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   202
                mantissa[startIndex + 1] = '.';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   203
                // copy only when significant digits are within the precision
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   204
                System.arraycopy(digits, 0, mantissa, startIndex + 2, t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            } else {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   206
                this.mantissa = create(isNegative, 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   207
                this.mantissa[startIndex] = '0';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } else {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   210
            if (nDigits > 1) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   211
                mantissa = create(isNegative, nDigits + 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   212
                mantissa[startIndex] = digits[0];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   213
                mantissa[startIndex + 1] = '.';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   214
                System.arraycopy(digits, 1, mantissa, startIndex + 2, nDigits - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            } else {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   216
                mantissa = create(isNegative, 3);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   217
                mantissa[startIndex] = digits[0];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   218
                mantissa[startIndex + 1] = '.';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   219
                mantissa[startIndex + 2] = '0';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   221
            int e, expStartIntex;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   222
            boolean isNegExp = (exp <= 0);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   223
            if (isNegExp) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   224
                e = -exp + 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   225
                expStartIntex = 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   226
            } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   227
                e = exp - 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   228
                expStartIntex = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   230
            // decExponent has 1, 2, or 3, digits
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   231
            if (e <= 9) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   232
                exponent = create(isNegExp,1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   233
                exponent[expStartIntex] = (char) (e + '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   234
            } else if (e <= 99) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   235
                exponent = create(isNegExp,2);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   236
                exponent[expStartIntex] = (char) (e / 10 + '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   237
                exponent[expStartIntex+1] = (char) (e % 10 + '0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            } else {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   239
                exponent = create(isNegExp,3);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   240
                exponent[expStartIntex] = (char) (e / 100 + '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   241
                e %= 100;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   242
                exponent[expStartIntex+1] = (char) (e / 10 + '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   243
                exponent[expStartIntex+2] = (char) (e % 10 + '0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   248
    private static char[] create(boolean isNegative, int size) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   249
        if(isNegative) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   250
            char[] r = new char[size +1];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   251
            r[0] = '-';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   252
            return r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } else {
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   254
            return new char[size];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /*
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   259
     * Fills mantissa char arrays for DECIMAL_FLOAT format.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   260
     * Exponent should be equal to null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   262
    private void fillDecimal(int precision, char[] digits, int nDigits, int exp, boolean isNegative) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   263
        int startIndex = isNegative ? 1 : 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   264
        if (exp > 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   265
            // print digits.digits.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   266
            if (nDigits < exp) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   267
                mantissa = create(isNegative,exp);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   268
                System.arraycopy(digits, 0, mantissa, startIndex, nDigits);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   269
                Arrays.fill(mantissa, startIndex + nDigits, startIndex + exp, '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   270
                // Do not append ".0" for formatted floats since the user
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   271
                // may request that it be omitted. It is added as necessary
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   272
                // by the Formatter.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   273
            } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   274
                int t = Math.min(nDigits - exp, precision);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   275
                mantissa = create(isNegative, exp + (t > 0 ? (t + 1) : 0));
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   276
                System.arraycopy(digits, 0, mantissa, startIndex, exp);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   277
                // Do not append ".0" for formatted floats since the user
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   278
                // may request that it be omitted. It is added as necessary
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   279
                // by the Formatter.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   280
                if (t > 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   281
                    mantissa[startIndex + exp] = '.';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   282
                    System.arraycopy(digits, exp, mantissa, startIndex + exp + 1, t);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   283
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   285
        } else if (exp <= 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   286
            int zeros = Math.max(0, Math.min(-exp, precision));
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   287
            int t = Math.max(0, Math.min(nDigits, precision + exp));
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   288
            // write '0' s before the significant digits
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   289
            if (zeros > 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   290
                mantissa = create(isNegative, zeros + 2 + t);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   291
                mantissa[startIndex] = '0';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   292
                mantissa[startIndex+1] = '.';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   293
                Arrays.fill(mantissa, startIndex + 2, startIndex + 2 + zeros, '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   294
                if (t > 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   295
                    // copy only when significant digits are within the precision
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   296
                    System.arraycopy(digits, 0, mantissa, startIndex + 2 + zeros, t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   298
            } else if (t > 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   299
                mantissa = create(isNegative, zeros + 2 + t);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   300
                mantissa[startIndex] = '0';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   301
                mantissa[startIndex + 1] = '.';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   302
                // copy only when significant digits are within the precision
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   303
                System.arraycopy(digits, 0, mantissa, startIndex + 2, t);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   304
            } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   305
                this.mantissa = create(isNegative, 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   306
                this.mantissa[startIndex] = '0';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   311
    /**
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   312
     * Fills mantissa and exponent char arrays for SCIENTIFIC format.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   314
    private void fillScientific(int precision, char[] digits, int nDigits, int exp, boolean isNegative) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   315
        int startIndex = isNegative ? 1 : 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   316
        int t = Math.max(0, Math.min(nDigits - 1, precision));
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   317
        if (t > 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   318
            mantissa = create(isNegative, t + 2);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   319
            mantissa[startIndex] = digits[0];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   320
            mantissa[startIndex + 1] = '.';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   321
            System.arraycopy(digits, 1, mantissa, startIndex + 2, t);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   322
        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   323
            mantissa = create(isNegative, 1);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   324
            mantissa[startIndex] = digits[0];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   325
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   326
        char expSign;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   327
        int e;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   328
        if (exp <= 0) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   329
            expSign = '-';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   330
            e = -exp + 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   331
        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   332
            expSign = '+' ;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   333
            e = exp - 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   334
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   335
        // decExponent has 1, 2, or 3, digits
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   336
        if (e <= 9) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   337
            exponent = new char[] { expSign,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   338
                    '0', (char) (e + '0') };
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   339
        } else if (e <= 99) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   340
            exponent = new char[] { expSign,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   341
                    (char) (e / 10 + '0'), (char) (e % 10 + '0') };
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   342
        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   343
            char hiExpChar = (char) (e / 100 + '0');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   344
            e %= 100;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   345
            exponent = new char[] { expSign,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   346
                    hiExpChar, (char) (e / 10 + '0'), (char) (e % 10 + '0') };
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   347
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents: 11131
diff changeset
   348
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
}