author | jbachorik |
Tue, 26 May 2015 11:57:51 +0200 (2015-05-26) | |
changeset 30906 | 1b67cbb0adce |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.misc; |
|
27 |
||
18143
b6ef7bd945ce
7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
11131
diff
changeset
|
28 |
import java.util.Arrays; |
2 | 29 |
|
9521 | 30 |
public class FormattedFloatingDecimal{ |
2 | 31 |
|
32 |
public enum Form { SCIENTIFIC, COMPATIBLE, DECIMAL_FLOAT, GENERAL }; |
|
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 | 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 | 55 |
} |
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 | 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 | 101 |
} |
102 |
} |
|
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 | 107 |
} |
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 | 111 |
} |
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 | 115 |
} |
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 | 124 |
} |
18143
b6ef7bd945ce
7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
11131
diff
changeset
|
125 |
if (prec == 0) { |
2 | 126 |
// only one digit (0 or 1) is returned because the precision |
127 |
// excludes all significant digits |
|
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 | 135 |
} |
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 | 140 |
q = digits[--i]; |
141 |
if ( q == '9' ) { |
|
142 |
while ( q == '9' && i > 0 ){ |
|
143 |
q = digits[--i]; |
|
144 |
} |
|
145 |
if ( q == '9' ){ |
|
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 | 150 |
} |
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 | 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 | 156 |
} |
18143
b6ef7bd945ce
7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
11131
diff
changeset
|
157 |
return decExp; |
2 | 158 |
} |
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 | 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 | 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 | 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 | 208 |
} |
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 | 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 | 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 | 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 | 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 | 244 |
} |
245 |
} |
|
246 |
} |
|
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 | 253 |
} else { |
18143
b6ef7bd945ce
7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
11131
diff
changeset
|
254 |
return new char[size]; |
2 | 255 |
} |
256 |
} |
|
257 |
||
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 | 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 | 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 | 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 | 307 |
} |
308 |
} |
|
309 |
} |
|
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 | 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 | 349 |
} |