jdk/src/java.base/share/classes/java/math/BigDecimal.java
author chegar
Wed, 11 Nov 2015 09:19:12 +0000
changeset 33674 566777f73c32
parent 32649 2ee9017c7597
child 36671 6650d41439b6
permissions -rw-r--r--
8140606: Update library code to use internal Unsafe Reviewed-by: alanb, mchung, psandoz, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
     2
 * Copyright (c) 1996, 2015, 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: 3710
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: 3710
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: 3710
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3710
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3710
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Portions Copyright IBM Corporation, 2001. All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
package java.math;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
    32
import static java.math.BigInteger.LONG_MASK;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    33
import java.util.Arrays;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Immutable, arbitrary-precision signed decimal numbers.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * {@code BigDecimal} consists of an arbitrary precision integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <i>unscaled value</i> and a 32-bit integer <i>scale</i>.  If zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * or positive, the scale is the number of digits to the right of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * decimal point.  If negative, the unscaled value of the number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * multiplied by ten to the power of the negation of the scale.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * value of the number represented by the {@code BigDecimal} is
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
    43
 * therefore <code>(unscaledValue &times; 10<sup>-scale</sup>)</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>The {@code BigDecimal} class provides operations for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * arithmetic, scale manipulation, rounding, comparison, hashing, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * format conversion.  The {@link #toString} method provides a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * canonical representation of a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>The {@code BigDecimal} class gives its user complete control
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * over rounding behavior.  If no rounding mode is specified and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * exact result cannot be represented, an exception is thrown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * otherwise, calculations can be carried out to a chosen precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * and rounding mode by supplying an appropriate {@link MathContext}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * object to the operation.  In either case, eight <em>rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * modes</em> are provided for the control of rounding.  Using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * integer fields in this class (such as {@link #ROUND_HALF_UP}) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * represent rounding mode is largely obsolete; the enumeration values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * of the {@code RoundingMode} {@code enum}, (such as {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * RoundingMode#HALF_UP}) should be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>When a {@code MathContext} object is supplied with a precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * setting of 0 (for example, {@link MathContext#UNLIMITED}),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * arithmetic operations are exact, as are the arithmetic methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * which take no {@code MathContext} object.  (This is the only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * behavior that was supported in releases prior to 5.)  As a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * corollary of computing the exact result, the rounding mode setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * of a {@code MathContext} object with a precision setting of 0 is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * not used and thus irrelevant.  In the case of divide, the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * quotient could have an infinitely long decimal expansion; for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * example, 1 divided by 3.  If the quotient has a nonterminating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * decimal expansion and the operation is specified to return an exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * result, an {@code ArithmeticException} is thrown.  Otherwise, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * exact result of the division is returned, as done for other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p>When the precision setting is not 0, the rules of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * {@code BigDecimal} arithmetic are broadly compatible with selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * modes of operation of the arithmetic defined in ANSI X3.274-1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * and ANSI X3.274-1996/AM 1-2000 (section 7.4).  Unlike those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * standards, {@code BigDecimal} includes many rounding modes, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * were mandatory for division in {@code BigDecimal} releases prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * to 5.  Any conflicts between these ANSI standards and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * {@code BigDecimal} specification are resolved in favor of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>Since the same numerical value can have different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * representations (with different scales), the rules of arithmetic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * and rounding must specify both the numerical result and the scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * used in the result's representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p>In general the rounding modes and precision setting determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * how operations return results with a limited number of digits when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * the exact result has more digits (perhaps infinitely many in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * case of division) than the number of digits returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * First, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * total number of digits to return is specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * {@code MathContext}'s {@code precision} setting; this determines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * the result's <i>precision</i>.  The digit count starts from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * leftmost nonzero digit of the exact result.  The rounding mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * determines how any discarded trailing digits affect the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <p>For all arithmetic operators , the operation is carried out as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * though an exact intermediate result were first calculated and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * rounded to the number of digits specified by the precision setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * (if necessary), using the selected rounding mode.  If the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * result is not returned, some digit positions of the exact result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * are discarded.  When rounding increases the magnitude of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * returned result, it is possible for a new digit position to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * created by a carry propagating to a leading {@literal "9"} digit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * For example, rounding the value 999.9 to three digits rounding up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * would be numerically equal to one thousand, represented as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * 100&times;10<sup>1</sup>.  In such cases, the new {@literal "1"} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * the leading digit position of the returned result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <p>Besides a logical exact result, each arithmetic operation has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * preferred scale for representing a result.  The preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * scale for each operation is listed in the table below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <table border>
8162
d91ca3f845b0 7015827: Fix HTML validation issues in java.math package
darcy
parents: 5506
diff changeset
   124
 * <caption><b>Preferred Scales for Results of Arithmetic Operations
d91ca3f845b0 7015827: Fix HTML validation issues in java.math package
darcy
parents: 5506
diff changeset
   125
 * </b></caption>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <tr><th>Operation</th><th>Preferred Scale of Result</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <tr><td>Add</td><td>max(addend.scale(), augend.scale())</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <tr><td>Subtract</td><td>max(minuend.scale(), subtrahend.scale())</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <tr><td>Multiply</td><td>multiplier.scale() + multiplicand.scale()</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <tr><td>Divide</td><td>dividend.scale() - divisor.scale()</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * These scales are the ones used by the methods which return exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * arithmetic results; except that an exact divide may have to use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * larger scale since the exact result may have more digits.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * example, {@code 1/32} is {@code 0.03125}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <p>Before rounding, the scale of the logical exact intermediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * result is the preferred scale for that operation.  If the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * numerical result cannot be represented in {@code precision}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * digits, rounding selects the set of digits to return and the scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * of the result is reduced from the scale of the intermediate result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * to the least scale which can represent the {@code precision}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * digits actually returned.  If the exact result can be represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * with at most {@code precision} digits, the representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * of the result with the scale closest to the preferred scale is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * returned.  In particular, an exactly representable quotient may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * represented in fewer than {@code precision} digits by removing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * trailing zeros and decreasing the scale.  For example, rounding to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * three digits using the {@linkplain RoundingMode#FLOOR floor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * rounding mode, <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * {@code 19/100 = 0.19   // integer=19,  scale=2} <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * but<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * {@code 21/110 = 0.190  // integer=190, scale=3} <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * <p>Note that for add, subtract, and multiply, the reduction in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * scale will equal the number of digit positions of the exact result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * which are discarded. If the rounding causes a carry propagation to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * create a new high-order digit position, an additional digit of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * result is discarded than when no new digit position is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * <p>Other methods may have slightly different rounding semantics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * For example, the result of the {@code pow} method using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * {@linkplain #pow(int, MathContext) specified algorithm} can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * occasionally differ from the rounded mathematical result by more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * than one unit in the last place, one <i>{@linkplain #ulp() ulp}</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <p>Two types of operations are provided for manipulating the scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * of a {@code BigDecimal}: scaling/rounding operations and decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * point motion operations.  Scaling/rounding operations ({@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * #setScale setScale} and {@link #round round}) return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * {@code BigDecimal} whose value is approximately (or exactly) equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * to that of the operand, but whose scale or precision is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * specified value; that is, they increase or decrease the precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * of the stored number with minimal effect on its value.  Decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * point motion operations ({@link #movePointLeft movePointLeft} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * {@link #movePointRight movePointRight}) return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * {@code BigDecimal} created from the operand by moving the decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * point a specified distance in the specified direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * <p>For the sake of brevity and clarity, pseudo-code is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * throughout the descriptions of {@code BigDecimal} methods.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * pseudo-code expression {@code (i + j)} is shorthand for "a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * {@code BigDecimal} whose value is that of the {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * {@code i} added to that of the {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * {@code j}." The pseudo-code expression {@code (i == j)} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * shorthand for "{@code true} if and only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * {@code BigDecimal} {@code i} represents the same value as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * {@code BigDecimal} {@code j}." Other pseudo-code expressions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * are interpreted similarly.  Square brackets are used to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * the particular {@code BigInteger} and scale pair defining a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * {@code BigDecimal} value; for example [19, 2] is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * {@code BigDecimal} numerically equal to 0.19 having a scale of 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * <p>Note: care should be exercised if {@code BigDecimal} objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * are used as keys in a {@link java.util.SortedMap SortedMap} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * elements in a {@link java.util.SortedSet SortedSet} since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * {@code BigDecimal}'s <i>natural ordering</i> is <i>inconsistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * with equals</i>.  See {@link Comparable}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * java.util.SortedMap} or {@link java.util.SortedSet} for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * <p>All methods and constructors for this class throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * {@code NullPointerException} when passed a {@code null} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * reference for any input parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * @see     BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * @see     MathContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * @see     RoundingMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * @see     java.util.SortedMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * @see     java.util.SortedSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * @author  Mike Cowlishaw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * @author  Joseph D. Darcy
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   218
 * @author  Sergey V. Kuksenko
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
public class BigDecimal extends Number implements Comparable<BigDecimal> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * The unscaled value of this BigDecimal, as returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * #unscaledValue}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @see #unscaledValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   228
    private final BigInteger intVal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * The scale of this BigDecimal, as returned by {@link #scale}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @see #scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   236
    private final int scale;  // Note: this may have any value, so
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   237
                              // calculations must be done in longs
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   238
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * The number of decimal digits in this BigDecimal, or 0 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * number of digits are not known (lookaside information).  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * nonzero, the value is guaranteed correct.  Use the precision()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * method to obtain and set the value if it might be 0.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * field is mutable until set nonzero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   248
    private transient int precision;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Used to store the canonical string representation, if computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   253
    private transient String stringCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Sentinel value for {@link #intCompact} indicating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * significand information is only available from {@code intVal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   259
    static final long INFLATED = Long.MIN_VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   261
    private static final BigInteger INFLATED_BIGINT = BigInteger.valueOf(INFLATED);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   262
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * If the absolute value of the significand of this BigDecimal is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * less than or equal to {@code Long.MAX_VALUE}, the value can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * compactly stored in this field and used in computations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   268
    private final transient long intCompact;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    // All 18-digit base ten strings fit into a long; not all 19-digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    // strings will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private static final int MAX_COMPACT_DIGITS = 18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /* Appease the serialization gods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private static final long serialVersionUID = 6108874887143696463L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   277
    private static final ThreadLocal<StringBuilderHelper>
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   278
        threadLocalStringBuilderHelper = new ThreadLocal<StringBuilderHelper>() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   279
        @Override
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   280
        protected StringBuilderHelper initialValue() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   281
            return new StringBuilderHelper();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   282
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   283
    };
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   284
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    // Cache of common small BigDecimal values.
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   286
    private static final BigDecimal ZERO_THROUGH_TEN[] = {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   287
        new BigDecimal(BigInteger.ZERO,       0,  0, 1),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   288
        new BigDecimal(BigInteger.ONE,        1,  0, 1),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   289
        new BigDecimal(BigInteger.valueOf(2), 2,  0, 1),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   290
        new BigDecimal(BigInteger.valueOf(3), 3,  0, 1),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   291
        new BigDecimal(BigInteger.valueOf(4), 4,  0, 1),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   292
        new BigDecimal(BigInteger.valueOf(5), 5,  0, 1),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   293
        new BigDecimal(BigInteger.valueOf(6), 6,  0, 1),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   294
        new BigDecimal(BigInteger.valueOf(7), 7,  0, 1),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   295
        new BigDecimal(BigInteger.valueOf(8), 8,  0, 1),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   296
        new BigDecimal(BigInteger.valueOf(9), 9,  0, 1),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   297
        new BigDecimal(BigInteger.TEN,        10, 0, 2),
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   298
    };
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   299
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   300
    // Cache of zero scaled by 0 - 15
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   301
    private static final BigDecimal[] ZERO_SCALED_BY = {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   302
        ZERO_THROUGH_TEN[0],
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   303
        new BigDecimal(BigInteger.ZERO, 0, 1, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   304
        new BigDecimal(BigInteger.ZERO, 0, 2, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   305
        new BigDecimal(BigInteger.ZERO, 0, 3, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   306
        new BigDecimal(BigInteger.ZERO, 0, 4, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   307
        new BigDecimal(BigInteger.ZERO, 0, 5, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   308
        new BigDecimal(BigInteger.ZERO, 0, 6, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   309
        new BigDecimal(BigInteger.ZERO, 0, 7, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   310
        new BigDecimal(BigInteger.ZERO, 0, 8, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   311
        new BigDecimal(BigInteger.ZERO, 0, 9, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   312
        new BigDecimal(BigInteger.ZERO, 0, 10, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   313
        new BigDecimal(BigInteger.ZERO, 0, 11, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   314
        new BigDecimal(BigInteger.ZERO, 0, 12, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   315
        new BigDecimal(BigInteger.ZERO, 0, 13, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   316
        new BigDecimal(BigInteger.ZERO, 0, 14, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   317
        new BigDecimal(BigInteger.ZERO, 0, 15, 1),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
3710
65a5d7914736 6876282: BigDecimal's divide(BigDecimal bd, RoundingFormat r) produces incorrect result
xlu
parents: 3053
diff changeset
   320
    // Half of Long.MIN_VALUE & Long.MAX_VALUE.
65a5d7914736 6876282: BigDecimal's divide(BigDecimal bd, RoundingFormat r) produces incorrect result
xlu
parents: 3053
diff changeset
   321
    private static final long HALF_LONG_MAX_VALUE = Long.MAX_VALUE / 2;
65a5d7914736 6876282: BigDecimal's divide(BigDecimal bd, RoundingFormat r) produces incorrect result
xlu
parents: 3053
diff changeset
   322
    private static final long HALF_LONG_MIN_VALUE = Long.MIN_VALUE / 2;
65a5d7914736 6876282: BigDecimal's divide(BigDecimal bd, RoundingFormat r) produces incorrect result
xlu
parents: 3053
diff changeset
   323
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    // Constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * The value 0, with a scale of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public static final BigDecimal ZERO =
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   331
        ZERO_THROUGH_TEN[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * The value 1, with a scale of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public static final BigDecimal ONE =
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   339
        ZERO_THROUGH_TEN[1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * The value 10, with a scale of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public static final BigDecimal TEN =
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   347
        ZERO_THROUGH_TEN[10];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    // Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   352
     * Trusted package private constructor.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   353
     * Trusted simply means if val is INFLATED, intVal could not be null and
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   354
     * if intVal is null, val could not be INFLATED.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   355
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   356
    BigDecimal(BigInteger intVal, long val, int scale, int prec) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   357
        this.scale = scale;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   358
        this.precision = prec;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   359
        this.intCompact = val;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   360
        this.intVal = intVal;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   361
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   362
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   363
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Translates a character array representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * {@code BigDecimal} into a {@code BigDecimal}, accepting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * same sequence of characters as the {@link #BigDecimal(String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * constructor, while allowing a sub-array to be specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * <p>Note that if the sequence of characters is already available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * within a character array, using this constructor is faster than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * converting the {@code char} array to string and using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * {@code BigDecimal(String)} constructor .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param  in {@code char} array that is the source of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @param  offset first character in the array to inspect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param  len number of characters to consider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @throws NumberFormatException if {@code in} is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *         representation of a {@code BigDecimal} or the defined subarray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *         is not wholly within {@code in}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public BigDecimal(char[] in, int offset, int len) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   383
        this(in,offset,len,MathContext.UNLIMITED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Translates a character array representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * {@code BigDecimal} into a {@code BigDecimal}, accepting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * same sequence of characters as the {@link #BigDecimal(String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * constructor, while allowing a sub-array to be specified and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <p>Note that if the sequence of characters is already available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * within a character array, using this constructor is faster than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * converting the {@code char} array to string and using the
28257
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   396
     * {@code BigDecimal(String)} constructor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @param  in {@code char} array that is the source of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param  offset first character in the array to inspect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param  len number of characters to consider..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @throws NumberFormatException if {@code in} is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *         representation of a {@code BigDecimal} or the defined subarray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *         is not wholly within {@code in}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public BigDecimal(char[] in, int offset, int len, MathContext mc) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   410
        // protect against huge length.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   411
        if (offset + len > in.length || offset < 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   412
            throw new NumberFormatException("Bad offset or len arguments for char[] input.");
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   413
        // This is the primary string to BigDecimal constructor; all
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   414
        // incoming strings end up here; it uses explicit (inline)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   415
        // parsing for speed and generates at most one intermediate
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   416
        // (temporary) object (a char[] array) for non-compact case.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   417
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   418
        // Use locals for all fields values until completion
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   419
        int prec = 0;                 // record precision value
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   420
        int scl = 0;                  // record scale value
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   421
        long rs = 0;                  // the compact value in long
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   422
        BigInteger rb = null;         // the inflated value in BigInteger
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   423
        // use array bounds checking to handle too-long, len == 0,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   424
        // bad offset, etc.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   425
        try {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   426
            // handle the sign
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   427
            boolean isneg = false;          // assume positive
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   428
            if (in[offset] == '-') {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   429
                isneg = true;               // leading minus means negative
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   430
                offset++;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   431
                len--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   432
            } else if (in[offset] == '+') { // leading + allowed
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   433
                offset++;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   434
                len--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   435
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   436
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   437
            // should now be at numeric part of the significand
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   438
            boolean dot = false;             // true when there is a '.'
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   439
            long exp = 0;                    // exponent
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   440
            char c;                          // current character
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   441
            boolean isCompact = (len <= MAX_COMPACT_DIGITS);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   442
            // integer significand array & idx is the index to it. The array
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   443
            // is ONLY used when we can't use a compact representation.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   444
            int idx = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   445
            if (isCompact) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   446
                // First compact case, we need not to preserve the character
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   447
                // and we can just compute the value in place.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   448
                for (; len > 0; offset++, len--) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   449
                    c = in[offset];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   450
                    if ((c == '0')) { // have zero
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   451
                        if (prec == 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   452
                            prec = 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   453
                        else if (rs != 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   454
                            rs *= 10;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   455
                            ++prec;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   456
                        } // else digit is a redundant leading zero
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   457
                        if (dot)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   458
                            ++scl;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   459
                    } else if ((c >= '1' && c <= '9')) { // have digit
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   460
                        int digit = c - '0';
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   461
                        if (prec != 1 || rs != 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   462
                            ++prec; // prec unchanged if preceded by 0s
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   463
                        rs = rs * 10 + digit;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   464
                        if (dot)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   465
                            ++scl;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   466
                    } else if (c == '.') {   // have dot
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   467
                        // have dot
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   468
                        if (dot) // two dots
28257
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   469
                            throw new NumberFormatException("Character array"
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   470
                                + " contains more than one decimal point.");
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   471
                        dot = true;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   472
                    } else if (Character.isDigit(c)) { // slow path
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   473
                        int digit = Character.digit(c, 10);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   474
                        if (digit == 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   475
                            if (prec == 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   476
                                prec = 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   477
                            else if (rs != 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   478
                                rs *= 10;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   479
                                ++prec;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   480
                            } // else digit is a redundant leading zero
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   481
                        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   482
                            if (prec != 1 || rs != 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   483
                                ++prec; // prec unchanged if preceded by 0s
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   484
                            rs = rs * 10 + digit;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   485
                        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   486
                        if (dot)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   487
                            ++scl;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   488
                    } else if ((c == 'e') || (c == 'E')) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   489
                        exp = parseExp(in, offset, len);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   490
                        // Next test is required for backwards compatibility
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   491
                        if ((int) exp != exp) // overflow
28257
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   492
                            throw new NumberFormatException("Exponent overflow.");
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   493
                        break; // [saves a test]
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   494
                    } else {
28257
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   495
                        throw new NumberFormatException("Character " + c
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   496
                            + " is neither a decimal digit number, decimal point, nor"
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   497
                            + " \"e\" notation exponential mark.");
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   498
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   499
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   500
                if (prec == 0) // no digits found
28257
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   501
                    throw new NumberFormatException("No digits found.");
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   502
                // Adjust scale if exp is not zero.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   503
                if (exp != 0) { // had significant exponent
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   504
                    scl = adjustScale(scl, exp);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   505
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   506
                rs = isneg ? -rs : rs;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   507
                int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   508
                int drop = prec - mcp; // prec has range [1, MAX_INT], mcp has range [0, MAX_INT];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   509
                                       // therefore, this subtract cannot overflow
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   510
                if (mcp > 0 && drop > 0) {  // do rounding
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   511
                    while (drop > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   512
                        scl = checkScaleNonZero((long) scl - drop);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   513
                        rs = divideAndRound(rs, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   514
                        prec = longDigitLength(rs);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   515
                        drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   516
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   517
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   518
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   519
                char coeff[] = new char[len];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   520
                for (; len > 0; offset++, len--) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   521
                    c = in[offset];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   522
                    // have digit
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   523
                    if ((c >= '0' && c <= '9') || Character.isDigit(c)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   524
                        // First compact case, we need not to preserve the character
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   525
                        // and we can just compute the value in place.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   526
                        if (c == '0' || Character.digit(c, 10) == 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   527
                            if (prec == 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   528
                                coeff[idx] = c;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   529
                                prec = 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   530
                            } else if (idx != 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   531
                                coeff[idx++] = c;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   532
                                ++prec;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   533
                            } // else c must be a redundant leading zero
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   534
                        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   535
                            if (prec != 1 || idx != 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   536
                                ++prec; // prec unchanged if preceded by 0s
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   537
                            coeff[idx++] = c;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   538
                        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   539
                        if (dot)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   540
                            ++scl;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   541
                        continue;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   542
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   543
                    // have dot
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   544
                    if (c == '.') {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   545
                        // have dot
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   546
                        if (dot) // two dots
28257
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   547
                            throw new NumberFormatException("Character array"
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   548
                                + " contains more than one decimal point.");
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   549
                        dot = true;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   550
                        continue;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   551
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   552
                    // exponent expected
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   553
                    if ((c != 'e') && (c != 'E'))
28257
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   554
                        throw new NumberFormatException("Character array"
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   555
                            + " is missing \"e\" notation exponential mark.");
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   556
                    exp = parseExp(in, offset, len);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   557
                    // Next test is required for backwards compatibility
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   558
                    if ((int) exp != exp) // overflow
28257
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   559
                        throw new NumberFormatException("Exponent overflow.");
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   560
                    break; // [saves a test]
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   561
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   562
                // here when no characters left
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   563
                if (prec == 0) // no digits found
28257
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   564
                    throw new NumberFormatException("No digits found.");
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   565
                // Adjust scale if exp is not zero.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   566
                if (exp != 0) { // had significant exponent
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   567
                    scl = adjustScale(scl, exp);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   568
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   569
                // Remove leading zeros from precision (digits count)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   570
                rb = new BigInteger(coeff, isneg ? -1 : 1, prec);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   571
                rs = compactValFor(rb);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   572
                int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   573
                if (mcp > 0 && (prec > mcp)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   574
                    if (rs == INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   575
                        int drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   576
                        while (drop > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   577
                            scl = checkScaleNonZero((long) scl - drop);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   578
                            rb = divideAndRoundByTenPow(rb, drop, mc.roundingMode.oldMode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   579
                            rs = compactValFor(rb);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   580
                            if (rs != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   581
                                prec = longDigitLength(rs);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   582
                                break;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   583
                            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   584
                            prec = bigDigitLength(rb);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   585
                            drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   586
                        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   587
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   588
                    if (rs != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   589
                        int drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   590
                        while (drop > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   591
                            scl = checkScaleNonZero((long) scl - drop);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   592
                            rs = divideAndRound(rs, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   593
                            prec = longDigitLength(rs);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   594
                            drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   595
                        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   596
                        rb = null;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   597
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   598
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   599
            }
28257
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   600
        } catch (ArrayIndexOutOfBoundsException | NegativeArraySizeException e) {
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   601
            NumberFormatException nfe = new NumberFormatException();
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   602
            nfe.initCause(e);
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   603
            throw nfe;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   604
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   605
        this.scale = scl;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   606
        this.precision = prec;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   607
        this.intCompact = rs;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   608
        this.intVal = rb;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   609
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   610
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   611
    private int adjustScale(int scl, long exp) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   612
        long adjustedScale = scl - exp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   613
        if (adjustedScale > Integer.MAX_VALUE || adjustedScale < Integer.MIN_VALUE)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   614
            throw new NumberFormatException("Scale out of range.");
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   615
        scl = (int) adjustedScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   616
        return scl;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   617
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   618
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   619
    /*
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   620
     * parse exponent
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   621
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   622
    private static long parseExp(char[] in, int offset, int len){
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   623
        long exp = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   624
        offset++;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   625
        char c = in[offset];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   626
        len--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   627
        boolean negexp = (c == '-');
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   628
        // optional sign
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   629
        if (negexp || c == '+') {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   630
            offset++;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   631
            c = in[offset];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   632
            len--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   633
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   634
        if (len <= 0) // no exponent digits
28257
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   635
            throw new NumberFormatException("No exponent digits.");
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   636
        // skip leading zeros in the exponent
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   637
        while (len > 10 && (c=='0' || (Character.digit(c, 10) == 0))) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   638
            offset++;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   639
            c = in[offset];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   640
            len--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   641
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   642
        if (len > 10) // too many nonzero exponent digits
28257
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   643
            throw new NumberFormatException("Too many nonzero exponent digits.");
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   644
        // c now holds first digit of exponent
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   645
        for (;; len--) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   646
            int v;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   647
            if (c >= '0' && c <= '9') {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   648
                v = c - '0';
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   649
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   650
                v = Character.digit(c, 10);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   651
                if (v < 0) // not a digit
28257
070ef0dc9f5c 8064463: BigDecimal should populate NumberFormatException message
bpb
parents: 25859
diff changeset
   652
                    throw new NumberFormatException("Not a digit.");
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   653
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   654
            exp = exp * 10 + v;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   655
            if (len == 1)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   656
                break; // that was final character
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   657
            offset++;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   658
            c = in[offset];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   659
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   660
        if (negexp) // apply sign
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   661
            exp = -exp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   662
        return exp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * Translates a character array representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * {@code BigDecimal} into a {@code BigDecimal}, accepting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * same sequence of characters as the {@link #BigDecimal(String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * <p>Note that if the sequence of characters is already available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * as a character array, using this constructor is faster than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * converting the {@code char} array to string and using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * {@code BigDecimal(String)} constructor .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @param in {@code char} array that is the source of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @throws NumberFormatException if {@code in} is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *         representation of a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    public BigDecimal(char[] in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        this(in, 0, in.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Translates a character array representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * {@code BigDecimal} into a {@code BigDecimal}, accepting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * same sequence of characters as the {@link #BigDecimal(String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * constructor and with rounding according to the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * <p>Note that if the sequence of characters is already available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * as a character array, using this constructor is faster than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * converting the {@code char} array to string and using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * {@code BigDecimal(String)} constructor .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @param  in {@code char} array that is the source of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @throws NumberFormatException if {@code in} is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *         representation of a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    public BigDecimal(char[] in, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        this(in, 0, in.length, mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * Translates the string representation of a {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * into a {@code BigDecimal}.  The string representation consists
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
   712
     * of an optional sign, {@code '+'} (<code> '&#92;u002B'</code>) or
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
   713
     * {@code '-'} (<code>'&#92;u002D'</code>), followed by a sequence of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * zero or more decimal digits ("the integer"), optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * followed by a fraction, optionally followed by an exponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * <p>The fraction consists of a decimal point followed by zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * or more decimal digits.  The string must contain at least one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * digit in either the integer or the fraction.  The number formed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * by the sign, the integer and the fraction is referred to as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * <i>significand</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * <p>The exponent consists of the character {@code 'e'}
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
   724
     * (<code>'&#92;u0065'</code>) or {@code 'E'} (<code>'&#92;u0045'</code>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * followed by one or more decimal digits.  The value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * exponent must lie between -{@link Integer#MAX_VALUE} ({@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * Integer#MIN_VALUE}+1) and {@link Integer#MAX_VALUE}, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * <p>More formally, the strings this constructor accepts are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * described by the following grammar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * <dt><i>BigDecimalString:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * <dd><i>Sign<sub>opt</sub> Significand Exponent<sub>opt</sub></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * <dt><i>Sign:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * <dd>{@code +}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * <dd>{@code -}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * <dt><i>Significand:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * <dd><i>IntegerPart</i> {@code .} <i>FractionPart<sub>opt</sub></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * <dd>{@code .} <i>FractionPart</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * <dd><i>IntegerPart</i>
8162
d91ca3f845b0 7015827: Fix HTML validation issues in java.math package
darcy
parents: 5506
diff changeset
   742
     * <dt><i>IntegerPart:</i>
d91ca3f845b0 7015827: Fix HTML validation issues in java.math package
darcy
parents: 5506
diff changeset
   743
     * <dd><i>Digits</i>
d91ca3f845b0 7015827: Fix HTML validation issues in java.math package
darcy
parents: 5506
diff changeset
   744
     * <dt><i>FractionPart:</i>
d91ca3f845b0 7015827: Fix HTML validation issues in java.math package
darcy
parents: 5506
diff changeset
   745
     * <dd><i>Digits</i>
d91ca3f845b0 7015827: Fix HTML validation issues in java.math package
darcy
parents: 5506
diff changeset
   746
     * <dt><i>Exponent:</i>
d91ca3f845b0 7015827: Fix HTML validation issues in java.math package
darcy
parents: 5506
diff changeset
   747
     * <dd><i>ExponentIndicator SignedInteger</i>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * <dt><i>ExponentIndicator:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * <dd>{@code e}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * <dd>{@code E}
8162
d91ca3f845b0 7015827: Fix HTML validation issues in java.math package
darcy
parents: 5506
diff changeset
   751
     * <dt><i>SignedInteger:</i>
d91ca3f845b0 7015827: Fix HTML validation issues in java.math package
darcy
parents: 5506
diff changeset
   752
     * <dd><i>Sign<sub>opt</sub> Digits</i>
d91ca3f845b0 7015827: Fix HTML validation issues in java.math package
darcy
parents: 5506
diff changeset
   753
     * <dt><i>Digits:</i>
d91ca3f845b0 7015827: Fix HTML validation issues in java.math package
darcy
parents: 5506
diff changeset
   754
     * <dd><i>Digit</i>
d91ca3f845b0 7015827: Fix HTML validation issues in java.math package
darcy
parents: 5506
diff changeset
   755
     * <dd><i>Digits Digit</i>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * <dt><i>Digit:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * <dd>any character for which {@link Character#isDigit}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * returns {@code true}, including 0, 1, 2 ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * <p>The scale of the returned {@code BigDecimal} will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * number of digits in the fraction, or zero if the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * contains no decimal point, subject to adjustment for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * exponent; if the string contains an exponent, the exponent is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * subtracted from the scale.  The value of the resulting scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * must lie between {@code Integer.MIN_VALUE} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * {@code Integer.MAX_VALUE}, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * <p>The character-to-digit mapping is provided by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * java.lang.Character#digit} set to convert to radix 10.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * String may not contain any extraneous characters (whitespace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * for example).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * <p><b>Examples:</b><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * The value of the returned {@code BigDecimal} is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * <i>significand</i> &times; 10<sup>&nbsp;<i>exponent</i></sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * For each string on the left, the resulting representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * [{@code BigInteger}, {@code scale}] is shown on the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * "0"            [0,0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * "0.00"         [0,2]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * "123"          [123,0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * "-123"         [-123,0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * "1.23E3"       [123,-1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * "1.23E+3"      [123,-1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * "12.3E+7"      [123,-6]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * "12.0"         [120,1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * "12.3"         [123,1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * "0.00123"      [123,5]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * "-1.23E-12"    [-123,14]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * "1234.5E-4"    [12345,5]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * "0E+7"         [0,-7]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * "-0"           [0,0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * <p>Note: For values other than {@code float} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * {@code double} NaN and &plusmn;Infinity, this constructor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * compatible with the values returned by {@link Float#toString}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * and {@link Double#toString}.  This is generally the preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * way to convert a {@code float} or {@code double} into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * BigDecimal, as it doesn't suffer from the unpredictability of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * the {@link #BigDecimal(double)} constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @param val String representation of {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @throws NumberFormatException if {@code val} is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *         representation of a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    public BigDecimal(String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        this(val.toCharArray(), 0, val.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * Translates the string representation of a {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * into a {@code BigDecimal}, accepting the same strings as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * {@link #BigDecimal(String)} constructor, with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @param  val string representation of a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @throws NumberFormatException if {@code val} is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *         representation of a BigDecimal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    public BigDecimal(String val, MathContext mc) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   829
        this(val.toCharArray(), 0, val.length(), mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * Translates a {@code double} into a {@code BigDecimal} which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * is the exact decimal representation of the {@code double}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * binary floating-point value.  The scale of the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * {@code BigDecimal} is the smallest value such that
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
   837
     * <code>(10<sup>scale</sup> &times; val)</code> is an integer.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * <b>Notes:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * The results of this constructor can be somewhat unpredictable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * One might assume that writing {@code new BigDecimal(0.1)} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * Java creates a {@code BigDecimal} which is exactly equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * 0.1 (an unscaled value of 1, with a scale of 1), but it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * actually equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * 0.1000000000000000055511151231257827021181583404541015625.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * This is because 0.1 cannot be represented exactly as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * {@code double} (or, for that matter, as a binary fraction of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * any finite length).  Thus, the value that is being passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * <i>in</i> to the constructor is not exactly equal to 0.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * appearances notwithstanding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * The {@code String} constructor, on the other hand, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * perfectly predictable: writing {@code new BigDecimal("0.1")}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * creates a {@code BigDecimal} which is <i>exactly</i> equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * 0.1, as one would expect.  Therefore, it is generally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * recommended that the {@linkplain #BigDecimal(String)
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
   860
     * String constructor} be used in preference to this one.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * When a {@code double} must be used as a source for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * {@code BigDecimal}, note that this constructor provides an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * exact conversion; it does not give the same result as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * converting the {@code double} to a {@code String} using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * {@link Double#toString(double)} method and then using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * {@link #BigDecimal(String)} constructor.  To get that result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * use the {@code static} {@link #valueOf(double)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @param val {@code double} value to be converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *        {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * @throws NumberFormatException if {@code val} is infinite or NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    public BigDecimal(double val) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   877
        this(val,MathContext.UNLIMITED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * Translates a {@code double} into a {@code BigDecimal}, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * rounding according to the context settings.  The scale of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * {@code BigDecimal} is the smallest value such that
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
   884
     * <code>(10<sup>scale</sup> &times; val)</code> is an integer.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * <p>The results of this constructor can be somewhat unpredictable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * and its use is generally not recommended; see the notes under
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * the {@link #BigDecimal(double)} constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @param  val {@code double} value to be converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *         {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *         RoundingMode is UNNECESSARY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @throws NumberFormatException if {@code val} is infinite or NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    public BigDecimal(double val, MathContext mc) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   899
        if (Double.isInfinite(val) || Double.isNaN(val))
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   900
            throw new NumberFormatException("Infinite or NaN");
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   901
        // Translate the double into sign, exponent and significand, according
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   902
        // to the formulae in JLS, Section 20.10.22.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   903
        long valBits = Double.doubleToLongBits(val);
10430
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
   904
        int sign = ((valBits >> 63) == 0 ? 1 : -1);
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
   905
        int exponent = (int) ((valBits >> 52) & 0x7ffL);
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
   906
        long significand = (exponent == 0
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
   907
                ? (valBits & ((1L << 52) - 1)) << 1
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
   908
                : (valBits & ((1L << 52) - 1)) | (1L << 52));
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
   909
        exponent -= 1075;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   910
        // At this point, val == sign * significand * 2**exponent.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   911
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   912
        /*
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   913
         * Special case zero to supress nonterminating normalization and bogus
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   914
         * scale calculation.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   915
         */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   916
        if (significand == 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   917
            this.intVal = BigInteger.ZERO;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   918
            this.scale = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   919
            this.intCompact = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   920
            this.precision = 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   921
            return;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   922
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   923
        // Normalize
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   924
        while ((significand & 1) == 0) { // i.e., significand is even
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   925
            significand >>= 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   926
            exponent++;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   927
        }
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   928
        int scl = 0;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   929
        // Calculate intVal and scale
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   930
        BigInteger rb;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   931
        long compactVal = sign * significand;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   932
        if (exponent == 0) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   933
            rb = (compactVal == INFLATED) ? INFLATED_BIGINT : null;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   934
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   935
            if (exponent < 0) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   936
                rb = BigInteger.valueOf(5).pow(-exponent).multiply(compactVal);
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   937
                scl = -exponent;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   938
            } else { //  (exponent > 0)
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   939
                rb = BigInteger.valueOf(2).pow(exponent).multiply(compactVal);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   940
            }
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   941
            compactVal = compactValFor(rb);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   942
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   943
        int prec = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   944
        int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   945
        if (mcp > 0) { // do rounding
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   946
            int mode = mc.roundingMode.oldMode;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   947
            int drop;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   948
            if (compactVal == INFLATED) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   949
                prec = bigDigitLength(rb);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   950
                drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   951
                while (drop > 0) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   952
                    scl = checkScaleNonZero((long) scl - drop);
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   953
                    rb = divideAndRoundByTenPow(rb, drop, mode);
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   954
                    compactVal = compactValFor(rb);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   955
                    if (compactVal != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   956
                        break;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   957
                    }
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   958
                    prec = bigDigitLength(rb);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   959
                    drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   960
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   961
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   962
            if (compactVal != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   963
                prec = longDigitLength(compactVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   964
                drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   965
                while (drop > 0) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   966
                    scl = checkScaleNonZero((long) scl - drop);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   967
                    compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   968
                    prec = longDigitLength(compactVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   969
                    drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   970
                }
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   971
                rb = null;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   972
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   973
        }
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   974
        this.intVal = rb;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   975
        this.intCompact = compactVal;
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
   976
        this.scale = scl;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   977
        this.precision = prec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * Translates a {@code BigInteger} into a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * The scale of the {@code BigDecimal} is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * @param val {@code BigInteger} value to be converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *            {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    public BigDecimal(BigInteger val) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   988
        scale = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
   989
        intVal = val;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   990
        intCompact = compactValFor(val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * Translates a {@code BigInteger} into a {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * rounding according to the context settings.  The scale of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * {@code BigDecimal} is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * @param val {@code BigInteger} value to be converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     *            {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    public BigDecimal(BigInteger val, MathContext mc) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1006
        this(val,0,mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * Translates a {@code BigInteger} unscaled value and an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * {@code int} scale into a {@code BigDecimal}.  The value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * the {@code BigDecimal} is
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  1013
     * <code>(unscaledVal &times; 10<sup>-scale</sup>)</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * @param unscaledVal unscaled value of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * @param scale scale of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    public BigDecimal(BigInteger unscaledVal, int scale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        // Negative scales are now allowed
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1020
        this.intVal = unscaledVal;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1021
        this.intCompact = compactValFor(unscaledVal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        this.scale = scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * Translates a {@code BigInteger} unscaled value and an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * {@code int} scale into a {@code BigDecimal}, with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * according to the context settings.  The value of the
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  1029
     * {@code BigDecimal} is <code>(unscaledVal &times;
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  1030
     * 10<sup>-scale</sup>)</code>, rounded according to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * {@code precision} and rounding mode settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * @param  unscaledVal unscaled value of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * @param  scale scale of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    public BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1041
        long compactVal = compactValFor(unscaledVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1042
        int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1043
        int prec = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1044
        if (mcp > 0) { // do rounding
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1045
            int mode = mc.roundingMode.oldMode;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1046
            if (compactVal == INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1047
                prec = bigDigitLength(unscaledVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1048
                int drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1049
                while (drop > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1050
                    scale = checkScaleNonZero((long) scale - drop);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1051
                    unscaledVal = divideAndRoundByTenPow(unscaledVal, drop, mode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1052
                    compactVal = compactValFor(unscaledVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1053
                    if (compactVal != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1054
                        break;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1055
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1056
                    prec = bigDigitLength(unscaledVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1057
                    drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1058
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1059
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1060
            if (compactVal != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1061
                prec = longDigitLength(compactVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1062
                int drop = prec - mcp;     // drop can't be more than 18
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1063
                while (drop > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1064
                    scale = checkScaleNonZero((long) scale - drop);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1065
                    compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1066
                    prec = longDigitLength(compactVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1067
                    drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1068
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1069
                unscaledVal = null;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1070
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1071
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1072
        this.intVal = unscaledVal;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1073
        this.intCompact = compactVal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        this.scale = scale;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1075
        this.precision = prec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * Translates an {@code int} into a {@code BigDecimal}.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * scale of the {@code BigDecimal} is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @param val {@code int} value to be converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *            {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    public BigDecimal(int val) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1087
        this.intCompact = val;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1088
        this.scale = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1089
        this.intVal = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * Translates an {@code int} into a {@code BigDecimal}, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * rounding according to the context settings.  The scale of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * {@code BigDecimal}, before any rounding, is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * @param  val {@code int} value to be converted to {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    public BigDecimal(int val, MathContext mc) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1104
        int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1105
        long compactVal = val;
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1106
        int scl = 0;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1107
        int prec = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1108
        if (mcp > 0) { // do rounding
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1109
            prec = longDigitLength(compactVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1110
            int drop = prec - mcp; // drop can't be more than 18
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1111
            while (drop > 0) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1112
                scl = checkScaleNonZero((long) scl - drop);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1113
                compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1114
                prec = longDigitLength(compactVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1115
                drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1116
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1117
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1118
        this.intVal = null;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1119
        this.intCompact = compactVal;
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1120
        this.scale = scl;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1121
        this.precision = prec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * Translates a {@code long} into a {@code BigDecimal}.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * scale of the {@code BigDecimal} is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * @param val {@code long} value to be converted to {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    public BigDecimal(long val) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1132
        this.intCompact = val;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1133
        this.intVal = (val == INFLATED) ? INFLATED_BIGINT : null;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1134
        this.scale = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * Translates a {@code long} into a {@code BigDecimal}, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * rounding according to the context settings.  The scale of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * {@code BigDecimal}, before any rounding, is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * @param  val {@code long} value to be converted to {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    public BigDecimal(long val, MathContext mc) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1149
        int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1150
        int mode = mc.roundingMode.oldMode;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1151
        int prec = 0;
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1152
        int scl = 0;
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1153
        BigInteger rb = (val == INFLATED) ? INFLATED_BIGINT : null;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1154
        if (mcp > 0) { // do rounding
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1155
            if (val == INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1156
                prec = 19;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1157
                int drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1158
                while (drop > 0) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1159
                    scl = checkScaleNonZero((long) scl - drop);
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1160
                    rb = divideAndRoundByTenPow(rb, drop, mode);
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1161
                    val = compactValFor(rb);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1162
                    if (val != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1163
                        break;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1164
                    }
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1165
                    prec = bigDigitLength(rb);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1166
                    drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1167
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1168
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1169
            if (val != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1170
                prec = longDigitLength(val);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1171
                int drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1172
                while (drop > 0) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1173
                    scl = checkScaleNonZero((long) scl - drop);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1174
                    val = divideAndRound(val, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1175
                    prec = longDigitLength(val);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1176
                    drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1177
                }
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1178
                rb = null;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1179
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1180
        }
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1181
        this.intVal = rb;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1182
        this.intCompact = val;
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1183
        this.scale = scl;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1184
        this.precision = prec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    // Static Factory Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * Translates a {@code long} unscaled value and an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * {@code int} scale into a {@code BigDecimal}.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * {@literal "static factory method"} is provided in preference to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * a ({@code long}, {@code int}) constructor because it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * allows for reuse of frequently used {@code BigDecimal} values..
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * @param unscaledVal unscaled value of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * @param scale scale of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * @return a {@code BigDecimal} whose value is
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  1199
     *         <code>(unscaledVal &times; 10<sup>-scale</sup>)</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    public static BigDecimal valueOf(long unscaledVal, int scale) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1202
        if (scale == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1203
            return valueOf(unscaledVal);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1204
        else if (unscaledVal == 0) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1205
            return zeroValueOf(scale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1207
        return new BigDecimal(unscaledVal == INFLATED ?
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1208
                              INFLATED_BIGINT : null,
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1209
                              unscaledVal, scale, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * Translates a {@code long} value into a {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * with a scale of zero.  This {@literal "static factory method"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * is provided in preference to a ({@code long}) constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * because it allows for reuse of frequently used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * {@code BigDecimal} values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * @param val value of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * @return a {@code BigDecimal} whose value is {@code val}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    public static BigDecimal valueOf(long val) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1223
        if (val >= 0 && val < ZERO_THROUGH_TEN.length)
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1224
            return ZERO_THROUGH_TEN[(int)val];
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1225
        else if (val != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1226
            return new BigDecimal(null, val, 0, 0);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1227
        return new BigDecimal(INFLATED_BIGINT, val, 0, 0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1228
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1229
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1230
    static BigDecimal valueOf(long unscaledVal, int scale, int prec) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1231
        if (scale == 0 && unscaledVal >= 0 && unscaledVal < ZERO_THROUGH_TEN.length) {
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1232
            return ZERO_THROUGH_TEN[(int) unscaledVal];
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1233
        } else if (unscaledVal == 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1234
            return zeroValueOf(scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1235
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1236
        return new BigDecimal(unscaledVal == INFLATED ? INFLATED_BIGINT : null,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1237
                unscaledVal, scale, prec);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1238
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1239
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1240
    static BigDecimal valueOf(BigInteger intVal, int scale, int prec) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1241
        long val = compactValFor(intVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1242
        if (val == 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1243
            return zeroValueOf(scale);
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1244
        } else if (scale == 0 && val >= 0 && val < ZERO_THROUGH_TEN.length) {
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  1245
            return ZERO_THROUGH_TEN[(int) val];
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1246
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1247
        return new BigDecimal(intVal, val, scale, prec);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1248
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1249
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1250
    static BigDecimal zeroValueOf(int scale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1251
        if (scale >= 0 && scale < ZERO_SCALED_BY.length)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1252
            return ZERO_SCALED_BY[scale];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1253
        else
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1254
            return new BigDecimal(BigInteger.ZERO, 0, scale, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * Translates a {@code double} into a {@code BigDecimal}, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * the {@code double}'s canonical string representation provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * by the {@link Double#toString(double)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * <p><b>Note:</b> This is generally the preferred way to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * a {@code double} (or {@code float}) into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * {@code BigDecimal}, as the value returned is equal to that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * resulting from constructing a {@code BigDecimal} from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * result of using {@link Double#toString(double)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * @param  val {@code double} to convert to a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * @return a {@code BigDecimal} whose value is equal to or approximately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     *         equal to the value of {@code val}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * @throws NumberFormatException if {@code val} is infinite or NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    public static BigDecimal valueOf(double val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        // Reminder: a zero double returns '0.0', so we cannot fastpath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        // to use the constant ZERO.  This might be important enough to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        // justify a factory approach, a cache, or a few private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        // constants, later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        return new BigDecimal(Double.toString(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    // Arithmetic Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * Returns a {@code BigDecimal} whose value is {@code (this +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * augend)}, and whose scale is {@code max(this.scale(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * augend.scale())}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * @param  augend value to be added to this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * @return {@code this + augend}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    public BigDecimal add(BigDecimal augend) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1292
        if (this.intCompact != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1293
            if ((augend.intCompact != INFLATED)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1294
                return add(this.intCompact, this.scale, augend.intCompact, augend.scale);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1295
            } else {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1296
                return add(this.intCompact, this.scale, augend.intVal, augend.scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1297
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1298
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1299
            if ((augend.intCompact != INFLATED)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1300
                return add(augend.intCompact, augend.scale, this.intVal, this.scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1301
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1302
                return add(this.intVal, this.scale, augend.intVal, augend.scale);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1303
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * Returns a {@code BigDecimal} whose value is {@code (this + augend)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * If either number is zero and the precision setting is nonzero then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * the other number, rounded if necessary, is used as the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * @param  augend value to be added to this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * @return {@code this + augend}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    public BigDecimal add(BigDecimal augend, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        if (mc.precision == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            return add(augend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        BigDecimal lhs = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        // If either number is zero then the other number, rounded and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        // scaled if necessary, is used as the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            boolean lhsIsZero = lhs.signum() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            boolean augendIsZero = augend.signum() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            if (lhsIsZero || augendIsZero) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                int preferredScale = Math.max(lhs.scale(), augend.scale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                BigDecimal result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                if (lhsIsZero && augendIsZero)
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1337
                    return zeroValueOf(preferredScale);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1338
                result = lhsIsZero ? doRound(augend, mc) : doRound(lhs, mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                if (result.scale() == preferredScale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                    return result;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1342
                else if (result.scale() > preferredScale) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1343
                    return stripZerosToMatchScale(result.intVal, result.intCompact, result.scale, preferredScale);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1344
                } else { // result.scale < preferredScale
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    int precisionDiff = mc.precision - result.precision();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                    int scaleDiff     = preferredScale - result.scale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                    if (precisionDiff >= scaleDiff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                        return result.setScale(preferredScale); // can achieve target scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                        return result.setScale(result.scale() + precisionDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1356
        long padding = (long) lhs.scale - augend.scale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1357
        if (padding != 0) { // scales differ; alignment needed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            BigDecimal arg[] = preAlign(lhs, augend, padding, mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            matchScale(arg);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1360
            lhs = arg[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            augend = arg[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        }
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1363
        return doRound(lhs.inflated().add(augend.inflated()), lhs.scale, mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * Returns an array of length two, the sum of whose entries is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * equal to the rounded sum of the {@code BigDecimal} arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * <p>If the digit positions of the arguments have a sufficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * gap between them, the value smaller in magnitude can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * condensed into a {@literal "sticky bit"} and the end result will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * round the same way <em>if</em> the precision of the final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * result does not include the high order digit of the small
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * magnitude operand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * <p>Note that while strictly speaking this is an optimization,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * it makes a much wider range of additions practical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * <p>This corresponds to a pre-shift operation in a fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * precision floating-point adder; this method is complicated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * variable precision of the result as determined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * MathContext.  A more nuanced operation could implement a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * {@literal "right shift"} on the smaller magnitude operand so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * that the number of digits of the smaller operand could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * reduced even though the significands partially overlapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     */
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1388
    private BigDecimal[] preAlign(BigDecimal lhs, BigDecimal augend, long padding, MathContext mc) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        assert padding != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        BigDecimal big;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        BigDecimal small;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1393
        if (padding < 0) { // lhs is big; augend is small
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1394
            big = lhs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            small = augend;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1396
        } else { // lhs is small; augend is big
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1397
            big = augend;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            small = lhs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        /*
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1402
         * This is the estimated scale of an ulp of the result; it assumes that
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1403
         * the result doesn't have a carry-out on a true add (e.g. 999 + 1 =>
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1404
         * 1000) or any subtractive cancellation on borrowing (e.g. 100 - 1.2 =>
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1405
         * 98.8)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
         */
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1407
        long estResultUlpScale = (long) big.scale - big.precision() + mc.precision;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
         * The low-order digit position of big is big.scale().  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
         * is true regardless of whether big has a positive or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
         * negative scale.  The high-order digit position of small is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
         * small.scale - (small.precision() - 1).  To do the full
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
         * condensation, the digit positions of big and small must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
         * disjoint *and* the digit positions of small should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
         * directly visible in the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
         */
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1418
        long smallHighDigitPos = (long) small.scale - small.precision() + 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1419
        if (smallHighDigitPos > big.scale + 2 && // big and small disjoint
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            smallHighDigitPos > estResultUlpScale + 2) { // small digits not visible
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1421
            small = BigDecimal.valueOf(small.signum(), this.checkScale(Math.max(big.scale, estResultUlpScale) + 3));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        // Since addition is symmetric, preserving input order in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        // returned operands doesn't matter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        BigDecimal[] result = {big, small};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * Returns a {@code BigDecimal} whose value is {@code (this -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * subtrahend)}, and whose scale is {@code max(this.scale(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * subtrahend.scale())}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * @param  subtrahend value to be subtracted from this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * @return {@code this - subtrahend}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    public BigDecimal subtract(BigDecimal subtrahend) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1439
        if (this.intCompact != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1440
            if ((subtrahend.intCompact != INFLATED)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1441
                return add(this.intCompact, this.scale, -subtrahend.intCompact, subtrahend.scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1442
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1443
                return add(this.intCompact, this.scale, subtrahend.intVal.negate(), subtrahend.scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1444
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1445
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1446
            if ((subtrahend.intCompact != INFLATED)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1447
                // Pair of subtrahend values given before pair of
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1448
                // values from this BigDecimal to avoid need for
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1449
                // method overloading on the specialized add method
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1450
                return add(-subtrahend.intCompact, subtrahend.scale, this.intVal, this.scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1451
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1452
                return add(this.intVal, this.scale, subtrahend.intVal.negate(), subtrahend.scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1453
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1454
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * Returns a {@code BigDecimal} whose value is {@code (this - subtrahend)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * If {@code subtrahend} is zero then this, rounded if necessary, is used as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * result.  If this is zero then the result is {@code subtrahend.negate(mc)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * @param  subtrahend value to be subtracted from this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * @return {@code this - subtrahend}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    public BigDecimal subtract(BigDecimal subtrahend, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        if (mc.precision == 0)
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1473
            return subtract(subtrahend);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        // share the special rounding code in add()
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1475
        return add(subtrahend.negate(), mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    /**
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  1479
     * Returns a {@code BigDecimal} whose value is <code>(this &times;
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  1480
     * multiplicand)</code>, and whose scale is {@code (this.scale() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * multiplicand.scale())}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * @param  multiplicand value to be multiplied by this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * @return {@code this * multiplicand}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    public BigDecimal multiply(BigDecimal multiplicand) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1487
        int productScale = checkScale((long) scale + multiplicand.scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1488
        if (this.intCompact != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1489
            if ((multiplicand.intCompact != INFLATED)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1490
                return multiply(this.intCompact, multiplicand.intCompact, productScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1491
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1492
                return multiply(this.intCompact, multiplicand.intVal, productScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1493
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1494
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1495
            if ((multiplicand.intCompact != INFLATED)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1496
                return multiply(multiplicand.intCompact, this.intVal, productScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1497
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1498
                return multiply(this.intVal, multiplicand.intVal, productScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1499
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    /**
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  1504
     * Returns a {@code BigDecimal} whose value is <code>(this &times;
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  1505
     * multiplicand)</code>, with rounding according to the context settings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     * @param  multiplicand value to be multiplied by this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * @return {@code this * multiplicand}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
    public BigDecimal multiply(BigDecimal multiplicand, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        if (mc.precision == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            return multiply(multiplicand);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1517
        int productScale = checkScale((long) scale + multiplicand.scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1518
        if (this.intCompact != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1519
            if ((multiplicand.intCompact != INFLATED)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1520
                return multiplyAndRound(this.intCompact, multiplicand.intCompact, productScale, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1521
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1522
                return multiplyAndRound(this.intCompact, multiplicand.intVal, productScale, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1523
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1524
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1525
            if ((multiplicand.intCompact != INFLATED)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1526
                return multiplyAndRound(multiplicand.intCompact, this.intVal, productScale, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1527
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1528
                return multiplyAndRound(this.intVal, multiplicand.intVal, productScale, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1529
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1530
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * Returns a {@code BigDecimal} whose value is {@code (this /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * divisor)}, and whose scale is as specified.  If rounding must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * be performed to generate a result with the specified scale, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * specified rounding mode is applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * <p>The new {@link #divide(BigDecimal, int, RoundingMode)} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * should be used in preference to this legacy method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * @param  scale scale of the {@code BigDecimal} quotient to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * @param  roundingMode rounding mode to apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * @return {@code this / divisor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * @throws ArithmeticException if {@code divisor} is zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     *         {@code roundingMode==ROUND_UNNECESSARY} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     *         the specified scale is insufficient to represent the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     *         of the division exactly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * @throws IllegalArgumentException if {@code roundingMode} does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     *         represent a valid rounding mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * @see    #ROUND_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * @see    #ROUND_DOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * @see    #ROUND_CEILING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * @see    #ROUND_FLOOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * @see    #ROUND_HALF_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * @see    #ROUND_HALF_DOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * @see    #ROUND_HALF_EVEN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * @see    #ROUND_UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
    public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            throw new IllegalArgumentException("Invalid rounding mode");
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1564
        if (this.intCompact != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1565
            if ((divisor.intCompact != INFLATED)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1566
                return divide(this.intCompact, this.scale, divisor.intCompact, divisor.scale, scale, roundingMode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1567
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1568
                return divide(this.intCompact, this.scale, divisor.intVal, divisor.scale, scale, roundingMode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1569
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        } else {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1571
            if ((divisor.intCompact != INFLATED)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1572
                return divide(this.intVal, this.scale, divisor.intCompact, divisor.scale, scale, roundingMode);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1573
            } else {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1574
                return divide(this.intVal, this.scale, divisor.intVal, divisor.scale, scale, roundingMode);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1575
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * Returns a {@code BigDecimal} whose value is {@code (this /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * divisor)}, and whose scale is as specified.  If rounding must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     * be performed to generate a result with the specified scale, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * specified rounding mode is applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * @param  scale scale of the {@code BigDecimal} quotient to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     * @param  roundingMode rounding mode to apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * @return {@code this / divisor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * @throws ArithmeticException if {@code divisor} is zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     *         {@code roundingMode==RoundingMode.UNNECESSARY} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     *         the specified scale is insufficient to represent the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     *         of the division exactly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        return divide(divisor, scale, roundingMode.oldMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * Returns a {@code BigDecimal} whose value is {@code (this /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     * divisor)}, and whose scale is {@code this.scale()}.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * rounding must be performed to generate a result with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * scale, the specified rounding mode is applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * <p>The new {@link #divide(BigDecimal, RoundingMode)} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * should be used in preference to this legacy method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * @param  roundingMode rounding mode to apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * @return {@code this / divisor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * @throws ArithmeticException if {@code divisor==0}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     *         {@code roundingMode==ROUND_UNNECESSARY} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     *         {@code this.scale()} is insufficient to represent the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     *         of the division exactly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * @throws IllegalArgumentException if {@code roundingMode} does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     *         represent a valid rounding mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * @see    #ROUND_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * @see    #ROUND_DOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * @see    #ROUND_CEILING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     * @see    #ROUND_FLOOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     * @see    #ROUND_HALF_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     * @see    #ROUND_HALF_DOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * @see    #ROUND_HALF_EVEN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * @see    #ROUND_UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    public BigDecimal divide(BigDecimal divisor, int roundingMode) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1627
        return this.divide(divisor, scale, roundingMode);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * Returns a {@code BigDecimal} whose value is {@code (this /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * divisor)}, and whose scale is {@code this.scale()}.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * rounding must be performed to generate a result with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * scale, the specified rounding mode is applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     * @param  roundingMode rounding mode to apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * @return {@code this / divisor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * @throws ArithmeticException if {@code divisor==0}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     *         {@code roundingMode==RoundingMode.UNNECESSARY} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     *         {@code this.scale()} is insufficient to represent the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     *         of the division exactly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    public BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        return this.divide(divisor, scale, roundingMode.oldMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     * Returns a {@code BigDecimal} whose value is {@code (this /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * divisor)}, and whose preferred scale is {@code (this.scale() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * divisor.scale())}; if the exact quotient cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * represented (because it has a non-terminating decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     * expansion) an {@code ArithmeticException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * @throws ArithmeticException if the exact quotient does not have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     *         terminating decimal expansion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     * @return {@code this / divisor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    public BigDecimal divide(BigDecimal divisor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
         * Handle zero cases first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        if (divisor.signum() == 0) {   // x/0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            if (this.signum() == 0)    // 0/0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                throw new ArithmeticException("Division undefined");  // NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            throw new ArithmeticException("Division by zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        // Calculate preferred scale
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1674
        int preferredScale = saturateLong((long) this.scale - divisor.scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1675
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1676
        if (this.signum() == 0) // 0/y
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1677
            return zeroValueOf(preferredScale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
             * If the quotient this/divisor has a terminating decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
             * expansion, the expansion can have no more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
             * (a.precision() + ceil(10*b.precision)/3) digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
             * Therefore, create a MathContext object with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
             * precision and do a divide with the UNNECESSARY rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
             * mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            MathContext mc = new MathContext( (int)Math.min(this.precision() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                                                            (long)Math.ceil(10.0*divisor.precision()/3.0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                                                            Integer.MAX_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                                              RoundingMode.UNNECESSARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            BigDecimal quotient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                quotient = this.divide(divisor, mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            } catch (ArithmeticException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                throw new ArithmeticException("Non-terminating decimal expansion; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                                              "no exact representable decimal result.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
            int quotientScale = quotient.scale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            // divide(BigDecimal, mc) tries to adjust the quotient to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            // the desired one by removing trailing zeros; since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
            // exact divide method does not have an explicit digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            // limit, we can add zeros too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            if (preferredScale > quotientScale)
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1706
                return quotient.setScale(preferredScale, ROUND_UNNECESSARY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            return quotient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * Returns a {@code BigDecimal} whose value is {@code (this /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * divisor)}, with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     * @return {@code this / divisor}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     *         rounding mode is {@code UNNECESSARY} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     *         {@code mc.precision == 0} and the quotient has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     *         non-terminating decimal expansion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
    public BigDecimal divide(BigDecimal divisor, MathContext mc) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1726
        int mcp = mc.precision;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1727
        if (mcp == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            return divide(divisor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1730
        BigDecimal dividend = this;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1731
        long preferredScale = (long)dividend.scale - divisor.scale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        // Now calculate the answer.  We use the existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        // divide-and-round method, but as this rounds to scale we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        // to normalize the values here to achieve the desired result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        // For x/y we first handle y=0 and x=0, and then normalize x and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        // y to give x' and y' with the following constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        //   (a) 0.1 <= x' < 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        //   (b)  x' <= y' < 10*x'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        // Dividing x'/y' with the required scale set to mc.precision then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        // will give a result in the range 0.1 to 1 rounded to exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        // the right number of digits (except in the case of a result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        // 1.000... which can arise when x=y, or when rounding overflows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        // The 1.000... case will reduce properly to 1.
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1744
        if (divisor.signum() == 0) {      // x/0
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1745
            if (dividend.signum() == 0)    // 0/0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                throw new ArithmeticException("Division undefined");  // NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            throw new ArithmeticException("Division by zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        }
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1749
        if (dividend.signum() == 0) // 0/y
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1750
            return zeroValueOf(saturateLong(preferredScale));
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1751
        int xscale = dividend.precision();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1752
        int yscale = divisor.precision();
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1753
        if(dividend.intCompact!=INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1754
            if(divisor.intCompact!=INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1755
                return divide(dividend.intCompact, xscale, divisor.intCompact, yscale, preferredScale, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1756
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1757
                return divide(dividend.intCompact, xscale, divisor.intVal, yscale, preferredScale, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1758
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1759
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1760
            if(divisor.intCompact!=INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1761
                return divide(dividend.intVal, xscale, divisor.intCompact, yscale, preferredScale, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1762
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1763
                return divide(dividend.intVal, xscale, divisor.intVal, yscale, preferredScale, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1764
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1765
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * Returns a {@code BigDecimal} whose value is the integer part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * of the quotient {@code (this / divisor)} rounded down.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * preferred scale of the result is {@code (this.scale() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * divisor.scale())}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * @return The integer part of {@code this / divisor}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     * @throws ArithmeticException if {@code divisor==0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    public BigDecimal divideToIntegralValue(BigDecimal divisor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        // Calculate preferred scale
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1781
        int preferredScale = saturateLong((long) this.scale - divisor.scale);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1782
        if (this.compareMagnitude(divisor) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            // much faster when this << divisor
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1784
            return zeroValueOf(preferredScale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1787
        if (this.signum() == 0 && divisor.signum() != 0)
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1788
            return this.setScale(preferredScale, ROUND_UNNECESSARY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        // Perform a divide with enough digits to round to a correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        // integer value; then remove any fractional digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        int maxDigits = (int)Math.min(this.precision() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                                      (long)Math.ceil(10.0*divisor.precision()/3.0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                                      Math.abs((long)this.scale() - divisor.scale()) + 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                                      Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        BigDecimal quotient = this.divide(divisor, new MathContext(maxDigits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                                                                   RoundingMode.DOWN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        if (quotient.scale > 0) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1800
            quotient = quotient.setScale(0, RoundingMode.DOWN);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1801
            quotient = stripZerosToMatchScale(quotient.intVal, quotient.intCompact, quotient.scale, preferredScale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        if (quotient.scale < preferredScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
            // pad with zeros if necessary
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1806
            quotient = quotient.setScale(preferredScale, ROUND_UNNECESSARY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        }
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1808
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        return quotient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * Returns a {@code BigDecimal} whose value is the integer part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * of {@code (this / divisor)}.  Since the integer part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * exact quotient does not depend on the rounding mode, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * rounding mode does not affect the values returned by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * method.  The preferred scale of the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * {@code (this.scale() - divisor.scale())}.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * {@code ArithmeticException} is thrown if the integer part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * the exact quotient needs more than {@code mc.precision}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     * @return The integer part of {@code this / divisor}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     * @throws ArithmeticException if {@code divisor==0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     * @throws ArithmeticException if {@code mc.precision} {@literal >} 0 and the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     *         requires a precision of more than {@code mc.precision} digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
    public BigDecimal divideToIntegralValue(BigDecimal divisor, MathContext mc) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1833
        if (mc.precision == 0 || // exact result
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1834
            (this.compareMagnitude(divisor) < 0)) // zero result
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            return divideToIntegralValue(divisor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        // Calculate preferred scale
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1838
        int preferredScale = saturateLong((long)this.scale - divisor.scale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
         * Perform a normal divide to mc.precision digits.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
         * remainder has absolute value less than the divisor, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
         * integer portion of the quotient fits into mc.precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
         * digits.  Next, remove any fractional digits from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
         * quotient and adjust the scale to the preferred value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
         */
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1847
        BigDecimal result = this.divide(divisor, new MathContext(mc.precision, RoundingMode.DOWN));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        if (result.scale() < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
             * Result is an integer. See if quotient represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
             * full integer portion of the exact quotient; if it does,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
             * the computed remainder will be less than the divisor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            BigDecimal product = result.multiply(divisor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            // If the quotient is the full integer value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            // |dividend-product| < |divisor|.
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1858
            if (this.subtract(product).compareMagnitude(divisor) >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                throw new ArithmeticException("Division impossible");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        } else if (result.scale() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
             * Integer portion of quotient will fit into precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
             * digits; recompute quotient to scale 0 to avoid double
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
             * rounding and then try to adjust, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            result = result.setScale(0, RoundingMode.DOWN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        // else result.scale() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        int precisionDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        if ((preferredScale > result.scale()) &&
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1873
            (precisionDiff = mc.precision - result.precision()) > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
            return result.setScale(result.scale() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                                   Math.min(precisionDiff, preferredScale - result.scale) );
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1876
        } else {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  1877
            return stripZerosToMatchScale(result.intVal,result.intCompact,result.scale,preferredScale);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1878
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * Returns a {@code BigDecimal} whose value is {@code (this % divisor)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * <p>The remainder is given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * {@code this.subtract(this.divideToIntegralValue(divisor).multiply(divisor))}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * Note that this is not the modulo operation (the result can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * negative).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * @return {@code this % divisor}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * @throws ArithmeticException if {@code divisor==0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
    public BigDecimal remainder(BigDecimal divisor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        BigDecimal divrem[] = this.divideAndRemainder(divisor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        return divrem[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     * Returns a {@code BigDecimal} whose value is {@code (this %
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * divisor)}, with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     * The {@code MathContext} settings affect the implicit divide
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * used to compute the remainder.  The remainder computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     * itself is by definition exact.  Therefore, the remainder may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * contain more than {@code mc.getPrecision()} digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * <p>The remainder is given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * {@code this.subtract(this.divideToIntegralValue(divisor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     * mc).multiply(divisor))}.  Note that this is not the modulo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     * operation (the result can be negative).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     * @return {@code this % divisor}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     * @throws ArithmeticException if {@code divisor==0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     *         rounding mode is {@code UNNECESSARY}, or {@code mc.precision}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     *         {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     *         require a precision of more than {@code mc.precision} digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     * @see    #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
    public BigDecimal remainder(BigDecimal divisor, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        BigDecimal divrem[] = this.divideAndRemainder(divisor, mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        return divrem[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     * Returns a two-element {@code BigDecimal} array containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * result of {@code divideToIntegralValue} followed by the result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * {@code remainder} on the two operands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * <p>Note that if both the integer quotient and remainder are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * needed, this method is faster than using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * {@code divideToIntegralValue} and {@code remainder} methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * separately because the division need only be carried out once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * @param  divisor value by which this {@code BigDecimal} is to be divided,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     *         and the remainder computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     * @return a two element {@code BigDecimal} array: the quotient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     *         (the result of {@code divideToIntegralValue}) is the initial element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     *         and the remainder is the final element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * @throws ArithmeticException if {@code divisor==0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * @see    #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * @see    #remainder(java.math.BigDecimal, java.math.MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
    public BigDecimal[] divideAndRemainder(BigDecimal divisor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        // we use the identity  x = i * y + r to determine r
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        BigDecimal[] result = new BigDecimal[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        result[0] = this.divideToIntegralValue(divisor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        result[1] = this.subtract(result[0].multiply(divisor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * Returns a two-element {@code BigDecimal} array containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * result of {@code divideToIntegralValue} followed by the result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * {@code remainder} on the two operands calculated with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     * according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     * <p>Note that if both the integer quotient and remainder are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     * needed, this method is faster than using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * {@code divideToIntegralValue} and {@code remainder} methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     * separately because the division need only be carried out once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     * @param  divisor value by which this {@code BigDecimal} is to be divided,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     *         and the remainder computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * @return a two element {@code BigDecimal} array: the quotient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     *         (the result of {@code divideToIntegralValue}) is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     *         initial element and the remainder is the final element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     * @throws ArithmeticException if {@code divisor==0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     *         rounding mode is {@code UNNECESSARY}, or {@code mc.precision}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     *         {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     *         require a precision of more than {@code mc.precision} digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     * @see    #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     * @see    #remainder(java.math.BigDecimal, java.math.MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
    public BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        if (mc.precision == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
            return divideAndRemainder(divisor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        BigDecimal[] result = new BigDecimal[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        BigDecimal lhs = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        result[0] = lhs.divideToIntegralValue(divisor, mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        result[1] = lhs.subtract(result[0].multiply(divisor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     * Returns a {@code BigDecimal} whose value is
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  1998
     * <code>(this<sup>n</sup>)</code>, The power is computed exactly, to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * unlimited precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     * <p>The parameter {@code n} must be in the range 0 through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     * 999999999, inclusive.  {@code ZERO.pow(0)} returns {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     * #ONE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     * Note that future releases may expand the allowable exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     * range of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     * @param  n power to raise this {@code BigDecimal} to.
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2009
     * @return <code>this<sup>n</sup></code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * @throws ArithmeticException if {@code n} is out of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
    public BigDecimal pow(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        if (n < 0 || n > 999999999)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            throw new ArithmeticException("Invalid operation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        // No need to calculate pow(n) if result will over/underflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        // Don't attempt to support "supernormal" numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        int newScale = checkScale((long)scale * n);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2019
        return new BigDecimal(this.inflated().pow(n), newScale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     * Returns a {@code BigDecimal} whose value is
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2025
     * <code>(this<sup>n</sup>)</code>.  The current implementation uses
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     * the core algorithm defined in ANSI standard X3.274-1996 with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * rounding according to the context settings.  In general, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * returned numerical value is within two ulps of the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     * numerical value for the chosen precision.  Note that future
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     * releases may use a different algorithm with a decreased
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * allowable error bound and increased allowable exponent range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * <p>The X3.274-1996 algorithm is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * <li> An {@code ArithmeticException} exception is thrown if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     *  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     *    <li>{@code abs(n) > 999999999}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     *    <li>{@code mc.precision == 0} and {@code n < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     *    <li>{@code mc.precision > 0} and {@code n} has more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     *    {@code mc.precision} decimal digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     *  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     * <li> if {@code n} is zero, {@link #ONE} is returned even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     * {@code this} is zero, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     *   <li> if {@code n} is positive, the result is calculated via
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     *   the repeated squaring technique into a single accumulator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     *   The individual multiplications with the accumulator use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     *   same math context settings as in {@code mc} except for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     *   precision increased to {@code mc.precision + elength + 1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     *   where {@code elength} is the number of decimal digits in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     *   {@code n}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     *   <li> if {@code n} is negative, the result is calculated as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     *   {@code n} were positive; this value is then divided into one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     *   using the working precision specified above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     *   <li> The final value from either the positive or negative case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     *   is then rounded to the destination precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     *   </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * @param  n power to raise this {@code BigDecimal} to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     * @param  mc the context to use.
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2066
     * @return <code>this<sup>n</sup></code> using the ANSI standard X3.274-1996
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     *         algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     *         rounding mode is {@code UNNECESSARY}, or {@code n} is out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     *         of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
    public BigDecimal pow(int n, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        if (mc.precision == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            return pow(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
        if (n < -999999999 || n > 999999999)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            throw new ArithmeticException("Invalid operation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
        if (n == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            return ONE;                      // x**0 == 1 in X3.274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
        BigDecimal lhs = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        MathContext workmc = mc;           // working settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
        int mag = Math.abs(n);               // magnitude of n
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        if (mc.precision > 0) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2084
            int elength = longDigitLength(mag); // length of n in digits
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            if (elength > mc.precision)        // X3.274 rule
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                throw new ArithmeticException("Invalid operation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            workmc = new MathContext(mc.precision + elength + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                                      mc.roundingMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        // ready to carry out power calculation...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        BigDecimal acc = ONE;           // accumulator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        boolean seenbit = false;        // set once we've seen a 1-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        for (int i=1;;i++) {            // for each bit [top bit ignored]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
            mag += mag;                 // shift left 1 bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
            if (mag < 0) {              // top bit is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                seenbit = true;         // OK, we're off
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                acc = acc.multiply(lhs, workmc); // acc=acc*x
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
            if (i == 31)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                break;                  // that was the last bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
            if (seenbit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                acc=acc.multiply(acc, workmc);   // acc=acc*acc [square]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                // else (!seenbit) no point in squaring ONE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        // if negative n, calculate the reciprocal using working precision
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2106
        if (n < 0) // [hence mc.precision>0]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
            acc=ONE.divide(acc, workmc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        // round to final precision and strip zeros
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2109
        return doRound(acc, mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * Returns a {@code BigDecimal} whose value is the absolute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * of this {@code BigDecimal}, and whose scale is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * {@code this.scale()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     * @return {@code abs(this)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
    public BigDecimal abs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
        return (signum() < 0 ? negate() : this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     * Returns a {@code BigDecimal} whose value is the absolute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * of this {@code BigDecimal}, with rounding according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     * context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     * @param mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     * @return {@code abs(this)}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
    public BigDecimal abs(MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
        return (signum() < 0 ? negate(mc) : plus(mc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     * Returns a {@code BigDecimal} whose value is {@code (-this)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     * and whose scale is {@code this.scale()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     * @return {@code -this}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    public BigDecimal negate() {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2145
        if (intCompact == INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2146
            return new BigDecimal(intVal.negate(), INFLATED, scale, precision);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2147
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2148
            return valueOf(-intCompact, scale, precision);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     * Returns a {@code BigDecimal} whose value is {@code (-this)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
     * with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * @param mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     * @return {@code -this}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
    public BigDecimal negate(MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        return negate().plus(mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     * Returns a {@code BigDecimal} whose value is {@code (+this)}, and whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     * scale is {@code this.scale()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     * <p>This method, which simply returns this {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * is included for symmetry with the unary minus method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     * #negate()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     * @return {@code this}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * @see #negate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
    public BigDecimal plus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     * Returns a {@code BigDecimal} whose value is {@code (+this)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     * with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     * <p>The effect of this method is identical to that of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     * #round(MathContext)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     * @param mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     * @return {@code this}, rounded as necessary.  A zero result will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     *         have a scale of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     * @see    #round(MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
    public BigDecimal plus(MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        if (mc.precision == 0)                 // no rounding please
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
            return this;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2200
        return doRound(this, mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     * Returns the signum function of this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     * @return -1, 0, or 1 as the value of this {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     *         is negative, zero, or positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
    public int signum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
        return (intCompact != INFLATED)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
            Long.signum(intCompact):
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
            intVal.signum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     * Returns the <i>scale</i> of this {@code BigDecimal}.  If zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * or positive, the scale is the number of digits to the right of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     * the decimal point.  If negative, the unscaled value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     * number is multiplied by ten to the power of the negation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     * scale.  For example, a scale of {@code -3} means the unscaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     * value is multiplied by 1000.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     * @return the scale of this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
    public int scale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        return scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
     * Returns the <i>precision</i> of this {@code BigDecimal}.  (The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
     * precision is the number of digits in the unscaled value.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
     * <p>The precision of a zero value is 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
     * @return the precision of this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
    public int precision() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
        int result = precision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
        if (result == 0) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2241
            long s = intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2242
            if (s != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2243
                result = longDigitLength(s);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2244
            else
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2245
                result = bigDigitLength(intVal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            precision = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
     * Returns a {@code BigInteger} whose value is the <i>unscaled
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2254
     * value</i> of this {@code BigDecimal}.  (Computes <code>(this *
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2255
     * 10<sup>this.scale()</sup>)</code>.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     * @return the unscaled value of this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
    public BigInteger unscaledValue() {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2261
        return this.inflated();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
    // Rounding Modes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
     * Rounding mode to round away from zero.  Always increments the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
     * digit prior to a nonzero discarded fraction.  Note that this rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     * mode never decreases the magnitude of the calculated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
  2271
    public static final int ROUND_UP =           0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
     * Rounding mode to round towards zero.  Never increments the digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
     * prior to a discarded fraction (i.e., truncates).  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     * rounding mode never increases the magnitude of the calculated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
  2278
    public static final int ROUND_DOWN =         1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     * Rounding mode to round towards positive infinity.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     * {@code BigDecimal} is positive, behaves as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
     * {@code ROUND_UP}; if negative, behaves as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
     * {@code ROUND_DOWN}.  Note that this rounding mode never
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
     * decreases the calculated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
  2287
    public static final int ROUND_CEILING =      2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     * Rounding mode to round towards negative infinity.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
     * {@code BigDecimal} is positive, behave as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
     * {@code ROUND_DOWN}; if negative, behave as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
     * {@code ROUND_UP}.  Note that this rounding mode never
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
     * increases the calculated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
  2296
    public static final int ROUND_FLOOR =        3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
     * Rounding mode to round towards {@literal "nearest neighbor"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
     * unless both neighbors are equidistant, in which case round up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
     * Behaves as for {@code ROUND_UP} if the discarded fraction is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
     * &ge; 0.5; otherwise, behaves as for {@code ROUND_DOWN}.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     * that this is the rounding mode that most of us were taught in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
     * grade school.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
  2306
    public static final int ROUND_HALF_UP =      4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     * Rounding mode to round towards {@literal "nearest neighbor"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     * unless both neighbors are equidistant, in which case round
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     * down.  Behaves as for {@code ROUND_UP} if the discarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     * fraction is {@literal >} 0.5; otherwise, behaves as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     * {@code ROUND_DOWN}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
  2315
    public static final int ROUND_HALF_DOWN =    5;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     * Rounding mode to round towards the {@literal "nearest neighbor"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     * unless both neighbors are equidistant, in which case, round
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
     * towards the even neighbor.  Behaves as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     * {@code ROUND_HALF_UP} if the digit to the left of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     * discarded fraction is odd; behaves as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     * {@code ROUND_HALF_DOWN} if it's even.  Note that this is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     * rounding mode that minimizes cumulative error when applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
     * repeatedly over a sequence of calculations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
  2327
    public static final int ROUND_HALF_EVEN =    6;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     * Rounding mode to assert that the requested operation has an exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     * result, hence no rounding is necessary.  If this rounding mode is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     * specified on an operation that yields an inexact result, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * {@code ArithmeticException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
  2335
    public static final int ROUND_UNNECESSARY =  7;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
    // Scaling/Rounding Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
     * Returns a {@code BigDecimal} rounded according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
     * {@code MathContext} settings.  If the precision setting is 0 then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
     * no rounding takes place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
     * <p>The effect of this method is identical to that of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
     * {@link #plus(MathContext)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
     * @param mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
     * @return a {@code BigDecimal} rounded according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
     *         {@code MathContext} settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
     * @throws ArithmeticException if the rounding mode is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
     *         {@code UNNECESSARY} and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     *         {@code BigDecimal}  operation would require rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
     * @see    #plus(MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
    public BigDecimal round(MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        return plus(mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     * Returns a {@code BigDecimal} whose scale is the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * value, and whose unscaled value is determined by multiplying or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     * dividing this {@code BigDecimal}'s unscaled value by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
     * appropriate power of ten to maintain its overall value.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     * scale is reduced by the operation, the unscaled value must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
     * divided (rather than multiplied), and the value may be changed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     * in this case, the specified rounding mode is applied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     * division.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
     * <p>Note that since BigDecimal objects are immutable, calls of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
     * this method do <i>not</i> result in the original object being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
     * modified, contrary to the usual convention of having methods
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2374
     * named <code>set<i>X</i></code> mutate field <i>{@code X}</i>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
     * Instead, {@code setScale} returns an object with the proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     * scale; the returned object may or may not be newly allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
     * @param  newScale scale of the {@code BigDecimal} value to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
     * @param  roundingMode The rounding mode to apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
     * @return a {@code BigDecimal} whose scale is the specified value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
     *         and whose unscaled value is determined by multiplying or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
     *         dividing this {@code BigDecimal}'s unscaled value by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
     *         appropriate power of ten to maintain its overall value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
     * @throws ArithmeticException if {@code roundingMode==UNNECESSARY}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
     *         and the specified scaling operation would require
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
     *         rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
     * @see    RoundingMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
    public BigDecimal setScale(int newScale, RoundingMode roundingMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        return setScale(newScale, roundingMode.oldMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     * Returns a {@code BigDecimal} whose scale is the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     * value, and whose unscaled value is determined by multiplying or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     * dividing this {@code BigDecimal}'s unscaled value by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     * appropriate power of ten to maintain its overall value.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     * scale is reduced by the operation, the unscaled value must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     * divided (rather than multiplied), and the value may be changed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     * in this case, the specified rounding mode is applied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     * division.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
     * <p>Note that since BigDecimal objects are immutable, calls of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
     * this method do <i>not</i> result in the original object being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     * modified, contrary to the usual convention of having methods
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2407
     * named <code>set<i>X</i></code> mutate field <i>{@code X}</i>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     * Instead, {@code setScale} returns an object with the proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     * scale; the returned object may or may not be newly allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     * <p>The new {@link #setScale(int, RoundingMode)} method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     * be used in preference to this legacy method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     * @param  newScale scale of the {@code BigDecimal} value to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     * @param  roundingMode The rounding mode to apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     * @return a {@code BigDecimal} whose scale is the specified value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     *         and whose unscaled value is determined by multiplying or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     *         dividing this {@code BigDecimal}'s unscaled value by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
     *         appropriate power of ten to maintain its overall value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
     * @throws ArithmeticException if {@code roundingMode==ROUND_UNNECESSARY}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
     *         and the specified scaling operation would require
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     *         rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     * @throws IllegalArgumentException if {@code roundingMode} does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
     *         represent a valid rounding mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     * @see    #ROUND_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     * @see    #ROUND_DOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
     * @see    #ROUND_CEILING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     * @see    #ROUND_FLOOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     * @see    #ROUND_HALF_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     * @see    #ROUND_HALF_DOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
     * @see    #ROUND_HALF_EVEN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     * @see    #ROUND_UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
    public BigDecimal setScale(int newScale, int roundingMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
        if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
            throw new IllegalArgumentException("Invalid rounding mode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2438
        int oldScale = this.scale;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2439
        if (newScale == oldScale)        // easy case
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
        if (this.signum() == 0)            // zero can have any scale
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2442
            return zeroValueOf(newScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2443
        if(this.intCompact!=INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2444
            long rs = this.intCompact;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2445
            if (newScale > oldScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2446
                int raise = checkScale((long) newScale - oldScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2447
                if ((rs = longMultiplyPowerTen(rs, raise)) != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2448
                    return valueOf(rs,newScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2449
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2450
                BigInteger rb = bigMultiplyPowerTen(raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2451
                return new BigDecimal(rb, INFLATED, newScale, (precision > 0) ? precision + raise : 0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2452
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2453
                // newScale < oldScale -- drop some digits
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2454
                // Can't predict the precision due to the effect of rounding.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2455
                int drop = checkScale((long) oldScale - newScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2456
                if (drop < LONG_TEN_POWERS_TABLE.length) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2457
                    return divideAndRound(rs, LONG_TEN_POWERS_TABLE[drop], newScale, roundingMode, newScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2458
                } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2459
                    return divideAndRound(this.inflated(), bigTenToThe(drop), newScale, roundingMode, newScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2460
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2461
            }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2462
        } else {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2463
            if (newScale > oldScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2464
                int raise = checkScale((long) newScale - oldScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2465
                BigInteger rb = bigMultiplyPowerTen(this.intVal,raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2466
                return new BigDecimal(rb, INFLATED, newScale, (precision > 0) ? precision + raise : 0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2467
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2468
                // newScale < oldScale -- drop some digits
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2469
                // Can't predict the precision due to the effect of rounding.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2470
                int drop = checkScale((long) oldScale - newScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2471
                if (drop < LONG_TEN_POWERS_TABLE.length)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2472
                    return divideAndRound(this.intVal, LONG_TEN_POWERS_TABLE[drop], newScale, roundingMode,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2473
                                          newScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2474
                else
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2475
                    return divideAndRound(this.intVal,  bigTenToThe(drop), newScale, roundingMode, newScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2476
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     * Returns a {@code BigDecimal} whose scale is the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
     * value, and whose value is numerically equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
     * {@code BigDecimal}'s.  Throws an {@code ArithmeticException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
     * if this is not possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
     * <p>This call is typically used to increase the scale, in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
     * case it is guaranteed that there exists a {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
     * of the specified scale and the correct value.  The call can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
     * also be used to reduce the scale if the caller knows that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
     * {@code BigDecimal} has sufficiently many zeros at the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
     * its fractional part (i.e., factors of ten in its integer value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
     * to allow for the rescaling without changing its value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
     * <p>This method returns the same result as the two-argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
     * versions of {@code setScale}, but saves the caller the trouble
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
     * of specifying a rounding mode in cases where it is irrelevant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
     * <p>Note that since {@code BigDecimal} objects are immutable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
     * calls of this method do <i>not</i> result in the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
     * object being modified, contrary to the usual convention of
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2501
     * having methods named <code>set<i>X</i></code> mutate field
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
     * <i>{@code X}</i>.  Instead, {@code setScale} returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
     * object with the proper scale; the returned object may or may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
     * not be newly allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
     * @param  newScale scale of the {@code BigDecimal} value to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
     * @return a {@code BigDecimal} whose scale is the specified value, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
     *         whose unscaled value is determined by multiplying or dividing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
     *         this {@code BigDecimal}'s unscaled value by the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
     *         power of ten to maintain its overall value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
     * @throws ArithmeticException if the specified scaling operation would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     *         require rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
     * @see    #setScale(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     * @see    #setScale(int, RoundingMode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
    public BigDecimal setScale(int newScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
        return setScale(newScale, ROUND_UNNECESSARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
    // Decimal Point Motion Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
     * Returns a {@code BigDecimal} which is equivalent to this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
     * with the decimal point moved {@code n} places to the left.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     * {@code n} is non-negative, the call merely adds {@code n} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     * the scale.  If {@code n} is negative, the call is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
     * to {@code movePointRight(-n)}.  The {@code BigDecimal}
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2528
     * returned by this call has value <code>(this &times;
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2529
     * 10<sup>-n</sup>)</code> and scale {@code max(this.scale()+n,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
     * 0)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
     * @param  n number of places to move the decimal point to the left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
     * @return a {@code BigDecimal} which is equivalent to this one with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
     *         decimal point moved {@code n} places to the left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
     * @throws ArithmeticException if scale overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
    public BigDecimal movePointLeft(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
        // Cannot use movePointRight(-n) in case of n==Integer.MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
        int newScale = checkScale((long)scale + n);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2540
        BigDecimal num = new BigDecimal(intVal, intCompact, newScale, 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2541
        return num.scale < 0 ? num.setScale(0, ROUND_UNNECESSARY) : num;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
     * Returns a {@code BigDecimal} which is equivalent to this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
     * with the decimal point moved {@code n} places to the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
     * If {@code n} is non-negative, the call merely subtracts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
     * {@code n} from the scale.  If {@code n} is negative, the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
     * is equivalent to {@code movePointLeft(-n)}.  The
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2550
     * {@code BigDecimal} returned by this call has value <code>(this
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2551
     * &times; 10<sup>n</sup>)</code> and scale {@code max(this.scale()-n,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
     * 0)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
     * @param  n number of places to move the decimal point to the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
     * @return a {@code BigDecimal} which is equivalent to this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
     *         with the decimal point moved {@code n} places to the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
     * @throws ArithmeticException if scale overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
    public BigDecimal movePointRight(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
        // Cannot use movePointLeft(-n) in case of n==Integer.MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
        int newScale = checkScale((long)scale - n);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2562
        BigDecimal num = new BigDecimal(intVal, intCompact, newScale, 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2563
        return num.scale < 0 ? num.setScale(0, ROUND_UNNECESSARY) : num;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
     * Returns a BigDecimal whose numerical value is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
     * ({@code this} * 10<sup>n</sup>).  The scale of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
     * the result is {@code (this.scale() - n)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
     *
18558
4c7fd49d28d0 7018139: Fix HTML accessibility and doclint issues in java.math
darcy
parents: 18286
diff changeset
  2571
     * @param n the exponent power of ten to scale by
4c7fd49d28d0 7018139: Fix HTML accessibility and doclint issues in java.math
darcy
parents: 18286
diff changeset
  2572
     * @return a BigDecimal whose numerical value is equal to
4c7fd49d28d0 7018139: Fix HTML accessibility and doclint issues in java.math
darcy
parents: 18286
diff changeset
  2573
     * ({@code this} * 10<sup>n</sup>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
     * @throws ArithmeticException if the scale would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
     *         outside the range of a 32-bit integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
    public BigDecimal scaleByPowerOfTen(int n) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2580
        return new BigDecimal(intVal, intCompact,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2581
                              checkScale((long)scale - n), precision);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
     * Returns a {@code BigDecimal} which is numerically equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
     * this one but with any trailing zeros removed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
     * representation.  For example, stripping the trailing zeros from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
     * the {@code BigDecimal} value {@code 600.0}, which has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
     * [{@code BigInteger}, {@code scale}] components equals to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
     * [6000, 1], yields {@code 6E2} with [{@code BigInteger},
18798
7109807f56e9 6480539: BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0")
bpb
parents: 18558
diff changeset
  2591
     * {@code scale}] components equals to [6, -2].  If
7109807f56e9 6480539: BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0")
bpb
parents: 18558
diff changeset
  2592
     * this BigDecimal is numerically equal to zero, then
7109807f56e9 6480539: BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0")
bpb
parents: 18558
diff changeset
  2593
     * {@code BigDecimal.ZERO} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
     * @return a numerically equal {@code BigDecimal} with any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
     * trailing zeros removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
    public BigDecimal stripTrailingZeros() {
18798
7109807f56e9 6480539: BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0")
bpb
parents: 18558
diff changeset
  2600
        if (intCompact == 0 || (intVal != null && intVal.signum() == 0)) {
7109807f56e9 6480539: BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0")
bpb
parents: 18558
diff changeset
  2601
            return BigDecimal.ZERO;
7109807f56e9 6480539: BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0")
bpb
parents: 18558
diff changeset
  2602
        } else if (intCompact != INFLATED) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2603
            return createAndStripZerosToMatchScale(intCompact, scale, Long.MIN_VALUE);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2604
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2605
            return createAndStripZerosToMatchScale(intVal, scale, Long.MIN_VALUE);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2606
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
    // Comparison Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
     * Compares this {@code BigDecimal} with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
     * {@code BigDecimal}.  Two {@code BigDecimal} objects that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
     * equal in value but have a different scale (like 2.0 and 2.00)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
     * are considered equal by this method.  This method is provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
     * in preference to individual methods for each of the six boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
     * comparison operators ({@literal <}, ==,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
     * {@literal >}, {@literal >=}, !=, {@literal <=}).  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
     * suggested idiom for performing these comparisons is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
     * {@code (x.compareTo(y)} &lt;<i>op</i>&gt; {@code 0)}, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
     * &lt;<i>op</i>&gt; is one of the six comparison operators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
     * @param  val {@code BigDecimal} to which this {@code BigDecimal} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
     *         to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
     * @return -1, 0, or 1 as this {@code BigDecimal} is numerically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
     *          less than, equal to, or greater than {@code val}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
     */
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  2628
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
    public int compareTo(BigDecimal val) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2630
        // Quick path for equal scale and non-inflated case.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2631
        if (scale == val.scale) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2632
            long xs = intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2633
            long ys = val.intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2634
            if (xs != INFLATED && ys != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2635
                return xs != ys ? ((xs > ys) ? 1 : -1) : 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2636
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2637
        int xsign = this.signum();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2638
        int ysign = val.signum();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2639
        if (xsign != ysign)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2640
            return (xsign > ysign) ? 1 : -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2641
        if (xsign == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2642
            return 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2643
        int cmp = compareMagnitude(val);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2644
        return (xsign > 0) ? cmp : -cmp;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2645
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2647
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2648
     * Version of compareTo that ignores sign.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2649
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2650
    private int compareMagnitude(BigDecimal val) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2651
        // Match scales, avoid unnecessary inflation
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2652
        long ys = val.intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2653
        long xs = this.intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2654
        if (xs == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2655
            return (ys == 0) ? 0 : -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2656
        if (ys == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2657
            return 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
19585
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  2659
        long sdiff = (long)this.scale - val.scale;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2660
        if (sdiff != 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2661
            // Avoid matching scales if the (adjusted) exponents differ
19585
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  2662
            long xae = (long)this.precision() - this.scale;   // [-1]
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  2663
            long yae = (long)val.precision() - val.scale;     // [-1]
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2664
            if (xae < yae)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2665
                return -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2666
            if (xae > yae)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2667
                return 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2668
            if (sdiff < 0) {
19585
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  2669
                // The cases sdiff <= Integer.MIN_VALUE intentionally fall through.
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  2670
                if ( sdiff > Integer.MIN_VALUE &&
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  2671
                      (xs == INFLATED ||
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  2672
                      (xs = longMultiplyPowerTen(xs, (int)-sdiff)) == INFLATED) &&
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2673
                     ys == INFLATED) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  2674
                    BigInteger rb = bigMultiplyPowerTen((int)-sdiff);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2675
                    return rb.compareMagnitude(val.intVal);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2676
                }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2677
            } else { // sdiff > 0
19585
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  2678
                // The cases sdiff > Integer.MAX_VALUE intentionally fall through.
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  2679
                if ( sdiff <= Integer.MAX_VALUE &&
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  2680
                      (ys == INFLATED ||
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  2681
                      (ys = longMultiplyPowerTen(ys, (int)sdiff)) == INFLATED) &&
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2682
                     xs == INFLATED) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  2683
                    BigInteger rb = val.bigMultiplyPowerTen((int)sdiff);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2684
                    return this.intVal.compareMagnitude(rb);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2685
                }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2686
            }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2687
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2688
        if (xs != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2689
            return (ys != INFLATED) ? longCompareMagnitude(xs, ys) : -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2690
        else if (ys != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2691
            return 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2692
        else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2693
            return this.intVal.compareMagnitude(val.intVal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
     * Compares this {@code BigDecimal} with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
     * {@code Object} for equality.  Unlike {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
     * #compareTo(BigDecimal) compareTo}, this method considers two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
     * {@code BigDecimal} objects equal only if they are equal in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
     * value and scale (thus 2.0 is not equal to 2.00 when compared by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
     * this method).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
     * @param  x {@code Object} to which this {@code BigDecimal} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
     *         to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
     * @return {@code true} if and only if the specified {@code Object} is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
     *         {@code BigDecimal} whose value and scale are equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
     *         {@code BigDecimal}'s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
     * @see    #compareTo(java.math.BigDecimal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
     * @see    #hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2712
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
    public boolean equals(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
        if (!(x instanceof BigDecimal))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
        BigDecimal xDec = (BigDecimal) x;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2717
        if (x == this)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2718
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
        if (scale != xDec.scale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
            return false;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2721
        long s = this.intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2722
        long xs = xDec.intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2723
        if (s != INFLATED) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2724
            if (xs == INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2725
                xs = compactValFor(xDec.intVal);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2726
            return xs == s;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2727
        } else if (xs != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2728
            return xs == compactValFor(this.intVal);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2729
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2730
        return this.inflated().equals(xDec.inflated());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
     * Returns the minimum of this {@code BigDecimal} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
     * {@code val}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
     * @param  val value with which the minimum is to be computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
     * @return the {@code BigDecimal} whose value is the lesser of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
     *         {@code BigDecimal} and {@code val}.  If they are equal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
     *         as defined by the {@link #compareTo(BigDecimal) compareTo}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
     *         method, {@code this} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
     * @see    #compareTo(java.math.BigDecimal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
    public BigDecimal min(BigDecimal val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
        return (compareTo(val) <= 0 ? this : val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
     * Returns the maximum of this {@code BigDecimal} and {@code val}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
     * @param  val value with which the maximum is to be computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
     * @return the {@code BigDecimal} whose value is the greater of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
     *         {@code BigDecimal} and {@code val}.  If they are equal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
     *         as defined by the {@link #compareTo(BigDecimal) compareTo}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
     *         method, {@code this} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
     * @see    #compareTo(java.math.BigDecimal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
    public BigDecimal max(BigDecimal val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
        return (compareTo(val) >= 0 ? this : val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
    // Hash Function
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
     * Returns the hash code for this {@code BigDecimal}.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
     * two {@code BigDecimal} objects that are numerically equal but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
     * differ in scale (like 2.0 and 2.00) will generally <i>not</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
     * have the same hash code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
     * @return hash code for this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
     * @see #equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2773
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
        if (intCompact != INFLATED) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2776
            long val2 = (intCompact < 0)? -intCompact : intCompact;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
            int temp = (int)( ((int)(val2 >>> 32)) * 31  +
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2778
                              (val2 & LONG_MASK));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
            return 31*((intCompact < 0) ?-temp:temp) + scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
            return 31*intVal.hashCode() + scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
    // Format Converters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
     * Returns the string representation of this {@code BigDecimal},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
     * using scientific notation if an exponent is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
     * <p>A standard canonical string form of the {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
     * is created as though by the following steps: first, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
     * absolute value of the unscaled value of the {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
     * is converted to a string in base ten using the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
     * {@code '0'} through {@code '9'} with no leading zeros (except
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
     * if its value is zero, in which case a single {@code '0'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
     * character is used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
     * <p>Next, an <i>adjusted exponent</i> is calculated; this is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
     * negated scale, plus the number of characters in the converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
     * unscaled value, less one.  That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
     * {@code -scale+(ulength-1)}, where {@code ulength} is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
     * length of the absolute value of the unscaled value in decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
     * digits (its <i>precision</i>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
     * <p>If the scale is greater than or equal to zero and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
     * adjusted exponent is greater than or equal to {@code -6}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
     * number will be converted to a character form without using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
     * exponential notation.  In this case, if the scale is zero then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
     * no decimal point is added and if the scale is positive a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
     * decimal point will be inserted with the scale specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
     * number of characters to the right of the decimal point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
     * {@code '0'} characters are added to the left of the converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
     * unscaled value as necessary.  If no character precedes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
     * decimal point after this insertion then a conventional
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
     * {@code '0'} character is prefixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
     * <p>Otherwise (that is, if the scale is negative, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
     * adjusted exponent is less than {@code -6}), the number will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
     * converted to a character form using exponential notation.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
     * this case, if the converted {@code BigInteger} has more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
     * one digit a decimal point is inserted after the first digit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
     * An exponent in character form is then suffixed to the converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
     * unscaled value (perhaps with inserted decimal point); this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
     * comprises the letter {@code 'E'} followed immediately by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
     * adjusted exponent converted to a character form.  The latter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
     * in base ten, using the characters {@code '0'} through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
     * {@code '9'} with no leading zeros, and is always prefixed by a
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2828
     * sign character {@code '-'} (<code>'&#92;u002D'</code>) if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
     * adjusted exponent is negative, {@code '+'}
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2830
     * (<code>'&#92;u002B'</code>) otherwise).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
     * <p>Finally, the entire string is prefixed by a minus sign
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2833
     * character {@code '-'} (<code>'&#92;u002D'</code>) if the unscaled
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
     * value is less than zero.  No sign character is prefixed if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
     * unscaled value is zero or positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
     * <p><b>Examples:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
     * <p>For each representation [<i>unscaled value</i>, <i>scale</i>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
     * on the left, the resulting string is shown on the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
     * [123,0]      "123"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
     * [-123,0]     "-123"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
     * [123,-1]     "1.23E+3"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
     * [123,-3]     "1.23E+5"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
     * [123,1]      "12.3"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
     * [123,5]      "0.00123"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
     * [123,10]     "1.23E-8"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
     * [-123,12]    "-1.23E-10"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
     * <b>Notes:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
     * <li>There is a one-to-one mapping between the distinguishable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
     * {@code BigDecimal} values and the result of this conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
     * That is, every distinguishable {@code BigDecimal} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
     * (unscaled value and scale) has a unique string representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
     * as a result of using {@code toString}.  If that string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
     * representation is converted back to a {@code BigDecimal} using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
     * the {@link #BigDecimal(String)} constructor, then the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
     * value will be recovered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
     * <li>The string produced for a given number is always the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
     * it is not affected by locale.  This means that it can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
     * as a canonical string representation for exchanging decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
     * data, or as a key for a Hashtable, etc.  Locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
     * number formatting and parsing is handled by the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
     * java.text.NumberFormat} class and its subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
     * <li>The {@link #toEngineeringString} method may be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
     * presenting numbers with exponents in engineering notation, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
     * {@link #setScale(int,RoundingMode) setScale} method may be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
     * rounding a {@code BigDecimal} so it has a known number of digits after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
     * the decimal point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
     * <li>The digit-to-character mapping provided by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
     * {@code Character.forDigit} is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
     * @return string representation of this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
     * @see    Character#forDigit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
     * @see    #BigDecimal(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2885
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
    public String toString() {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2887
        String sc = stringCache;
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  2888
        if (sc == null) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2889
            stringCache = sc = layoutChars(true);
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  2890
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2891
        return sc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
     * Returns a string representation of this {@code BigDecimal},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
     * using engineering notation if an exponent is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
     * <p>Returns a string that represents the {@code BigDecimal} as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
     * described in the {@link #toString()} method, except that if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
     * exponential notation is used, the power of ten is adjusted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
     * be a multiple of three (engineering notation) such that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
     * integer part of nonzero values will be in the range 1 through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
     * 999.  If exponential notation is used for zero values, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
     * decimal point and one or two fractional zero digits are used so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
     * that the scale of the zero value is preserved.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
     * unlike the output of {@link #toString()}, the output of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
     * method is <em>not</em> guaranteed to recover the same [integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
     * scale] pair of this {@code BigDecimal} if the output string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
     * converting back to a {@code BigDecimal} using the {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
     * #BigDecimal(String) string constructor}.  The result of this method meets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
     * the weaker constraint of always producing a numerically equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
     * result from applying the string constructor to the method's output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
     * @return string representation of this {@code BigDecimal}, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
     *         engineering notation if an exponent is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
    public String toEngineeringString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
        return layoutChars(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
     * Returns a string representation of this {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
     * without an exponent field.  For values with a positive scale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
     * the number of digits to the right of the decimal point is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
     * to indicate scale.  For values with a zero or negative scale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
     * the resulting string is generated as if the value were
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
     * converted to a numerically equal value with zero scale and as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
     * if all the trailing zeros of the zero scale value were present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
     * in the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
     * The entire string is prefixed by a minus sign character '-'
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30797
diff changeset
  2933
     * (<code>'&#92;u002D'</code>) if the unscaled value is less than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
     * zero. No sign character is prefixed if the unscaled value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
     * zero or positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
     * Note that if the result of this method is passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
     * {@linkplain #BigDecimal(String) string constructor}, only the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
     * numerical value of this {@code BigDecimal} will necessarily be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
     * recovered; the representation of the new {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
     * may have a different scale.  In particular, if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
     * {@code BigDecimal} has a negative scale, the string resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
     * from this method will have a scale of zero when processed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
     * the string constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
     * (This method behaves analogously to the {@code toString}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
     * method in 1.4 and earlier releases.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
     * @return a string representation of this {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
     * without an exponent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
     * @see #toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
     * @see #toEngineeringString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
    public String toPlainString() {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2956
        if(scale==0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2957
            if(intCompact!=INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2958
                return Long.toString(intCompact);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2959
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2960
                return intVal.toString();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2961
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2962
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2963
        if(this.scale<0) { // No decimal point
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2964
            if(signum()==0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2965
                return "0";
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2966
            }
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  2967
            int trailingZeros = checkScaleNonZero((-(long)scale));
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2968
            StringBuilder buf;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2969
            if(intCompact!=INFLATED) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  2970
                buf = new StringBuilder(20+trailingZeros);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2971
                buf.append(intCompact);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2972
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2973
                String str = intVal.toString();
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  2974
                buf = new StringBuilder(str.length()+trailingZeros);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2975
                buf.append(str);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2976
            }
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  2977
            for (int i = 0; i < trailingZeros; i++) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2978
                buf.append('0');
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  2979
            }
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2980
            return buf.toString();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2981
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2982
        String str ;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2983
        if(intCompact!=INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2984
            str = Long.toString(Math.abs(intCompact));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2985
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2986
            str = intVal.abs().toString();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2987
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  2988
        return getValueString(signum(), str, scale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
    /* Returns a digit.digit string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
    private String getValueString(int signum, String intString, int scale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
        /* Insert decimal point */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
        StringBuilder buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
        int insertionPoint = intString.length() - scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
        if (insertionPoint == 0) {  /* Point goes right before intVal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
            return (signum<0 ? "-0." : "0.") + intString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
        } else if (insertionPoint > 0) { /* Point goes inside intVal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
            buf = new StringBuilder(intString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
            buf.insert(insertionPoint, '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
            if (signum < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
                buf.insert(0, '-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
        } else { /* We must insert zeros between point and intVal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
            buf = new StringBuilder(3-insertionPoint + intString.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
            buf.append(signum<0 ? "-0." : "0.");
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3006
            for (int i=0; i<-insertionPoint; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
                buf.append('0');
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3008
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
            buf.append(intString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
     * Converts this {@code BigDecimal} to a {@code BigInteger}.
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3016
     * This conversion is analogous to the
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3017
     * <i>narrowing primitive conversion</i> from {@code double} to
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3018
     * {@code long} as defined in section 5.1.3 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3019
     * <cite>The Java&trade; Language Specification</cite>:
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3020
     * any fractional part of this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
     * {@code BigDecimal} will be discarded.  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
     * conversion can lose information about the precision of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
     * {@code BigDecimal} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
     * To have an exception thrown if the conversion is inexact (in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
     * other words if a nonzero fractional part is discarded), use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
     * {@link #toBigIntegerExact()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
     * @return this {@code BigDecimal} converted to a {@code BigInteger}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
    public BigInteger toBigInteger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
        // force to an integer, quietly
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3033
        return this.setScale(0, ROUND_DOWN).inflated();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
     * Converts this {@code BigDecimal} to a {@code BigInteger},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
     * checking for lost information.  An exception is thrown if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
     * {@code BigDecimal} has a nonzero fractional part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
     * @return this {@code BigDecimal} converted to a {@code BigInteger}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
     * @throws ArithmeticException if {@code this} has a nonzero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
     *         fractional part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
    public BigInteger toBigIntegerExact() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
        // round to an integer, with Exception if decimal part non-0
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3048
        return this.setScale(0, ROUND_UNNECESSARY).inflated();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
    /**
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3052
     * Converts this {@code BigDecimal} to a {@code long}.
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3053
     * This conversion is analogous to the
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3054
     * <i>narrowing primitive conversion</i> from {@code double} to
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3055
     * {@code short} as defined in section 5.1.3 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3056
     * <cite>The Java&trade; Language Specification</cite>:
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3057
     * any fractional part of this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
     * {@code BigDecimal} will be discarded, and if the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
     * "{@code BigInteger}" is too big to fit in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
     * {@code long}, only the low-order 64 bits are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
     * Note that this conversion can lose information about the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
     * overall magnitude and precision of this {@code BigDecimal} value as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
     * as return a result with the opposite sign.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
     * @return this {@code BigDecimal} converted to a {@code long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
     */
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3067
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
    public long longValue(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
        return (intCompact != INFLATED && scale == 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
            intCompact:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
            toBigInteger().longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
     * Converts this {@code BigDecimal} to a {@code long}, checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
     * for lost information.  If this {@code BigDecimal} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
     * nonzero fractional part or is out of the possible range for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
     * {@code long} result then an {@code ArithmeticException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
     * @return this {@code BigDecimal} converted to a {@code long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
     * @throws ArithmeticException if {@code this} has a nonzero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
     *         fractional part, or will not fit in a {@code long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
    public long longValueExact() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
        if (intCompact != INFLATED && scale == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
            return intCompact;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
        // If more than 19 digits in integer part it cannot possibly fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
        if ((precision() - scale) > 19) // [OK for negative scale too]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
            throw new java.lang.ArithmeticException("Overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
        // Fastpath zero and < 1.0 numbers (the latter can be very slow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
        // to round if very small)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
        if (this.signum() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
        if ((this.precision() - this.scale) <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
            throw new ArithmeticException("Rounding necessary");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
        // round to an integer, with Exception if decimal part non-0
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3099
        BigDecimal num = this.setScale(0, ROUND_UNNECESSARY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
        if (num.precision() >= 19) // need to check carefully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
            LongOverflow.check(num);
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3102
        return num.inflated().longValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
    private static class LongOverflow {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
        /** BigInteger equal to Long.MIN_VALUE. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
        private static final BigInteger LONGMIN = BigInteger.valueOf(Long.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
        /** BigInteger equal to Long.MAX_VALUE. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
        private static final BigInteger LONGMAX = BigInteger.valueOf(Long.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
        public static void check(BigDecimal num) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3113
            BigInteger intVal = num.inflated();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3114
            if (intVal.compareTo(LONGMIN) < 0 ||
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3115
                intVal.compareTo(LONGMAX) > 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
                throw new java.lang.ArithmeticException("Overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
    /**
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3121
     * Converts this {@code BigDecimal} to an {@code int}.
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3122
     * This conversion is analogous to the
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3123
     * <i>narrowing primitive conversion</i> from {@code double} to
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3124
     * {@code short} as defined in section 5.1.3 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3125
     * <cite>The Java&trade; Language Specification</cite>:
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3126
     * any fractional part of this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
     * {@code BigDecimal} will be discarded, and if the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
     * "{@code BigInteger}" is too big to fit in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
     * {@code int}, only the low-order 32 bits are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
     * Note that this conversion can lose information about the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
     * overall magnitude and precision of this {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
     * value as well as return a result with the opposite sign.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
     * @return this {@code BigDecimal} converted to an {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
     */
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3136
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
    public int intValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
        return  (intCompact != INFLATED && scale == 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
            (int)intCompact :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
            toBigInteger().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
     * Converts this {@code BigDecimal} to an {@code int}, checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
     * for lost information.  If this {@code BigDecimal} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
     * nonzero fractional part or is out of the possible range for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
     * {@code int} result then an {@code ArithmeticException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
     * @return this {@code BigDecimal} converted to an {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
     * @throws ArithmeticException if {@code this} has a nonzero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
     *         fractional part, or will not fit in an {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
    public int intValueExact() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
       long num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
       num = this.longValueExact();     // will check decimal part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
       if ((int)num != num)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
           throw new java.lang.ArithmeticException("Overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
       return (int)num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
     * Converts this {@code BigDecimal} to a {@code short}, checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
     * for lost information.  If this {@code BigDecimal} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
     * nonzero fractional part or is out of the possible range for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
     * {@code short} result then an {@code ArithmeticException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
     * @return this {@code BigDecimal} converted to a {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
     * @throws ArithmeticException if {@code this} has a nonzero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
     *         fractional part, or will not fit in a {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
    public short shortValueExact() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
       long num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
       num = this.longValueExact();     // will check decimal part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
       if ((short)num != num)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
           throw new java.lang.ArithmeticException("Overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
       return (short)num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
     * Converts this {@code BigDecimal} to a {@code byte}, checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
     * for lost information.  If this {@code BigDecimal} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
     * nonzero fractional part or is out of the possible range for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
     * {@code byte} result then an {@code ArithmeticException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
     * @return this {@code BigDecimal} converted to a {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
     * @throws ArithmeticException if {@code this} has a nonzero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
     *         fractional part, or will not fit in a {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
    public byte byteValueExact() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
       long num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
       num = this.longValueExact();     // will check decimal part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
       if ((byte)num != num)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
           throw new java.lang.ArithmeticException("Overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
       return (byte)num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
     * Converts this {@code BigDecimal} to a {@code float}.
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3205
     * This conversion is similar to the
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3206
     * <i>narrowing primitive conversion</i> from {@code double} to
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3207
     * {@code float} as defined in section 5.1.3 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3208
     * <cite>The Java&trade; Language Specification</cite>:
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3209
     * if this {@code BigDecimal} has too great a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
     * magnitude to represent as a {@code float}, it will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
     * converted to {@link Float#NEGATIVE_INFINITY} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
     * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
     * the return value is finite, this conversion can lose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
     * information about the precision of the {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
     * @return this {@code BigDecimal} converted to a {@code float}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
     */
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3219
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
    public float floatValue(){
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3221
        if(intCompact != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3222
            if (scale == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
                return (float)intCompact;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3224
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3225
                /*
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3226
                 * If both intCompact and the scale can be exactly
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3227
                 * represented as float values, perform a single float
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3228
                 * multiply or divide to compute the (properly
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3229
                 * rounded) result.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3230
                 */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3231
                if (Math.abs(intCompact) < 1L<<22 ) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3232
                    // Don't have too guard against
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3233
                    // Math.abs(MIN_VALUE) because of outer check
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3234
                    // against INFLATED.
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3235
                    if (scale > 0 && scale < FLOAT_10_POW.length) {
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3236
                        return (float)intCompact / FLOAT_10_POW[scale];
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3237
                    } else if (scale < 0 && scale > -FLOAT_10_POW.length) {
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3238
                        return (float)intCompact * FLOAT_10_POW[-scale];
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3239
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3240
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3241
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3242
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
        // Somewhat inefficient, but guaranteed to work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
        return Float.parseFloat(this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
     * Converts this {@code BigDecimal} to a {@code double}.
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3249
     * This conversion is similar to the
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3250
     * <i>narrowing primitive conversion</i> from {@code double} to
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3251
     * {@code float} as defined in section 5.1.3 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3252
     * <cite>The Java&trade; Language Specification</cite>:
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8162
diff changeset
  3253
     * if this {@code BigDecimal} has too great a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
     * magnitude represent as a {@code double}, it will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
     * converted to {@link Double#NEGATIVE_INFINITY} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
     * Double#POSITIVE_INFINITY} as appropriate.  Note that even when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
     * the return value is finite, this conversion can lose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
     * information about the precision of the {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
     * @return this {@code BigDecimal} converted to a {@code double}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
     */
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3263
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
    public double doubleValue(){
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3265
        if(intCompact != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3266
            if (scale == 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3267
                return (double)intCompact;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3268
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3269
                /*
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3270
                 * If both intCompact and the scale can be exactly
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3271
                 * represented as double values, perform a single
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3272
                 * double multiply or divide to compute the (properly
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3273
                 * rounded) result.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3274
                 */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3275
                if (Math.abs(intCompact) < 1L<<52 ) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3276
                    // Don't have too guard against
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3277
                    // Math.abs(MIN_VALUE) because of outer check
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3278
                    // against INFLATED.
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3279
                    if (scale > 0 && scale < DOUBLE_10_POW.length) {
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3280
                        return (double)intCompact / DOUBLE_10_POW[scale];
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3281
                    } else if (scale < 0 && scale > -DOUBLE_10_POW.length) {
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3282
                        return (double)intCompact * DOUBLE_10_POW[-scale];
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3283
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3284
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3285
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3286
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
        // Somewhat inefficient, but guaranteed to work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
        return Double.parseDouble(this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
    /**
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3292
     * Powers of 10 which can be represented exactly in {@code
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3293
     * double}.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3294
     */
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3295
    private static final double DOUBLE_10_POW[] = {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3296
        1.0e0,  1.0e1,  1.0e2,  1.0e3,  1.0e4,  1.0e5,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3297
        1.0e6,  1.0e7,  1.0e8,  1.0e9,  1.0e10, 1.0e11,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3298
        1.0e12, 1.0e13, 1.0e14, 1.0e15, 1.0e16, 1.0e17,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3299
        1.0e18, 1.0e19, 1.0e20, 1.0e21, 1.0e22
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3300
    };
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3301
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3302
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3303
     * Powers of 10 which can be represented exactly in {@code
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3304
     * float}.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3305
     */
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3306
    private static final float FLOAT_10_POW[] = {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3307
        1.0e0f, 1.0e1f, 1.0e2f, 1.0e3f, 1.0e4f, 1.0e5f,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3308
        1.0e6f, 1.0e7f, 1.0e8f, 1.0e9f, 1.0e10f
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3309
    };
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3310
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3311
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
     * Returns the size of an ulp, a unit in the last place, of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
     * {@code BigDecimal}.  An ulp of a nonzero {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
     * value is the positive distance between this value and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
     * {@code BigDecimal} value next larger in magnitude with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
     * same number of digits.  An ulp of a zero value is numerically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
     * equal to 1 with the scale of {@code this}.  The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
     * stored with the same scale as {@code this} so the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
     * for zero and nonzero values is equal to {@code [1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
     * this.scale()]}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
     * @return the size of an ulp of {@code this}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
    public BigDecimal ulp() {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3326
        return BigDecimal.valueOf(1, this.scale(), 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3329
    // Private class to build a string representation for BigDecimal object.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3330
    // "StringBuilderHelper" is constructed as a thread local variable so it is
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3331
    // thread safe. The StringBuilder field acts as a buffer to hold the temporary
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3332
    // representation of BigDecimal. The cmpCharArray holds all the characters for
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3333
    // the compact representation of BigDecimal (except for '-' sign' if it is
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3334
    // negative) if its intCompact field is not INFLATED. It is shared by all
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3335
    // calls to toString() and its variants in that particular thread.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3336
    static class StringBuilderHelper {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3337
        final StringBuilder sb;    // Placeholder for BigDecimal string
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3338
        final char[] cmpCharArray; // character array to place the intCompact
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3339
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3340
        StringBuilderHelper() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3341
            sb = new StringBuilder();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3342
            // All non negative longs can be made to fit into 19 character array.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3343
            cmpCharArray = new char[19];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3344
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3345
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3346
        // Accessors.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3347
        StringBuilder getStringBuilder() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3348
            sb.setLength(0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3349
            return sb;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3350
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3351
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3352
        char[] getCompactCharArray() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3353
            return cmpCharArray;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3354
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3355
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3356
        /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3357
         * Places characters representing the intCompact in {@code long} into
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3358
         * cmpCharArray and returns the offset to the array where the
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3359
         * representation starts.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3360
         *
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3361
         * @param intCompact the number to put into the cmpCharArray.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3362
         * @return offset to the array where the representation starts.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3363
         * Note: intCompact must be greater or equal to zero.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3364
         */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3365
        int putIntCompact(long intCompact) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3366
            assert intCompact >= 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3367
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3368
            long q;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3369
            int r;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3370
            // since we start from the least significant digit, charPos points to
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3371
            // the last character in cmpCharArray.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3372
            int charPos = cmpCharArray.length;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3373
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3374
            // Get 2 digits/iteration using longs until quotient fits into an int
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3375
            while (intCompact > Integer.MAX_VALUE) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3376
                q = intCompact / 100;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3377
                r = (int)(intCompact - q * 100);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3378
                intCompact = q;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3379
                cmpCharArray[--charPos] = DIGIT_ONES[r];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3380
                cmpCharArray[--charPos] = DIGIT_TENS[r];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3381
            }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3382
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3383
            // Get 2 digits/iteration using ints when i2 >= 100
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3384
            int q2;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3385
            int i2 = (int)intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3386
            while (i2 >= 100) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3387
                q2 = i2 / 100;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3388
                r  = i2 - q2 * 100;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3389
                i2 = q2;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3390
                cmpCharArray[--charPos] = DIGIT_ONES[r];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3391
                cmpCharArray[--charPos] = DIGIT_TENS[r];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3392
            }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3393
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3394
            cmpCharArray[--charPos] = DIGIT_ONES[i2];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3395
            if (i2 >= 10)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3396
                cmpCharArray[--charPos] = DIGIT_TENS[i2];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3397
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3398
            return charPos;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3399
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3400
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
  3401
        static final char[] DIGIT_TENS = {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3402
            '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3403
            '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3404
            '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3405
            '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3406
            '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3407
            '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3408
            '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3409
            '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3410
            '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3411
            '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3412
        };
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3413
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
  3414
        static final char[] DIGIT_ONES = {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3415
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3416
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3417
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3418
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3419
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3420
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3421
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3422
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3423
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3424
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3425
        };
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3426
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
     * Lay out this {@code BigDecimal} into a {@code char[]} array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
     * The Java 1.2 equivalent to this was called {@code getValueString}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
     * @param  sci {@code true} for Scientific exponential notation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
     *          {@code false} for Engineering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
     * @return string with canonical string representation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
     *         {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
    private String layoutChars(boolean sci) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
        if (scale == 0)                      // zero scale is trivial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
            return (intCompact != INFLATED) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
                Long.toString(intCompact):
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
                intVal.toString();
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3442
        if (scale == 2  &&
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3443
            intCompact >= 0 && intCompact < Integer.MAX_VALUE) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3444
            // currency fast path
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3445
            int lowInt = (int)intCompact % 100;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3446
            int highInt = (int)intCompact / 100;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3447
            return (Integer.toString(highInt) + '.' +
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3448
                    StringBuilderHelper.DIGIT_TENS[lowInt] +
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3449
                    StringBuilderHelper.DIGIT_ONES[lowInt]) ;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3450
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3452
        StringBuilderHelper sbHelper = threadLocalStringBuilderHelper.get();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3453
        char[] coeff;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3454
        int offset;  // offset is the starting index for coeff array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
        // Get the significand as an absolute value
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3456
        if (intCompact != INFLATED) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3457
            offset = sbHelper.putIntCompact(Math.abs(intCompact));
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3458
            coeff  = sbHelper.getCompactCharArray();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3459
        } else {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3460
            offset = 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3461
            coeff  = intVal.abs().toString().toCharArray();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3462
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
        // Construct a buffer, with sufficient capacity for all cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
        // If E-notation is needed, length will be: +1 if negative, +1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
        // if '.' needed, +2 for "E+", + up to 10 for adjusted exponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
        // Otherwise it could have +1 if negative, plus leading "0.00000"
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3468
        StringBuilder buf = sbHelper.getStringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
        if (signum() < 0)             // prefix '-' if negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
            buf.append('-');
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3471
        int coeffLen = coeff.length - offset;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3472
        long adjusted = -(long)scale + (coeffLen -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
        if ((scale >= 0) && (adjusted >= -6)) { // plain number
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3474
            int pad = scale - coeffLen;         // count of padding zeros
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3475
            if (pad >= 0) {                     // 0.xxx form
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
                buf.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
                buf.append('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
                for (; pad>0; pad--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
                    buf.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
                }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3481
                buf.append(coeff, offset, coeffLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
            } else {                         // xx.xx form
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3483
                buf.append(coeff, offset, -pad);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
                buf.append('.');
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3485
                buf.append(coeff, -pad + offset, scale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
        } else { // E-notation is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
            if (sci) {                       // Scientific notation
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3489
                buf.append(coeff[offset]);   // first character
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3490
                if (coeffLen > 1) {          // more to come
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
                    buf.append('.');
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3492
                    buf.append(coeff, offset + 1, coeffLen - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
            } else {                         // Engineering notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
                int sig = (int)(adjusted % 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
                if (sig < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
                    sig += 3;                // [adjusted was negative]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
                adjusted -= sig;             // now a multiple of 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
                sig++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
                if (signum() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
                    switch (sig) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
                    case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
                        buf.append('0'); // exponent is a multiple of three
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
                    case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
                        buf.append("0.00");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
                        adjusted += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
                    case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
                        buf.append("0.0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
                        adjusted += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
                        throw new AssertionError("Unexpected sig value " + sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
                    }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3516
                } else if (sig >= coeffLen) {   // significand all in integer
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3517
                    buf.append(coeff, offset, coeffLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
                    // may need some zeros, too
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3519
                    for (int i = sig - coeffLen; i > 0; i--) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
                        buf.append('0');
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3521
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
                } else {                     // xx.xxE form
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3523
                    buf.append(coeff, offset, sig);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
                    buf.append('.');
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3525
                    buf.append(coeff, offset + sig, coeffLen - sig);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
            if (adjusted != 0) {             // [!sci could have made 0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
                buf.append('E');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
                if (adjusted > 0)            // force sign for positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
                    buf.append('+');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
                buf.append(adjusted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
     * Return 10 to the power n, as a {@code BigInteger}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
     * @param  n the power of ten to be returned (>=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
     * @return a {@code BigInteger} with the value (10<sup>n</sup>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3544
    private static BigInteger bigTenToThe(int n) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3545
        if (n < 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3546
            return BigInteger.ZERO;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3547
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3548
        if (n < BIG_TEN_POWERS_TABLE_MAX) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3549
            BigInteger[] pows = BIG_TEN_POWERS_TABLE;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3550
            if (n < pows.length)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3551
                return pows[n];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3552
            else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3553
                return expandBigIntegerTenPowers(n);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3554
        }
15530
d918415aea3f 6471906: java.lang.NegativeArraySizeException in tenToThe
dmeetry
parents: 10431
diff changeset
  3555
18286
b38489d5aadf 4837946: Faster multiplication and exponentiation of large integers
bpb
parents: 15530
diff changeset
  3556
        return BigInteger.TEN.pow(n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
    }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3558
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3559
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3560
     * Expand the BIG_TEN_POWERS_TABLE array to contain at least 10**n.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3561
     *
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3562
     * @param n the power of ten to be returned (>=0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3563
     * @return a {@code BigDecimal} with the value (10<sup>n</sup>) and
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3564
     *         in the meantime, the BIG_TEN_POWERS_TABLE array gets
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3565
     *         expanded to the size greater than n.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3566
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3567
    private static BigInteger expandBigIntegerTenPowers(int n) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3568
        synchronized(BigDecimal.class) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3569
            BigInteger[] pows = BIG_TEN_POWERS_TABLE;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3570
            int curLen = pows.length;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3571
            // The following comparison and the above synchronized statement is
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3572
            // to prevent multiple threads from expanding the same array.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3573
            if (curLen <= n) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3574
                int newLen = curLen << 1;
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3575
                while (newLen <= n) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3576
                    newLen <<= 1;
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3577
                }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3578
                pows = Arrays.copyOf(pows, newLen);
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3579
                for (int i = curLen; i < newLen; i++) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3580
                    pows[i] = pows[i - 1].multiply(BigInteger.TEN);
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3581
                }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3582
                // Based on the following facts:
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3583
                // 1. pows is a private local varible;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3584
                // 2. the following store is a volatile store.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3585
                // the newly created array elements can be safely published.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3586
                BIG_TEN_POWERS_TABLE = pows;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3587
            }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3588
            return pows[n];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3589
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3590
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3591
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3592
    private static final long[] LONG_TEN_POWERS_TABLE = {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3593
        1,                     // 0 / 10^0
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3594
        10,                    // 1 / 10^1
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3595
        100,                   // 2 / 10^2
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3596
        1000,                  // 3 / 10^3
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3597
        10000,                 // 4 / 10^4
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3598
        100000,                // 5 / 10^5
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3599
        1000000,               // 6 / 10^6
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3600
        10000000,              // 7 / 10^7
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3601
        100000000,             // 8 / 10^8
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3602
        1000000000,            // 9 / 10^9
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3603
        10000000000L,          // 10 / 10^10
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3604
        100000000000L,         // 11 / 10^11
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3605
        1000000000000L,        // 12 / 10^12
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3606
        10000000000000L,       // 13 / 10^13
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3607
        100000000000000L,      // 14 / 10^14
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3608
        1000000000000000L,     // 15 / 10^15
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3609
        10000000000000000L,    // 16 / 10^16
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3610
        100000000000000000L,   // 17 / 10^17
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3611
        1000000000000000000L   // 18 / 10^18
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3612
    };
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3613
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3614
    private static volatile BigInteger BIG_TEN_POWERS_TABLE[] = {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3615
        BigInteger.ONE,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3616
        BigInteger.valueOf(10),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3617
        BigInteger.valueOf(100),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3618
        BigInteger.valueOf(1000),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3619
        BigInteger.valueOf(10000),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3620
        BigInteger.valueOf(100000),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3621
        BigInteger.valueOf(1000000),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3622
        BigInteger.valueOf(10000000),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3623
        BigInteger.valueOf(100000000),
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3624
        BigInteger.valueOf(1000000000),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3625
        BigInteger.valueOf(10000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3626
        BigInteger.valueOf(100000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3627
        BigInteger.valueOf(1000000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3628
        BigInteger.valueOf(10000000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3629
        BigInteger.valueOf(100000000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3630
        BigInteger.valueOf(1000000000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3631
        BigInteger.valueOf(10000000000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3632
        BigInteger.valueOf(100000000000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3633
        BigInteger.valueOf(1000000000000000000L)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3634
    };
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3635
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3636
    private static final int BIG_TEN_POWERS_TABLE_INITLEN =
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3637
        BIG_TEN_POWERS_TABLE.length;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3638
    private static final int BIG_TEN_POWERS_TABLE_MAX =
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3639
        16 * BIG_TEN_POWERS_TABLE_INITLEN;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3640
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3641
    private static final long THRESHOLDS_TABLE[] = {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3642
        Long.MAX_VALUE,                     // 0
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3643
        Long.MAX_VALUE/10L,                 // 1
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3644
        Long.MAX_VALUE/100L,                // 2
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3645
        Long.MAX_VALUE/1000L,               // 3
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3646
        Long.MAX_VALUE/10000L,              // 4
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3647
        Long.MAX_VALUE/100000L,             // 5
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3648
        Long.MAX_VALUE/1000000L,            // 6
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3649
        Long.MAX_VALUE/10000000L,           // 7
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3650
        Long.MAX_VALUE/100000000L,          // 8
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3651
        Long.MAX_VALUE/1000000000L,         // 9
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3652
        Long.MAX_VALUE/10000000000L,        // 10
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3653
        Long.MAX_VALUE/100000000000L,       // 11
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3654
        Long.MAX_VALUE/1000000000000L,      // 12
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3655
        Long.MAX_VALUE/10000000000000L,     // 13
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3656
        Long.MAX_VALUE/100000000000000L,    // 14
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3657
        Long.MAX_VALUE/1000000000000000L,   // 15
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3658
        Long.MAX_VALUE/10000000000000000L,  // 16
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3659
        Long.MAX_VALUE/100000000000000000L, // 17
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3660
        Long.MAX_VALUE/1000000000000000000L // 18
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3661
    };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
     * Compute val * 10 ^ n; return this product if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
     * representable as a long, INFLATED otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3667
    private static long longMultiplyPowerTen(long val, int n) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3668
        if (val == 0 || n <= 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3669
            return val;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3670
        long[] tab = LONG_TEN_POWERS_TABLE;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3671
        long[] bounds = THRESHOLDS_TABLE;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3672
        if (n < tab.length && n < bounds.length) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3673
            long tenpower = tab[n];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3674
            if (val == 1)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3675
                return tenpower;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3676
            if (Math.abs(val) <= bounds[n])
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3677
                return val * tenpower;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
        return INFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3682
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3683
     * Compute this * 10 ^ n.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3684
     * Needed mainly to allow special casing to trap zero value
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3685
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3686
    private BigInteger bigMultiplyPowerTen(int n) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3687
        if (n <= 0)
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3688
            return this.inflated();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3690
        if (intCompact != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3691
            return bigTenToThe(n).multiply(intCompact);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3692
        else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3693
            return intVal.multiply(bigTenToThe(n));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
    /**
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3697
     * Returns appropriate BigInteger from intVal field if intVal is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
     * null, i.e. the compact representation is in use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
     */
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3700
    private BigInteger inflated() {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3701
        if (intVal == null) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3702
            return BigInteger.valueOf(intCompact);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3703
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3704
        return intVal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
     * Match the scales of two {@code BigDecimal}s to align their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
     * least significant digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
     * <p>If the scales of val[0] and val[1] differ, rescale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
     * (non-destructively) the lower-scaled {@code BigDecimal} so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
     * they match.  That is, the lower-scaled reference will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
     * replaced by a reference to a new object with the same scale as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
     * the other {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
     * @param  val array of two elements referring to the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
     *         {@code BigDecimal}s to be aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
    private static void matchScale(BigDecimal[] val) {
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  3721
        if (val[0].scale < val[1].scale) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3722
            val[0] = val[0].setScale(val[1].scale, ROUND_UNNECESSARY);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3723
        } else if (val[1].scale < val[0].scale) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3724
            val[1] = val[1].setScale(val[0].scale, ROUND_UNNECESSARY);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3725
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
10431
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3728
    private static class UnsafeHolder {
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 32649
diff changeset
  3729
        private static final jdk.internal.misc.Unsafe unsafe;
10431
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3730
        private static final long intCompactOffset;
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3731
        private static final long intValOffset;
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3732
        static {
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3733
            try {
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 32649
diff changeset
  3734
                unsafe = jdk.internal.misc.Unsafe.getUnsafe();
10431
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3735
                intCompactOffset = unsafe.objectFieldOffset
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3736
                    (BigDecimal.class.getDeclaredField("intCompact"));
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3737
                intValOffset = unsafe.objectFieldOffset
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3738
                    (BigDecimal.class.getDeclaredField("intVal"));
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3739
            } catch (Exception ex) {
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3740
                throw new ExceptionInInitializerError(ex);
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3741
            }
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3742
        }
29220
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28869
diff changeset
  3743
        static void setIntCompact(BigDecimal bd, long val) {
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28869
diff changeset
  3744
            unsafe.putLong(bd, intCompactOffset, val);
10431
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3745
        }
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3746
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3747
        static void setIntValVolatile(BigDecimal bd, BigInteger val) {
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3748
            unsafe.putObjectVolatile(bd, intValOffset, val);
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3749
        }
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3750
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3751
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
     * Reconstitute the {@code BigDecimal} instance from a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
     * @param s the stream being read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
        // Read in all fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
        // validate possibly bad fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
        if (intVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
            String message = "BigDecimal: null intVal in stream";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
            throw new java.io.StreamCorruptedException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
        // [all values of scale are now allowed]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
        }
29220
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28869
diff changeset
  3768
        UnsafeHolder.setIntCompact(this, compactValFor(intVal));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
    * Serialize this {@code BigDecimal} to the stream in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
    * @param s the stream to serialize to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
   private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
       throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
       // Must inflate to maintain compatible serial form.
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3779
       if (this.intVal == null)
10431
448fc54a8e23 6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents: 10430
diff changeset
  3780
           UnsafeHolder.setIntValVolatile(this, BigInteger.valueOf(this.intCompact));
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3781
       // Could reset intVal back to null if it has to be set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
       s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
    /**
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3786
     * Returns the length of the absolute value of a {@code long}, in decimal
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3787
     * digits.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
     *
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3789
     * @param x the {@code long}
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3790
     * @return the length of the unscaled value, in deciaml digits.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3791
     */
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3792
    static int longDigitLength(long x) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3793
        /*
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3794
         * As described in "Bit Twiddling Hacks" by Sean Anderson,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3795
         * (http://graphics.stanford.edu/~seander/bithacks.html)
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3796
         * integer log 10 of x is within 1 of (1233/4096)* (1 +
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3797
         * integer log 2 of x). The fraction 1233/4096 approximates
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3798
         * log10(2). So we first do a version of log2 (a variant of
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3799
         * Long class with pre-checks and opposite directionality) and
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3800
         * then scale and check against powers table. This is a little
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3801
         * simpler in present context than the version in Hacker's
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3802
         * Delight sec 11-4. Adding one to bit length allows comparing
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3803
         * downward from the LONG_TEN_POWERS_TABLE that we need
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3804
         * anyway.
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3805
         */
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3806
        assert x != BigDecimal.INFLATED;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3807
        if (x < 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3808
            x = -x;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3809
        if (x < 10) // must screen for 0, might as well 10
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3810
            return 1;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3811
        int r = ((64 - Long.numberOfLeadingZeros(x) + 1) * 1233) >>> 12;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3812
        long[] tab = LONG_TEN_POWERS_TABLE;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3813
        // if r >= length, must have max possible digits for long
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3814
        return (r >= tab.length || x < tab[r]) ? r : r + 1;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3815
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3816
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3817
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3818
     * Returns the length of the absolute value of a BigInteger, in
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3819
     * decimal digits.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
     *
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3821
     * @param b the BigInteger
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
     * @return the length of the unscaled value, in decimal digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3824
    private static int bigDigitLength(BigInteger b) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3825
        /*
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3826
         * Same idea as the long version, but we need a better
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3827
         * approximation of log10(2). Using 646456993/2^31
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3828
         * is accurate up to max possible reported bitLength.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3829
         */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3830
        if (b.signum == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
            return 1;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3832
        int r = (int)((((long)b.bitLength() + 1) * 646456993) >>> 31);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3833
        return b.compareMagnitude(bigTenToThe(r)) < 0? r : r+1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
     * Check a scale for Underflow or Overflow.  If this BigDecimal is
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3838
     * nonzero, throw an exception if the scale is outof range. If this
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3839
     * is zero, saturate the scale to the extreme value of the right
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3840
     * sign if the scale is out of range.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
     * @param val The new scale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
     * @throws ArithmeticException (overflow or underflow) if the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
     *         scale is out of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
     * @return validated scale as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
    private int checkScale(long val) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3848
        int asInt = (int)val;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3849
        if (asInt != val) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3850
            asInt = val>Integer.MAX_VALUE ? Integer.MAX_VALUE : Integer.MIN_VALUE;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3851
            BigInteger b;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3852
            if (intCompact != 0 &&
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3853
                ((b = intVal) == null || b.signum() != 0))
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3854
                throw new ArithmeticException(asInt>0 ? "Underflow":"Overflow");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3856
        return asInt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3859
   /**
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3860
     * Returns the compact value for given {@code BigInteger}, or
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3861
     * INFLATED if too big. Relies on internal representation of
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3862
     * {@code BigInteger}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3864
    private static long compactValFor(BigInteger b) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3865
        int[] m = b.mag;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3866
        int len = m.length;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3867
        if (len == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3868
            return 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3869
        int d = m[0];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3870
        if (len > 2 || (len == 2 && d < 0))
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3871
            return INFLATED;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3873
        long u = (len == 2)?
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3874
            (((long) m[1] & LONG_MASK) + (((long)d) << 32)) :
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3875
            (((long)d)   & LONG_MASK);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3876
        return (b.signum < 0)? -u : u;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3879
    private static int longCompareMagnitude(long x, long y) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3880
        if (x < 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3881
            x = -x;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3882
        if (y < 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3883
            y = -y;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3884
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3885
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3886
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3887
    private static int saturateLong(long s) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3888
        int i = (int)s;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3889
        return (s == i) ? i : (s < 0 ? Integer.MIN_VALUE : Integer.MAX_VALUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
     * Internal printing routine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
    private static void print(String name, BigDecimal bd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
        System.err.format("%s:\tintCompact %d\tintVal %d\tscale %d\tprecision %d%n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
                          name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
                          bd.intCompact,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
                          bd.intVal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
                          bd.scale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
                          bd.precision);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
     * Check internal invariants of this BigDecimal.  These invariants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
     * include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
     * <li>The object must be initialized; either intCompact must not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
     * INFLATED or intVal is non-null.  Both of these conditions may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
     * be true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
     * <li>If both intCompact and intVal and set, their values must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
     * consistent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
     * <li>If precision is nonzero, it must have the right value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
     * </ul>
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3919
     *
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3920
     * Note: Since this is an audit method, we are not supposed to change the
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3921
     * state of this BigDecimal object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3922
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
    private BigDecimal audit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
        if (intCompact == INFLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
            if (intVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
                print("audit", this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
                throw new AssertionError("null intVal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
            }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3929
            // Check precision
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3930
            if (precision > 0 && precision != bigDigitLength(intVal)) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3931
                print("audit", this);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3932
                throw new AssertionError("precision mismatch");
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3933
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
            if (intVal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
                long val = intVal.longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
                if (val != intCompact) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
                    print("audit", this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
                    throw new AssertionError("Inconsistent state, intCompact=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
                                             intCompact + "\t intVal=" + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
            }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3943
            // Check precision
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3944
            if (precision > 0 && precision != longDigitLength(intCompact)) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3945
                print("audit", this);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3946
                throw new AssertionError("precision mismatch");
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3947
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
    }
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3951
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3952
    /* the same as checkScale where value!=0 */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3953
    private static int checkScaleNonZero(long val) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3954
        int asInt = (int)val;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3955
        if (asInt != val) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3956
            throw new ArithmeticException(asInt>0 ? "Underflow":"Overflow");
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3957
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3958
        return asInt;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3959
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3960
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3961
    private static int checkScale(long intCompact, long val) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3962
        int asInt = (int)val;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3963
        if (asInt != val) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3964
            asInt = val>Integer.MAX_VALUE ? Integer.MAX_VALUE : Integer.MIN_VALUE;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3965
            if (intCompact != 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3966
                throw new ArithmeticException(asInt>0 ? "Underflow":"Overflow");
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3967
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3968
        return asInt;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3969
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3970
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3971
    private static int checkScale(BigInteger intVal, long val) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3972
        int asInt = (int)val;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3973
        if (asInt != val) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3974
            asInt = val>Integer.MAX_VALUE ? Integer.MAX_VALUE : Integer.MIN_VALUE;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3975
            if (intVal.signum() != 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3976
                throw new ArithmeticException(asInt>0 ? "Underflow":"Overflow");
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3977
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3978
        return asInt;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3979
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3980
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3981
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3982
     * Returns a {@code BigDecimal} rounded according to the MathContext
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3983
     * settings;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3984
     * If rounding is needed a new {@code BigDecimal} is created and returned.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3985
     *
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3986
     * @param val the value to be rounded
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3987
     * @param mc the context to use.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3988
     * @return a {@code BigDecimal} rounded according to the MathContext
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3989
     *         settings.  May return {@code value}, if no rounding needed.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3990
     * @throws ArithmeticException if the rounding mode is
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3991
     *         {@code RoundingMode.UNNECESSARY} and the
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3992
     *         result is inexact.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3993
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3994
    private static BigDecimal doRound(BigDecimal val, MathContext mc) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3995
        int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3996
        boolean wasDivided = false;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3997
        if (mcp > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3998
            BigInteger intVal = val.intVal;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  3999
            long compactVal = val.intCompact;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4000
            int scale = val.scale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4001
            int prec = val.precision();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4002
            int mode = mc.roundingMode.oldMode;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4003
            int drop;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4004
            if (compactVal == INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4005
                drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4006
                while (drop > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4007
                    scale = checkScaleNonZero((long) scale - drop);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4008
                    intVal = divideAndRoundByTenPow(intVal, drop, mode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4009
                    wasDivided = true;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4010
                    compactVal = compactValFor(intVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4011
                    if (compactVal != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4012
                        prec = longDigitLength(compactVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4013
                        break;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4014
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4015
                    prec = bigDigitLength(intVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4016
                    drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4017
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4018
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4019
            if (compactVal != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4020
                drop = prec - mcp;  // drop can't be more than 18
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4021
                while (drop > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4022
                    scale = checkScaleNonZero((long) scale - drop);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4023
                    compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4024
                    wasDivided = true;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4025
                    prec = longDigitLength(compactVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4026
                    drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4027
                    intVal = null;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4028
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4029
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4030
            return wasDivided ? new BigDecimal(intVal,compactVal,scale,prec) : val;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4031
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4032
        return val;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4033
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4034
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4035
    /*
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4036
     * Returns a {@code BigDecimal} created from {@code long} value with
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4037
     * given scale rounded according to the MathContext settings
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4038
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4039
    private static BigDecimal doRound(long compactVal, int scale, MathContext mc) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4040
        int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4041
        if (mcp > 0 && mcp < 19) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4042
            int prec = longDigitLength(compactVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4043
            int drop = prec - mcp;  // drop can't be more than 18
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4044
            while (drop > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4045
                scale = checkScaleNonZero((long) scale - drop);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4046
                compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4047
                prec = longDigitLength(compactVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4048
                drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4049
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4050
            return valueOf(compactVal, scale, prec);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4051
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4052
        return valueOf(compactVal, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4053
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4054
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4055
    /*
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4056
     * Returns a {@code BigDecimal} created from {@code BigInteger} value with
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4057
     * given scale rounded according to the MathContext settings
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4058
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4059
    private static BigDecimal doRound(BigInteger intVal, int scale, MathContext mc) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4060
        int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4061
        int prec = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4062
        if (mcp > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4063
            long compactVal = compactValFor(intVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4064
            int mode = mc.roundingMode.oldMode;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4065
            int drop;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4066
            if (compactVal == INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4067
                prec = bigDigitLength(intVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4068
                drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4069
                while (drop > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4070
                    scale = checkScaleNonZero((long) scale - drop);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4071
                    intVal = divideAndRoundByTenPow(intVal, drop, mode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4072
                    compactVal = compactValFor(intVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4073
                    if (compactVal != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4074
                        break;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4075
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4076
                    prec = bigDigitLength(intVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4077
                    drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4078
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4079
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4080
            if (compactVal != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4081
                prec = longDigitLength(compactVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4082
                drop = prec - mcp;     // drop can't be more than 18
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4083
                while (drop > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4084
                    scale = checkScaleNonZero((long) scale - drop);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4085
                    compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4086
                    prec = longDigitLength(compactVal);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4087
                    drop = prec - mcp;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4088
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4089
                return valueOf(compactVal,scale,prec);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4090
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4091
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4092
        return new BigDecimal(intVal,INFLATED,scale,prec);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4093
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4094
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4095
    /*
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4096
     * Divides {@code BigInteger} value by ten power.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4097
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4098
    private static BigInteger divideAndRoundByTenPow(BigInteger intVal, int tenPow, int roundingMode) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4099
        if (tenPow < LONG_TEN_POWERS_TABLE.length)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4100
            intVal = divideAndRound(intVal, LONG_TEN_POWERS_TABLE[tenPow], roundingMode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4101
        else
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4102
            intVal = divideAndRound(intVal, bigTenToThe(tenPow), roundingMode);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4103
        return intVal;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4104
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4105
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4106
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4107
     * Internally used for division operation for division {@code long} by
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4108
     * {@code long}.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4109
     * The returned {@code BigDecimal} object is the quotient whose scale is set
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4110
     * to the passed in scale. If the remainder is not zero, it will be rounded
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4111
     * based on the passed in roundingMode. Also, if the remainder is zero and
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4112
     * the last parameter, i.e. preferredScale is NOT equal to scale, the
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4113
     * trailing zeros of the result is stripped to match the preferredScale.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4114
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4115
    private static BigDecimal divideAndRound(long ldividend, long ldivisor, int scale, int roundingMode,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4116
                                             int preferredScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4117
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4118
        int qsign; // quotient sign
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4119
        long q = ldividend / ldivisor; // store quotient in long
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4120
        if (roundingMode == ROUND_DOWN && scale == preferredScale)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4121
            return valueOf(q, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4122
        long r = ldividend % ldivisor; // store remainder in long
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4123
        qsign = ((ldividend < 0) == (ldivisor < 0)) ? 1 : -1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4124
        if (r != 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4125
            boolean increment = needIncrement(ldivisor, roundingMode, qsign, q, r);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4126
            return valueOf((increment ? q + qsign : q), scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4127
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4128
            if (preferredScale != scale)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4129
                return createAndStripZerosToMatchScale(q, scale, preferredScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4130
            else
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4131
                return valueOf(q, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4132
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4133
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4134
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4135
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4136
     * Divides {@code long} by {@code long} and do rounding based on the
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4137
     * passed in roundingMode.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4138
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4139
    private static long divideAndRound(long ldividend, long ldivisor, int roundingMode) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4140
        int qsign; // quotient sign
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4141
        long q = ldividend / ldivisor; // store quotient in long
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4142
        if (roundingMode == ROUND_DOWN)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4143
            return q;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4144
        long r = ldividend % ldivisor; // store remainder in long
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4145
        qsign = ((ldividend < 0) == (ldivisor < 0)) ? 1 : -1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4146
        if (r != 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4147
            boolean increment = needIncrement(ldivisor, roundingMode, qsign, q,     r);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4148
            return increment ? q + qsign : q;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4149
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4150
            return q;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4151
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4152
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4153
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4154
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4155
     * Shared logic of need increment computation.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4156
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4157
    private static boolean commonNeedIncrement(int roundingMode, int qsign,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4158
                                        int cmpFracHalf, boolean oddQuot) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4159
        switch(roundingMode) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4160
        case ROUND_UNNECESSARY:
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4161
            throw new ArithmeticException("Rounding necessary");
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4162
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4163
        case ROUND_UP: // Away from zero
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4164
            return true;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4165
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4166
        case ROUND_DOWN: // Towards zero
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4167
            return false;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4168
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4169
        case ROUND_CEILING: // Towards +infinity
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4170
            return qsign > 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4171
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4172
        case ROUND_FLOOR: // Towards -infinity
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4173
            return qsign < 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4174
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4175
        default: // Some kind of half-way rounding
10430
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4176
            assert roundingMode >= ROUND_HALF_UP &&
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4177
                roundingMode <= ROUND_HALF_EVEN: "Unexpected rounding mode" + RoundingMode.valueOf(roundingMode);
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4178
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4179
            if (cmpFracHalf < 0 ) // We're closer to higher digit
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4180
                return false;
10430
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4181
            else if (cmpFracHalf > 0 ) // We're closer to lower digit
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4182
                return true;
10430
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4183
            else { // half-way
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4184
                assert cmpFracHalf == 0;
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4185
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4186
                switch(roundingMode) {
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4187
                case ROUND_HALF_DOWN:
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4188
                    return false;
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4189
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4190
                case ROUND_HALF_UP:
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4191
                    return true;
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4192
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4193
                case ROUND_HALF_EVEN:
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4194
                    return oddQuot;
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4195
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4196
                default:
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4197
                    throw new AssertionError("Unexpected rounding mode" + roundingMode);
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4198
                }
f338d4485f5c 7086710: java/util/Formatter/Basic.java failing after 7082971
darcy
parents: 10423
diff changeset
  4199
            }
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4200
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4201
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4202
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4203
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4204
     * Tests if quotient has to be incremented according the roundingMode
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4205
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4206
    private static boolean needIncrement(long ldivisor, int roundingMode,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4207
                                         int qsign, long q, long r) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4208
        assert r != 0L;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4209
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4210
        int cmpFracHalf;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4211
        if (r <= HALF_LONG_MIN_VALUE || r > HALF_LONG_MAX_VALUE) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4212
            cmpFracHalf = 1; // 2 * r can't fit into long
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4213
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4214
            cmpFracHalf = longCompareMagnitude(2 * r, ldivisor);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4215
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4216
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4217
        return commonNeedIncrement(roundingMode, qsign, cmpFracHalf, (q & 1L) != 0L);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4218
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4219
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4220
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4221
     * Divides {@code BigInteger} value by {@code long} value and
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4222
     * do rounding based on the passed in roundingMode.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4223
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4224
    private static BigInteger divideAndRound(BigInteger bdividend, long ldivisor, int roundingMode) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4225
        // Descend into mutables for faster remainder checks
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4226
        MutableBigInteger mdividend = new MutableBigInteger(bdividend.mag);
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4227
        // store quotient
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4228
        MutableBigInteger mq = new MutableBigInteger();
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4229
        // store quotient & remainder in long
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4230
        long r = mdividend.divide(ldivisor, mq);
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4231
        // record remainder is zero or not
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4232
        boolean isRemainderZero = (r == 0);
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4233
        // quotient sign
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4234
        int qsign = (ldivisor < 0) ? -bdividend.signum : bdividend.signum;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4235
        if (!isRemainderZero) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4236
            if(needIncrement(ldivisor, roundingMode, qsign, mq, r)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4237
                mq.add(MutableBigInteger.ONE);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4238
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4239
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4240
        return mq.toBigInteger(qsign);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4241
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4242
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4243
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4244
     * Internally used for division operation for division {@code BigInteger}
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4245
     * by {@code long}.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4246
     * The returned {@code BigDecimal} object is the quotient whose scale is set
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4247
     * to the passed in scale. If the remainder is not zero, it will be rounded
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4248
     * based on the passed in roundingMode. Also, if the remainder is zero and
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4249
     * the last parameter, i.e. preferredScale is NOT equal to scale, the
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4250
     * trailing zeros of the result is stripped to match the preferredScale.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4251
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4252
    private static BigDecimal divideAndRound(BigInteger bdividend,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4253
                                             long ldivisor, int scale, int roundingMode, int preferredScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4254
        // Descend into mutables for faster remainder checks
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4255
        MutableBigInteger mdividend = new MutableBigInteger(bdividend.mag);
23934
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4256
        // store quotient
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4257
        MutableBigInteger mq = new MutableBigInteger();
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4258
        // store quotient & remainder in long
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4259
        long r = mdividend.divide(ldivisor, mq);
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4260
        // record remainder is zero or not
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4261
        boolean isRemainderZero = (r == 0);
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4262
        // quotient sign
e99a4768951e 6375303: Review use of caching in BigDecimal
bpb
parents: 20883
diff changeset
  4263
        int qsign = (ldivisor < 0) ? -bdividend.signum : bdividend.signum;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4264
        if (!isRemainderZero) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4265
            if(needIncrement(ldivisor, roundingMode, qsign, mq, r)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4266
                mq.add(MutableBigInteger.ONE);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4267
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4268
            return mq.toBigDecimal(qsign, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4269
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4270
            if (preferredScale != scale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4271
                long compactVal = mq.toCompactValue(qsign);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4272
                if(compactVal!=INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4273
                    return createAndStripZerosToMatchScale(compactVal, scale, preferredScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4274
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4275
                BigInteger intVal =  mq.toBigInteger(qsign);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4276
                return createAndStripZerosToMatchScale(intVal,scale, preferredScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4277
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4278
                return mq.toBigDecimal(qsign, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4279
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4280
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4281
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4282
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4283
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4284
     * Tests if quotient has to be incremented according the roundingMode
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4285
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4286
    private static boolean needIncrement(long ldivisor, int roundingMode,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4287
                                         int qsign, MutableBigInteger mq, long r) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4288
        assert r != 0L;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4289
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4290
        int cmpFracHalf;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4291
        if (r <= HALF_LONG_MIN_VALUE || r > HALF_LONG_MAX_VALUE) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4292
            cmpFracHalf = 1; // 2 * r can't fit into long
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4293
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4294
            cmpFracHalf = longCompareMagnitude(2 * r, ldivisor);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4295
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4296
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4297
        return commonNeedIncrement(roundingMode, qsign, cmpFracHalf, mq.isOdd());
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4298
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4299
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4300
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4301
     * Divides {@code BigInteger} value by {@code BigInteger} value and
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4302
     * do rounding based on the passed in roundingMode.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4303
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4304
    private static BigInteger divideAndRound(BigInteger bdividend, BigInteger bdivisor, int roundingMode) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4305
        boolean isRemainderZero; // record remainder is zero or not
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4306
        int qsign; // quotient sign
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4307
        // Descend into mutables for faster remainder checks
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4308
        MutableBigInteger mdividend = new MutableBigInteger(bdividend.mag);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4309
        MutableBigInteger mq = new MutableBigInteger();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4310
        MutableBigInteger mdivisor = new MutableBigInteger(bdivisor.mag);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4311
        MutableBigInteger mr = mdividend.divide(mdivisor, mq);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4312
        isRemainderZero = mr.isZero();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4313
        qsign = (bdividend.signum != bdivisor.signum) ? -1 : 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4314
        if (!isRemainderZero) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4315
            if (needIncrement(mdivisor, roundingMode, qsign, mq, mr)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4316
                mq.add(MutableBigInteger.ONE);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4317
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4318
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4319
        return mq.toBigInteger(qsign);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4320
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4321
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4322
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4323
     * Internally used for division operation for division {@code BigInteger}
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4324
     * by {@code BigInteger}.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4325
     * The returned {@code BigDecimal} object is the quotient whose scale is set
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4326
     * to the passed in scale. If the remainder is not zero, it will be rounded
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4327
     * based on the passed in roundingMode. Also, if the remainder is zero and
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4328
     * the last parameter, i.e. preferredScale is NOT equal to scale, the
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4329
     * trailing zeros of the result is stripped to match the preferredScale.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4330
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4331
    private static BigDecimal divideAndRound(BigInteger bdividend, BigInteger bdivisor, int scale, int roundingMode,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4332
                                             int preferredScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4333
        boolean isRemainderZero; // record remainder is zero or not
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4334
        int qsign; // quotient sign
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4335
        // Descend into mutables for faster remainder checks
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4336
        MutableBigInteger mdividend = new MutableBigInteger(bdividend.mag);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4337
        MutableBigInteger mq = new MutableBigInteger();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4338
        MutableBigInteger mdivisor = new MutableBigInteger(bdivisor.mag);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4339
        MutableBigInteger mr = mdividend.divide(mdivisor, mq);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4340
        isRemainderZero = mr.isZero();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4341
        qsign = (bdividend.signum != bdivisor.signum) ? -1 : 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4342
        if (!isRemainderZero) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4343
            if (needIncrement(mdivisor, roundingMode, qsign, mq, mr)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4344
                mq.add(MutableBigInteger.ONE);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4345
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4346
            return mq.toBigDecimal(qsign, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4347
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4348
            if (preferredScale != scale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4349
                long compactVal = mq.toCompactValue(qsign);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4350
                if (compactVal != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4351
                    return createAndStripZerosToMatchScale(compactVal, scale, preferredScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4352
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4353
                BigInteger intVal = mq.toBigInteger(qsign);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4354
                return createAndStripZerosToMatchScale(intVal, scale, preferredScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4355
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4356
                return mq.toBigDecimal(qsign, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4357
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4358
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4359
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4360
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4361
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4362
     * Tests if quotient has to be incremented according the roundingMode
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4363
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4364
    private static boolean needIncrement(MutableBigInteger mdivisor, int roundingMode,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4365
                                         int qsign, MutableBigInteger mq, MutableBigInteger mr) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4366
        assert !mr.isZero();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4367
        int cmpFracHalf = mr.compareHalf(mdivisor);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4368
        return commonNeedIncrement(roundingMode, qsign, cmpFracHalf, mq.isOdd());
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4369
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4370
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4371
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4372
     * Remove insignificant trailing zeros from this
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4373
     * {@code BigInteger} value until the preferred scale is reached or no
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4374
     * more zeros can be removed.  If the preferred scale is less than
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4375
     * Integer.MIN_VALUE, all the trailing zeros will be removed.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4376
     *
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4377
     * @return new {@code BigDecimal} with a scale possibly reduced
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4378
     * to be closed to the preferred scale.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4379
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4380
    private static BigDecimal createAndStripZerosToMatchScale(BigInteger intVal, int scale, long preferredScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4381
        BigInteger qr[]; // quotient-remainder pair
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4382
        while (intVal.compareMagnitude(BigInteger.TEN) >= 0
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4383
               && scale > preferredScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4384
            if (intVal.testBit(0))
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4385
                break; // odd number cannot end in 0
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4386
            qr = intVal.divideAndRemainder(BigInteger.TEN);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4387
            if (qr[1].signum() != 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4388
                break; // non-0 remainder
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4389
            intVal = qr[0];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4390
            scale = checkScale(intVal,(long) scale - 1); // could Overflow
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4391
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4392
        return valueOf(intVal, scale, 0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4393
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4394
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4395
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4396
     * Remove insignificant trailing zeros from this
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4397
     * {@code long} value until the preferred scale is reached or no
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4398
     * more zeros can be removed.  If the preferred scale is less than
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4399
     * Integer.MIN_VALUE, all the trailing zeros will be removed.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4400
     *
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4401
     * @return new {@code BigDecimal} with a scale possibly reduced
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4402
     * to be closed to the preferred scale.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4403
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4404
    private static BigDecimal createAndStripZerosToMatchScale(long compactVal, int scale, long preferredScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4405
        while (Math.abs(compactVal) >= 10L && scale > preferredScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4406
            if ((compactVal & 1L) != 0L)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4407
                break; // odd number cannot end in 0
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4408
            long r = compactVal % 10L;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4409
            if (r != 0L)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4410
                break; // non-0 remainder
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4411
            compactVal /= 10;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4412
            scale = checkScale(compactVal, (long) scale - 1); // could Overflow
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4413
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4414
        return valueOf(compactVal, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4415
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4416
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4417
    private static BigDecimal stripZerosToMatchScale(BigInteger intVal, long intCompact, int scale, int preferredScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4418
        if(intCompact!=INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4419
            return createAndStripZerosToMatchScale(intCompact, scale, preferredScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4420
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4421
            return createAndStripZerosToMatchScale(intVal==null ? INFLATED_BIGINT : intVal,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4422
                                                   scale, preferredScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4423
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4424
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4425
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4426
    /*
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4427
     * returns INFLATED if oveflow
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4428
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4429
    private static long add(long xs, long ys){
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4430
        long sum = xs + ys;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4431
        // See "Hacker's Delight" section 2-12 for explanation of
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4432
        // the overflow test.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4433
        if ( (((sum ^ xs) & (sum ^ ys))) >= 0L) { // not overflowed
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4434
            return sum;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4435
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4436
        return INFLATED;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4437
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4438
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4439
    private static BigDecimal add(long xs, long ys, int scale){
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4440
        long sum = add(xs, ys);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4441
        if (sum!=INFLATED)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4442
            return BigDecimal.valueOf(sum, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4443
        return new BigDecimal(BigInteger.valueOf(xs).add(ys), scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4444
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4445
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4446
    private static BigDecimal add(final long xs, int scale1, final long ys, int scale2) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4447
        long sdiff = (long) scale1 - scale2;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4448
        if (sdiff == 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4449
            return add(xs, ys, scale1);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4450
        } else if (sdiff < 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4451
            int raise = checkScale(xs,-sdiff);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4452
            long scaledX = longMultiplyPowerTen(xs, raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4453
            if (scaledX != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4454
                return add(scaledX, ys, scale2);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4455
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4456
                BigInteger bigsum = bigMultiplyPowerTen(xs,raise).add(ys);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4457
                return ((xs^ys)>=0) ? // same sign test
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4458
                    new BigDecimal(bigsum, INFLATED, scale2, 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4459
                    : valueOf(bigsum, scale2, 0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4460
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4461
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4462
            int raise = checkScale(ys,sdiff);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4463
            long scaledY = longMultiplyPowerTen(ys, raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4464
            if (scaledY != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4465
                return add(xs, scaledY, scale1);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4466
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4467
                BigInteger bigsum = bigMultiplyPowerTen(ys,raise).add(xs);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4468
                return ((xs^ys)>=0) ?
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4469
                    new BigDecimal(bigsum, INFLATED, scale1, 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4470
                    : valueOf(bigsum, scale1, 0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4471
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4472
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4473
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4474
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4475
    private static BigDecimal add(final long xs, int scale1, BigInteger snd, int scale2) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4476
        int rscale = scale1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4477
        long sdiff = (long)rscale - scale2;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4478
        boolean sameSigns =  (Long.signum(xs) == snd.signum);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4479
        BigInteger sum;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4480
        if (sdiff < 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4481
            int raise = checkScale(xs,-sdiff);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4482
            rscale = scale2;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4483
            long scaledX = longMultiplyPowerTen(xs, raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4484
            if (scaledX == INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4485
                sum = snd.add(bigMultiplyPowerTen(xs,raise));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4486
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4487
                sum = snd.add(scaledX);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4488
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4489
        } else { //if (sdiff > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4490
            int raise = checkScale(snd,sdiff);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4491
            snd = bigMultiplyPowerTen(snd,raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4492
            sum = snd.add(xs);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4493
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4494
        return (sameSigns) ?
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4495
            new BigDecimal(sum, INFLATED, rscale, 0) :
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4496
            valueOf(sum, rscale, 0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4497
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4498
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4499
    private static BigDecimal add(BigInteger fst, int scale1, BigInteger snd, int scale2) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4500
        int rscale = scale1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4501
        long sdiff = (long)rscale - scale2;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4502
        if (sdiff != 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4503
            if (sdiff < 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4504
                int raise = checkScale(fst,-sdiff);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4505
                rscale = scale2;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4506
                fst = bigMultiplyPowerTen(fst,raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4507
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4508
                int raise = checkScale(snd,sdiff);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4509
                snd = bigMultiplyPowerTen(snd,raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4510
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4511
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4512
        BigInteger sum = fst.add(snd);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4513
        return (fst.signum == snd.signum) ?
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4514
                new BigDecimal(sum, INFLATED, rscale, 0) :
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4515
                valueOf(sum, rscale, 0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4516
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4517
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4518
    private static BigInteger bigMultiplyPowerTen(long value, int n) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4519
        if (n <= 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4520
            return BigInteger.valueOf(value);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4521
        return bigTenToThe(n).multiply(value);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4522
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4523
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4524
    private static BigInteger bigMultiplyPowerTen(BigInteger value, int n) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4525
        if (n <= 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4526
            return value;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4527
        if(n<LONG_TEN_POWERS_TABLE.length) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4528
                return value.multiply(LONG_TEN_POWERS_TABLE[n]);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4529
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4530
        return value.multiply(bigTenToThe(n));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4531
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4532
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4533
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4534
     * Returns a {@code BigDecimal} whose value is {@code (xs /
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4535
     * ys)}, with rounding according to the context settings.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4536
     *
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4537
     * Fast path - used only when (xscale <= yscale && yscale < 18
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4538
     *  && mc.presision<18) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4539
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4540
    private static BigDecimal divideSmallFastPath(final long xs, int xscale,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4541
                                                  final long ys, int yscale,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4542
                                                  long preferredScale, MathContext mc) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4543
        int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4544
        int roundingMode = mc.roundingMode.oldMode;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4545
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4546
        assert (xscale <= yscale) && (yscale < 18) && (mcp < 18);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4547
        int xraise = yscale - xscale; // xraise >=0
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4548
        long scaledX = (xraise==0) ? xs :
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4549
            longMultiplyPowerTen(xs, xraise); // can't overflow here!
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4550
        BigDecimal quotient;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4551
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4552
        int cmp = longCompareMagnitude(scaledX, ys);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4553
        if(cmp > 0) { // satisfy constraint (b)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4554
            yscale -= 1; // [that is, divisor *= 10]
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4555
            int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
19585
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  4556
            if (checkScaleNonZero((long) mcp + yscale - xscale) > 0) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4557
                // assert newScale >= xscale
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4558
                int raise = checkScaleNonZero((long) mcp + yscale - xscale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4559
                long scaledXs;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4560
                if ((scaledXs = longMultiplyPowerTen(xs, raise)) == INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4561
                    quotient = null;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4562
                    if((mcp-1) >=0 && (mcp-1)<LONG_TEN_POWERS_TABLE.length) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4563
                        quotient = multiplyDivideAndRound(LONG_TEN_POWERS_TABLE[mcp-1], scaledX, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4564
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4565
                    if(quotient==null) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4566
                        BigInteger rb = bigMultiplyPowerTen(scaledX,mcp-1);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4567
                        quotient = divideAndRound(rb, ys,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4568
                                                  scl, roundingMode, checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4569
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4570
                } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4571
                    quotient = divideAndRound(scaledXs, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4572
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4573
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4574
                int newScale = checkScaleNonZero((long) xscale - mcp);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4575
                // assert newScale >= yscale
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4576
                if (newScale == yscale) { // easy case
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4577
                    quotient = divideAndRound(xs, ys, scl, roundingMode,checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4578
                } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4579
                    int raise = checkScaleNonZero((long) newScale - yscale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4580
                    long scaledYs;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4581
                    if ((scaledYs = longMultiplyPowerTen(ys, raise)) == INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4582
                        BigInteger rb = bigMultiplyPowerTen(ys,raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4583
                        quotient = divideAndRound(BigInteger.valueOf(xs),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4584
                                                  rb, scl, roundingMode,checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4585
                    } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4586
                        quotient = divideAndRound(xs, scaledYs, scl, roundingMode,checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4587
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4588
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4589
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4590
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4591
            // abs(scaledX) <= abs(ys)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4592
            // result is "scaledX * 10^msp / ys"
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4593
            int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4594
            if(cmp==0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4595
                // abs(scaleX)== abs(ys) => result will be scaled 10^mcp + correct sign
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4596
                quotient = roundedTenPower(((scaledX < 0) == (ys < 0)) ? 1 : -1, mcp, scl, checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4597
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4598
                // abs(scaledX) < abs(ys)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4599
                long scaledXs;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4600
                if ((scaledXs = longMultiplyPowerTen(scaledX, mcp)) == INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4601
                    quotient = null;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4602
                    if(mcp<LONG_TEN_POWERS_TABLE.length) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4603
                        quotient = multiplyDivideAndRound(LONG_TEN_POWERS_TABLE[mcp], scaledX, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4604
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4605
                    if(quotient==null) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4606
                        BigInteger rb = bigMultiplyPowerTen(scaledX,mcp);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4607
                        quotient = divideAndRound(rb, ys,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4608
                                                  scl, roundingMode, checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4609
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4610
                } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4611
                    quotient = divideAndRound(scaledXs, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4612
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4613
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4614
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4615
        // doRound, here, only affects 1000000000 case.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4616
        return doRound(quotient,mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4617
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4618
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4619
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4620
     * Returns a {@code BigDecimal} whose value is {@code (xs /
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4621
     * ys)}, with rounding according to the context settings.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4622
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4623
    private static BigDecimal divide(final long xs, int xscale, final long ys, int yscale, long preferredScale, MathContext mc) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4624
        int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4625
        if(xscale <= yscale && yscale < 18 && mcp<18) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4626
            return divideSmallFastPath(xs, xscale, ys, yscale, preferredScale, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4627
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4628
        if (compareMagnitudeNormalized(xs, xscale, ys, yscale) > 0) {// satisfy constraint (b)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4629
            yscale -= 1; // [that is, divisor *= 10]
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4630
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4631
        int roundingMode = mc.roundingMode.oldMode;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4632
        // In order to find out whether the divide generates the exact result,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4633
        // we avoid calling the above divide method. 'quotient' holds the
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4634
        // return BigDecimal object whose scale will be set to 'scl'.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4635
        int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4636
        BigDecimal quotient;
19585
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  4637
        if (checkScaleNonZero((long) mcp + yscale - xscale) > 0) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4638
            int raise = checkScaleNonZero((long) mcp + yscale - xscale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4639
            long scaledXs;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4640
            if ((scaledXs = longMultiplyPowerTen(xs, raise)) == INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4641
                BigInteger rb = bigMultiplyPowerTen(xs,raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4642
                quotient = divideAndRound(rb, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4643
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4644
                quotient = divideAndRound(scaledXs, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4645
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4646
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4647
            int newScale = checkScaleNonZero((long) xscale - mcp);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4648
            // assert newScale >= yscale
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4649
            if (newScale == yscale) { // easy case
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4650
                quotient = divideAndRound(xs, ys, scl, roundingMode,checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4651
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4652
                int raise = checkScaleNonZero((long) newScale - yscale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4653
                long scaledYs;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4654
                if ((scaledYs = longMultiplyPowerTen(ys, raise)) == INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4655
                    BigInteger rb = bigMultiplyPowerTen(ys,raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4656
                    quotient = divideAndRound(BigInteger.valueOf(xs),
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4657
                                              rb, scl, roundingMode,checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4658
                } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4659
                    quotient = divideAndRound(xs, scaledYs, scl, roundingMode,checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4660
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4661
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4662
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4663
        // doRound, here, only affects 1000000000 case.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4664
        return doRound(quotient,mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4665
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4666
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4667
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4668
     * Returns a {@code BigDecimal} whose value is {@code (xs /
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4669
     * ys)}, with rounding according to the context settings.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4670
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4671
    private static BigDecimal divide(BigInteger xs, int xscale, long ys, int yscale, long preferredScale, MathContext mc) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4672
        // Normalize dividend & divisor so that both fall into [0.1, 0.999...]
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4673
        if ((-compareMagnitudeNormalized(ys, yscale, xs, xscale)) > 0) {// satisfy constraint (b)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4674
            yscale -= 1; // [that is, divisor *= 10]
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4675
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4676
        int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4677
        int roundingMode = mc.roundingMode.oldMode;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4678
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4679
        // In order to find out whether the divide generates the exact result,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4680
        // we avoid calling the above divide method. 'quotient' holds the
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4681
        // return BigDecimal object whose scale will be set to 'scl'.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4682
        BigDecimal quotient;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4683
        int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
19585
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  4684
        if (checkScaleNonZero((long) mcp + yscale - xscale) > 0) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4685
            int raise = checkScaleNonZero((long) mcp + yscale - xscale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4686
            BigInteger rb = bigMultiplyPowerTen(xs,raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4687
            quotient = divideAndRound(rb, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4688
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4689
            int newScale = checkScaleNonZero((long) xscale - mcp);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4690
            // assert newScale >= yscale
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4691
            if (newScale == yscale) { // easy case
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4692
                quotient = divideAndRound(xs, ys, scl, roundingMode,checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4693
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4694
                int raise = checkScaleNonZero((long) newScale - yscale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4695
                long scaledYs;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4696
                if ((scaledYs = longMultiplyPowerTen(ys, raise)) == INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4697
                    BigInteger rb = bigMultiplyPowerTen(ys,raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4698
                    quotient = divideAndRound(xs, rb, scl, roundingMode,checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4699
                } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4700
                    quotient = divideAndRound(xs, scaledYs, scl, roundingMode,checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4701
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4702
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4703
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4704
        // doRound, here, only affects 1000000000 case.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4705
        return doRound(quotient, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4706
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4707
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4708
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4709
     * Returns a {@code BigDecimal} whose value is {@code (xs /
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4710
     * ys)}, with rounding according to the context settings.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4711
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4712
    private static BigDecimal divide(long xs, int xscale, BigInteger ys, int yscale, long preferredScale, MathContext mc) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4713
        // Normalize dividend & divisor so that both fall into [0.1, 0.999...]
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4714
        if (compareMagnitudeNormalized(xs, xscale, ys, yscale) > 0) {// satisfy constraint (b)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4715
            yscale -= 1; // [that is, divisor *= 10]
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4716
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4717
        int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4718
        int roundingMode = mc.roundingMode.oldMode;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4719
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4720
        // In order to find out whether the divide generates the exact result,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4721
        // we avoid calling the above divide method. 'quotient' holds the
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4722
        // return BigDecimal object whose scale will be set to 'scl'.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4723
        BigDecimal quotient;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4724
        int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
19585
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  4725
        if (checkScaleNonZero((long) mcp + yscale - xscale) > 0) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4726
            int raise = checkScaleNonZero((long) mcp + yscale - xscale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4727
            BigInteger rb = bigMultiplyPowerTen(xs,raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4728
            quotient = divideAndRound(rb, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4729
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4730
            int newScale = checkScaleNonZero((long) xscale - mcp);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4731
            int raise = checkScaleNonZero((long) newScale - yscale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4732
            BigInteger rb = bigMultiplyPowerTen(ys,raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4733
            quotient = divideAndRound(BigInteger.valueOf(xs), rb, scl, roundingMode,checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4734
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4735
        // doRound, here, only affects 1000000000 case.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4736
        return doRound(quotient, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4737
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4738
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4739
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4740
     * Returns a {@code BigDecimal} whose value is {@code (xs /
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4741
     * ys)}, with rounding according to the context settings.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4742
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4743
    private static BigDecimal divide(BigInteger xs, int xscale, BigInteger ys, int yscale, long preferredScale, MathContext mc) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4744
        // Normalize dividend & divisor so that both fall into [0.1, 0.999...]
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4745
        if (compareMagnitudeNormalized(xs, xscale, ys, yscale) > 0) {// satisfy constraint (b)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4746
            yscale -= 1; // [that is, divisor *= 10]
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4747
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4748
        int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4749
        int roundingMode = mc.roundingMode.oldMode;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4750
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4751
        // In order to find out whether the divide generates the exact result,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4752
        // we avoid calling the above divide method. 'quotient' holds the
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4753
        // return BigDecimal object whose scale will be set to 'scl'.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4754
        BigDecimal quotient;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4755
        int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
19585
b57abf89019f 6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents: 18798
diff changeset
  4756
        if (checkScaleNonZero((long) mcp + yscale - xscale) > 0) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4757
            int raise = checkScaleNonZero((long) mcp + yscale - xscale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4758
            BigInteger rb = bigMultiplyPowerTen(xs,raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4759
            quotient = divideAndRound(rb, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4760
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4761
            int newScale = checkScaleNonZero((long) xscale - mcp);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4762
            int raise = checkScaleNonZero((long) newScale - yscale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4763
            BigInteger rb = bigMultiplyPowerTen(ys,raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4764
            quotient = divideAndRound(xs, rb, scl, roundingMode,checkScaleNonZero(preferredScale));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4765
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4766
        // doRound, here, only affects 1000000000 case.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4767
        return doRound(quotient, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4768
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4769
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4770
    /*
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4771
     * performs divideAndRound for (dividend0*dividend1, divisor)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4772
     * returns null if quotient can't fit into long value;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4773
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4774
    private static BigDecimal multiplyDivideAndRound(long dividend0, long dividend1, long divisor, int scale, int roundingMode,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4775
                                                     int preferredScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4776
        int qsign = Long.signum(dividend0)*Long.signum(dividend1)*Long.signum(divisor);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4777
        dividend0 = Math.abs(dividend0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4778
        dividend1 = Math.abs(dividend1);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4779
        divisor = Math.abs(divisor);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4780
        // multiply dividend0 * dividend1
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4781
        long d0_hi = dividend0 >>> 32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4782
        long d0_lo = dividend0 & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4783
        long d1_hi = dividend1 >>> 32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4784
        long d1_lo = dividend1 & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4785
        long product = d0_lo * d1_lo;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4786
        long d0 = product & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4787
        long d1 = product >>> 32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4788
        product = d0_hi * d1_lo + d1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4789
        d1 = product & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4790
        long d2 = product >>> 32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4791
        product = d0_lo * d1_hi + d1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4792
        d1 = product & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4793
        d2 += product >>> 32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4794
        long d3 = d2>>>32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4795
        d2 &= LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4796
        product = d0_hi*d1_hi + d2;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4797
        d2 = product & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4798
        d3 = ((product>>>32) + d3) & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4799
        final long dividendHi = make64(d3,d2);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4800
        final long dividendLo = make64(d1,d0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4801
        // divide
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4802
        return divideAndRound128(dividendHi, dividendLo, divisor, qsign, scale, roundingMode, preferredScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4803
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4804
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4805
    private static final long DIV_NUM_BASE = (1L<<32); // Number base (32 bits).
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4806
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4807
    /*
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4808
     * divideAndRound 128-bit value by long divisor.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4809
     * returns null if quotient can't fit into long value;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4810
     * Specialized version of Knuth's division
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4811
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4812
    private static BigDecimal divideAndRound128(final long dividendHi, final long dividendLo, long divisor, int sign,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4813
                                                int scale, int roundingMode, int preferredScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4814
        if (dividendHi >= divisor) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4815
            return null;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4816
        }
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4817
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4818
        final int shift = Long.numberOfLeadingZeros(divisor);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4819
        divisor <<= shift;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4820
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4821
        final long v1 = divisor >>> 32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4822
        final long v0 = divisor & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4823
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4824
        long tmp = dividendLo << shift;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4825
        long u1 = tmp >>> 32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4826
        long u0 = tmp & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4827
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4828
        tmp = (dividendHi << shift) | (dividendLo >>> 64 - shift);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4829
        long u2 = tmp & LONG_MASK;
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4830
        long q1, r_tmp;
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4831
        if (v1 == 1) {
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4832
            q1 = tmp;
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4833
            r_tmp = 0;
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4834
        } else if (tmp >= 0) {
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4835
            q1 = tmp / v1;
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4836
            r_tmp = tmp - q1 * v1;
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4837
        } else {
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4838
            long[] rq = divRemNegativeLong(tmp, v1);
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4839
            q1 = rq[1];
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4840
            r_tmp = rq[0];
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4841
        }
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4842
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4843
        while(q1 >= DIV_NUM_BASE || unsignedLongCompare(q1*v0, make64(r_tmp, u1))) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4844
            q1--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4845
            r_tmp += v1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4846
            if (r_tmp >= DIV_NUM_BASE)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4847
                break;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4848
        }
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4849
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4850
        tmp = mulsub(u2,u1,v1,v0,q1);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4851
        u1 = tmp & LONG_MASK;
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4852
        long q0;
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4853
        if (v1 == 1) {
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4854
            q0 = tmp;
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4855
            r_tmp = 0;
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4856
        } else if (tmp >= 0) {
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4857
            q0 = tmp / v1;
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4858
            r_tmp = tmp - q0 * v1;
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4859
        } else {
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4860
            long[] rq = divRemNegativeLong(tmp, v1);
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4861
            q0 = rq[1];
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4862
            r_tmp = rq[0];
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4863
        }
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4864
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4865
        while(q0 >= DIV_NUM_BASE || unsignedLongCompare(q0*v0,make64(r_tmp,u0))) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4866
            q0--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4867
            r_tmp += v1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4868
            if (r_tmp >= DIV_NUM_BASE)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4869
                break;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4870
        }
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4871
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4872
        if((int)q1 < 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4873
            // result (which is positive and unsigned here)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4874
            // can't fit into long due to sign bit is used for value
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4875
            MutableBigInteger mq = new MutableBigInteger(new int[]{(int)q1, (int)q0});
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4876
            if (roundingMode == ROUND_DOWN && scale == preferredScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4877
                return mq.toBigDecimal(sign, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4878
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4879
            long r = mulsub(u1, u0, v1, v0, q0) >>> shift;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4880
            if (r != 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4881
                if(needIncrement(divisor >>> shift, roundingMode, sign, mq, r)){
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4882
                    mq.add(MutableBigInteger.ONE);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4883
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4884
                return mq.toBigDecimal(sign, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4885
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4886
                if (preferredScale != scale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4887
                    BigInteger intVal =  mq.toBigInteger(sign);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4888
                    return createAndStripZerosToMatchScale(intVal,scale, preferredScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4889
                } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4890
                    return mq.toBigDecimal(sign, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4891
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4892
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4893
        }
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4894
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4895
        long q = make64(q1,q0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4896
        q*=sign;
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4897
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4898
        if (roundingMode == ROUND_DOWN && scale == preferredScale)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4899
            return valueOf(q, scale);
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4900
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4901
        long r = mulsub(u1, u0, v1, v0, q0) >>> shift;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4902
        if (r != 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4903
            boolean increment = needIncrement(divisor >>> shift, roundingMode, sign, q, r);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4904
            return valueOf((increment ? q + sign : q), scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4905
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4906
            if (preferredScale != scale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4907
                return createAndStripZerosToMatchScale(q, scale, preferredScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4908
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4909
                return valueOf(q, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4910
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4911
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4912
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4913
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4914
    /*
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4915
     * calculate divideAndRound for ldividend*10^raise / divisor
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4916
     * when abs(dividend)==abs(divisor);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4917
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4918
    private static BigDecimal roundedTenPower(int qsign, int raise, int scale, int preferredScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4919
        if (scale > preferredScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4920
            int diff = scale - preferredScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4921
            if(diff < raise) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4922
                return scaledTenPow(raise - diff, qsign, preferredScale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4923
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4924
                return valueOf(qsign,scale-raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4925
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4926
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4927
            return scaledTenPow(raise, qsign, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4928
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4929
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4930
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4931
    static BigDecimal scaledTenPow(int n, int sign, int scale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4932
        if (n < LONG_TEN_POWERS_TABLE.length)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4933
            return valueOf(sign*LONG_TEN_POWERS_TABLE[n],scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4934
        else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4935
            BigInteger unscaledVal = bigTenToThe(n);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4936
            if(sign==-1) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4937
                unscaledVal = unscaledVal.negate();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4938
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4939
            return new BigDecimal(unscaledVal, INFLATED, scale, n+1);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4940
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4941
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4942
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4943
    /**
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4944
     * Calculate the quotient and remainder of dividing a negative long by
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4945
     * another long.
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4946
     *
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4947
     * @param n the numerator; must be negative
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4948
     * @param d the denominator; must not be unity
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4949
     * @return a two-element {@long} array with the remainder and quotient in
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4950
     *         the initial and final elements, respectively
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4951
     */
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4952
    private static long[] divRemNegativeLong(long n, long d) {
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4953
        assert n < 0 : "Non-negative numerator " + n;
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4954
        assert d != 1 : "Unity denominator";
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4955
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4956
        // Approximate the quotient and remainder
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4957
        long q = (n >>> 1) / (d >>> 1);
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4958
        long r = n - q * d;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4959
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4960
        // Correct the approximation
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4961
        while (r < 0) {
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4962
            r += d;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4963
            q--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4964
        }
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4965
        while (r >= d) {
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4966
            r -= d;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4967
            q++;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4968
        }
28869
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4969
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4970
        // n - q*d == r && 0 <= r < d, hence we're done.
9725237b107d 8066842: java.math.BigDecimal.divide(BigDecimal, RoundingMode) produces incorrect result
bpb
parents: 28257
diff changeset
  4971
        return new long[] {r, q};
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4972
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4973
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4974
    private static long make64(long hi, long lo) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4975
        return hi<<32 | lo;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4976
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4977
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4978
    private static long mulsub(long u1, long u0, final long v1, final long v0, long q0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4979
        long tmp = u0 - q0*v0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4980
        return make64(u1 + (tmp>>>32) - q0*v1,tmp & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4981
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4982
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4983
    private static boolean unsignedLongCompare(long one, long two) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4984
        return (one+Long.MIN_VALUE) > (two+Long.MIN_VALUE);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4985
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4986
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4987
    private static boolean unsignedLongCompareEq(long one, long two) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4988
        return (one+Long.MIN_VALUE) >= (two+Long.MIN_VALUE);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4989
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4990
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4991
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4992
    // Compare Normalize dividend & divisor so that both fall into [0.1, 0.999...]
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4993
    private static int compareMagnitudeNormalized(long xs, int xscale, long ys, int yscale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4994
        // assert xs!=0 && ys!=0
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4995
        int sdiff = xscale - yscale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4996
        if (sdiff != 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4997
            if (sdiff < 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4998
                xs = longMultiplyPowerTen(xs, -sdiff);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  4999
            } else { // sdiff > 0
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5000
                ys = longMultiplyPowerTen(ys, sdiff);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5001
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5002
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5003
        if (xs != INFLATED)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5004
            return (ys != INFLATED) ? longCompareMagnitude(xs, ys) : -1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5005
        else
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5006
            return 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5007
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5008
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5009
    // Compare Normalize dividend & divisor so that both fall into [0.1, 0.999...]
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5010
    private static int compareMagnitudeNormalized(long xs, int xscale, BigInteger ys, int yscale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5011
        // assert "ys can't be represented as long"
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5012
        if (xs == 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5013
            return -1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5014
        int sdiff = xscale - yscale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5015
        if (sdiff < 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5016
            if (longMultiplyPowerTen(xs, -sdiff) == INFLATED ) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5017
                return bigMultiplyPowerTen(xs, -sdiff).compareMagnitude(ys);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5018
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5019
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5020
        return -1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5021
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5022
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5023
    // Compare Normalize dividend & divisor so that both fall into [0.1, 0.999...]
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5024
    private static int compareMagnitudeNormalized(BigInteger xs, int xscale, BigInteger ys, int yscale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5025
        int sdiff = xscale - yscale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5026
        if (sdiff < 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5027
            return bigMultiplyPowerTen(xs, -sdiff).compareMagnitude(ys);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5028
        } else { // sdiff >= 0
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5029
            return xs.compareMagnitude(bigMultiplyPowerTen(ys, sdiff));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5030
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5031
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5032
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5033
    private static long multiply(long x, long y){
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5034
                long product = x * y;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5035
        long ax = Math.abs(x);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5036
        long ay = Math.abs(y);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5037
        if (((ax | ay) >>> 31 == 0) || (y == 0) || (product / y == x)){
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5038
                        return product;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5039
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5040
        return INFLATED;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5041
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5042
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5043
    private static BigDecimal multiply(long x, long y, int scale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5044
        long product = multiply(x, y);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5045
        if(product!=INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5046
            return valueOf(product,scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5047
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5048
        return new BigDecimal(BigInteger.valueOf(x).multiply(y),INFLATED,scale,0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5049
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5050
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5051
    private static BigDecimal multiply(long x, BigInteger y, int scale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5052
        if(x==0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5053
            return zeroValueOf(scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5054
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5055
        return new BigDecimal(y.multiply(x),INFLATED,scale,0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5056
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5057
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5058
    private static BigDecimal multiply(BigInteger x, BigInteger y, int scale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5059
        return new BigDecimal(x.multiply(y),INFLATED,scale,0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5060
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5061
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5062
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5063
     * Multiplies two long values and rounds according {@code MathContext}
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5064
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5065
    private static BigDecimal multiplyAndRound(long x, long y, int scale, MathContext mc) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5066
        long product = multiply(x, y);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5067
        if(product!=INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5068
            return doRound(product, scale, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5069
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5070
        // attempt to do it in 128 bits
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5071
        int rsign = 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5072
        if(x < 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5073
            x = -x;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5074
            rsign = -1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5075
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5076
        if(y < 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5077
            y = -y;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5078
            rsign *= -1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5079
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5080
        // multiply dividend0 * dividend1
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5081
        long m0_hi = x >>> 32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5082
        long m0_lo = x & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5083
        long m1_hi = y >>> 32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5084
        long m1_lo = y & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5085
        product = m0_lo * m1_lo;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5086
        long m0 = product & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5087
        long m1 = product >>> 32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5088
        product = m0_hi * m1_lo + m1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5089
        m1 = product & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5090
        long m2 = product >>> 32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5091
        product = m0_lo * m1_hi + m1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5092
        m1 = product & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5093
        m2 += product >>> 32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5094
        long m3 = m2>>>32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5095
        m2 &= LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5096
        product = m0_hi*m1_hi + m2;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5097
        m2 = product & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5098
        m3 = ((product>>>32) + m3) & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5099
        final long mHi = make64(m3,m2);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5100
        final long mLo = make64(m1,m0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5101
        BigDecimal res = doRound128(mHi, mLo, rsign, scale, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5102
        if(res!=null) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5103
            return res;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5104
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5105
        res = new BigDecimal(BigInteger.valueOf(x).multiply(y*rsign), INFLATED, scale, 0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5106
        return doRound(res,mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5107
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5108
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5109
    private static BigDecimal multiplyAndRound(long x, BigInteger y, int scale, MathContext mc) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5110
        if(x==0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5111
            return zeroValueOf(scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5112
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5113
        return doRound(y.multiply(x), scale, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5114
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5115
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5116
    private static BigDecimal multiplyAndRound(BigInteger x, BigInteger y, int scale, MathContext mc) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5117
        return doRound(x.multiply(y), scale, mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5118
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5119
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5120
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5121
     * rounds 128-bit value according {@code MathContext}
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5122
     * returns null if result can't be repsented as compact BigDecimal.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5123
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5124
    private static BigDecimal doRound128(long hi, long lo, int sign, int scale, MathContext mc) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5125
        int mcp = mc.precision;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5126
        int drop;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5127
        BigDecimal res = null;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5128
        if(((drop = precision(hi, lo) - mcp) > 0)&&(drop<LONG_TEN_POWERS_TABLE.length)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5129
            scale = checkScaleNonZero((long)scale - drop);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5130
            res = divideAndRound128(hi, lo, LONG_TEN_POWERS_TABLE[drop], sign, scale, mc.roundingMode.oldMode, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5131
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5132
        if(res!=null) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5133
            return doRound(res,mc);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5134
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5135
        return null;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5136
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5137
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5138
    private static final long[][] LONGLONG_TEN_POWERS_TABLE = {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5139
        {   0L, 0x8AC7230489E80000L },  //10^19
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5140
        {       0x5L, 0x6bc75e2d63100000L },  //10^20
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5141
        {       0x36L, 0x35c9adc5dea00000L },  //10^21
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5142
        {       0x21eL, 0x19e0c9bab2400000L  },  //10^22
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5143
        {       0x152dL, 0x02c7e14af6800000L  },  //10^23
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5144
        {       0xd3c2L, 0x1bcecceda1000000L  },  //10^24
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5145
        {       0x84595L, 0x161401484a000000L  },  //10^25
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5146
        {       0x52b7d2L, 0xdcc80cd2e4000000L  },  //10^26
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5147
        {       0x33b2e3cL, 0x9fd0803ce8000000L  },  //10^27
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5148
        {       0x204fce5eL, 0x3e25026110000000L  },  //10^28
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5149
        {       0x1431e0faeL, 0x6d7217caa0000000L  },  //10^29
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5150
        {       0xc9f2c9cd0L, 0x4674edea40000000L  },  //10^30
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5151
        {       0x7e37be2022L, 0xc0914b2680000000L  },  //10^31
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5152
        {       0x4ee2d6d415bL, 0x85acef8100000000L  },  //10^32
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5153
        {       0x314dc6448d93L, 0x38c15b0a00000000L  },  //10^33
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5154
        {       0x1ed09bead87c0L, 0x378d8e6400000000L  },  //10^34
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5155
        {       0x13426172c74d82L, 0x2b878fe800000000L  },  //10^35
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5156
        {       0xc097ce7bc90715L, 0xb34b9f1000000000L  },  //10^36
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5157
        {       0x785ee10d5da46d9L, 0x00f436a000000000L  },  //10^37
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5158
        {       0x4b3b4ca85a86c47aL, 0x098a224000000000L  },  //10^38
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5159
    };
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5160
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5161
    /*
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5162
     * returns precision of 128-bit value
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5163
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5164
    private static int precision(long hi, long lo){
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5165
        if(hi==0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5166
            if(lo>=0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5167
                return longDigitLength(lo);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5168
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5169
            return (unsignedLongCompareEq(lo, LONGLONG_TEN_POWERS_TABLE[0][1])) ? 20 : 19;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5170
            // 0x8AC7230489E80000L  = unsigned 2^19
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5171
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5172
        int r = ((128 - Long.numberOfLeadingZeros(hi) + 1) * 1233) >>> 12;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5173
        int idx = r-19;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5174
        return (idx >= LONGLONG_TEN_POWERS_TABLE.length || longLongCompareMagnitude(hi, lo,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5175
                                                                                    LONGLONG_TEN_POWERS_TABLE[idx][0], LONGLONG_TEN_POWERS_TABLE[idx][1])) ? r : r + 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5176
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5177
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5178
    /*
30797
9cf3d0361db4 8040147: minor cleanup for docs
avstepan
parents: 29220
diff changeset
  5179
     * returns true if 128 bit number <hi0,lo0> is less than <hi1,lo1>
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5180
     * hi0 & hi1 should be non-negative
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5181
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5182
    private static boolean longLongCompareMagnitude(long hi0, long lo0, long hi1, long lo1) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5183
        if(hi0!=hi1) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5184
            return hi0<hi1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5185
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5186
        return (lo0+Long.MIN_VALUE) <(lo1+Long.MIN_VALUE);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5187
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5188
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5189
    private static BigDecimal divide(long dividend, int dividendScale, long divisor, int divisorScale, int scale, int roundingMode) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5190
        if (checkScale(dividend,(long)scale + divisorScale) > dividendScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5191
            int newScale = scale + divisorScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5192
            int raise = newScale - dividendScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5193
            if(raise<LONG_TEN_POWERS_TABLE.length) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5194
                long xs = dividend;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5195
                if ((xs = longMultiplyPowerTen(xs, raise)) != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5196
                    return divideAndRound(xs, divisor, scale, roundingMode, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5197
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5198
                BigDecimal q = multiplyDivideAndRound(LONG_TEN_POWERS_TABLE[raise], dividend, divisor, scale, roundingMode, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5199
                if(q!=null) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5200
                    return q;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5201
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5202
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5203
            BigInteger scaledDividend = bigMultiplyPowerTen(dividend, raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5204
            return divideAndRound(scaledDividend, divisor, scale, roundingMode, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5205
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5206
            int newScale = checkScale(divisor,(long)dividendScale - scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5207
            int raise = newScale - divisorScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5208
            if(raise<LONG_TEN_POWERS_TABLE.length) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5209
                long ys = divisor;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5210
                if ((ys = longMultiplyPowerTen(ys, raise)) != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5211
                    return divideAndRound(dividend, ys, scale, roundingMode, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5212
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5213
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5214
            BigInteger scaledDivisor = bigMultiplyPowerTen(divisor, raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5215
            return divideAndRound(BigInteger.valueOf(dividend), scaledDivisor, scale, roundingMode, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5216
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5217
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5218
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5219
    private static BigDecimal divide(BigInteger dividend, int dividendScale, long divisor, int divisorScale, int scale, int roundingMode) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5220
        if (checkScale(dividend,(long)scale + divisorScale) > dividendScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5221
            int newScale = scale + divisorScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5222
            int raise = newScale - dividendScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5223
            BigInteger scaledDividend = bigMultiplyPowerTen(dividend, raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5224
            return divideAndRound(scaledDividend, divisor, scale, roundingMode, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5225
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5226
            int newScale = checkScale(divisor,(long)dividendScale - scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5227
            int raise = newScale - divisorScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5228
            if(raise<LONG_TEN_POWERS_TABLE.length) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5229
                long ys = divisor;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5230
                if ((ys = longMultiplyPowerTen(ys, raise)) != INFLATED) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5231
                    return divideAndRound(dividend, ys, scale, roundingMode, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5232
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5233
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5234
            BigInteger scaledDivisor = bigMultiplyPowerTen(divisor, raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5235
            return divideAndRound(dividend, scaledDivisor, scale, roundingMode, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5236
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5237
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5238
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5239
    private static BigDecimal divide(long dividend, int dividendScale, BigInteger divisor, int divisorScale, int scale, int roundingMode) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5240
        if (checkScale(dividend,(long)scale + divisorScale) > dividendScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5241
            int newScale = scale + divisorScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5242
            int raise = newScale - dividendScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5243
            BigInteger scaledDividend = bigMultiplyPowerTen(dividend, raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5244
            return divideAndRound(scaledDividend, divisor, scale, roundingMode, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5245
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5246
            int newScale = checkScale(divisor,(long)dividendScale - scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5247
            int raise = newScale - divisorScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5248
            BigInteger scaledDivisor = bigMultiplyPowerTen(divisor, raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5249
            return divideAndRound(BigInteger.valueOf(dividend), scaledDivisor, scale, roundingMode, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5250
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5251
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5252
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5253
    private static BigDecimal divide(BigInteger dividend, int dividendScale, BigInteger divisor, int divisorScale, int scale, int roundingMode) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5254
        if (checkScale(dividend,(long)scale + divisorScale) > dividendScale) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5255
            int newScale = scale + divisorScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5256
            int raise = newScale - dividendScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5257
            BigInteger scaledDividend = bigMultiplyPowerTen(dividend, raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5258
            return divideAndRound(scaledDividend, divisor, scale, roundingMode, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5259
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5260
            int newScale = checkScale(divisor,(long)dividendScale - scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5261
            int raise = newScale - divisorScale;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5262
            BigInteger scaledDivisor = bigMultiplyPowerTen(divisor, raise);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5263
            return divideAndRound(dividend, scaledDivisor, scale, roundingMode, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5264
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5265
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 9266
diff changeset
  5266
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5267
}