jdk/src/share/classes/java/math/MutableBigInteger.java
author xlu
Sun, 24 May 2009 16:29:57 -0700
changeset 2922 dd6d609861f0
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6622432: RFE: Performance improvements to java.math.BigDecimal Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.math;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * A class used to represent multiprecision integers that makes efficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * use of allocated space by allowing a number to occupy only part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * an array so that the arrays do not have to be reallocated as often.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * When performing an operation with many iterations the array used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * hold a number is only reallocated when necessary and does not have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * be the same size as the number it represents. A mutable number allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * calculations to occur on the same number without having to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * a new number for every step of the calculation as occurs with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * BigIntegers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see     BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author  Michael McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    44
import java.util.Arrays;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    45
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    46
import static java.math.BigInteger.LONG_MASK;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    47
import static java.math.BigDecimal.INFLATED;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    48
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
class MutableBigInteger {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Holds the magnitude of this MutableBigInteger in big endian order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * The magnitude may start at an offset into the value array, and it may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * end before the length of the value array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    int[] value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * The number of ints of the value array that are currently used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * to hold the magnitude of this MutableBigInteger. The magnitude starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * at an offset and offset + intLen may be less than value.length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The offset into the value array where the magnitude of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * MutableBigInteger begins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    70
    // Constants
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    72
     * MutableBigInteger with one element value array with the value 1. Used by
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    73
     * BigDecimal divideAndRound to increment the quotient. Use this constant
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    74
     * only when the method is not going to modify this object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    76
    static final MutableBigInteger ONE = new MutableBigInteger(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * The default constructor. An empty MutableBigInteger is created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * a one word capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    MutableBigInteger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        value = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        intLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Construct a new MutableBigInteger with a magnitude specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * the int val.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    MutableBigInteger(int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        value = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        intLen = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        value[0] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Construct a new MutableBigInteger with the specified value array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * up to the length of the array supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    MutableBigInteger(int[] val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        value = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        intLen = val.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Construct a new MutableBigInteger with a magnitude equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * specified BigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    MutableBigInteger(BigInteger b) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   113
        intLen = b.mag.length;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   114
        value = Arrays.copyOf(b.mag, intLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Construct a new MutableBigInteger with a magnitude equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * specified MutableBigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    MutableBigInteger(MutableBigInteger val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        intLen = val.intLen;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   123
        value = Arrays.copyOfRange(val.value, val.offset, val.offset + intLen);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   124
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   125
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   126
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   127
     * Internal helper method to return the magnitude array. The caller is not
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   128
     * supposed to modify the returned array.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   129
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   130
    private int[] getMagnitudeArray() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   131
        if (offset > 0 || value.length != intLen)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   132
            return Arrays.copyOfRange(value, offset, offset + intLen);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   133
        return value;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   134
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   135
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   136
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   137
     * Convert this MutableBigInteger to a long value. The caller has to make
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   138
     * sure this MutableBigInteger can be fit into long.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   139
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   140
    private long toLong() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   141
        assert (intLen <= 2) : "this MutableBigInteger exceeds the range of long";
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   142
        if (intLen == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   143
            return 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   144
        long d = value[offset] & LONG_MASK;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   145
        return (intLen == 2) ? d << 32 | (value[offset + 1] & LONG_MASK) : d;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   146
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   148
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   149
     * Convert this MutableBigInteger to a BigInteger object.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   150
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   151
    BigInteger toBigInteger(int sign) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   152
        if (intLen == 0 || sign == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   153
            return BigInteger.ZERO;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   154
        return new BigInteger(getMagnitudeArray(), sign);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   155
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   156
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   157
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   158
     * Convert this MutableBigInteger to BigDecimal object with the specified sign
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   159
     * and scale.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   160
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   161
    BigDecimal toBigDecimal(int sign, int scale) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   162
        if (intLen == 0 || sign == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   163
            return BigDecimal.valueOf(0, scale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   164
        int[] mag = getMagnitudeArray();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   165
        int len = mag.length;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   166
        int d = mag[0];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   167
        // If this MutableBigInteger can't be fit into long, we need to
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   168
        // make a BigInteger object for the resultant BigDecimal object.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   169
        if (len > 2 || (d < 0 && len == 2))
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   170
            return new BigDecimal(new BigInteger(mag, sign), INFLATED, scale, 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   171
        long v = (len == 2) ?
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   172
            ((mag[1] & LONG_MASK) | (d & LONG_MASK) << 32) :
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   173
            d & LONG_MASK;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   174
        return new BigDecimal(null, sign == -1 ? -v : v, scale, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Clear out a MutableBigInteger for reuse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        offset = intLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        for (int index=0, n=value.length; index < n; index++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            value[index] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Set a MutableBigInteger to zero, removing its offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        offset = intLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Compare the magnitude of two MutableBigIntegers. Returns -1, 0 or 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * as this MutableBigInteger is numerically less than, equal to, or
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   196
     * greater than <tt>b</tt>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    final int compare(MutableBigInteger b) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   199
        int blen = b.intLen;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   200
        if (intLen < blen)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return -1;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   202
        if (intLen > blen)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   203
           return 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   205
        // Add Integer.MIN_VALUE to make the comparison act as unsigned integer
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   206
        // comparison.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   207
        int[] bval = b.value;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   208
        for (int i = offset, j = b.offset; i < intLen + offset; i++, j++) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   209
            int b1 = value[i] + 0x80000000;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   210
            int b2 = bval[j]  + 0x80000000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (b1 < b2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (b1 > b2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   220
     * Compare this against half of a MutableBigInteger object (Needed for
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   221
     * remainder tests).
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   222
     * Assumes no leading unnecessary zeros, which holds for results
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   223
     * from divide().
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   224
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   225
    final int compareHalf(MutableBigInteger b) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   226
        int blen = b.intLen;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   227
        int len = intLen;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   228
        if (len <= 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   229
            return blen <=0 ? 0 : -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   230
        if (len > blen)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   231
            return 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   232
        if (len < blen - 1)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   233
            return -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   234
        int[] bval = b.value;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   235
        int bstart = 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   236
        int carry = 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   237
        // Only 2 cases left:len == blen or len == blen - 1
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   238
        if (len != blen) { // len == blen - 1
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   239
            if (bval[bstart] == 1) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   240
                ++bstart;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   241
                carry = 0x80000000;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   242
            } else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   243
                return -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   244
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   245
        // compare values with right-shifted values of b,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   246
        // carrying shifted-out bits across words
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   247
        int[] val = value;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   248
        for (int i = offset, j = bstart; i < len + offset;) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   249
            int bv = bval[j++];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   250
            long hb = ((bv >>> 1) + carry) & LONG_MASK;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   251
            long v = val[i++] & LONG_MASK;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   252
            if (v != hb)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   253
                return v < hb ? -1 : 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   254
            carry = (bv & 1) << 31; // carray will be either 0x80000000 or 0
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   255
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   256
        return carry == 0? 0 : -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   257
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   258
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   259
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Return the index of the lowest set bit in this MutableBigInteger. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * magnitude of this MutableBigInteger is zero, -1 is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private final int getLowestSetBit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (intLen == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        int j, b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        for (j=intLen-1; (j>0) && (value[j+offset]==0); j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        b = value[j+offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (b==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            return -1;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   272
        return ((intLen-1-j)<<5) + Integer.numberOfTrailingZeros(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Return the int in use in this MutableBigInteger at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * index. This method is not used because it is not inlined on all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * platforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private final int getInt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return value[offset+index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Return a long which is equal to the unsigned value of the int in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * use in this MutableBigInteger at the specified index. This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * not used because it is not inlined on all platforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private final long getLong(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return value[offset+index] & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Ensure that the MutableBigInteger is in normal form, specifically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * making sure that there are no leading zeros, and that if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * magnitude is zero, then intLen is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    final void normalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (intLen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        int index = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (value[index] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        int indexBound = index+intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        } while(index < indexBound && value[index]==0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        int numZeros = index - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        intLen -= numZeros;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        offset = (intLen==0 ?  0 : offset+numZeros);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * If this MutableBigInteger cannot hold len words, increase the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * of the value array to len words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    private final void ensureCapacity(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (value.length < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            value = new int[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            intLen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Convert this MutableBigInteger into an int array with no leading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * zeros, of a length that is equal to this MutableBigInteger's intLen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    int[] toIntArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        int[] result = new int[intLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        for(int i=0; i<intLen; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            result[i] = value[offset+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Sets the int at index+offset in this MutableBigInteger to val.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * This does not get inlined on all platforms so it is not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * as often as originally intended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    void setInt(int index, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        value[offset + index] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Sets this MutableBigInteger's value array to the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * The intLen is set to the specified length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    void setValue(int[] val, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        value = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        intLen = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Sets this MutableBigInteger's value array to a copy of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * array. The intLen is set to the length of the new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   364
    void copyValue(MutableBigInteger src) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   365
        int len = src.intLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (value.length < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            value = new int[len];
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   368
        System.arraycopy(src.value, src.offset, value, 0, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        intLen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Sets this MutableBigInteger's value array to a copy of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * array. The intLen is set to the length of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    void copyValue(int[] val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        int len = val.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (value.length < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            value = new int[len];
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   381
        System.arraycopy(val, 0, value, 0, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        intLen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        offset = 0;
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
     * Returns true iff this MutableBigInteger has a value of one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    boolean isOne() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return (intLen == 1) && (value[offset] == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Returns true iff this MutableBigInteger has a value of zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    boolean isZero() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return (intLen == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Returns true iff this MutableBigInteger is even.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    boolean isEven() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return (intLen == 0) || ((value[offset + intLen - 1] & 1) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Returns true iff this MutableBigInteger is odd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    boolean isOdd() {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   411
        return isZero() ? false : ((value[offset + intLen - 1] & 1) == 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Returns true iff this MutableBigInteger is in normal form. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * MutableBigInteger is in normal form if it has no leading zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * after the offset, and intLen + offset <= value.length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    boolean isNormal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (intLen + offset > value.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (intLen ==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return (value[offset] != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Returns a String representation of this MutableBigInteger in radix 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public String toString() {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   431
        BigInteger b = toBigInteger(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return b.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Right shift this MutableBigInteger n bits. The MutableBigInteger is left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * in normal form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    void rightShift(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (intLen == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        int nInts = n >>> 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        int nBits = n & 0x1F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        this.intLen -= nInts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (nBits == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            return;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   447
        int bitsInHighWord = BigInteger.bitLengthForInt(value[offset]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (nBits >= bitsInHighWord) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            this.primitiveLeftShift(32 - nBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            this.intLen--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            primitiveRightShift(nBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Left shift this MutableBigInteger n bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    void leftShift(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
         * If there is enough storage space in this MutableBigInteger already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
         * the available space will be used. Space to the right of the used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         * ints in the value array is faster to utilize, so the extra space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         * will be taken from the right if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (intLen == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        int nInts = n >>> 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        int nBits = n&0x1F;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   470
        int bitsInHighWord = BigInteger.bitLengthForInt(value[offset]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        // If shift can be done without moving words, do so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (n <= (32-bitsInHighWord)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            primitiveLeftShift(nBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        int newLen = intLen + nInts +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (nBits <= (32-bitsInHighWord))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            newLen--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (value.length < newLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            // The array must grow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            int[] result = new int[newLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            for (int i=0; i<intLen; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                result[i] = value[offset+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            setValue(result, newLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        } else if (value.length - offset >= newLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            // Use space on right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            for(int i=0; i<newLen - intLen; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                value[offset+intLen+i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            // Must use space on left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            for (int i=0; i<intLen; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                value[i] = value[offset+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            for (int i=intLen; i<newLen; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                value[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        intLen = newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (nBits == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (nBits <= (32-bitsInHighWord))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            primitiveLeftShift(nBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            primitiveRightShift(32 -nBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * A primitive used for division. This method adds in one multiple of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * divisor a back to the dividend result at a specified offset. It is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * when qhat was estimated too large, and must be adjusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    private int divadd(int[] a, int[] result, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        long carry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        for (int j=a.length-1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            long sum = (a[j] & LONG_MASK) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                       (result[j+offset] & LONG_MASK) + carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            result[j+offset] = (int)sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            carry = sum >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        return (int)carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * This method is used for division. It multiplies an n word input a by one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * word input x, and subtracts the n word product from q. This is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * when subtracting qhat*divisor from dividend.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    private int mulsub(int[] q, int[] a, int x, int len, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        long xLong = x & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        long carry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        offset += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        for (int j=len-1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            long product = (a[j] & LONG_MASK) * xLong + carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            long difference = q[offset] - product;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            q[offset--] = (int)difference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            carry = (product >>> 32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                     + (((difference & LONG_MASK) >
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                         (((~(int)product) & LONG_MASK))) ? 1:0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        return (int)carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Right shift this MutableBigInteger n bits, where n is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * less than 32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Assumes that intLen > 0, n > 0 for speed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    private final void primitiveRightShift(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        int[] val = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        int n2 = 32 - n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        for (int i=offset+intLen-1, c=val[i]; i>offset; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            int b = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            c = val[i-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            val[i] = (c << n2) | (b >>> n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        val[offset] >>>= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Left shift this MutableBigInteger n bits, where n is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * less than 32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * Assumes that intLen > 0, n > 0 for speed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    private final void primitiveLeftShift(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        int[] val = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        int n2 = 32 - n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        for (int i=offset, c=val[i], m=i+intLen-1; i<m; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            int b = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            c = val[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            val[i] = (b << n) | (c >>> n2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        val[offset+intLen-1] <<= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Adds the contents of two MutableBigInteger objects.The result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * is placed within this MutableBigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * The contents of the addend are not changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    void add(MutableBigInteger addend) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        int x = intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        int y = addend.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        int resultLen = (intLen > addend.intLen ? intLen : addend.intLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        int[] result = (value.length < resultLen ? new int[resultLen] : value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        int rstart = result.length-1;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   590
        long sum;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   591
        long carry = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        // Add common parts of both numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        while(x>0 && y>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            x--; y--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            sum = (value[x+offset] & LONG_MASK) +
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   597
                (addend.value[y+addend.offset] & LONG_MASK) + carry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            result[rstart--] = (int)sum;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   599
            carry = sum >>> 32;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        // Add remainder of the longer number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        while(x>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            x--;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   605
            if (carry == 0 && result == value && rstart == (x + offset))
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   606
                return;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   607
            sum = (value[x+offset] & LONG_MASK) + carry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            result[rstart--] = (int)sum;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   609
            carry = sum >>> 32;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        while(y>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            y--;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   613
            sum = (addend.value[y+addend.offset] & LONG_MASK) + carry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            result[rstart--] = (int)sum;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   615
            carry = sum >>> 32;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   618
        if (carry > 0) { // Result must grow in length
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            resultLen++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            if (result.length < resultLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                int temp[] = new int[resultLen];
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   622
                // Result one word longer from carry-out; copy low-order
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   623
                // bits into new result.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   624
                System.arraycopy(result, 0, temp, 1, result.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                temp[0] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                result = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                result[rstart--] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        value = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        intLen = resultLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        offset = result.length - resultLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * Subtracts the smaller of this and b from the larger and places the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * result into this MutableBigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    int subtract(MutableBigInteger b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        MutableBigInteger a = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        int[] result = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        int sign = a.compare(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if (sign == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        if (sign < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            MutableBigInteger tmp = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            a = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            b = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        int resultLen = a.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if (result.length < resultLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            result = new int[resultLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        long diff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        int x = a.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        int y = b.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        int rstart = result.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        // Subtract common parts of both numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        while (y>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            x--; y--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            diff = (a.value[x+a.offset] & LONG_MASK) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                   (b.value[y+b.offset] & LONG_MASK) - ((int)-(diff>>32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            result[rstart--] = (int)diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        // Subtract remainder of longer number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        while (x>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            x--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            diff = (a.value[x+a.offset] & LONG_MASK) - ((int)-(diff>>32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            result[rstart--] = (int)diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        value = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        intLen = resultLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        offset = value.length - resultLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        normalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        return sign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * Subtracts the smaller of a and b from the larger and places the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * into the larger. Returns 1 if the answer is in a, -1 if in b, 0 if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * operation was performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    private int difference(MutableBigInteger b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        MutableBigInteger a = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        int sign = a.compare(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (sign ==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        if (sign < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            MutableBigInteger tmp = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            a = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            b = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        long diff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        int x = a.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        int y = b.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        // Subtract common parts of both numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        while (y>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            x--; y--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            diff = (a.value[a.offset+ x] & LONG_MASK) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                (b.value[b.offset+ y] & LONG_MASK) - ((int)-(diff>>32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            a.value[a.offset+x] = (int)diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        // Subtract remainder of longer number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        while (x>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            x--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            diff = (a.value[a.offset+ x] & LONG_MASK) - ((int)-(diff>>32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            a.value[a.offset+x] = (int)diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        a.normalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        return sign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * Multiply the contents of two MutableBigInteger objects. The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * placed into MutableBigInteger z. The contents of y are not changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    void multiply(MutableBigInteger y, MutableBigInteger z) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        int xLen = intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        int yLen = y.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        int newLen = xLen + yLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        // Put z into an appropriate state to receive product
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (z.value.length < newLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            z.value = new int[newLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        z.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        z.intLen = newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        // The first iteration is hoisted out of the loop to avoid extra add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        long carry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        for (int j=yLen-1, k=yLen+xLen-1; j >= 0; j--, k--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                long product = (y.value[j+y.offset] & LONG_MASK) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                               (value[xLen-1+offset] & LONG_MASK) + carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                z.value[k] = (int)product;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                carry = product >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        z.value[xLen-1] = (int)carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        // Perform the multiplication word by word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        for (int i = xLen-2; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            carry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            for (int j=yLen-1, k=yLen+i; j >= 0; j--, k--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                long product = (y.value[j+y.offset] & LONG_MASK) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                               (value[i+offset] & LONG_MASK) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                               (z.value[k] & LONG_MASK) + carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                z.value[k] = (int)product;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                carry = product >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            z.value[i] = (int)carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        // Remove leading zeros from product
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        z.normalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Multiply the contents of this MutableBigInteger by the word y. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * result is placed into z.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    void mul(int y, MutableBigInteger z) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        if (y == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            z.copyValue(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if (y == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            z.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        // Perform the multiplication word by word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        long ylong = y & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        int[] zval = (z.value.length<intLen+1 ? new int[intLen + 1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                                              : z.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        long carry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        for (int i = intLen-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            long product = ylong * (value[i+offset] & LONG_MASK) + carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            zval[i+1] = (int)product;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            carry = product >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        if (carry == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            z.offset = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            z.intLen = intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            z.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            z.intLen = intLen + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            zval[0] = (int)carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        z.value = zval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   806
     /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * This method is used for division of an n word dividend by a one word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * divisor. The quotient is placed into quotient. The one word divisor is
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   809
     * specified by divisor.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   810
     *
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   811
     * @return the remainder of the division is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   814
    int divideOneWord(int divisor, MutableBigInteger quotient) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   815
        long divisorLong = divisor & LONG_MASK;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        // Special case of one word dividend
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        if (intLen == 1) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   819
            long dividendValue = value[offset] & LONG_MASK;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   820
            int q = (int) (dividendValue / divisorLong);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   821
            int r = (int) (dividendValue - q * divisorLong);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   822
            quotient.value[0] = q;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   823
            quotient.intLen = (q == 0) ? 0 : 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            quotient.offset = 0;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   825
            return r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (quotient.value.length < intLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            quotient.value = new int[intLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        quotient.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        quotient.intLen = intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        // Normalize the divisor
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   834
        int shift = Integer.numberOfLeadingZeros(divisor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        int rem = value[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        long remLong = rem & LONG_MASK;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   838
        if (remLong < divisorLong) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            quotient.value[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        } else {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   841
            quotient.value[0] = (int)(remLong / divisorLong);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   842
            rem = (int) (remLong - (quotient.value[0] * divisorLong));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            remLong = rem & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        int xlen = intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        int[] qWord = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        while (--xlen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            long dividendEstimate = (remLong<<32) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                (value[offset + intLen - xlen] & LONG_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            if (dividendEstimate >= 0) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   852
                qWord[0] = (int) (dividendEstimate / divisorLong);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   853
                qWord[1] = (int) (dividendEstimate - qWord[0] * divisorLong);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                divWord(qWord, dividendEstimate, divisor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            quotient.value[intLen - xlen] = qWord[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            rem = qWord[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            remLong = rem & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   862
        quotient.normalize();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        // Unnormalize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        if (shift > 0)
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   865
            return rem % divisor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        else
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   867
            return rem;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    /**
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   871
     * Calculates the quotient of this div b and places the quotient in the
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   872
     * provided MutableBigInteger objects and the remainder object is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Uses Algorithm D in Knuth section 4.3.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * Many optimizations to that algorithm have been adapted from the Colin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * Plumb C library.
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   877
     * It special cases one word divisors for speed. The content of b is not
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   878
     * changed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   881
    MutableBigInteger divide(MutableBigInteger b, MutableBigInteger quotient) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        if (b.intLen == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            throw new ArithmeticException("BigInteger divide by zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        // Dividend is zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        if (intLen == 0) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   887
            quotient.intLen = quotient.offset;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   888
            return new MutableBigInteger();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        int cmp = compare(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        // Dividend less than divisor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        if (cmp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            quotient.intLen = quotient.offset = 0;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   895
            return new MutableBigInteger(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        // Dividend equal to divisor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        if (cmp == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            quotient.value[0] = quotient.intLen = 1;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   900
            quotient.offset = 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   901
            return new MutableBigInteger();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        quotient.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        // Special case one word divisor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        if (b.intLen == 1) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   907
            int r = divideOneWord(b.value[b.offset], quotient);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   908
            if (r == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   909
                return new MutableBigInteger();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   910
            return new MutableBigInteger(r);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        // Copy divisor value to protect divisor
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   914
        int[] div = Arrays.copyOfRange(b.value, b.offset, b.offset + b.intLen);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   915
        return divideMagnitude(div, quotient);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   916
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   917
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   918
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   919
     * Internally used  to calculate the quotient of this div v and places the
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   920
     * quotient in the provided MutableBigInteger object and the remainder is
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   921
     * returned.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   922
     *
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   923
     * @return the remainder of the division will be returned.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   924
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   925
    long divide(long v, MutableBigInteger quotient) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   926
        if (v == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   927
            throw new ArithmeticException("BigInteger divide by zero");
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   928
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   929
        // Dividend is zero
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   930
        if (intLen == 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   931
            quotient.intLen = quotient.offset = 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   932
            return 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   933
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   934
        if (v < 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   935
            v = -v;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   936
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   937
        int d = (int)(v >>> 32);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   938
        quotient.clear();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   939
        // Special case on word divisor
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   940
        if (d == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   941
            return divideOneWord((int)v, quotient) & LONG_MASK;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   942
        else {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   943
            int[] div = new int[]{ d, (int)(v & LONG_MASK) };
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   944
            return divideMagnitude(div, quotient).toLong();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   945
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   946
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   947
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   948
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   949
     * Divide this MutableBigInteger by the divisor represented by its magnitude
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   950
     * array. The quotient will be placed into the provided quotient object &
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   951
     * the remainder object is returned.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   952
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   953
    private MutableBigInteger divideMagnitude(int[] divisor,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   954
                                              MutableBigInteger quotient) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        // Remainder starts as dividend with space for a leading zero
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   957
        MutableBigInteger rem = new MutableBigInteger(new int[intLen + 1]);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   958
        System.arraycopy(value, offset, rem.value, 1, intLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        rem.intLen = intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        rem.offset = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        int nlen = rem.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        // Set the quotient size
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   965
        int dlen = divisor.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        int limit = nlen - dlen + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        if (quotient.value.length < limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            quotient.value = new int[limit];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            quotient.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        quotient.intLen = limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        int[] q = quotient.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        // D1 normalize the divisor
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   975
        int shift = Integer.numberOfLeadingZeros(divisor[0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        if (shift > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            // First shift will not grow array
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   978
            BigInteger.primitiveLeftShift(divisor, dlen, shift);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            // But this one might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            rem.leftShift(shift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        // Must insert leading 0 in rem if its length did not change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        if (rem.intLen == nlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            rem.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            rem.value[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            rem.intLen++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   990
        int dh = divisor[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        long dhLong = dh & LONG_MASK;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   992
        int dl = divisor[1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        int[] qWord = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        // D2 Initialize j
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        for(int j=0; j<limit; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            // D3 Calculate qhat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            // estimate qhat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            int qhat = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            int qrem = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            boolean skipCorrection = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            int nh = rem.value[j+rem.offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            int nh2 = nh + 0x80000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            int nm = rem.value[j+1+rem.offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            if (nh == dh) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                qhat = ~0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                qrem = nh + nm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                skipCorrection = qrem + 0x80000000 < nh2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                long nChunk = (((long)nh) << 32) | (nm & LONG_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                if (nChunk >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                    qhat = (int) (nChunk / dhLong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    qrem = (int) (nChunk - (qhat * dhLong));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                    divWord(qWord, nChunk, dh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    qhat = qWord[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                    qrem = qWord[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            if (qhat == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            if (!skipCorrection) { // Correct qhat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                long nl = rem.value[j+2+rem.offset] & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                long rs = ((qrem & LONG_MASK) << 32) | nl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                long estProduct = (dl & LONG_MASK) * (qhat & LONG_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                if (unsignedLongCompare(estProduct, rs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                    qhat--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                    qrem = (int)((qrem & LONG_MASK) + dhLong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                    if ((qrem & LONG_MASK) >=  dhLong) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1034
                        estProduct -= (dl & LONG_MASK);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                        rs = ((qrem & LONG_MASK) << 32) | nl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                        if (unsignedLongCompare(estProduct, rs))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                            qhat--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            // D4 Multiply and subtract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            rem.value[j+rem.offset] = 0;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1044
            int borrow = mulsub(rem.value, divisor, qhat, dlen, j+rem.offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            // D5 Test remainder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            if (borrow + 0x80000000 > nh2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                // D6 Add back
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1049
                divadd(divisor, rem.value, j+1+rem.offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                qhat--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            // Store the quotient digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            q[j] = qhat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        } // D7 loop on j
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        // D8 Unnormalize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        if (shift > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            rem.rightShift(shift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1061
        quotient.normalize();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        rem.normalize();
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1063
        return rem;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * Compare two longs as if they were unsigned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * Returns true iff one is bigger than two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    private boolean unsignedLongCompare(long one, long two) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        return (one+Long.MIN_VALUE) > (two+Long.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * This method divides a long quantity by an int to estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * qhat for two multi precision numbers. It is used when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * the signed value of n is less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    private void divWord(int[] result, long n, int d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        long dLong = d & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        if (dLong == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            result[0] = (int)n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            result[1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        // Approximate the quotient and remainder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        long q = (n >>> 1) / (dLong >>> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        long r = n - q*dLong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        // Correct the approximation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        while (r < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            r += dLong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            q--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        while (r >= dLong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            r -= dLong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            q++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        // n - q*dlong == r && 0 <= r <dLong, hence we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        result[0] = (int)q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        result[1] = (int)r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * Calculate GCD of this and b. This and b are changed by the computation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    MutableBigInteger hybridGCD(MutableBigInteger b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        // Use Euclid's algorithm until the numbers are approximately the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        // same length, then use the binary GCD algorithm to find the GCD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        MutableBigInteger a = this;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1114
        MutableBigInteger q = new MutableBigInteger();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        while (b.intLen != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            if (Math.abs(a.intLen - b.intLen) < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                return a.binaryGCD(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1120
            MutableBigInteger r = a.divide(b, q);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1121
            a = b;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1122
            b = r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * Calculate GCD of this and v.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * Assumes that this and v are not zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    private MutableBigInteger binaryGCD(MutableBigInteger v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        // Algorithm B from Knuth section 4.5.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        MutableBigInteger u = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        MutableBigInteger r = new MutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        // step B1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        int s1 = u.getLowestSetBit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        int s2 = v.getLowestSetBit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        int k = (s1 < s2) ? s1 : s2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        if (k != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            u.rightShift(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            v.rightShift(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        // step B2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        boolean uOdd = (k==s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        MutableBigInteger t = uOdd ? v: u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        int tsign = uOdd ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        int lb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        while ((lb = t.getLowestSetBit()) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            // steps B3 and B4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            t.rightShift(lb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            // step B5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            if (tsign > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                u = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                v = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            // Special case one word numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            if (u.intLen < 2 && v.intLen < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                int x = u.value[u.offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                int y = v.value[v.offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                x  = binaryGcd(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                r.value[0] = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                r.intLen = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                r.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                if (k > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                    r.leftShift(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            // step B6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            if ((tsign = u.difference(v)) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            t = (tsign >= 0) ? u : v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        if (k > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            u.leftShift(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * Calculate GCD of a and b interpreted as unsigned integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    static int binaryGcd(int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        if (b==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        if (a==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1193
        // Right shift a & b till their last bits equal to 1.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1194
        int aZeros = Integer.numberOfTrailingZeros(a);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1195
        int bZeros = Integer.numberOfTrailingZeros(b);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1196
        a >>>= aZeros;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1197
        b >>>= bZeros;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        int t = (aZeros < bZeros ? aZeros : bZeros);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        while (a != b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            if ((a+0x80000000) > (b+0x80000000)) {  // a > b as unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                a -= b;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1204
                a >>>= Integer.numberOfTrailingZeros(a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                b -= a;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1207
                b >>>= Integer.numberOfTrailingZeros(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        return a<<t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * Returns the modInverse of this mod p. This and p are not affected by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    MutableBigInteger mutableModInverse(MutableBigInteger p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        // Modulus is odd, use Schroeppel's algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        if (p.isOdd())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            return modInverse(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        // Base and modulus are even, throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        if (isEven())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            throw new ArithmeticException("BigInteger not invertible.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        // Get even part of modulus expressed as a power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        int powersOf2 = p.getLowestSetBit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        // Construct odd part of modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        MutableBigInteger oddMod = new MutableBigInteger(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        oddMod.rightShift(powersOf2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        if (oddMod.isOne())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            return modInverseMP2(powersOf2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        // Calculate 1/a mod oddMod
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        MutableBigInteger oddPart = modInverse(oddMod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        // Calculate 1/a mod evenMod
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        MutableBigInteger evenPart = modInverseMP2(powersOf2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        // Combine the results using Chinese Remainder Theorem
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        MutableBigInteger y1 = modInverseBP2(oddMod, powersOf2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        MutableBigInteger y2 = oddMod.modInverseMP2(powersOf2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        MutableBigInteger temp1 = new MutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        MutableBigInteger temp2 = new MutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        MutableBigInteger result = new MutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        oddPart.leftShift(powersOf2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        oddPart.multiply(y1, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        evenPart.multiply(oddMod, temp1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        temp1.multiply(y2, temp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        result.add(temp2);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1257
        return result.divide(p, temp1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * Calculate the multiplicative inverse of this mod 2^k.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    MutableBigInteger modInverseMP2(int k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        if (isEven())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            throw new ArithmeticException("Non-invertible. (GCD != 1)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        if (k > 64)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            return euclidModInverse(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        int t = inverseMod32(value[offset+intLen-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        if (k < 33) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            t = (k == 32 ? t : t & ((1 << k) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            return new MutableBigInteger(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        long pLong = (value[offset+intLen-1] & LONG_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        if (intLen > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            pLong |=  ((long)value[offset+intLen-2] << 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        long tLong = t & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        tLong = tLong * (2 - pLong * tLong);  // 1 more Newton iter step
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        tLong = (k == 64 ? tLong : tLong & ((1L << k) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        MutableBigInteger result = new MutableBigInteger(new int[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        result.value[0] = (int)(tLong >>> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        result.value[1] = (int)tLong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        result.intLen = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        result.normalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * Returns the multiplicative inverse of val mod 2^32.  Assumes val is odd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    static int inverseMod32(int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        // Newton's iteration!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        int t = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        t *= 2 - val*t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        t *= 2 - val*t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        t *= 2 - val*t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        t *= 2 - val*t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * Calculate the multiplicative inverse of 2^k mod mod, where mod is odd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    static MutableBigInteger modInverseBP2(MutableBigInteger mod, int k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        // Copy the mod to protect original
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        return fixup(new MutableBigInteger(1), new MutableBigInteger(mod), k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * Calculate the multiplicative inverse of this mod mod, where mod is odd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * This and mod are not changed by the calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * This method implements an algorithm due to Richard Schroeppel, that uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * the same intermediate representation as Montgomery Reduction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * ("Montgomery Form").  The algorithm is described in an unpublished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * manuscript entitled "Fast Modular Reciprocals."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    private MutableBigInteger modInverse(MutableBigInteger mod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        MutableBigInteger p = new MutableBigInteger(mod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        MutableBigInteger f = new MutableBigInteger(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        MutableBigInteger g = new MutableBigInteger(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        SignedMutableBigInteger c = new SignedMutableBigInteger(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        SignedMutableBigInteger d = new SignedMutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        MutableBigInteger temp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        SignedMutableBigInteger sTemp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        // Right shift f k times until odd, left shift d k times
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        if (f.isEven()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            int trailingZeros = f.getLowestSetBit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            f.rightShift(trailingZeros);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            d.leftShift(trailingZeros);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            k = trailingZeros;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        // The Almost Inverse Algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        while(!f.isOne()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            // If gcd(f, g) != 1, number is not invertible modulo mod
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            if (f.isZero())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                throw new ArithmeticException("BigInteger not invertible.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            // If f < g exchange f, g and c, d
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            if (f.compare(g) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                temp = f; f = g; g = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                sTemp = d; d = c; c = sTemp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            // If f == g (mod 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            if (((f.value[f.offset + f.intLen - 1] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                 g.value[g.offset + g.intLen - 1]) & 3) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                f.subtract(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                c.signedSubtract(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            } else { // If f != g (mod 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                f.add(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                c.signedAdd(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            // Right shift f k times until odd, left shift d k times
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            int trailingZeros = f.getLowestSetBit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            f.rightShift(trailingZeros);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            d.leftShift(trailingZeros);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            k += trailingZeros;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        while (c.sign < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
           c.signedAdd(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        return fixup(c, p, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * The Fixup Algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * Calculates X such that X = C * 2^(-k) (mod P)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * Assumes C<P and P is odd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    static MutableBigInteger fixup(MutableBigInteger c, MutableBigInteger p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                                                                      int k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        MutableBigInteger temp = new MutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        // Set r to the multiplicative inverse of p mod 2^32
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        int r = -inverseMod32(p.value[p.offset+p.intLen-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        for(int i=0, numWords = k >> 5; i<numWords; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            // V = R * c (mod 2^j)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            int  v = r * c.value[c.offset + c.intLen-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            // c = c + (v * p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            p.mul(v, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            c.add(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            // c = c / 2^j
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            c.intLen--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        int numBits = k & 0x1f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        if (numBits != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            // V = R * c (mod 2^j)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            int v = r * c.value[c.offset + c.intLen-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            v &= ((1<<numBits) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            // c = c + (v * p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            p.mul(v, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            c.add(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            // c = c / 2^j
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            c.rightShift(numBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        // In theory, c may be greater than p at this point (Very rare!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        while (c.compare(p) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            c.subtract(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * Uses the extended Euclidean algorithm to compute the modInverse of base
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * mod a modulus that is a power of 2. The modulus is 2^k.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    MutableBigInteger euclidModInverse(int k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        MutableBigInteger b = new MutableBigInteger(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        b.leftShift(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        MutableBigInteger mod = new MutableBigInteger(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        MutableBigInteger a = new MutableBigInteger(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        MutableBigInteger q = new MutableBigInteger();
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1425
        MutableBigInteger r = b.divide(a, q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1427
        MutableBigInteger swapper = b;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1428
        // swap b & r
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1429
        b = r;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1430
        r = swapper;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        MutableBigInteger t1 = new MutableBigInteger(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        MutableBigInteger t0 = new MutableBigInteger(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        MutableBigInteger temp = new MutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        while (!b.isOne()) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1437
            r = a.divide(b, q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            if (r.intLen == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                throw new ArithmeticException("BigInteger not invertible.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1442
            swapper = r;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1443
            a = swapper;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            if (q.intLen == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                t1.mul(q.value[q.offset], temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                q.multiply(t1, temp);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1449
            swapper = q;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1450
            q = temp;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1451
            temp = swapper;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            t0.add(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            if (a.isOne())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                return t0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1457
            r = b.divide(a, q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            if (r.intLen == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                throw new ArithmeticException("BigInteger not invertible.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1462
            swapper = b;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1463
            b =  r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            if (q.intLen == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                t0.mul(q.value[q.offset], temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                q.multiply(t0, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            swapper = q; q = temp; temp = swapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            t1.add(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        mod.subtract(t1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        return mod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
}