jdk/src/java.base/share/classes/java/math/MutableBigInteger.java
author coleenp
Wed, 30 Aug 2017 19:18:22 -0400
changeset 47098 e704f55561c3
parent 35302 e4d2275861c3
permissions -rw-r--r--
8164207: Checking missing load-acquire in relation to _pd_set in dictionary.cpp Summary: Use load_acquire for accessing DictionaryEntry::_pd_set since it's accessed outside the SystemDictionary_lock Reviewed-by: zgu, twisti, dholmes, adinn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
34538
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
     2
 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package 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
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    41
 * @author  Timothy Buktu
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    45
import static java.math.BigDecimal.INFLATED;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    46
import static java.math.BigInteger.LONG_MASK;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    47
import java.util.Arrays;
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
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    78
    /**
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    79
     * The minimum {@code intLen} for cancelling powers of two before
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    80
     * dividing.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    81
     * If the number of ints is less than this threshold,
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    82
     * {@code divideKnuth} does not eliminate common powers of two from
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    83
     * the dividend and divisor.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    84
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    85
    static final int KNUTH_POW2_THRESH_LEN = 6;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    86
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    87
    /**
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    88
     * The minimum number of trailing zero ints for cancelling powers of two
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    89
     * before dividing.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    90
     * If the dividend and divisor don't share at least this many zero ints
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    91
     * at the end, {@code divideKnuth} does not eliminate common powers
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    92
     * of two from the dividend and divisor.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    93
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    94
    static final int KNUTH_POW2_THRESH_ZEROS = 3;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
    95
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The default constructor. An empty MutableBigInteger is created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * a one word capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    MutableBigInteger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        value = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        intLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Construct a new MutableBigInteger with a magnitude specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * the int val.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    MutableBigInteger(int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        value = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        intLen = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        value[0] = val;
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 the specified value array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * up to the length of the array supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    MutableBigInteger(int[] val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        value = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        intLen = val.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Construct a new MutableBigInteger with a magnitude equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * specified BigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    MutableBigInteger(BigInteger b) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   131
        intLen = b.mag.length;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   132
        value = Arrays.copyOf(b.mag, intLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Construct a new MutableBigInteger with a magnitude equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * specified MutableBigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    MutableBigInteger(MutableBigInteger val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        intLen = val.intLen;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   141
        value = Arrays.copyOfRange(val.value, val.offset, val.offset + intLen);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   142
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   143
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   144
    /**
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   145
     * Makes this number an {@code n}-int number all of whose bits are ones.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   146
     * Used by Burnikel-Ziegler division.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   147
     * @param n number of ints in the {@code value} array
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   148
     * @return a number equal to {@code ((1<<(32*n)))-1}
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   149
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   150
    private void ones(int n) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   151
        if (n > value.length)
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   152
            value = new int[n];
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   153
        Arrays.fill(value, -1);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   154
        offset = 0;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   155
        intLen = n;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   156
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   157
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   158
    /**
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   159
     * 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
   160
     * supposed to modify the returned array.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   161
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   162
    private int[] getMagnitudeArray() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   163
        if (offset > 0 || value.length != intLen)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   164
            return Arrays.copyOfRange(value, offset, offset + intLen);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   165
        return value;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   166
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   167
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   168
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   169
     * 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
   170
     * sure this MutableBigInteger can be fit into long.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   171
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   172
    private long toLong() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   173
        assert (intLen <= 2) : "this MutableBigInteger exceeds the range of long";
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   174
        if (intLen == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   175
            return 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   176
        long d = value[offset] & LONG_MASK;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   177
        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
   178
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   180
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   181
     * Convert this MutableBigInteger to a BigInteger object.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   182
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   183
    BigInteger toBigInteger(int sign) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   184
        if (intLen == 0 || sign == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   185
            return BigInteger.ZERO;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   186
        return new BigInteger(getMagnitudeArray(), sign);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   187
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   188
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   189
    /**
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   190
     * Converts this number to a nonnegative {@code BigInteger}.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   191
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   192
    BigInteger toBigInteger() {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   193
        normalize();
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   194
        return toBigInteger(isZero() ? 0 : 1);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   195
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   196
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   197
    /**
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   198
     * Convert this MutableBigInteger to BigDecimal object with the specified sign
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   199
     * and scale.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   200
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   201
    BigDecimal toBigDecimal(int sign, int scale) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   202
        if (intLen == 0 || sign == 0)
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   203
            return BigDecimal.zeroValueOf(scale);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   204
        int[] mag = getMagnitudeArray();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   205
        int len = mag.length;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   206
        int d = mag[0];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   207
        // 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
   208
        // make a BigInteger object for the resultant BigDecimal object.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   209
        if (len > 2 || (d < 0 && len == 2))
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   210
            return new BigDecimal(new BigInteger(mag, sign), INFLATED, scale, 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   211
        long v = (len == 2) ?
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   212
            ((mag[1] & LONG_MASK) | (d & LONG_MASK) << 32) :
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   213
            d & LONG_MASK;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   214
        return BigDecimal.valueOf(sign == -1 ? -v : v, scale);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   215
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   216
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   217
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   218
     * This is for internal use in converting from a MutableBigInteger
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   219
     * object into a long value given a specified sign.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   220
     * returns INFLATED if value is not fit into long
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   221
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   222
    long toCompactValue(int sign) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   223
        if (intLen == 0 || sign == 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   224
            return 0L;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   225
        int[] mag = getMagnitudeArray();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   226
        int len = mag.length;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   227
        int d = mag[0];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   228
        // If this MutableBigInteger can not be fitted into long, we need to
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   229
        // make a BigInteger object for the resultant BigDecimal object.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   230
        if (len > 2 || (d < 0 && len == 2))
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   231
            return INFLATED;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   232
        long v = (len == 2) ?
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   233
            ((mag[1] & LONG_MASK) | (d & LONG_MASK) << 32) :
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   234
            d & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   235
        return sign == -1 ? -v : v;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Clear out a MutableBigInteger for reuse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        offset = intLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        for (int index=0, n=value.length; index < n; index++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            value[index] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Set a MutableBigInteger to zero, removing its offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        offset = intLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Compare the magnitude of two MutableBigIntegers. Returns -1, 0 or 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * as this MutableBigInteger is numerically less than, equal to, or
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31667
diff changeset
   257
     * greater than {@code b}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    final int compare(MutableBigInteger b) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   260
        int blen = b.intLen;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   261
        if (intLen < blen)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return -1;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   263
        if (intLen > blen)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   264
           return 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   266
        // 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
   267
        // comparison.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   268
        int[] bval = b.value;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   269
        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
   270
            int b1 = value[i] + 0x80000000;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   271
            int b2 = bval[j]  + 0x80000000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (b1 < b2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (b1 > b2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   281
     * Returns a value equal to what {@code b.leftShift(32*ints); return compare(b);}
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   282
     * would return, but doesn't change the value of {@code b}.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   283
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   284
    private int compareShifted(MutableBigInteger b, int ints) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   285
        int blen = b.intLen;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   286
        int alen = intLen - ints;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   287
        if (alen < blen)
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   288
            return -1;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   289
        if (alen > blen)
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   290
           return 1;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   291
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   292
        // Add Integer.MIN_VALUE to make the comparison act as unsigned integer
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   293
        // comparison.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   294
        int[] bval = b.value;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   295
        for (int i = offset, j = b.offset; i < alen + offset; i++, j++) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   296
            int b1 = value[i] + 0x80000000;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   297
            int b2 = bval[j]  + 0x80000000;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   298
            if (b1 < b2)
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   299
                return -1;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   300
            if (b1 > b2)
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   301
                return 1;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   302
        }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   303
        return 0;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   304
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   305
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   306
    /**
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   307
     * Compare this against half of a MutableBigInteger object (Needed for
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   308
     * remainder tests).
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   309
     * Assumes no leading unnecessary zeros, which holds for results
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   310
     * from divide().
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   311
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   312
    final int compareHalf(MutableBigInteger b) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   313
        int blen = b.intLen;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   314
        int len = intLen;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   315
        if (len <= 0)
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   316
            return blen <= 0 ? 0 : -1;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   317
        if (len > blen)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   318
            return 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   319
        if (len < blen - 1)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   320
            return -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   321
        int[] bval = b.value;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   322
        int bstart = 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   323
        int carry = 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   324
        // Only 2 cases left:len == blen or len == blen - 1
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   325
        if (len != blen) { // len == blen - 1
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   326
            if (bval[bstart] == 1) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   327
                ++bstart;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   328
                carry = 0x80000000;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   329
            } else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   330
                return -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   331
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   332
        // compare values with right-shifted values of b,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   333
        // carrying shifted-out bits across words
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   334
        int[] val = value;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   335
        for (int i = offset, j = bstart; i < len + offset;) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   336
            int bv = bval[j++];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   337
            long hb = ((bv >>> 1) + carry) & LONG_MASK;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   338
            long v = val[i++] & LONG_MASK;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   339
            if (v != hb)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   340
                return v < hb ? -1 : 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   341
            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
   342
        }
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   343
        return carry == 0 ? 0 : -1;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   344
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   345
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   346
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Return the index of the lowest set bit in this MutableBigInteger. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * magnitude of this MutableBigInteger is zero, -1 is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private final int getLowestSetBit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (intLen == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int j, b;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   354
        for (j=intLen-1; (j > 0) && (value[j+offset] == 0); j--)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        b = value[j+offset];
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   357
        if (b == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            return -1;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   359
        return ((intLen-1-j)<<5) + Integer.numberOfTrailingZeros(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Return the int in use in this MutableBigInteger at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * index. This method is not used because it is not inlined on all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * platforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private final int getInt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return value[offset+index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Return a long which is equal to the unsigned value of the int in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * use in this MutableBigInteger at the specified index. This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * not used because it is not inlined on all platforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    private final long getLong(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return value[offset+index] & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Ensure that the MutableBigInteger is in normal form, specifically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * making sure that there are no leading zeros, and that if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * magnitude is zero, then intLen is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    final void normalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (intLen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        int index = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (value[index] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        int indexBound = index+intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            index++;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   398
        } while(index < indexBound && value[index] == 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        int numZeros = index - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        intLen -= numZeros;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   402
        offset = (intLen == 0 ?  0 : offset+numZeros);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * If this MutableBigInteger cannot hold len words, increase the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * of the value array to len words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    private final void ensureCapacity(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (value.length < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            value = new int[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            intLen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Convert this MutableBigInteger into an int array with no leading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * zeros, of a length that is equal to this MutableBigInteger's intLen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    int[] toIntArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        int[] result = new int[intLen];
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   423
        for(int i=0; i < intLen; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            result[i] = value[offset+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Sets the int at index+offset in this MutableBigInteger to val.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * This does not get inlined on all platforms so it is not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * as often as originally intended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    void setInt(int index, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        value[offset + index] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Sets this MutableBigInteger's value array to the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * The intLen is set to the specified length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    void setValue(int[] val, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        value = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        intLen = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Sets this MutableBigInteger's value array to a copy of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * array. The intLen is set to the length of the new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   451
    void copyValue(MutableBigInteger src) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   452
        int len = src.intLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (value.length < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            value = new int[len];
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   455
        System.arraycopy(src.value, src.offset, value, 0, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        intLen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Sets this MutableBigInteger's value array to a copy of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * array. The intLen is set to the length of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    void copyValue(int[] val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        int len = val.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (value.length < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            value = new int[len];
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   468
        System.arraycopy(val, 0, value, 0, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        intLen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Returns true iff this MutableBigInteger has a value of one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    boolean isOne() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        return (intLen == 1) && (value[offset] == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * Returns true iff this MutableBigInteger has a value of zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    boolean isZero() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return (intLen == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Returns true iff this MutableBigInteger is even.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    boolean isEven() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return (intLen == 0) || ((value[offset + intLen - 1] & 1) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * Returns true iff this MutableBigInteger is odd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    boolean isOdd() {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   498
        return isZero() ? false : ((value[offset + intLen - 1] & 1) == 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Returns true iff this MutableBigInteger is in normal form. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * MutableBigInteger is in normal form if it has no leading zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * after the offset, and intLen + offset <= value.length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    boolean isNormal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (intLen + offset > value.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            return false;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   509
        if (intLen == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        return (value[offset] != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Returns a String representation of this MutableBigInteger in radix 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public String toString() {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   518
        BigInteger b = toBigInteger(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return b.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   523
     * Like {@link #rightShift(int)} but {@code n} can be greater than the length of the number.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   524
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   525
    void safeRightShift(int n) {
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   526
        if (n/32 >= intLen) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   527
            reset();
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   528
        } else {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   529
            rightShift(n);
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   530
        }
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   531
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   532
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   533
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * Right shift this MutableBigInteger n bits. The MutableBigInteger is left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * in normal form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    void rightShift(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (intLen == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        int nInts = n >>> 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        int nBits = n & 0x1F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        this.intLen -= nInts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (nBits == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            return;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   545
        int bitsInHighWord = BigInteger.bitLengthForInt(value[offset]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (nBits >= bitsInHighWord) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            this.primitiveLeftShift(32 - nBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            this.intLen--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            primitiveRightShift(nBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   555
     * Like {@link #leftShift(int)} but {@code n} can be zero.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   556
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   557
    void safeLeftShift(int n) {
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   558
        if (n > 0) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   559
            leftShift(n);
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   560
        }
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   561
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   562
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   563
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * Left shift this MutableBigInteger n bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    void leftShift(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
         * If there is enough storage space in this MutableBigInteger already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
         * the available space will be used. Space to the right of the used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
         * ints in the value array is faster to utilize, so the extra space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
         * will be taken from the right if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (intLen == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        int nInts = n >>> 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        int nBits = n&0x1F;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   577
        int bitsInHighWord = BigInteger.bitLengthForInt(value[offset]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        // If shift can be done without moving words, do so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (n <= (32-bitsInHighWord)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            primitiveLeftShift(nBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        int newLen = intLen + nInts +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if (nBits <= (32-bitsInHighWord))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            newLen--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (value.length < newLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            // The array must grow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            int[] result = new int[newLen];
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   591
            for (int i=0; i < intLen; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                result[i] = value[offset+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            setValue(result, newLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        } else if (value.length - offset >= newLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            // Use space on right
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   596
            for(int i=0; i < newLen - intLen; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                value[offset+intLen+i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            // Must use space on left
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   600
            for (int i=0; i < intLen; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                value[i] = value[offset+i];
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   602
            for (int i=intLen; i < newLen; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                value[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        intLen = newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        if (nBits == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        if (nBits <= (32-bitsInHighWord))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            primitiveLeftShift(nBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            primitiveRightShift(32 -nBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * A primitive used for division. This method adds in one multiple of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * divisor a back to the dividend result at a specified offset. It is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * when qhat was estimated too large, and must be adjusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    private int divadd(int[] a, int[] result, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        long carry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        for (int j=a.length-1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            long sum = (a[j] & LONG_MASK) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                       (result[j+offset] & LONG_MASK) + carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            result[j+offset] = (int)sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            carry = sum >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        return (int)carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * This method is used for division. It multiplies an n word input a by one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * word input x, and subtracts the n word product from q. This is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * when subtracting qhat*divisor from dividend.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    private int mulsub(int[] q, int[] a, int x, int len, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        long xLong = x & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        long carry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        offset += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        for (int j=len-1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            long product = (a[j] & LONG_MASK) * xLong + carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            long difference = q[offset] - product;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            q[offset--] = (int)difference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            carry = (product >>> 32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                     + (((difference & LONG_MASK) >
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                         (((~(int)product) & LONG_MASK))) ? 1:0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        return (int)carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   654
     * The method is the same as mulsun, except the fact that q array is not
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   655
     * updated, the only result of the method is borrow flag.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   656
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   657
    private int mulsubBorrow(int[] q, int[] a, int x, int len, int offset) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   658
        long xLong = x & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   659
        long carry = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   660
        offset += len;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   661
        for (int j=len-1; j >= 0; j--) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   662
            long product = (a[j] & LONG_MASK) * xLong + carry;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   663
            long difference = q[offset--] - product;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   664
            carry = (product >>> 32)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   665
                     + (((difference & LONG_MASK) >
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   666
                         (((~(int)product) & LONG_MASK))) ? 1:0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   667
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   668
        return (int)carry;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   669
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   670
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
   671
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * Right shift this MutableBigInteger n bits, where n is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * less than 32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * Assumes that intLen > 0, n > 0 for speed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    private final void primitiveRightShift(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        int[] val = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        int n2 = 32 - n;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   679
        for (int i=offset+intLen-1, c=val[i]; i > offset; i--) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            int b = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            c = val[i-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            val[i] = (c << n2) | (b >>> n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        val[offset] >>>= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Left shift this MutableBigInteger n bits, where n is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * less than 32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * Assumes that intLen > 0, n > 0 for speed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    private final void primitiveLeftShift(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        int[] val = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        int n2 = 32 - n;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   695
        for (int i=offset, c=val[i], m=i+intLen-1; i < m; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            int b = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            c = val[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            val[i] = (b << n) | (c >>> n2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        val[offset+intLen-1] <<= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /**
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   704
     * Returns a {@code BigInteger} equal to the {@code n}
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   705
     * low ints of this number.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   706
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   707
    private BigInteger getLower(int n) {
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   708
        if (isZero()) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   709
            return BigInteger.ZERO;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   710
        } else if (intLen < n) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   711
            return toBigInteger(1);
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   712
        } else {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   713
            // strip zeros
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   714
            int len = n;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   715
            while (len > 0 && value[offset+intLen-len] == 0)
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   716
                len--;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   717
            int sign = len > 0 ? 1 : 0;
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   718
            return new BigInteger(Arrays.copyOfRange(value, offset+intLen-len, offset+intLen), sign);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   719
        }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   720
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   721
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   722
    /**
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   723
     * Discards all ints whose index is greater than {@code n}.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   724
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   725
    private void keepLower(int n) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   726
        if (intLen >= n) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   727
            offset += intLen - n;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   728
            intLen = n;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   729
        }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   730
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   731
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   732
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * Adds the contents of two MutableBigInteger objects.The result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * is placed within this MutableBigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * The contents of the addend are not changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    void add(MutableBigInteger addend) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        int x = intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        int y = addend.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        int resultLen = (intLen > addend.intLen ? intLen : addend.intLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        int[] result = (value.length < resultLen ? new int[resultLen] : value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        int rstart = result.length-1;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   744
        long sum;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   745
        long carry = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        // Add common parts of both numbers
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   748
        while(x > 0 && y > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            x--; y--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            sum = (value[x+offset] & LONG_MASK) +
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   751
                (addend.value[y+addend.offset] & LONG_MASK) + carry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            result[rstart--] = (int)sum;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   753
            carry = sum >>> 32;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        // Add remainder of the longer number
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   757
        while(x > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            x--;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   759
            if (carry == 0 && result == value && rstart == (x + offset))
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   760
                return;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   761
            sum = (value[x+offset] & LONG_MASK) + carry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            result[rstart--] = (int)sum;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   763
            carry = sum >>> 32;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   765
        while(y > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            y--;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   767
            sum = (addend.value[y+addend.offset] & LONG_MASK) + carry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            result[rstart--] = (int)sum;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   769
            carry = sum >>> 32;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   772
        if (carry > 0) { // Result must grow in length
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            resultLen++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            if (result.length < resultLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                int temp[] = new int[resultLen];
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   776
                // Result one word longer from carry-out; copy low-order
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   777
                // bits into new result.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   778
                System.arraycopy(result, 0, temp, 1, result.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                temp[0] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                result = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                result[rstart--] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        value = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        intLen = resultLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        offset = result.length - resultLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   791
    /**
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   792
     * Adds the value of {@code addend} shifted {@code n} ints to the left.
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   793
     * Has the same effect as {@code addend.leftShift(32*ints); add(addend);}
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   794
     * but doesn't change the value of {@code addend}.
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   795
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   796
    void addShifted(MutableBigInteger addend, int n) {
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   797
        if (addend.isZero()) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   798
            return;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   799
        }
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   800
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   801
        int x = intLen;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   802
        int y = addend.intLen + n;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   803
        int resultLen = (intLen > y ? intLen : y);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   804
        int[] result = (value.length < resultLen ? new int[resultLen] : value);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   805
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   806
        int rstart = result.length-1;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   807
        long sum;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   808
        long carry = 0;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   809
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   810
        // Add common parts of both numbers
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   811
        while (x > 0 && y > 0) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   812
            x--; y--;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   813
            int bval = y+addend.offset < addend.value.length ? addend.value[y+addend.offset] : 0;
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   814
            sum = (value[x+offset] & LONG_MASK) +
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   815
                (bval & LONG_MASK) + carry;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   816
            result[rstart--] = (int)sum;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   817
            carry = sum >>> 32;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   818
        }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   819
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   820
        // Add remainder of the longer number
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   821
        while (x > 0) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   822
            x--;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   823
            if (carry == 0 && result == value && rstart == (x + offset)) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   824
                return;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   825
            }
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   826
            sum = (value[x+offset] & LONG_MASK) + carry;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   827
            result[rstart--] = (int)sum;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   828
            carry = sum >>> 32;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   829
        }
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   830
        while (y > 0) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   831
            y--;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   832
            int bval = y+addend.offset < addend.value.length ? addend.value[y+addend.offset] : 0;
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   833
            sum = (bval & LONG_MASK) + carry;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   834
            result[rstart--] = (int)sum;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   835
            carry = sum >>> 32;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   836
        }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   837
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   838
        if (carry > 0) { // Result must grow in length
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   839
            resultLen++;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   840
            if (result.length < resultLen) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   841
                int temp[] = new int[resultLen];
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   842
                // Result one word longer from carry-out; copy low-order
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   843
                // bits into new result.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   844
                System.arraycopy(result, 0, temp, 1, result.length);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   845
                temp[0] = 1;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   846
                result = temp;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   847
            } else {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   848
                result[rstart--] = 1;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   849
            }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   850
        }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   851
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   852
        value = result;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   853
        intLen = resultLen;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   854
        offset = result.length - resultLen;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   855
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   856
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   857
    /**
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   858
     * Like {@link #addShifted(MutableBigInteger, int)} but {@code this.intLen} must
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   859
     * not be greater than {@code n}. In other words, concatenates {@code this}
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   860
     * and {@code addend}.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   861
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   862
    void addDisjoint(MutableBigInteger addend, int n) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   863
        if (addend.isZero())
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   864
            return;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   865
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   866
        int x = intLen;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   867
        int y = addend.intLen + n;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   868
        int resultLen = (intLen > y ? intLen : y);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   869
        int[] result;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   870
        if (value.length < resultLen)
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   871
            result = new int[resultLen];
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   872
        else {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   873
            result = value;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   874
            Arrays.fill(value, offset+intLen, value.length, 0);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   875
        }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   876
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   877
        int rstart = result.length-1;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   878
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   879
        // copy from this if needed
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   880
        System.arraycopy(value, offset, result, rstart+1-x, x);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   881
        y -= x;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   882
        rstart -= x;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   883
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   884
        int len = Math.min(y, addend.value.length-addend.offset);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   885
        System.arraycopy(addend.value, addend.offset, result, rstart+1-y, len);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   886
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   887
        // zero the gap
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   888
        for (int i=rstart+1-y+len; i < rstart+1; i++)
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   889
            result[i] = 0;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   890
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   891
        value = result;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   892
        intLen = resultLen;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   893
        offset = result.length - resultLen;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   894
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   895
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   896
    /**
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   897
     * Adds the low {@code n} ints of {@code addend}.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   898
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   899
    void addLower(MutableBigInteger addend, int n) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   900
        MutableBigInteger a = new MutableBigInteger(addend);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   901
        if (a.offset + a.intLen >= n) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   902
            a.offset = a.offset + a.intLen - n;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   903
            a.intLen = n;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   904
        }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   905
        a.normalize();
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   906
        add(a);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
   907
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * Subtracts the smaller of this and b from the larger and places the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * result into this MutableBigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    int subtract(MutableBigInteger b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        MutableBigInteger a = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        int[] result = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        int sign = a.compare(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        if (sign == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        if (sign < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            MutableBigInteger tmp = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            a = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            b = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        int resultLen = a.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        if (result.length < resultLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            result = new int[resultLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        long diff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        int x = a.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        int y = b.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        int rstart = result.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        // Subtract common parts of both numbers
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   939
        while (y > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            x--; y--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            diff = (a.value[x+a.offset] & LONG_MASK) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                   (b.value[y+b.offset] & LONG_MASK) - ((int)-(diff>>32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            result[rstart--] = (int)diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        // Subtract remainder of longer number
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   947
        while (x > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            x--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            diff = (a.value[x+a.offset] & LONG_MASK) - ((int)-(diff>>32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            result[rstart--] = (int)diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        value = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        intLen = resultLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        offset = value.length - resultLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        normalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        return sign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Subtracts the smaller of a and b from the larger and places the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * 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
   963
     * operation was performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    private int difference(MutableBigInteger b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        MutableBigInteger a = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        int sign = a.compare(b);
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   968
        if (sign == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        if (sign < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            MutableBigInteger tmp = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            a = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            b = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        long diff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        int x = a.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        int y = b.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        // Subtract common parts of both numbers
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   981
        while (y > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            x--; y--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            diff = (a.value[a.offset+ x] & LONG_MASK) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                (b.value[b.offset+ y] & LONG_MASK) - ((int)-(diff>>32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            a.value[a.offset+x] = (int)diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        // Subtract remainder of longer number
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
   988
        while (x > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            x--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            diff = (a.value[a.offset+ x] & LONG_MASK) - ((int)-(diff>>32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            a.value[a.offset+x] = (int)diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        a.normalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        return sign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * Multiply the contents of two MutableBigInteger objects. The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * placed into MutableBigInteger z. The contents of y are not changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    void multiply(MutableBigInteger y, MutableBigInteger z) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        int xLen = intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        int yLen = y.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        int newLen = xLen + yLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        // Put z into an appropriate state to receive product
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        if (z.value.length < newLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            z.value = new int[newLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        z.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        z.intLen = newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        // The first iteration is hoisted out of the loop to avoid extra add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        long carry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        for (int j=yLen-1, k=yLen+xLen-1; j >= 0; j--, k--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                long product = (y.value[j+y.offset] & LONG_MASK) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                               (value[xLen-1+offset] & LONG_MASK) + carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                z.value[k] = (int)product;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                carry = product >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        z.value[xLen-1] = (int)carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        // Perform the multiplication word by word
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        for (int i = xLen-2; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            carry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            for (int j=yLen-1, k=yLen+i; j >= 0; j--, k--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                long product = (y.value[j+y.offset] & LONG_MASK) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                               (value[i+offset] & LONG_MASK) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                               (z.value[k] & LONG_MASK) + carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                z.value[k] = (int)product;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                carry = product >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            z.value[i] = (int)carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        // Remove leading zeros from product
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        z.normalize();
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
     * Multiply the contents of this MutableBigInteger by the word y. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * result is placed into z.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    void mul(int y, MutableBigInteger z) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        if (y == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            z.copyValue(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        if (y == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            z.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        // Perform the multiplication word by word
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        long ylong = y & LONG_MASK;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1057
        int[] zval = (z.value.length < intLen+1 ? new int[intLen + 1]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                                              : z.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        long carry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        for (int i = intLen-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            long product = ylong * (value[i+offset] & LONG_MASK) + carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            zval[i+1] = (int)product;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            carry = product >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        if (carry == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            z.offset = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            z.intLen = intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            z.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            z.intLen = intLen + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            zval[0] = (int)carry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        z.value = zval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1077
     /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * This method is used for division of an n word dividend by a one word
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * 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
  1080
     * specified by divisor.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1081
     *
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1082
     * @return the remainder of the division is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1085
    int divideOneWord(int divisor, MutableBigInteger quotient) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1086
        long divisorLong = divisor & LONG_MASK;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        // Special case of one word dividend
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        if (intLen == 1) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1090
            long dividendValue = value[offset] & LONG_MASK;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1091
            int q = (int) (dividendValue / divisorLong);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1092
            int r = (int) (dividendValue - q * divisorLong);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1093
            quotient.value[0] = q;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1094
            quotient.intLen = (q == 0) ? 0 : 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            quotient.offset = 0;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1096
            return r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        if (quotient.value.length < intLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            quotient.value = new int[intLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        quotient.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        quotient.intLen = intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        // Normalize the divisor
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1105
        int shift = Integer.numberOfLeadingZeros(divisor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        int rem = value[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        long remLong = rem & LONG_MASK;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1109
        if (remLong < divisorLong) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            quotient.value[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        } else {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1112
            quotient.value[0] = (int)(remLong / divisorLong);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1113
            rem = (int) (remLong - (quotient.value[0] * divisorLong));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            remLong = rem & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        int xlen = intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        while (--xlen > 0) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1118
            long dividendEstimate = (remLong << 32) |
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1119
                    (value[offset + intLen - xlen] & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1120
            int q;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            if (dividendEstimate >= 0) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1122
                q = (int) (dividendEstimate / divisorLong);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1123
                rem = (int) (dividendEstimate - q * divisorLong);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            } else {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1125
                long tmp = divWord(dividendEstimate, divisor);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1126
                q = (int) (tmp & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1127
                rem = (int) (tmp >>> 32);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            }
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1129
            quotient.value[intLen - xlen] = q;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            remLong = rem & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1133
        quotient.normalize();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        // Unnormalize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        if (shift > 0)
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1136
            return rem % divisor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        else
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1138
            return rem;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    /**
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1142
     * 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
  1143
     * provided MutableBigInteger objects and the remainder object is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     *
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1145
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1146
    MutableBigInteger divide(MutableBigInteger b, MutableBigInteger quotient) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1147
        return divide(b,quotient,true);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1148
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1149
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1150
    MutableBigInteger divide(MutableBigInteger b, MutableBigInteger quotient, boolean needRemainder) {
21984
4dff16390b67 8029501: BigInteger division algorithm selection heuristic is incorrect
bpb
parents: 21420
diff changeset
  1151
        if (b.intLen < BigInteger.BURNIKEL_ZIEGLER_THRESHOLD ||
4dff16390b67 8029501: BigInteger division algorithm selection heuristic is incorrect
bpb
parents: 21420
diff changeset
  1152
                intLen - b.intLen < BigInteger.BURNIKEL_ZIEGLER_OFFSET) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1153
            return divideKnuth(b, quotient, needRemainder);
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1154
        } else {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1155
            return divideAndRemainderBurnikelZiegler(b, quotient);
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1156
        }
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1157
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1158
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1159
    /**
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1160
     * @see #divideKnuth(MutableBigInteger, MutableBigInteger, boolean)
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1161
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1162
    MutableBigInteger divideKnuth(MutableBigInteger b, MutableBigInteger quotient) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1163
        return divideKnuth(b,quotient,true);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1164
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1165
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1166
    /**
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1167
     * Calculates the quotient of this div b and places the quotient in the
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1168
     * provided MutableBigInteger objects and the remainder object is returned.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1169
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * Uses Algorithm D in Knuth section 4.3.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * Many optimizations to that algorithm have been adapted from the Colin
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * Plumb C library.
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1173
     * 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
  1174
     * changed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     */
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1177
    MutableBigInteger divideKnuth(MutableBigInteger b, MutableBigInteger quotient, boolean needRemainder) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        if (b.intLen == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            throw new ArithmeticException("BigInteger divide by zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        // Dividend is zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        if (intLen == 0) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1183
            quotient.intLen = quotient.offset = 0;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1184
            return needRemainder ? new MutableBigInteger() : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        int cmp = compare(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        // Dividend less than divisor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        if (cmp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            quotient.intLen = quotient.offset = 0;
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1191
            return needRemainder ? new MutableBigInteger(this) : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        // Dividend equal to divisor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        if (cmp == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            quotient.value[0] = quotient.intLen = 1;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1196
            quotient.offset = 0;
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1197
            return needRemainder ? new MutableBigInteger() : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        quotient.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        // Special case one word divisor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        if (b.intLen == 1) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1203
            int r = divideOneWord(b.value[b.offset], quotient);
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1204
            if(needRemainder) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1205
                if (r == 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1206
                    return new MutableBigInteger();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1207
                return new MutableBigInteger(r);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1208
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1209
                return null;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1210
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        }
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1212
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1213
        // Cancel common powers of two if we're above the KNUTH_POW2_* thresholds
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1214
        if (intLen >= KNUTH_POW2_THRESH_LEN) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1215
            int trailingZeroBits = Math.min(getLowestSetBit(), b.getLowestSetBit());
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1216
            if (trailingZeroBits >= KNUTH_POW2_THRESH_ZEROS*32) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1217
                MutableBigInteger a = new MutableBigInteger(this);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1218
                b = new MutableBigInteger(b);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1219
                a.rightShift(trailingZeroBits);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1220
                b.rightShift(trailingZeroBits);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1221
                MutableBigInteger r = a.divideKnuth(b, quotient);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1222
                r.leftShift(trailingZeroBits);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1223
                return r;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1224
            }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1225
        }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1226
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1227
        return divideMagnitude(b, quotient, needRemainder);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1228
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1229
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1230
    /**
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1231
     * Computes {@code this/b} and {@code this%b} using the
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1232
     * <a href="http://cr.yp.to/bib/1998/burnikel.ps"> Burnikel-Ziegler algorithm</a>.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1233
     * This method implements algorithm 3 from pg. 9 of the Burnikel-Ziegler paper.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1234
     * The parameter beta was chosen to b 2<sup>32</sup> so almost all shifts are
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1235
     * multiples of 32 bits.<br/>
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1236
     * {@code this} and {@code b} must be nonnegative.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1237
     * @param b the divisor
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1238
     * @param quotient output parameter for {@code this/b}
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1239
     * @return the remainder
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1240
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1241
    MutableBigInteger divideAndRemainderBurnikelZiegler(MutableBigInteger b, MutableBigInteger quotient) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1242
        int r = intLen;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1243
        int s = b.intLen;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1244
19393
d99a9ebf9c10 8022180: BigInteger Burnikel-Ziegler quotient and remainder calculation assumes quotient parameter is zero
bpb
parents: 19061
diff changeset
  1245
        // Clear the quotient
d99a9ebf9c10 8022180: BigInteger Burnikel-Ziegler quotient and remainder calculation assumes quotient parameter is zero
bpb
parents: 19061
diff changeset
  1246
        quotient.offset = quotient.intLen = 0;
d99a9ebf9c10 8022180: BigInteger Burnikel-Ziegler quotient and remainder calculation assumes quotient parameter is zero
bpb
parents: 19061
diff changeset
  1247
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1248
        if (r < s) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1249
            return this;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1250
        } else {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1251
            // Unlike Knuth division, we don't check for common powers of two here because
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1252
            // BZ already runs faster if both numbers contain powers of two and cancelling them has no
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1253
            // additional benefit.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1254
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1255
            // step 1: let m = min{2^k | (2^k)*BURNIKEL_ZIEGLER_THRESHOLD > s}
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1256
            int m = 1 << (32-Integer.numberOfLeadingZeros(s/BigInteger.BURNIKEL_ZIEGLER_THRESHOLD));
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1257
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1258
            int j = (s+m-1) / m;      // step 2a: j = ceil(s/m)
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1259
            int n = j * m;            // step 2b: block length in 32-bit units
21420
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents: 19393
diff changeset
  1260
            long n32 = 32L * n;         // block length in bits
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents: 19393
diff changeset
  1261
            int sigma = (int) Math.max(0, n32 - b.bitLength());   // step 3: sigma = max{T | (2^T)*B < beta^n}
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1262
            MutableBigInteger bShifted = new MutableBigInteger(b);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1263
            bShifted.safeLeftShift(sigma);   // step 4a: shift b so its length is a multiple of n
26620
6c427d708df2 8057793: BigDecimal is no longer effectively immutable
bpb
parents: 25859
diff changeset
  1264
            MutableBigInteger aShifted = new MutableBigInteger (this);
6c427d708df2 8057793: BigDecimal is no longer effectively immutable
bpb
parents: 25859
diff changeset
  1265
            aShifted.safeLeftShift(sigma);     // step 4b: shift a by the same amount
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1266
26620
6c427d708df2 8057793: BigDecimal is no longer effectively immutable
bpb
parents: 25859
diff changeset
  1267
            // step 5: t is the number of blocks needed to accommodate a plus one additional bit
6c427d708df2 8057793: BigDecimal is no longer effectively immutable
bpb
parents: 25859
diff changeset
  1268
            int t = (int) ((aShifted.bitLength()+n32) / n32);
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1269
            if (t < 2) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1270
                t = 2;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1271
            }
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1272
26620
6c427d708df2 8057793: BigDecimal is no longer effectively immutable
bpb
parents: 25859
diff changeset
  1273
            // step 6: conceptually split a into blocks a[t-1], ..., a[0]
6c427d708df2 8057793: BigDecimal is no longer effectively immutable
bpb
parents: 25859
diff changeset
  1274
            MutableBigInteger a1 = aShifted.getBlock(t-1, t, n);   // the most significant block of a
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1275
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1276
            // step 7: z[t-2] = [a[t-1], a[t-2]]
26620
6c427d708df2 8057793: BigDecimal is no longer effectively immutable
bpb
parents: 25859
diff changeset
  1277
            MutableBigInteger z = aShifted.getBlock(t-2, t, n);    // the second to most significant block
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1278
            z.addDisjoint(a1, n);   // z[t-2]
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1279
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1280
            // do schoolbook division on blocks, dividing 2-block numbers by 1-block numbers
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1281
            MutableBigInteger qi = new MutableBigInteger();
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1282
            MutableBigInteger ri;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1283
            for (int i=t-2; i > 0; i--) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1284
                // step 8a: compute (qi,ri) such that z=b*qi+ri
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1285
                ri = z.divide2n1n(bShifted, qi);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1286
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1287
                // step 8b: z = [ri, a[i-1]]
26620
6c427d708df2 8057793: BigDecimal is no longer effectively immutable
bpb
parents: 25859
diff changeset
  1288
                z = aShifted.getBlock(i-1, t, n);   // a[i-1]
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1289
                z.addDisjoint(ri, n);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1290
                quotient.addShifted(qi, i*n);   // update q (part of step 9)
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1291
            }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1292
            // final iteration of step 8: do the loop one more time for i=0 but leave z unchanged
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1293
            ri = z.divide2n1n(bShifted, qi);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1294
            quotient.add(qi);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1295
26620
6c427d708df2 8057793: BigDecimal is no longer effectively immutable
bpb
parents: 25859
diff changeset
  1296
            ri.rightShift(sigma);   // step 9: a and b were shifted, so shift back
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1297
            return ri;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1298
        }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1299
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1300
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1301
    /**
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1302
     * This method implements algorithm 1 from pg. 4 of the Burnikel-Ziegler paper.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1303
     * It divides a 2n-digit number by a n-digit number.<br/>
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1304
     * The parameter beta is 2<sup>32</sup> so all shifts are multiples of 32 bits.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1305
     * <br/>
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1306
     * {@code this} must be a nonnegative number such that {@code this.bitLength() <= 2*b.bitLength()}
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1307
     * @param b a positive number such that {@code b.bitLength()} is even
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1308
     * @param quotient output parameter for {@code this/b}
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1309
     * @return {@code this%b}
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1310
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1311
    private MutableBigInteger divide2n1n(MutableBigInteger b, MutableBigInteger quotient) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1312
        int n = b.intLen;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1313
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1314
        // step 1: base case
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1315
        if (n%2 != 0 || n < BigInteger.BURNIKEL_ZIEGLER_THRESHOLD) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1316
            return divideKnuth(b, quotient);
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1317
        }
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1318
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1319
        // step 2: view this as [a1,a2,a3,a4] where each ai is n/2 ints or less
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1320
        MutableBigInteger aUpper = new MutableBigInteger(this);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1321
        aUpper.safeRightShift(32*(n/2));   // aUpper = [a1,a2,a3]
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1322
        keepLower(n/2);   // this = a4
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1323
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1324
        // step 3: q1=aUpper/b, r1=aUpper%b
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1325
        MutableBigInteger q1 = new MutableBigInteger();
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1326
        MutableBigInteger r1 = aUpper.divide3n2n(b, q1);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1327
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1328
        // step 4: quotient=[r1,this]/b, r2=[r1,this]%b
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1329
        addDisjoint(r1, n/2);   // this = [r1,this]
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1330
        MutableBigInteger r2 = divide3n2n(b, quotient);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1331
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1332
        // step 5: let quotient=[q1,quotient] and return r2
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1333
        quotient.addDisjoint(q1, n/2);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1334
        return r2;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1335
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1336
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1337
    /**
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1338
     * This method implements algorithm 2 from pg. 5 of the Burnikel-Ziegler paper.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1339
     * It divides a 3n-digit number by a 2n-digit number.<br/>
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1340
     * The parameter beta is 2<sup>32</sup> so all shifts are multiples of 32 bits.<br/>
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1341
     * <br/>
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1342
     * {@code this} must be a nonnegative number such that {@code 2*this.bitLength() <= 3*b.bitLength()}
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1343
     * @param quotient output parameter for {@code this/b}
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1344
     * @return {@code this%b}
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1345
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1346
    private MutableBigInteger divide3n2n(MutableBigInteger b, MutableBigInteger quotient) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1347
        int n = b.intLen / 2;   // half the length of b in ints
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1348
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1349
        // step 1: view this as [a1,a2,a3] where each ai is n ints or less; let a12=[a1,a2]
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1350
        MutableBigInteger a12 = new MutableBigInteger(this);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1351
        a12.safeRightShift(32*n);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1352
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1353
        // step 2: view b as [b1,b2] where each bi is n ints or less
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1354
        MutableBigInteger b1 = new MutableBigInteger(b);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1355
        b1.safeRightShift(n * 32);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1356
        BigInteger b2 = b.getLower(n);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1357
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1358
        MutableBigInteger r;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1359
        MutableBigInteger d;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1360
        if (compareShifted(b, n) < 0) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1361
            // step 3a: if a1<b1, let quotient=a12/b1 and r=a12%b1
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1362
            r = a12.divide2n1n(b1, quotient);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1363
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1364
            // step 4: d=quotient*b2
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1365
            d = new MutableBigInteger(quotient.toBigInteger().multiply(b2));
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1366
        } else {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1367
            // step 3b: if a1>=b1, let quotient=beta^n-1 and r=a12-b1*2^n+b1
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1368
            quotient.ones(n);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1369
            a12.add(b1);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1370
            b1.leftShift(32*n);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1371
            a12.subtract(b1);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1372
            r = a12;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1373
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1374
            // step 4: d=quotient*b2=(b2 << 32*n) - b2
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1375
            d = new MutableBigInteger(b2);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1376
            d.leftShift(32 * n);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1377
            d.subtract(new MutableBigInteger(b2));
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1378
        }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1379
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1380
        // step 5: r = r*beta^n + a3 - d (paper says a4)
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1381
        // However, don't subtract d until after the while loop so r doesn't become negative
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1382
        r.leftShift(32 * n);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1383
        r.addLower(this, n);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1384
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1385
        // step 6: add b until r>=d
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1386
        while (r.compare(d) < 0) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1387
            r.add(b);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1388
            quotient.subtract(MutableBigInteger.ONE);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1389
        }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1390
        r.subtract(d);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1391
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1392
        return r;
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1393
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1394
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1395
    /**
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1396
     * Returns a {@code MutableBigInteger} containing {@code blockLength} ints from
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1397
     * {@code this} number, starting at {@code index*blockLength}.<br/>
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1398
     * Used by Burnikel-Ziegler division.
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1399
     * @param index the block index
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1400
     * @param numBlocks the total number of blocks in {@code this} number
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1401
     * @param blockLength length of one block in units of 32 bits
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1402
     * @return
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1403
     */
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1404
    private MutableBigInteger getBlock(int index, int numBlocks, int blockLength) {
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1405
        int blockStart = index * blockLength;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1406
        if (blockStart >= intLen) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1407
            return new MutableBigInteger();
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1408
        }
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1409
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1410
        int blockEnd;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1411
        if (index == numBlocks-1) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1412
            blockEnd = intLen;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1413
        } else {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1414
            blockEnd = (index+1) * blockLength;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1415
        }
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1416
        if (blockEnd > intLen) {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1417
            return new MutableBigInteger();
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1418
        }
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1419
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1420
        int[] newVal = Arrays.copyOfRange(value, offset+intLen-blockEnd, offset+intLen-blockStart);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1421
        return new MutableBigInteger(newVal);
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1422
    }
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1423
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1424
    /** @see BigInteger#bitLength() */
21420
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents: 19393
diff changeset
  1425
    long bitLength() {
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1426
        if (intLen == 0)
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1427
            return 0;
21420
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents: 19393
diff changeset
  1428
        return intLen*32L - Integer.numberOfLeadingZeros(value[offset]);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1429
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1430
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1431
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1432
     * 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
  1433
     * quotient in the provided MutableBigInteger object and the remainder is
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1434
     * returned.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1435
     *
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1436
     * @return the remainder of the division will be returned.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1437
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1438
    long divide(long v, MutableBigInteger quotient) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1439
        if (v == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1440
            throw new ArithmeticException("BigInteger divide by zero");
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1441
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1442
        // Dividend is zero
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1443
        if (intLen == 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1444
            quotient.intLen = quotient.offset = 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1445
            return 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1446
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1447
        if (v < 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1448
            v = -v;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1449
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1450
        int d = (int)(v >>> 32);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1451
        quotient.clear();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1452
        // Special case on word divisor
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1453
        if (d == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1454
            return divideOneWord((int)v, quotient) & LONG_MASK;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1455
        else {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1456
            return divideLongMagnitude(v, quotient).toLong();
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1457
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1458
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1459
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1460
    private static void copyAndShift(int[] src, int srcFrom, int srcLen, int[] dst, int dstFrom, int shift) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1461
        int n2 = 32 - shift;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1462
        int c=src[srcFrom];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1463
        for (int i=0; i < srcLen-1; i++) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1464
            int b = c;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1465
            c = src[++srcFrom];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1466
            dst[dstFrom+i] = (b << shift) | (c >>> n2);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1467
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1468
        dst[dstFrom+srcLen-1] = c << shift;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1469
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1470
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1471
    /**
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1472
     * Divide this MutableBigInteger by the divisor.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1473
     * The quotient will be placed into the provided quotient object &
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1474
     * the remainder object is returned.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1475
     */
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1476
    private MutableBigInteger divideMagnitude(MutableBigInteger div,
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1477
                                              MutableBigInteger quotient,
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1478
                                              boolean needRemainder ) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1479
        // assert div.intLen > 1
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1480
        // D1 normalize the divisor
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1481
        int shift = Integer.numberOfLeadingZeros(div.value[div.offset]);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1482
        // Copy divisor value to protect divisor
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1483
        final int dlen = div.intLen;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1484
        int[] divisor;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1485
        MutableBigInteger rem; // Remainder starts as dividend with space for a leading zero
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1486
        if (shift > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1487
            divisor = new int[dlen];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1488
            copyAndShift(div.value,div.offset,dlen,divisor,0,shift);
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1489
            if (Integer.numberOfLeadingZeros(value[offset]) >= shift) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1490
                int[] remarr = new int[intLen + 1];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1491
                rem = new MutableBigInteger(remarr);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1492
                rem.intLen = intLen;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1493
                rem.offset = 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1494
                copyAndShift(value,offset,intLen,remarr,1,shift);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1495
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1496
                int[] remarr = new int[intLen + 2];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1497
                rem = new MutableBigInteger(remarr);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1498
                rem.intLen = intLen+1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1499
                rem.offset = 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1500
                int rFrom = offset;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1501
                int c=0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1502
                int n2 = 32 - shift;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1503
                for (int i=1; i < intLen+1; i++,rFrom++) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1504
                    int b = c;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1505
                    c = value[rFrom];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1506
                    remarr[i] = (b << shift) | (c >>> n2);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1507
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1508
                remarr[intLen+1] = c << shift;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1509
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1510
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1511
            divisor = Arrays.copyOfRange(div.value, div.offset, div.offset + div.intLen);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1512
            rem = new MutableBigInteger(new int[intLen + 1]);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1513
            System.arraycopy(value, offset, rem.value, 1, intLen);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1514
            rem.intLen = intLen;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1515
            rem.offset = 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1516
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        int nlen = rem.intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        // Set the quotient size
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1521
        final int limit = nlen - dlen + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        if (quotient.value.length < limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            quotient.value = new int[limit];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            quotient.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        quotient.intLen = limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        int[] q = quotient.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        // Must insert leading 0 in rem if its length did not change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        if (rem.intLen == nlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            rem.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            rem.value[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            rem.intLen++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1537
        int dh = divisor[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        long dhLong = dh & LONG_MASK;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1539
        int dl = divisor[1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        // D2 Initialize j
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1542
        for (int j=0; j < limit-1; j++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            // D3 Calculate qhat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
            // estimate qhat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            int qhat = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            int qrem = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            boolean skipCorrection = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            int nh = rem.value[j+rem.offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            int nh2 = nh + 0x80000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            int nm = rem.value[j+1+rem.offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            if (nh == dh) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                qhat = ~0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                qrem = nh + nm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                skipCorrection = qrem + 0x80000000 < nh2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                long nChunk = (((long)nh) << 32) | (nm & LONG_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                if (nChunk >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                    qhat = (int) (nChunk / dhLong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                    qrem = (int) (nChunk - (qhat * dhLong));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                } else {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1562
                    long tmp = divWord(nChunk, dh);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1563
                    qhat = (int) (tmp & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1564
                    qrem = (int) (tmp >>> 32);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            if (qhat == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            if (!skipCorrection) { // Correct qhat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                long nl = rem.value[j+2+rem.offset] & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                long rs = ((qrem & LONG_MASK) << 32) | nl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                long estProduct = (dl & LONG_MASK) * (qhat & LONG_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                if (unsignedLongCompare(estProduct, rs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                    qhat--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                    qrem = (int)((qrem & LONG_MASK) + dhLong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                    if ((qrem & LONG_MASK) >=  dhLong) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1580
                        estProduct -= (dl & LONG_MASK);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                        rs = ((qrem & LONG_MASK) << 32) | nl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                        if (unsignedLongCompare(estProduct, rs))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                            qhat--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            // D4 Multiply and subtract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            rem.value[j+rem.offset] = 0;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1590
            int borrow = mulsub(rem.value, divisor, qhat, dlen, j+rem.offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            // D5 Test remainder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            if (borrow + 0x80000000 > nh2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                // D6 Add back
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1595
                divadd(divisor, rem.value, j+1+rem.offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                qhat--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            // Store the quotient digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            q[j] = qhat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        } // D7 loop on j
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1602
        // D3 Calculate qhat
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1603
        // estimate qhat
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1604
        int qhat = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1605
        int qrem = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1606
        boolean skipCorrection = false;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1607
        int nh = rem.value[limit - 1 + rem.offset];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1608
        int nh2 = nh + 0x80000000;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1609
        int nm = rem.value[limit + rem.offset];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1610
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1611
        if (nh == dh) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1612
            qhat = ~0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1613
            qrem = nh + nm;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1614
            skipCorrection = qrem + 0x80000000 < nh2;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1615
        } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1616
            long nChunk = (((long) nh) << 32) | (nm & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1617
            if (nChunk >= 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1618
                qhat = (int) (nChunk / dhLong);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1619
                qrem = (int) (nChunk - (qhat * dhLong));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1620
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1621
                long tmp = divWord(nChunk, dh);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1622
                qhat = (int) (tmp & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1623
                qrem = (int) (tmp >>> 32);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1624
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1625
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1626
        if (qhat != 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1627
            if (!skipCorrection) { // Correct qhat
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1628
                long nl = rem.value[limit + 1 + rem.offset] & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1629
                long rs = ((qrem & LONG_MASK) << 32) | nl;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1630
                long estProduct = (dl & LONG_MASK) * (qhat & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1631
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1632
                if (unsignedLongCompare(estProduct, rs)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1633
                    qhat--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1634
                    qrem = (int) ((qrem & LONG_MASK) + dhLong);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1635
                    if ((qrem & LONG_MASK) >= dhLong) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1636
                        estProduct -= (dl & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1637
                        rs = ((qrem & LONG_MASK) << 32) | nl;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1638
                        if (unsignedLongCompare(estProduct, rs))
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1639
                            qhat--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1640
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1641
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1642
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1643
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1644
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1645
            // D4 Multiply and subtract
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1646
            int borrow;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1647
            rem.value[limit - 1 + rem.offset] = 0;
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1648
            if(needRemainder)
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1649
                borrow = mulsub(rem.value, divisor, qhat, dlen, limit - 1 + rem.offset);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1650
            else
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1651
                borrow = mulsubBorrow(rem.value, divisor, qhat, dlen, limit - 1 + rem.offset);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1652
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1653
            // D5 Test remainder
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1654
            if (borrow + 0x80000000 > nh2) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1655
                // D6 Add back
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1656
                if(needRemainder)
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1657
                    divadd(divisor, rem.value, limit - 1 + 1 + rem.offset);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1658
                qhat--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1659
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1660
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1661
            // Store the quotient digit
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1662
            q[(limit - 1)] = qhat;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1663
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1664
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1665
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1666
        if (needRemainder) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1667
            // D8 Unnormalize
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1668
            if (shift > 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1669
                rem.rightShift(shift);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1670
            rem.normalize();
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1671
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1672
        quotient.normalize();
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1673
        return needRemainder ? rem : null;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1674
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1675
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1676
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1677
     * Divide this MutableBigInteger by the divisor represented by positive long
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1678
     * value. The quotient will be placed into the provided quotient object &
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1679
     * the remainder object is returned.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1680
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1681
    private MutableBigInteger divideLongMagnitude(long ldivisor, MutableBigInteger quotient) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1682
        // Remainder starts as dividend with space for a leading zero
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1683
        MutableBigInteger rem = new MutableBigInteger(new int[intLen + 1]);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1684
        System.arraycopy(value, offset, rem.value, 1, intLen);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1685
        rem.intLen = intLen;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1686
        rem.offset = 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1687
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1688
        int nlen = rem.intLen;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1689
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1690
        int limit = nlen - 2 + 1;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1691
        if (quotient.value.length < limit) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1692
            quotient.value = new int[limit];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1693
            quotient.offset = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1694
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1695
        quotient.intLen = limit;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1696
        int[] q = quotient.value;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1697
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1698
        // D1 normalize the divisor
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1699
        int shift = Long.numberOfLeadingZeros(ldivisor);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1700
        if (shift > 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1701
            ldivisor<<=shift;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1702
            rem.leftShift(shift);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1703
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1704
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1705
        // Must insert leading 0 in rem if its length did not change
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1706
        if (rem.intLen == nlen) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1707
            rem.offset = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1708
            rem.value[0] = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1709
            rem.intLen++;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1710
        }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1711
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1712
        int dh = (int)(ldivisor >>> 32);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1713
        long dhLong = dh & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1714
        int dl = (int)(ldivisor & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1715
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1716
        // D2 Initialize j
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1717
        for (int j = 0; j < limit; j++) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1718
            // D3 Calculate qhat
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1719
            // estimate qhat
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1720
            int qhat = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1721
            int qrem = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1722
            boolean skipCorrection = false;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1723
            int nh = rem.value[j + rem.offset];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1724
            int nh2 = nh + 0x80000000;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1725
            int nm = rem.value[j + 1 + rem.offset];
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1726
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1727
            if (nh == dh) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1728
                qhat = ~0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1729
                qrem = nh + nm;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1730
                skipCorrection = qrem + 0x80000000 < nh2;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1731
            } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1732
                long nChunk = (((long) nh) << 32) | (nm & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1733
                if (nChunk >= 0) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1734
                    qhat = (int) (nChunk / dhLong);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1735
                    qrem = (int) (nChunk - (qhat * dhLong));
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1736
                } else {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1737
                    long tmp = divWord(nChunk, dh);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1738
                    qhat =(int)(tmp & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1739
                    qrem = (int)(tmp>>>32);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1740
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1741
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1742
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1743
            if (qhat == 0)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1744
                continue;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1745
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1746
            if (!skipCorrection) { // Correct qhat
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1747
                long nl = rem.value[j + 2 + rem.offset] & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1748
                long rs = ((qrem & LONG_MASK) << 32) | nl;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1749
                long estProduct = (dl & LONG_MASK) * (qhat & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1750
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1751
                if (unsignedLongCompare(estProduct, rs)) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1752
                    qhat--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1753
                    qrem = (int) ((qrem & LONG_MASK) + dhLong);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1754
                    if ((qrem & LONG_MASK) >= dhLong) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1755
                        estProduct -= (dl & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1756
                        rs = ((qrem & LONG_MASK) << 32) | nl;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1757
                        if (unsignedLongCompare(estProduct, rs))
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1758
                            qhat--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1759
                    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1760
                }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1761
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1762
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1763
            // D4 Multiply and subtract
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1764
            rem.value[j + rem.offset] = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1765
            int borrow = mulsubLong(rem.value, dh, dl, qhat,  j + rem.offset);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1766
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1767
            // D5 Test remainder
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1768
            if (borrow + 0x80000000 > nh2) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1769
                // D6 Add back
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1770
                divaddLong(dh,dl, rem.value, j + 1 + rem.offset);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1771
                qhat--;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1772
            }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1773
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1774
            // Store the quotient digit
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1775
            q[j] = qhat;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1776
        } // D7 loop on j
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        // D8 Unnormalize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        if (shift > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            rem.rightShift(shift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1782
        quotient.normalize();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        rem.normalize();
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1784
        return rem;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
    /**
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1788
     * A primitive used for division by long.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1789
     * Specialized version of the method divadd.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1790
     * dh is a high part of the divisor, dl is a low part
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1791
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1792
    private int divaddLong(int dh, int dl, int[] result, int offset) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1793
        long carry = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1794
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1795
        long sum = (dl & LONG_MASK) + (result[1+offset] & LONG_MASK);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1796
        result[1+offset] = (int)sum;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1797
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1798
        sum = (dh & LONG_MASK) + (result[offset] & LONG_MASK) + carry;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1799
        result[offset] = (int)sum;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1800
        carry = sum >>> 32;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1801
        return (int)carry;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1802
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1803
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1804
    /**
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1805
     * This method is used for division by long.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1806
     * Specialized version of the method sulsub.
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1807
     * dh is a high part of the divisor, dl is a low part
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1808
     */
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1809
    private int mulsubLong(int[] q, int dh, int dl, int x, int offset) {
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1810
        long xLong = x & LONG_MASK;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1811
        offset += 2;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1812
        long product = (dl & LONG_MASK) * xLong;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1813
        long difference = q[offset] - product;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1814
        q[offset--] = (int)difference;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1815
        long carry = (product >>> 32)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1816
                 + (((difference & LONG_MASK) >
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1817
                     (((~(int)product) & LONG_MASK))) ? 1:0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1818
        product = (dh & LONG_MASK) * xLong + carry;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1819
        difference = q[offset] - product;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1820
        q[offset--] = (int)difference;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1821
        carry = (product >>> 32)
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1822
                 + (((difference & LONG_MASK) >
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1823
                     (((~(int)product) & LONG_MASK))) ? 1:0);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1824
        return (int)carry;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1825
    }
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1826
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1827
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     * Compare two longs as if they were unsigned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     * Returns true iff one is bigger than two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
    private boolean unsignedLongCompare(long one, long two) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        return (one+Long.MIN_VALUE) > (two+Long.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     * This method divides a long quantity by an int to estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     * qhat for two multi precision numbers. It is used when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
     * the signed value of n is less than zero.
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  1839
     * Returns long value where high 32 bits contain remainder value and
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1840
     * low 32 bits contain quotient value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     */
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1842
    static long divWord(long n, int d) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        long dLong = d & LONG_MASK;
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1844
        long r;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1845
        long q;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        if (dLong == 1) {
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1847
            q = (int)n;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1848
            r = 0;
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1849
            return (r << 32) | (q & LONG_MASK);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        // Approximate the quotient and remainder
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1853
        q = (n >>> 1) / (dLong >>> 1);
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1854
        r = n - q*dLong;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        // Correct the approximation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        while (r < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            r += dLong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            q--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        while (r >= dLong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            r -= dLong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            q++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        // n - q*dlong == r && 0 <= r <dLong, hence we're done.
10423
2c852092a4e5 7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents: 5506
diff changeset
  1866
        return (r << 32) | (q & LONG_MASK);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
    /**
34538
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1870
     * Calculate the integer square root {@code floor(sqrt(this))} where
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1871
     * {@code sqrt(.)} denotes the mathematical square root. The contents of
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1872
     * {@code this} are <b>not</b> changed. The value of {@code this} is assumed
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1873
     * to be non-negative.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1874
     *
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1875
     * @implNote The implementation is based on the material in Henry S. Warren,
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1876
     * Jr., <i>Hacker's Delight (2nd ed.)</i> (Addison Wesley, 2013), 279-282.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1877
     *
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1878
     * @throws ArithmeticException if the value returned by {@code bitLength()}
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1879
     * overflows the range of {@code int}.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1880
     * @return the integer square root of {@code this}
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34538
diff changeset
  1881
     * @since 9
34538
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1882
     */
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1883
    MutableBigInteger sqrt() {
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1884
        // Special cases.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1885
        if (this.isZero()) {
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1886
            return new MutableBigInteger(0);
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1887
        } else if (this.value.length == 1
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1888
                && (this.value[0] & LONG_MASK) < 4) { // result is unity
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1889
            return ONE;
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1890
        }
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1891
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1892
        if (bitLength() <= 63) {
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1893
            // Initial estimate is the square root of the positive long value.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1894
            long v = new BigInteger(this.value, 1).longValueExact();
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1895
            long xk = (long)Math.floor(Math.sqrt(v));
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1896
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1897
            // Refine the estimate.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1898
            do {
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1899
                long xk1 = (xk + v/xk)/2;
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1900
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1901
                // Terminate when non-decreasing.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1902
                if (xk1 >= xk) {
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1903
                    return new MutableBigInteger(new int[] {
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1904
                        (int)(xk >>> 32), (int)(xk & LONG_MASK)
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1905
                    });
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1906
                }
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1907
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1908
                xk = xk1;
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1909
            } while (true);
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1910
        } else {
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1911
            // Set up the initial estimate of the iteration.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1912
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1913
            // Obtain the bitLength > 63.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1914
            int bitLength = (int) this.bitLength();
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1915
            if (bitLength != this.bitLength()) {
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1916
                throw new ArithmeticException("bitLength() integer overflow");
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1917
            }
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1918
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1919
            // Determine an even valued right shift into positive long range.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1920
            int shift = bitLength - 63;
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1921
            if (shift % 2 == 1) {
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1922
                shift++;
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1923
            }
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1924
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1925
            // Shift the value into positive long range.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1926
            MutableBigInteger xk = new MutableBigInteger(this);
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1927
            xk.rightShift(shift);
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1928
            xk.normalize();
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1929
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1930
            // Use the square root of the shifted value as an approximation.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1931
            double d = new BigInteger(xk.value, 1).doubleValue();
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1932
            BigInteger bi = BigInteger.valueOf((long)Math.ceil(Math.sqrt(d)));
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1933
            xk = new MutableBigInteger(bi.mag);
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1934
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1935
            // Shift the approximate square root back into the original range.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1936
            xk.leftShift(shift / 2);
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1937
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1938
            // Refine the estimate.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1939
            MutableBigInteger xk1 = new MutableBigInteger();
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1940
            do {
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1941
                // xk1 = (xk + n/xk)/2
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1942
                this.divide(xk, xk1, false);
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1943
                xk1.add(xk);
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1944
                xk1.rightShift(1);
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1945
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1946
                // Terminate when non-decreasing.
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1947
                if (xk1.compare(xk) >= 0) {
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1948
                    return xk;
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1949
                }
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1950
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1951
                // xk = xk1
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1952
                xk.copyValue(xk1);
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1953
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1954
                xk1.reset();
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1955
            } while (true);
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1956
        }
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1957
    }
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1958
b0705127fbba 8032027: Add BigInteger square root methods
bpb
parents: 32033
diff changeset
  1959
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * Calculate GCD of this and b. This and b are changed by the computation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
    MutableBigInteger hybridGCD(MutableBigInteger b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        // Use Euclid's algorithm until the numbers are approximately the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        // same length, then use the binary GCD algorithm to find the GCD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        MutableBigInteger a = this;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1966
        MutableBigInteger q = new MutableBigInteger();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        while (b.intLen != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            if (Math.abs(a.intLen - b.intLen) < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                return a.binaryGCD(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1972
            MutableBigInteger r = a.divide(b, q);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1973
            a = b;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1974
            b = r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     * Calculate GCD of this and v.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     * Assumes that this and v are not zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
    private MutableBigInteger binaryGCD(MutableBigInteger v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        // Algorithm B from Knuth section 4.5.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        MutableBigInteger u = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        MutableBigInteger r = new MutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        // step B1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        int s1 = u.getLowestSetBit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        int s2 = v.getLowestSetBit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        int k = (s1 < s2) ? s1 : s2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        if (k != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            u.rightShift(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            v.rightShift(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
        // step B2
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  1998
        boolean uOdd = (k == s1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        MutableBigInteger t = uOdd ? v: u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        int tsign = uOdd ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        int lb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        while ((lb = t.getLowestSetBit()) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
            // steps B3 and B4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            t.rightShift(lb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
            // step B5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
            if (tsign > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                u = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                v = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
            // Special case one word numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            if (u.intLen < 2 && v.intLen < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                int x = u.value[u.offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                int y = v.value[v.offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                x  = binaryGcd(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                r.value[0] = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                r.intLen = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                r.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                if (k > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                    r.leftShift(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
            // step B6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            if ((tsign = u.difference(v)) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
            t = (tsign >= 0) ? u : v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        if (k > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            u.leftShift(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     * Calculate GCD of a and b interpreted as unsigned integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
    static int binaryGcd(int a, int b) {
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  2040
        if (b == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            return a;
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  2042
        if (a == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2045
        // 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
  2046
        int aZeros = Integer.numberOfTrailingZeros(a);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2047
        int bZeros = Integer.numberOfTrailingZeros(b);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2048
        a >>>= aZeros;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2049
        b >>>= bZeros;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        int t = (aZeros < bZeros ? aZeros : bZeros);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        while (a != b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            if ((a+0x80000000) > (b+0x80000000)) {  // a > b as unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                a -= b;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2056
                a >>>= Integer.numberOfTrailingZeros(a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                b -= a;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2059
                b >>>= Integer.numberOfTrailingZeros(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
        return a<<t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * Returns the modInverse of this mod p. This and p are not affected by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     * the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
    MutableBigInteger mutableModInverse(MutableBigInteger p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
        // Modulus is odd, use Schroeppel's algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
        if (p.isOdd())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
            return modInverse(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        // Base and modulus are even, throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
        if (isEven())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            throw new ArithmeticException("BigInteger not invertible.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
        // Get even part of modulus expressed as a power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        int powersOf2 = p.getLowestSetBit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        // Construct odd part of modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
        MutableBigInteger oddMod = new MutableBigInteger(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        oddMod.rightShift(powersOf2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        if (oddMod.isOne())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
            return modInverseMP2(powersOf2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        // Calculate 1/a mod oddMod
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
        MutableBigInteger oddPart = modInverse(oddMod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        // Calculate 1/a mod evenMod
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        MutableBigInteger evenPart = modInverseMP2(powersOf2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        // Combine the results using Chinese Remainder Theorem
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
        MutableBigInteger y1 = modInverseBP2(oddMod, powersOf2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        MutableBigInteger y2 = oddMod.modInverseMP2(powersOf2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        MutableBigInteger temp1 = new MutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        MutableBigInteger temp2 = new MutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        MutableBigInteger result = new MutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
        oddPart.leftShift(powersOf2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
        oddPart.multiply(y1, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        evenPart.multiply(oddMod, temp1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        temp1.multiply(y2, temp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        result.add(temp2);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2109
        return result.divide(p, temp1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * Calculate the multiplicative inverse of this mod 2^k.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
    MutableBigInteger modInverseMP2(int k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        if (isEven())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            throw new ArithmeticException("Non-invertible. (GCD != 1)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        if (k > 64)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            return euclidModInverse(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        int t = inverseMod32(value[offset+intLen-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        if (k < 33) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
            t = (k == 32 ? t : t & ((1 << k) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            return new MutableBigInteger(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        long pLong = (value[offset+intLen-1] & LONG_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        if (intLen > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            pLong |=  ((long)value[offset+intLen-2] << 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
        long tLong = t & LONG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        tLong = tLong * (2 - pLong * tLong);  // 1 more Newton iter step
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
        tLong = (k == 64 ? tLong : tLong & ((1L << k) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
        MutableBigInteger result = new MutableBigInteger(new int[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
        result.value[0] = (int)(tLong >>> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
        result.value[1] = (int)tLong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        result.intLen = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        result.normalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  2144
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     * Returns the multiplicative inverse of val mod 2^32.  Assumes val is odd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
    static int inverseMod32(int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        // Newton's iteration!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
        int t = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
        t *= 2 - val*t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
        t *= 2 - val*t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        t *= 2 - val*t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
        t *= 2 - val*t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  2157
    /**
31667
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2158
     * Returns the multiplicative inverse of val mod 2^64.  Assumes val is odd.
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2159
     */
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2160
    static long inverseMod64(long val) {
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2161
        // Newton's iteration!
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2162
        long t = val;
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2163
        t *= 2 - val*t;
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2164
        t *= 2 - val*t;
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2165
        t *= 2 - val*t;
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2166
        t *= 2 - val*t;
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2167
        t *= 2 - val*t;
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2168
        assert(t * val == 1);
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2169
        return t;
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2170
    }
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2171
15a14e8fcfb0 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents: 26620
diff changeset
  2172
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     * Calculate the multiplicative inverse of 2^k mod mod, where mod is odd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
    static MutableBigInteger modInverseBP2(MutableBigInteger mod, int k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        // Copy the mod to protect original
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        return fixup(new MutableBigInteger(1), new MutableBigInteger(mod), k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
     * Calculate the multiplicative inverse of this mod mod, where mod is odd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
     * This and mod are not changed by the calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     * This method implements an algorithm due to Richard Schroeppel, that uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     * the same intermediate representation as Montgomery Reduction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     * ("Montgomery Form").  The algorithm is described in an unpublished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     * manuscript entitled "Fast Modular Reciprocals."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
    private MutableBigInteger modInverse(MutableBigInteger mod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
        MutableBigInteger p = new MutableBigInteger(mod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        MutableBigInteger f = new MutableBigInteger(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
        MutableBigInteger g = new MutableBigInteger(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
        SignedMutableBigInteger c = new SignedMutableBigInteger(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
        SignedMutableBigInteger d = new SignedMutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
        MutableBigInteger temp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        SignedMutableBigInteger sTemp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
        // Right shift f k times until odd, left shift d k times
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
        if (f.isEven()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            int trailingZeros = f.getLowestSetBit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            f.rightShift(trailingZeros);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            d.leftShift(trailingZeros);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
            k = trailingZeros;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
        // The Almost Inverse Algorithm
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  2208
        while (!f.isOne()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
            // If gcd(f, g) != 1, number is not invertible modulo mod
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
            if (f.isZero())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                throw new ArithmeticException("BigInteger not invertible.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
            // If f < g exchange f, g and c, d
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
            if (f.compare(g) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                temp = f; f = g; g = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                sTemp = d; d = c; c = sTemp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
            // If f == g (mod 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            if (((f.value[f.offset + f.intLen - 1] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                 g.value[g.offset + g.intLen - 1]) & 3) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                f.subtract(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                c.signedSubtract(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            } else { // If f != g (mod 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                f.add(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                c.signedAdd(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
            // Right shift f k times until odd, left shift d k times
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
            int trailingZeros = f.getLowestSetBit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
            f.rightShift(trailingZeros);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
            d.leftShift(trailingZeros);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
            k += trailingZeros;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
        while (c.sign < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
           c.signedAdd(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
        return fixup(c, p, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
19060
4d949a65ad7f 8014319: Faster division of large integers
bpb
parents: 10423
diff changeset
  2242
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
     * The Fixup Algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
     * Calculates X such that X = C * 2^(-k) (mod P)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
     * Assumes C<P and P is odd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
    static MutableBigInteger fixup(MutableBigInteger c, MutableBigInteger p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                                                                      int k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
        MutableBigInteger temp = new MutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
        // Set r to the multiplicative inverse of p mod 2^32
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
        int r = -inverseMod32(p.value[p.offset+p.intLen-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
19061
d48848ef5670 8020641: Clean up some code style in recent BigInteger contributions
bpb
parents: 19060
diff changeset
  2253
        for (int i=0, numWords = k >> 5; i < numWords; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
            // V = R * c (mod 2^j)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            int  v = r * c.value[c.offset + c.intLen-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            // c = c + (v * p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            p.mul(v, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            c.add(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
            // c = c / 2^j
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            c.intLen--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        int numBits = k & 0x1f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        if (numBits != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
            // V = R * c (mod 2^j)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
            int v = r * c.value[c.offset + c.intLen-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            v &= ((1<<numBits) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            // c = c + (v * p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            p.mul(v, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
            c.add(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            // c = c / 2^j
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            c.rightShift(numBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
        // In theory, c may be greater than p at this point (Very rare!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
        while (c.compare(p) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
            c.subtract(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     * Uses the extended Euclidean algorithm to compute the modInverse of base
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
     * mod a modulus that is a power of 2. The modulus is 2^k.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
    MutableBigInteger euclidModInverse(int k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
        MutableBigInteger b = new MutableBigInteger(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
        b.leftShift(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
        MutableBigInteger mod = new MutableBigInteger(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
        MutableBigInteger a = new MutableBigInteger(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
        MutableBigInteger q = new MutableBigInteger();
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2292
        MutableBigInteger r = b.divide(a, q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2294
        MutableBigInteger swapper = b;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2295
        // swap b & r
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2296
        b = r;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2297
        r = swapper;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
        MutableBigInteger t1 = new MutableBigInteger(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
        MutableBigInteger t0 = new MutableBigInteger(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
        MutableBigInteger temp = new MutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
        while (!b.isOne()) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2304
            r = a.divide(b, q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
            if (r.intLen == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                throw new ArithmeticException("BigInteger not invertible.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2309
            swapper = r;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2310
            a = swapper;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
            if (q.intLen == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                t1.mul(q.value[q.offset], temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                q.multiply(t1, temp);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2316
            swapper = q;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2317
            q = temp;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2318
            temp = swapper;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
            t0.add(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            if (a.isOne())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                return t0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2324
            r = b.divide(a, q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            if (r.intLen == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                throw new ArithmeticException("BigInteger not invertible.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2329
            swapper = b;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2330
            b =  r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            if (q.intLen == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                t0.mul(q.value[q.offset], temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                q.multiply(t0, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
            swapper = q; q = temp; temp = swapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            t1.add(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
        mod.subtract(t1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
        return mod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
}