author | bpb |
Wed, 09 Oct 2013 17:22:34 -0700 | |
changeset 20756 | d998355e6a5c |
parent 19585 | b57abf89019f |
child 21420 | a56f40ab71ce |
permissions | -rw-r--r-- |
2 | 1 |
/* |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2 |
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* Portions Copyright (c) 1995 Colin Plumb. All rights reserved. |
|
28 |
*/ |
|
29 |
||
30 |
package java.math; |
|
31 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
32 |
import java.io.IOException; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
33 |
import java.io.ObjectInputStream; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
34 |
import java.io.ObjectOutputStream; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
35 |
import java.io.ObjectStreamField; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
36 |
import java.util.Arrays; |
2 | 37 |
import java.util.Random; |
20756
d998355e6a5c
7189139: BigInteger's staticRandom field can be a source of bottlenecks.
bpb
parents:
19585
diff
changeset
|
38 |
import java.util.concurrent.ThreadLocalRandom; |
18538
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
39 |
import sun.misc.DoubleConsts; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
40 |
import sun.misc.FloatConsts; |
2 | 41 |
|
42 |
/** |
|
43 |
* Immutable arbitrary-precision integers. All operations behave as if |
|
44 |
* BigIntegers were represented in two's-complement notation (like Java's |
|
45 |
* primitive integer types). BigInteger provides analogues to all of Java's |
|
46 |
* primitive integer operators, and all relevant methods from java.lang.Math. |
|
47 |
* Additionally, BigInteger provides operations for modular arithmetic, GCD |
|
48 |
* calculation, primality testing, prime generation, bit manipulation, |
|
49 |
* and a few other miscellaneous operations. |
|
50 |
* |
|
51 |
* <p>Semantics of arithmetic operations exactly mimic those of Java's integer |
|
52 |
* arithmetic operators, as defined in <i>The Java Language Specification</i>. |
|
53 |
* For example, division by zero throws an {@code ArithmeticException}, and |
|
54 |
* division of a negative by a positive yields a negative (or zero) remainder. |
|
55 |
* All of the details in the Spec concerning overflow are ignored, as |
|
56 |
* BigIntegers are made as large as necessary to accommodate the results of an |
|
57 |
* operation. |
|
58 |
* |
|
59 |
* <p>Semantics of shift operations extend those of Java's shift operators |
|
60 |
* to allow for negative shift distances. A right-shift with a negative |
|
61 |
* shift distance results in a left shift, and vice-versa. The unsigned |
|
62 |
* right shift operator ({@code >>>}) is omitted, as this operation makes |
|
63 |
* little sense in combination with the "infinite word size" abstraction |
|
64 |
* provided by this class. |
|
65 |
* |
|
66 |
* <p>Semantics of bitwise logical operations exactly mimic those of Java's |
|
67 |
* bitwise integer operators. The binary operators ({@code and}, |
|
68 |
* {@code or}, {@code xor}) implicitly perform sign extension on the shorter |
|
69 |
* of the two operands prior to performing the operation. |
|
70 |
* |
|
71 |
* <p>Comparison operations perform signed integer comparisons, analogous to |
|
72 |
* those performed by Java's relational and equality operators. |
|
73 |
* |
|
74 |
* <p>Modular arithmetic operations are provided to compute residues, perform |
|
75 |
* exponentiation, and compute multiplicative inverses. These methods always |
|
76 |
* return a non-negative result, between {@code 0} and {@code (modulus - 1)}, |
|
77 |
* inclusive. |
|
78 |
* |
|
79 |
* <p>Bit operations operate on a single bit of the two's-complement |
|
80 |
* representation of their operand. If necessary, the operand is sign- |
|
81 |
* extended so that it contains the designated bit. None of the single-bit |
|
82 |
* operations can produce a BigInteger with a different sign from the |
|
83 |
* BigInteger being operated on, as they affect only a single bit, and the |
|
84 |
* "infinite word size" abstraction provided by this class ensures that there |
|
85 |
* are infinitely many "virtual sign bits" preceding each BigInteger. |
|
86 |
* |
|
87 |
* <p>For the sake of brevity and clarity, pseudo-code is used throughout the |
|
88 |
* descriptions of BigInteger methods. The pseudo-code expression |
|
89 |
* {@code (i + j)} is shorthand for "a BigInteger whose value is |
|
90 |
* that of the BigInteger {@code i} plus that of the BigInteger {@code j}." |
|
91 |
* The pseudo-code expression {@code (i == j)} is shorthand for |
|
92 |
* "{@code true} if and only if the BigInteger {@code i} represents the same |
|
93 |
* value as the BigInteger {@code j}." Other pseudo-code expressions are |
|
94 |
* interpreted similarly. |
|
95 |
* |
|
96 |
* <p>All methods and constructors in this class throw |
|
97 |
* {@code NullPointerException} when passed |
|
98 |
* a null object reference for any input parameter. |
|
99 |
* |
|
100 |
* @see BigDecimal |
|
101 |
* @author Josh Bloch |
|
102 |
* @author Michael McCloskey |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
103 |
* @author Alan Eliasen |
19060 | 104 |
* @author Timothy Buktu |
2 | 105 |
* @since JDK1.1 |
106 |
*/ |
|
107 |
||
108 |
public class BigInteger extends Number implements Comparable<BigInteger> { |
|
109 |
/** |
|
110 |
* The signum of this BigInteger: -1 for negative, 0 for zero, or |
|
111 |
* 1 for positive. Note that the BigInteger zero <i>must</i> have |
|
112 |
* a signum of 0. This is necessary to ensures that there is exactly one |
|
113 |
* representation for each BigInteger value. |
|
114 |
* |
|
115 |
* @serial |
|
116 |
*/ |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
117 |
final int signum; |
2 | 118 |
|
119 |
/** |
|
120 |
* The magnitude of this BigInteger, in <i>big-endian</i> order: the |
|
121 |
* zeroth element of this array is the most-significant int of the |
|
122 |
* magnitude. The magnitude must be "minimal" in that the most-significant |
|
123 |
* int ({@code mag[0]}) must be non-zero. This is necessary to |
|
124 |
* ensure that there is exactly one representation for each BigInteger |
|
125 |
* value. Note that this implies that the BigInteger zero has a |
|
126 |
* zero-length mag array. |
|
127 |
*/ |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
128 |
final int[] mag; |
2 | 129 |
|
130 |
// These "redundant fields" are initialized with recognizable nonsense |
|
131 |
// values, and cached the first time they are needed (or never, if they |
|
132 |
// aren't needed). |
|
133 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
134 |
/** |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
135 |
* One plus the bitCount of this BigInteger. Zeros means unitialized. |
2 | 136 |
* |
137 |
* @serial |
|
138 |
* @see #bitCount |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
139 |
* @deprecated Deprecated since logical value is offset from stored |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
140 |
* value and correction factor is applied in accessor method. |
2 | 141 |
*/ |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
142 |
@Deprecated |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
143 |
private int bitCount; |
2 | 144 |
|
145 |
/** |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
146 |
* One plus the bitLength of this BigInteger. Zeros means unitialized. |
2 | 147 |
* (either value is acceptable). |
148 |
* |
|
149 |
* @serial |
|
150 |
* @see #bitLength() |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
151 |
* @deprecated Deprecated since logical value is offset from stored |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
152 |
* value and correction factor is applied in accessor method. |
2 | 153 |
*/ |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
154 |
@Deprecated |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
155 |
private int bitLength; |
2 | 156 |
|
157 |
/** |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
158 |
* Two plus the lowest set bit of this BigInteger, as returned by |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
159 |
* getLowestSetBit(). |
2 | 160 |
* |
161 |
* @serial |
|
162 |
* @see #getLowestSetBit |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
163 |
* @deprecated Deprecated since logical value is offset from stored |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
164 |
* value and correction factor is applied in accessor method. |
2 | 165 |
*/ |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
166 |
@Deprecated |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
167 |
private int lowestSetBit; |
2 | 168 |
|
169 |
/** |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
170 |
* Two plus the index of the lowest-order int in the magnitude of this |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
171 |
* BigInteger that contains a nonzero int, or -2 (either value is acceptable). |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
172 |
* The least significant int has int-number 0, the next int in order of |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
173 |
* increasing significance has int-number 1, and so forth. |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
174 |
* @deprecated Deprecated since logical value is offset from stored |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
175 |
* value and correction factor is applied in accessor method. |
2 | 176 |
*/ |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
177 |
@Deprecated |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
178 |
private int firstNonzeroIntNum; |
2 | 179 |
|
180 |
/** |
|
181 |
* This mask is used to obtain the value of an int as if it were unsigned. |
|
182 |
*/ |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
183 |
final static long LONG_MASK = 0xffffffffL; |
2 | 184 |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
185 |
/** |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
186 |
* The threshold value for using Karatsuba multiplication. If the number |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
187 |
* of ints in both mag arrays are greater than this number, then |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
188 |
* Karatsuba multiplication will be used. This value is found |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
189 |
* experimentally to work well. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
190 |
*/ |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
191 |
private static final int KARATSUBA_THRESHOLD = 50; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
192 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
193 |
/** |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
194 |
* The threshold value for using 3-way Toom-Cook multiplication. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
195 |
* If the number of ints in each mag array is greater than the |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
196 |
* Karatsuba threshold, and the number of ints in at least one of |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
197 |
* the mag arrays is greater than this threshold, then Toom-Cook |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
198 |
* multiplication will be used. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
199 |
*/ |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
200 |
private static final int TOOM_COOK_THRESHOLD = 75; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
201 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
202 |
/** |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
203 |
* The threshold value for using Karatsuba squaring. If the number |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
204 |
* of ints in the number are larger than this value, |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
205 |
* Karatsuba squaring will be used. This value is found |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
206 |
* experimentally to work well. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
207 |
*/ |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
208 |
private static final int KARATSUBA_SQUARE_THRESHOLD = 90; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
209 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
210 |
/** |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
211 |
* The threshold value for using Toom-Cook squaring. If the number |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
212 |
* of ints in the number are larger than this value, |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
213 |
* Toom-Cook squaring will be used. This value is found |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
214 |
* experimentally to work well. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
215 |
*/ |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
216 |
private static final int TOOM_COOK_SQUARE_THRESHOLD = 140; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
217 |
|
18548 | 218 |
/** |
19060 | 219 |
* The threshold value for using Burnikel-Ziegler division. If the number |
220 |
* of ints in the number are larger than this value, |
|
221 |
* Burnikel-Ziegler division will be used. This value is found |
|
222 |
* experimentally to work well. |
|
223 |
*/ |
|
224 |
static final int BURNIKEL_ZIEGLER_THRESHOLD = 50; |
|
225 |
||
226 |
/** |
|
18548 | 227 |
* The threshold value for using Schoenhage recursive base conversion. If |
228 |
* the number of ints in the number are larger than this value, |
|
229 |
* the Schoenhage algorithm will be used. In practice, it appears that the |
|
230 |
* Schoenhage routine is faster for any threshold down to 2, and is |
|
231 |
* relatively flat for thresholds between 2-25, so this choice may be |
|
232 |
* varied within this range for very small effect. |
|
233 |
*/ |
|
234 |
private static final int SCHOENHAGE_BASE_CONVERSION_THRESHOLD = 8; |
|
235 |
||
2 | 236 |
//Constructors |
237 |
||
238 |
/** |
|
239 |
* Translates a byte array containing the two's-complement binary |
|
240 |
* representation of a BigInteger into a BigInteger. The input array is |
|
241 |
* assumed to be in <i>big-endian</i> byte-order: the most significant |
|
242 |
* byte is in the zeroth element. |
|
243 |
* |
|
244 |
* @param val big-endian two's-complement binary representation of |
|
245 |
* BigInteger. |
|
246 |
* @throws NumberFormatException {@code val} is zero bytes long. |
|
247 |
*/ |
|
248 |
public BigInteger(byte[] val) { |
|
249 |
if (val.length == 0) |
|
250 |
throw new NumberFormatException("Zero length BigInteger"); |
|
251 |
||
252 |
if (val[0] < 0) { |
|
253 |
mag = makePositive(val); |
|
254 |
signum = -1; |
|
255 |
} else { |
|
256 |
mag = stripLeadingZeroBytes(val); |
|
257 |
signum = (mag.length == 0 ? 0 : 1); |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
/** |
|
262 |
* This private constructor translates an int array containing the |
|
263 |
* two's-complement binary representation of a BigInteger into a |
|
264 |
* BigInteger. The input array is assumed to be in <i>big-endian</i> |
|
265 |
* int-order: the most significant int is in the zeroth element. |
|
266 |
*/ |
|
267 |
private BigInteger(int[] val) { |
|
268 |
if (val.length == 0) |
|
269 |
throw new NumberFormatException("Zero length BigInteger"); |
|
270 |
||
271 |
if (val[0] < 0) { |
|
272 |
mag = makePositive(val); |
|
273 |
signum = -1; |
|
274 |
} else { |
|
275 |
mag = trustedStripLeadingZeroInts(val); |
|
276 |
signum = (mag.length == 0 ? 0 : 1); |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
/** |
|
281 |
* Translates the sign-magnitude representation of a BigInteger into a |
|
282 |
* BigInteger. The sign is represented as an integer signum value: -1 for |
|
283 |
* negative, 0 for zero, or 1 for positive. The magnitude is a byte array |
|
284 |
* in <i>big-endian</i> byte-order: the most significant byte is in the |
|
285 |
* zeroth element. A zero-length magnitude array is permissible, and will |
|
286 |
* result in a BigInteger value of 0, whether signum is -1, 0 or 1. |
|
287 |
* |
|
288 |
* @param signum signum of the number (-1 for negative, 0 for zero, 1 |
|
289 |
* for positive). |
|
290 |
* @param magnitude big-endian binary representation of the magnitude of |
|
291 |
* the number. |
|
292 |
* @throws NumberFormatException {@code signum} is not one of the three |
|
293 |
* legal values (-1, 0, and 1), or {@code signum} is 0 and |
|
294 |
* {@code magnitude} contains one or more non-zero bytes. |
|
295 |
*/ |
|
296 |
public BigInteger(int signum, byte[] magnitude) { |
|
297 |
this.mag = stripLeadingZeroBytes(magnitude); |
|
298 |
||
299 |
if (signum < -1 || signum > 1) |
|
300 |
throw(new NumberFormatException("Invalid signum value")); |
|
301 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
302 |
if (this.mag.length == 0) { |
2 | 303 |
this.signum = 0; |
304 |
} else { |
|
305 |
if (signum == 0) |
|
306 |
throw(new NumberFormatException("signum-magnitude mismatch")); |
|
307 |
this.signum = signum; |
|
308 |
} |
|
309 |
} |
|
310 |
||
311 |
/** |
|
312 |
* A constructor for internal use that translates the sign-magnitude |
|
313 |
* representation of a BigInteger into a BigInteger. It checks the |
|
314 |
* arguments and copies the magnitude so this constructor would be |
|
315 |
* safe for external use. |
|
316 |
*/ |
|
317 |
private BigInteger(int signum, int[] magnitude) { |
|
318 |
this.mag = stripLeadingZeroInts(magnitude); |
|
319 |
||
320 |
if (signum < -1 || signum > 1) |
|
321 |
throw(new NumberFormatException("Invalid signum value")); |
|
322 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
323 |
if (this.mag.length == 0) { |
2 | 324 |
this.signum = 0; |
325 |
} else { |
|
326 |
if (signum == 0) |
|
327 |
throw(new NumberFormatException("signum-magnitude mismatch")); |
|
328 |
this.signum = signum; |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
/** |
|
333 |
* Translates the String representation of a BigInteger in the |
|
334 |
* specified radix into a BigInteger. The String representation |
|
335 |
* consists of an optional minus or plus sign followed by a |
|
336 |
* sequence of one or more digits in the specified radix. The |
|
337 |
* character-to-digit mapping is provided by {@code |
|
338 |
* Character.digit}. The String may not contain any extraneous |
|
339 |
* characters (whitespace, for example). |
|
340 |
* |
|
341 |
* @param val String representation of BigInteger. |
|
342 |
* @param radix radix to be used in interpreting {@code val}. |
|
343 |
* @throws NumberFormatException {@code val} is not a valid representation |
|
344 |
* of a BigInteger in the specified radix, or {@code radix} is |
|
345 |
* outside the range from {@link Character#MIN_RADIX} to |
|
346 |
* {@link Character#MAX_RADIX}, inclusive. |
|
347 |
* @see Character#digit |
|
348 |
*/ |
|
349 |
public BigInteger(String val, int radix) { |
|
350 |
int cursor = 0, numDigits; |
|
4182
ffdc79c88a0e
6897550: BigInteger constructor should use local cached String length
darcy
parents:
4160
diff
changeset
|
351 |
final int len = val.length(); |
2 | 352 |
|
353 |
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) |
|
354 |
throw new NumberFormatException("Radix out of range"); |
|
4182
ffdc79c88a0e
6897550: BigInteger constructor should use local cached String length
darcy
parents:
4160
diff
changeset
|
355 |
if (len == 0) |
2 | 356 |
throw new NumberFormatException("Zero length BigInteger"); |
357 |
||
358 |
// Check for at most one leading sign |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
359 |
int sign = 1; |
2 | 360 |
int index1 = val.lastIndexOf('-'); |
361 |
int index2 = val.lastIndexOf('+'); |
|
362 |
if ((index1 + index2) <= -1) { |
|
363 |
// No leading sign character or at most one leading sign character |
|
364 |
if (index1 == 0 || index2 == 0) { |
|
365 |
cursor = 1; |
|
4182
ffdc79c88a0e
6897550: BigInteger constructor should use local cached String length
darcy
parents:
4160
diff
changeset
|
366 |
if (len == 1) |
2 | 367 |
throw new NumberFormatException("Zero length BigInteger"); |
368 |
} |
|
369 |
if (index1 == 0) |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
370 |
sign = -1; |
2 | 371 |
} else |
372 |
throw new NumberFormatException("Illegal embedded sign character"); |
|
373 |
||
374 |
// Skip leading zeros and compute number of digits in magnitude |
|
375 |
while (cursor < len && |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
376 |
Character.digit(val.charAt(cursor), radix) == 0) { |
2 | 377 |
cursor++; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
378 |
} |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
379 |
|
2 | 380 |
if (cursor == len) { |
381 |
signum = 0; |
|
382 |
mag = ZERO.mag; |
|
383 |
return; |
|
384 |
} |
|
385 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
386 |
numDigits = len - cursor; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
387 |
signum = sign; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
388 |
|
2 | 389 |
// Pre-allocate array of expected size. May be too large but can |
390 |
// never be too small. Typically exact. |
|
391 |
int numBits = (int)(((numDigits * bitsPerDigit[radix]) >>> 10) + 1); |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
392 |
int numWords = (numBits + 31) >>> 5; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
393 |
int[] magnitude = new int[numWords]; |
2 | 394 |
|
395 |
// Process first (potentially short) digit group |
|
396 |
int firstGroupLen = numDigits % digitsPerInt[radix]; |
|
397 |
if (firstGroupLen == 0) |
|
398 |
firstGroupLen = digitsPerInt[radix]; |
|
399 |
String group = val.substring(cursor, cursor += firstGroupLen); |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
400 |
magnitude[numWords - 1] = Integer.parseInt(group, radix); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
401 |
if (magnitude[numWords - 1] < 0) |
2 | 402 |
throw new NumberFormatException("Illegal digit"); |
403 |
||
404 |
// Process remaining digit groups |
|
405 |
int superRadix = intRadix[radix]; |
|
406 |
int groupVal = 0; |
|
4182
ffdc79c88a0e
6897550: BigInteger constructor should use local cached String length
darcy
parents:
4160
diff
changeset
|
407 |
while (cursor < len) { |
2 | 408 |
group = val.substring(cursor, cursor += digitsPerInt[radix]); |
409 |
groupVal = Integer.parseInt(group, radix); |
|
410 |
if (groupVal < 0) |
|
411 |
throw new NumberFormatException("Illegal digit"); |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
412 |
destructiveMulAdd(magnitude, superRadix, groupVal); |
2 | 413 |
} |
414 |
// Required for cases where the array was overallocated. |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
415 |
mag = trustedStripLeadingZeroInts(magnitude); |
2 | 416 |
} |
417 |
||
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
418 |
/* |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
419 |
* Constructs a new BigInteger using a char array with radix=10. |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
420 |
* Sign is precalculated outside and not allowed in the val. |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
421 |
*/ |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
422 |
BigInteger(char[] val, int sign, int len) { |
2 | 423 |
int cursor = 0, numDigits; |
424 |
||
425 |
// Skip leading zeros and compute number of digits in magnitude |
|
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
426 |
while (cursor < len && Character.digit(val[cursor], 10) == 0) { |
2 | 427 |
cursor++; |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
428 |
} |
2 | 429 |
if (cursor == len) { |
430 |
signum = 0; |
|
431 |
mag = ZERO.mag; |
|
432 |
return; |
|
433 |
} |
|
434 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
435 |
numDigits = len - cursor; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
436 |
signum = sign; |
2 | 437 |
// Pre-allocate array of expected size |
438 |
int numWords; |
|
439 |
if (len < 10) { |
|
440 |
numWords = 1; |
|
441 |
} else { |
|
442 |
int numBits = (int)(((numDigits * bitsPerDigit[10]) >>> 10) + 1); |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
443 |
numWords = (numBits + 31) >>> 5; |
2 | 444 |
} |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
445 |
int[] magnitude = new int[numWords]; |
2 | 446 |
|
447 |
// Process first (potentially short) digit group |
|
448 |
int firstGroupLen = numDigits % digitsPerInt[10]; |
|
449 |
if (firstGroupLen == 0) |
|
450 |
firstGroupLen = digitsPerInt[10]; |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
451 |
magnitude[numWords - 1] = parseInt(val, cursor, cursor += firstGroupLen); |
2 | 452 |
|
453 |
// Process remaining digit groups |
|
454 |
while (cursor < len) { |
|
455 |
int groupVal = parseInt(val, cursor, cursor += digitsPerInt[10]); |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
456 |
destructiveMulAdd(magnitude, intRadix[10], groupVal); |
2 | 457 |
} |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
458 |
mag = trustedStripLeadingZeroInts(magnitude); |
2 | 459 |
} |
460 |
||
461 |
// Create an integer with the digits between the two indexes |
|
462 |
// Assumes start < end. The result may be negative, but it |
|
463 |
// is to be treated as an unsigned value. |
|
464 |
private int parseInt(char[] source, int start, int end) { |
|
465 |
int result = Character.digit(source[start++], 10); |
|
466 |
if (result == -1) |
|
467 |
throw new NumberFormatException(new String(source)); |
|
468 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
469 |
for (int index = start; index < end; index++) { |
2 | 470 |
int nextVal = Character.digit(source[index], 10); |
471 |
if (nextVal == -1) |
|
472 |
throw new NumberFormatException(new String(source)); |
|
473 |
result = 10*result + nextVal; |
|
474 |
} |
|
475 |
||
476 |
return result; |
|
477 |
} |
|
478 |
||
479 |
// bitsPerDigit in the given radix times 1024 |
|
480 |
// Rounded up to avoid underallocation. |
|
481 |
private static long bitsPerDigit[] = { 0, 0, |
|
482 |
1024, 1624, 2048, 2378, 2648, 2875, 3072, 3247, 3402, 3543, 3672, |
|
483 |
3790, 3899, 4001, 4096, 4186, 4271, 4350, 4426, 4498, 4567, 4633, |
|
484 |
4696, 4756, 4814, 4870, 4923, 4975, 5025, 5074, 5120, 5166, 5210, |
|
485 |
5253, 5295}; |
|
486 |
||
487 |
// Multiply x array times word y in place, and add word z |
|
488 |
private static void destructiveMulAdd(int[] x, int y, int z) { |
|
489 |
// Perform the multiplication word by word |
|
490 |
long ylong = y & LONG_MASK; |
|
491 |
long zlong = z & LONG_MASK; |
|
492 |
int len = x.length; |
|
493 |
||
494 |
long product = 0; |
|
495 |
long carry = 0; |
|
496 |
for (int i = len-1; i >= 0; i--) { |
|
497 |
product = ylong * (x[i] & LONG_MASK) + carry; |
|
498 |
x[i] = (int)product; |
|
499 |
carry = product >>> 32; |
|
500 |
} |
|
501 |
||
502 |
// Perform the addition |
|
503 |
long sum = (x[len-1] & LONG_MASK) + zlong; |
|
504 |
x[len-1] = (int)sum; |
|
505 |
carry = sum >>> 32; |
|
506 |
for (int i = len-2; i >= 0; i--) { |
|
507 |
sum = (x[i] & LONG_MASK) + carry; |
|
508 |
x[i] = (int)sum; |
|
509 |
carry = sum >>> 32; |
|
510 |
} |
|
511 |
} |
|
512 |
||
513 |
/** |
|
514 |
* Translates the decimal String representation of a BigInteger into a |
|
515 |
* BigInteger. The String representation consists of an optional minus |
|
516 |
* sign followed by a sequence of one or more decimal digits. The |
|
517 |
* character-to-digit mapping is provided by {@code Character.digit}. |
|
518 |
* The String may not contain any extraneous characters (whitespace, for |
|
519 |
* example). |
|
520 |
* |
|
521 |
* @param val decimal String representation of BigInteger. |
|
522 |
* @throws NumberFormatException {@code val} is not a valid representation |
|
523 |
* of a BigInteger. |
|
524 |
* @see Character#digit |
|
525 |
*/ |
|
526 |
public BigInteger(String val) { |
|
527 |
this(val, 10); |
|
528 |
} |
|
529 |
||
530 |
/** |
|
531 |
* Constructs a randomly generated BigInteger, uniformly distributed over |
|
4160
bda0a85afcb7
6560935: BigInteger.modPow() throws ArithmeticException for negative exponent
darcy
parents:
4151
diff
changeset
|
532 |
* the range 0 to (2<sup>{@code numBits}</sup> - 1), inclusive. |
2 | 533 |
* The uniformity of the distribution assumes that a fair source of random |
534 |
* bits is provided in {@code rnd}. Note that this constructor always |
|
535 |
* constructs a non-negative BigInteger. |
|
536 |
* |
|
537 |
* @param numBits maximum bitLength of the new BigInteger. |
|
538 |
* @param rnd source of randomness to be used in computing the new |
|
539 |
* BigInteger. |
|
540 |
* @throws IllegalArgumentException {@code numBits} is negative. |
|
541 |
* @see #bitLength() |
|
542 |
*/ |
|
543 |
public BigInteger(int numBits, Random rnd) { |
|
544 |
this(1, randomBits(numBits, rnd)); |
|
545 |
} |
|
546 |
||
547 |
private static byte[] randomBits(int numBits, Random rnd) { |
|
548 |
if (numBits < 0) |
|
549 |
throw new IllegalArgumentException("numBits must be non-negative"); |
|
550 |
int numBytes = (int)(((long)numBits+7)/8); // avoid overflow |
|
551 |
byte[] randomBits = new byte[numBytes]; |
|
552 |
||
553 |
// Generate random bytes and mask out any excess bits |
|
554 |
if (numBytes > 0) { |
|
555 |
rnd.nextBytes(randomBits); |
|
556 |
int excessBits = 8*numBytes - numBits; |
|
557 |
randomBits[0] &= (1 << (8-excessBits)) - 1; |
|
558 |
} |
|
559 |
return randomBits; |
|
560 |
} |
|
561 |
||
562 |
/** |
|
563 |
* Constructs a randomly generated positive BigInteger that is probably |
|
564 |
* prime, with the specified bitLength. |
|
565 |
* |
|
566 |
* <p>It is recommended that the {@link #probablePrime probablePrime} |
|
567 |
* method be used in preference to this constructor unless there |
|
568 |
* is a compelling need to specify a certainty. |
|
569 |
* |
|
570 |
* @param bitLength bitLength of the returned BigInteger. |
|
571 |
* @param certainty a measure of the uncertainty that the caller is |
|
572 |
* willing to tolerate. The probability that the new BigInteger |
|
573 |
* represents a prime number will exceed |
|
574 |
* (1 - 1/2<sup>{@code certainty}</sup>). The execution time of |
|
575 |
* this constructor is proportional to the value of this parameter. |
|
576 |
* @param rnd source of random bits used to select candidates to be |
|
577 |
* tested for primality. |
|
578 |
* @throws ArithmeticException {@code bitLength < 2}. |
|
579 |
* @see #bitLength() |
|
580 |
*/ |
|
581 |
public BigInteger(int bitLength, int certainty, Random rnd) { |
|
582 |
BigInteger prime; |
|
583 |
||
584 |
if (bitLength < 2) |
|
585 |
throw new ArithmeticException("bitLength < 2"); |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
586 |
prime = (bitLength < SMALL_PRIME_THRESHOLD |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
587 |
? smallPrime(bitLength, certainty, rnd) |
2 | 588 |
: largePrime(bitLength, certainty, rnd)); |
589 |
signum = 1; |
|
590 |
mag = prime.mag; |
|
591 |
} |
|
592 |
||
593 |
// Minimum size in bits that the requested prime number has |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
594 |
// before we use the large prime number generating algorithms. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
595 |
// The cutoff of 95 was chosen empirically for best performance. |
2 | 596 |
private static final int SMALL_PRIME_THRESHOLD = 95; |
597 |
||
598 |
// Certainty required to meet the spec of probablePrime |
|
599 |
private static final int DEFAULT_PRIME_CERTAINTY = 100; |
|
600 |
||
601 |
/** |
|
602 |
* Returns a positive BigInteger that is probably prime, with the |
|
603 |
* specified bitLength. The probability that a BigInteger returned |
|
604 |
* by this method is composite does not exceed 2<sup>-100</sup>. |
|
605 |
* |
|
606 |
* @param bitLength bitLength of the returned BigInteger. |
|
607 |
* @param rnd source of random bits used to select candidates to be |
|
608 |
* tested for primality. |
|
609 |
* @return a BigInteger of {@code bitLength} bits that is probably prime |
|
610 |
* @throws ArithmeticException {@code bitLength < 2}. |
|
611 |
* @see #bitLength() |
|
612 |
* @since 1.4 |
|
613 |
*/ |
|
614 |
public static BigInteger probablePrime(int bitLength, Random rnd) { |
|
615 |
if (bitLength < 2) |
|
616 |
throw new ArithmeticException("bitLength < 2"); |
|
617 |
||
618 |
return (bitLength < SMALL_PRIME_THRESHOLD ? |
|
619 |
smallPrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd) : |
|
620 |
largePrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd)); |
|
621 |
} |
|
622 |
||
623 |
/** |
|
624 |
* Find a random number of the specified bitLength that is probably prime. |
|
625 |
* This method is used for smaller primes, its performance degrades on |
|
626 |
* larger bitlengths. |
|
627 |
* |
|
628 |
* This method assumes bitLength > 1. |
|
629 |
*/ |
|
630 |
private static BigInteger smallPrime(int bitLength, int certainty, Random rnd) { |
|
631 |
int magLen = (bitLength + 31) >>> 5; |
|
632 |
int temp[] = new int[magLen]; |
|
633 |
int highBit = 1 << ((bitLength+31) & 0x1f); // High bit of high int |
|
634 |
int highMask = (highBit << 1) - 1; // Bits to keep in high int |
|
635 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
636 |
while (true) { |
2 | 637 |
// Construct a candidate |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
638 |
for (int i=0; i < magLen; i++) |
2 | 639 |
temp[i] = rnd.nextInt(); |
640 |
temp[0] = (temp[0] & highMask) | highBit; // Ensure exact length |
|
641 |
if (bitLength > 2) |
|
642 |
temp[magLen-1] |= 1; // Make odd if bitlen > 2 |
|
643 |
||
644 |
BigInteger p = new BigInteger(temp, 1); |
|
645 |
||
646 |
// Do cheap "pre-test" if applicable |
|
647 |
if (bitLength > 6) { |
|
648 |
long r = p.remainder(SMALL_PRIME_PRODUCT).longValue(); |
|
649 |
if ((r%3==0) || (r%5==0) || (r%7==0) || (r%11==0) || |
|
650 |
(r%13==0) || (r%17==0) || (r%19==0) || (r%23==0) || |
|
651 |
(r%29==0) || (r%31==0) || (r%37==0) || (r%41==0)) |
|
652 |
continue; // Candidate is composite; try another |
|
653 |
} |
|
654 |
||
655 |
// All candidates of bitLength 2 and 3 are prime by this point |
|
656 |
if (bitLength < 4) |
|
657 |
return p; |
|
658 |
||
659 |
// Do expensive test if we survive pre-test (or it's inapplicable) |
|
660 |
if (p.primeToCertainty(certainty, rnd)) |
|
661 |
return p; |
|
662 |
} |
|
663 |
} |
|
664 |
||
665 |
private static final BigInteger SMALL_PRIME_PRODUCT |
|
666 |
= valueOf(3L*5*7*11*13*17*19*23*29*31*37*41); |
|
667 |
||
668 |
/** |
|
669 |
* Find a random number of the specified bitLength that is probably prime. |
|
670 |
* This method is more appropriate for larger bitlengths since it uses |
|
671 |
* a sieve to eliminate most composites before using a more expensive |
|
672 |
* test. |
|
673 |
*/ |
|
674 |
private static BigInteger largePrime(int bitLength, int certainty, Random rnd) { |
|
675 |
BigInteger p; |
|
676 |
p = new BigInteger(bitLength, rnd).setBit(bitLength-1); |
|
677 |
p.mag[p.mag.length-1] &= 0xfffffffe; |
|
678 |
||
679 |
// Use a sieve length likely to contain the next prime number |
|
680 |
int searchLen = (bitLength / 20) * 64; |
|
681 |
BitSieve searchSieve = new BitSieve(p, searchLen); |
|
682 |
BigInteger candidate = searchSieve.retrieve(p, certainty, rnd); |
|
683 |
||
684 |
while ((candidate == null) || (candidate.bitLength() != bitLength)) { |
|
685 |
p = p.add(BigInteger.valueOf(2*searchLen)); |
|
686 |
if (p.bitLength() != bitLength) |
|
687 |
p = new BigInteger(bitLength, rnd).setBit(bitLength-1); |
|
688 |
p.mag[p.mag.length-1] &= 0xfffffffe; |
|
689 |
searchSieve = new BitSieve(p, searchLen); |
|
690 |
candidate = searchSieve.retrieve(p, certainty, rnd); |
|
691 |
} |
|
692 |
return candidate; |
|
693 |
} |
|
694 |
||
695 |
/** |
|
696 |
* Returns the first integer greater than this {@code BigInteger} that |
|
697 |
* is probably prime. The probability that the number returned by this |
|
698 |
* method is composite does not exceed 2<sup>-100</sup>. This method will |
|
699 |
* never skip over a prime when searching: if it returns {@code p}, there |
|
700 |
* is no prime {@code q} such that {@code this < q < p}. |
|
701 |
* |
|
702 |
* @return the first integer greater than this {@code BigInteger} that |
|
703 |
* is probably prime. |
|
704 |
* @throws ArithmeticException {@code this < 0}. |
|
705 |
* @since 1.5 |
|
706 |
*/ |
|
707 |
public BigInteger nextProbablePrime() { |
|
708 |
if (this.signum < 0) |
|
709 |
throw new ArithmeticException("start < 0: " + this); |
|
710 |
||
711 |
// Handle trivial cases |
|
712 |
if ((this.signum == 0) || this.equals(ONE)) |
|
713 |
return TWO; |
|
714 |
||
715 |
BigInteger result = this.add(ONE); |
|
716 |
||
717 |
// Fastpath for small numbers |
|
718 |
if (result.bitLength() < SMALL_PRIME_THRESHOLD) { |
|
719 |
||
720 |
// Ensure an odd number |
|
721 |
if (!result.testBit(0)) |
|
722 |
result = result.add(ONE); |
|
723 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
724 |
while (true) { |
2 | 725 |
// Do cheap "pre-test" if applicable |
726 |
if (result.bitLength() > 6) { |
|
727 |
long r = result.remainder(SMALL_PRIME_PRODUCT).longValue(); |
|
728 |
if ((r%3==0) || (r%5==0) || (r%7==0) || (r%11==0) || |
|
729 |
(r%13==0) || (r%17==0) || (r%19==0) || (r%23==0) || |
|
730 |
(r%29==0) || (r%31==0) || (r%37==0) || (r%41==0)) { |
|
731 |
result = result.add(TWO); |
|
732 |
continue; // Candidate is composite; try another |
|
733 |
} |
|
734 |
} |
|
735 |
||
736 |
// All candidates of bitLength 2 and 3 are prime by this point |
|
737 |
if (result.bitLength() < 4) |
|
738 |
return result; |
|
739 |
||
740 |
// The expensive test |
|
741 |
if (result.primeToCertainty(DEFAULT_PRIME_CERTAINTY, null)) |
|
742 |
return result; |
|
743 |
||
744 |
result = result.add(TWO); |
|
745 |
} |
|
746 |
} |
|
747 |
||
748 |
// Start at previous even number |
|
749 |
if (result.testBit(0)) |
|
750 |
result = result.subtract(ONE); |
|
751 |
||
752 |
// Looking for the next large prime |
|
753 |
int searchLen = (result.bitLength() / 20) * 64; |
|
754 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
755 |
while (true) { |
2 | 756 |
BitSieve searchSieve = new BitSieve(result, searchLen); |
757 |
BigInteger candidate = searchSieve.retrieve(result, |
|
758 |
DEFAULT_PRIME_CERTAINTY, null); |
|
759 |
if (candidate != null) |
|
760 |
return candidate; |
|
761 |
result = result.add(BigInteger.valueOf(2 * searchLen)); |
|
762 |
} |
|
763 |
} |
|
764 |
||
765 |
/** |
|
766 |
* Returns {@code true} if this BigInteger is probably prime, |
|
767 |
* {@code false} if it's definitely composite. |
|
768 |
* |
|
769 |
* This method assumes bitLength > 2. |
|
770 |
* |
|
771 |
* @param certainty a measure of the uncertainty that the caller is |
|
772 |
* willing to tolerate: if the call returns {@code true} |
|
773 |
* the probability that this BigInteger is prime exceeds |
|
774 |
* {@code (1 - 1/2<sup>certainty</sup>)}. The execution time of |
|
775 |
* this method is proportional to the value of this parameter. |
|
776 |
* @return {@code true} if this BigInteger is probably prime, |
|
777 |
* {@code false} if it's definitely composite. |
|
778 |
*/ |
|
779 |
boolean primeToCertainty(int certainty, Random random) { |
|
780 |
int rounds = 0; |
|
781 |
int n = (Math.min(certainty, Integer.MAX_VALUE-1)+1)/2; |
|
782 |
||
783 |
// The relationship between the certainty and the number of rounds |
|
784 |
// we perform is given in the draft standard ANSI X9.80, "PRIME |
|
785 |
// NUMBER GENERATION, PRIMALITY TESTING, AND PRIMALITY CERTIFICATES". |
|
786 |
int sizeInBits = this.bitLength(); |
|
787 |
if (sizeInBits < 100) { |
|
788 |
rounds = 50; |
|
789 |
rounds = n < rounds ? n : rounds; |
|
790 |
return passesMillerRabin(rounds, random); |
|
791 |
} |
|
792 |
||
793 |
if (sizeInBits < 256) { |
|
794 |
rounds = 27; |
|
795 |
} else if (sizeInBits < 512) { |
|
796 |
rounds = 15; |
|
797 |
} else if (sizeInBits < 768) { |
|
798 |
rounds = 8; |
|
799 |
} else if (sizeInBits < 1024) { |
|
800 |
rounds = 4; |
|
801 |
} else { |
|
802 |
rounds = 2; |
|
803 |
} |
|
804 |
rounds = n < rounds ? n : rounds; |
|
805 |
||
806 |
return passesMillerRabin(rounds, random) && passesLucasLehmer(); |
|
807 |
} |
|
808 |
||
809 |
/** |
|
810 |
* Returns true iff this BigInteger is a Lucas-Lehmer probable prime. |
|
811 |
* |
|
812 |
* The following assumptions are made: |
|
813 |
* This BigInteger is a positive, odd number. |
|
814 |
*/ |
|
815 |
private boolean passesLucasLehmer() { |
|
816 |
BigInteger thisPlusOne = this.add(ONE); |
|
817 |
||
818 |
// Step 1 |
|
819 |
int d = 5; |
|
820 |
while (jacobiSymbol(d, this) != -1) { |
|
821 |
// 5, -7, 9, -11, ... |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
822 |
d = (d < 0) ? Math.abs(d)+2 : -(d+2); |
2 | 823 |
} |
824 |
||
825 |
// Step 2 |
|
826 |
BigInteger u = lucasLehmerSequence(d, thisPlusOne, this); |
|
827 |
||
828 |
// Step 3 |
|
829 |
return u.mod(this).equals(ZERO); |
|
830 |
} |
|
831 |
||
832 |
/** |
|
833 |
* Computes Jacobi(p,n). |
|
834 |
* Assumes n positive, odd, n>=3. |
|
835 |
*/ |
|
836 |
private static int jacobiSymbol(int p, BigInteger n) { |
|
837 |
if (p == 0) |
|
838 |
return 0; |
|
839 |
||
840 |
// Algorithm and comments adapted from Colin Plumb's C library. |
|
841 |
int j = 1; |
|
842 |
int u = n.mag[n.mag.length-1]; |
|
843 |
||
844 |
// Make p positive |
|
845 |
if (p < 0) { |
|
846 |
p = -p; |
|
847 |
int n8 = u & 7; |
|
848 |
if ((n8 == 3) || (n8 == 7)) |
|
849 |
j = -j; // 3 (011) or 7 (111) mod 8 |
|
850 |
} |
|
851 |
||
852 |
// Get rid of factors of 2 in p |
|
853 |
while ((p & 3) == 0) |
|
854 |
p >>= 2; |
|
855 |
if ((p & 1) == 0) { |
|
856 |
p >>= 1; |
|
857 |
if (((u ^ (u>>1)) & 2) != 0) |
|
858 |
j = -j; // 3 (011) or 5 (101) mod 8 |
|
859 |
} |
|
860 |
if (p == 1) |
|
861 |
return j; |
|
862 |
// Then, apply quadratic reciprocity |
|
863 |
if ((p & u & 2) != 0) // p = u = 3 (mod 4)? |
|
864 |
j = -j; |
|
865 |
// And reduce u mod p |
|
866 |
u = n.mod(BigInteger.valueOf(p)).intValue(); |
|
867 |
||
868 |
// Now compute Jacobi(u,p), u < p |
|
869 |
while (u != 0) { |
|
870 |
while ((u & 3) == 0) |
|
871 |
u >>= 2; |
|
872 |
if ((u & 1) == 0) { |
|
873 |
u >>= 1; |
|
874 |
if (((p ^ (p>>1)) & 2) != 0) |
|
875 |
j = -j; // 3 (011) or 5 (101) mod 8 |
|
876 |
} |
|
877 |
if (u == 1) |
|
878 |
return j; |
|
879 |
// Now both u and p are odd, so use quadratic reciprocity |
|
880 |
assert (u < p); |
|
881 |
int t = u; u = p; p = t; |
|
882 |
if ((u & p & 2) != 0) // u = p = 3 (mod 4)? |
|
883 |
j = -j; |
|
884 |
// Now u >= p, so it can be reduced |
|
885 |
u %= p; |
|
886 |
} |
|
887 |
return 0; |
|
888 |
} |
|
889 |
||
890 |
private static BigInteger lucasLehmerSequence(int z, BigInteger k, BigInteger n) { |
|
891 |
BigInteger d = BigInteger.valueOf(z); |
|
892 |
BigInteger u = ONE; BigInteger u2; |
|
893 |
BigInteger v = ONE; BigInteger v2; |
|
894 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
895 |
for (int i=k.bitLength()-2; i >= 0; i--) { |
2 | 896 |
u2 = u.multiply(v).mod(n); |
897 |
||
898 |
v2 = v.square().add(d.multiply(u.square())).mod(n); |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
899 |
if (v2.testBit(0)) |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
900 |
v2 = v2.subtract(n); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
901 |
|
2 | 902 |
v2 = v2.shiftRight(1); |
903 |
||
904 |
u = u2; v = v2; |
|
905 |
if (k.testBit(i)) { |
|
906 |
u2 = u.add(v).mod(n); |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
907 |
if (u2.testBit(0)) |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
908 |
u2 = u2.subtract(n); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
909 |
|
2 | 910 |
u2 = u2.shiftRight(1); |
911 |
v2 = v.add(d.multiply(u)).mod(n); |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
912 |
if (v2.testBit(0)) |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
913 |
v2 = v2.subtract(n); |
2 | 914 |
v2 = v2.shiftRight(1); |
915 |
||
916 |
u = u2; v = v2; |
|
917 |
} |
|
918 |
} |
|
919 |
return u; |
|
920 |
} |
|
921 |
||
922 |
/** |
|
923 |
* Returns true iff this BigInteger passes the specified number of |
|
924 |
* Miller-Rabin tests. This test is taken from the DSA spec (NIST FIPS |
|
925 |
* 186-2). |
|
926 |
* |
|
927 |
* The following assumptions are made: |
|
928 |
* This BigInteger is a positive, odd number greater than 2. |
|
929 |
* iterations<=50. |
|
930 |
*/ |
|
931 |
private boolean passesMillerRabin(int iterations, Random rnd) { |
|
932 |
// Find a and m such that m is odd and this == 1 + 2**a * m |
|
933 |
BigInteger thisMinusOne = this.subtract(ONE); |
|
934 |
BigInteger m = thisMinusOne; |
|
935 |
int a = m.getLowestSetBit(); |
|
936 |
m = m.shiftRight(a); |
|
937 |
||
938 |
// Do the tests |
|
939 |
if (rnd == null) { |
|
20756
d998355e6a5c
7189139: BigInteger's staticRandom field can be a source of bottlenecks.
bpb
parents:
19585
diff
changeset
|
940 |
rnd = ThreadLocalRandom.current(); |
2 | 941 |
} |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
942 |
for (int i=0; i < iterations; i++) { |
2 | 943 |
// Generate a uniform random on (1, this) |
944 |
BigInteger b; |
|
945 |
do { |
|
946 |
b = new BigInteger(this.bitLength(), rnd); |
|
947 |
} while (b.compareTo(ONE) <= 0 || b.compareTo(this) >= 0); |
|
948 |
||
949 |
int j = 0; |
|
950 |
BigInteger z = b.modPow(m, this); |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
951 |
while (!((j == 0 && z.equals(ONE)) || z.equals(thisMinusOne))) { |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
952 |
if (j > 0 && z.equals(ONE) || ++j == a) |
2 | 953 |
return false; |
954 |
z = z.modPow(TWO, this); |
|
955 |
} |
|
956 |
} |
|
957 |
return true; |
|
958 |
} |
|
959 |
||
960 |
/** |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
961 |
* This internal constructor differs from its public cousin |
2 | 962 |
* with the arguments reversed in two ways: it assumes that its |
963 |
* arguments are correct, and it doesn't copy the magnitude array. |
|
964 |
*/ |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
965 |
BigInteger(int[] magnitude, int signum) { |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
966 |
this.signum = (magnitude.length == 0 ? 0 : signum); |
2 | 967 |
this.mag = magnitude; |
968 |
} |
|
969 |
||
970 |
/** |
|
971 |
* This private constructor is for internal use and assumes that its |
|
972 |
* arguments are correct. |
|
973 |
*/ |
|
974 |
private BigInteger(byte[] magnitude, int signum) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
975 |
this.signum = (magnitude.length == 0 ? 0 : signum); |
2 | 976 |
this.mag = stripLeadingZeroBytes(magnitude); |
977 |
} |
|
978 |
||
979 |
//Static Factory Methods |
|
980 |
||
981 |
/** |
|
982 |
* Returns a BigInteger whose value is equal to that of the |
|
983 |
* specified {@code long}. This "static factory method" is |
|
984 |
* provided in preference to a ({@code long}) constructor |
|
985 |
* because it allows for reuse of frequently used BigIntegers. |
|
986 |
* |
|
987 |
* @param val value of the BigInteger to return. |
|
988 |
* @return a BigInteger with the specified value. |
|
989 |
*/ |
|
990 |
public static BigInteger valueOf(long val) { |
|
991 |
// If -MAX_CONSTANT < val < MAX_CONSTANT, return stashed constant |
|
992 |
if (val == 0) |
|
993 |
return ZERO; |
|
994 |
if (val > 0 && val <= MAX_CONSTANT) |
|
995 |
return posConst[(int) val]; |
|
996 |
else if (val < 0 && val >= -MAX_CONSTANT) |
|
997 |
return negConst[(int) -val]; |
|
998 |
||
999 |
return new BigInteger(val); |
|
1000 |
} |
|
1001 |
||
1002 |
/** |
|
1003 |
* Constructs a BigInteger with the specified value, which may not be zero. |
|
1004 |
*/ |
|
1005 |
private BigInteger(long val) { |
|
1006 |
if (val < 0) { |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1007 |
val = -val; |
2 | 1008 |
signum = -1; |
1009 |
} else { |
|
1010 |
signum = 1; |
|
1011 |
} |
|
1012 |
||
1013 |
int highWord = (int)(val >>> 32); |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1014 |
if (highWord == 0) { |
2 | 1015 |
mag = new int[1]; |
1016 |
mag[0] = (int)val; |
|
1017 |
} else { |
|
1018 |
mag = new int[2]; |
|
1019 |
mag[0] = highWord; |
|
1020 |
mag[1] = (int)val; |
|
1021 |
} |
|
1022 |
} |
|
1023 |
||
1024 |
/** |
|
1025 |
* Returns a BigInteger with the given two's complement representation. |
|
1026 |
* Assumes that the input array will not be modified (the returned |
|
1027 |
* BigInteger will reference the input array if feasible). |
|
1028 |
*/ |
|
1029 |
private static BigInteger valueOf(int val[]) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1030 |
return (val[0] > 0 ? new BigInteger(val, 1) : new BigInteger(val)); |
2 | 1031 |
} |
1032 |
||
1033 |
// Constants |
|
1034 |
||
1035 |
/** |
|
1036 |
* Initialize static constant array when class is loaded. |
|
1037 |
*/ |
|
1038 |
private final static int MAX_CONSTANT = 16; |
|
1039 |
private static BigInteger posConst[] = new BigInteger[MAX_CONSTANT+1]; |
|
1040 |
private static BigInteger negConst[] = new BigInteger[MAX_CONSTANT+1]; |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1041 |
|
18548 | 1042 |
/** |
1043 |
* The cache of powers of each radix. This allows us to not have to |
|
1044 |
* recalculate powers of radix^(2^n) more than once. This speeds |
|
1045 |
* Schoenhage recursive base conversion significantly. |
|
1046 |
*/ |
|
18588
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
1047 |
private static volatile BigInteger[][] powerCache; |
18548 | 1048 |
|
1049 |
/** The cache of logarithms of radices for base conversion. */ |
|
1050 |
private static final double[] logCache; |
|
1051 |
||
1052 |
/** The natural log of 2. This is used in computing cache indices. */ |
|
1053 |
private static final double LOG_TWO = Math.log(2.0); |
|
1054 |
||
2 | 1055 |
static { |
1056 |
for (int i = 1; i <= MAX_CONSTANT; i++) { |
|
1057 |
int[] magnitude = new int[1]; |
|
1058 |
magnitude[0] = i; |
|
1059 |
posConst[i] = new BigInteger(magnitude, 1); |
|
1060 |
negConst[i] = new BigInteger(magnitude, -1); |
|
1061 |
} |
|
18548 | 1062 |
|
1063 |
/* |
|
1064 |
* Initialize the cache of radix^(2^x) values used for base conversion |
|
1065 |
* with just the very first value. Additional values will be created |
|
1066 |
* on demand. |
|
1067 |
*/ |
|
18588
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
1068 |
powerCache = new BigInteger[Character.MAX_RADIX+1][]; |
18548 | 1069 |
logCache = new double[Character.MAX_RADIX+1]; |
1070 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1071 |
for (int i=Character.MIN_RADIX; i <= Character.MAX_RADIX; i++) { |
18588
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
1072 |
powerCache[i] = new BigInteger[] { BigInteger.valueOf(i) }; |
18548 | 1073 |
logCache[i] = Math.log(i); |
1074 |
} |
|
2 | 1075 |
} |
1076 |
||
1077 |
/** |
|
1078 |
* The BigInteger constant zero. |
|
1079 |
* |
|
1080 |
* @since 1.2 |
|
1081 |
*/ |
|
1082 |
public static final BigInteger ZERO = new BigInteger(new int[0], 0); |
|
1083 |
||
1084 |
/** |
|
1085 |
* The BigInteger constant one. |
|
1086 |
* |
|
1087 |
* @since 1.2 |
|
1088 |
*/ |
|
1089 |
public static final BigInteger ONE = valueOf(1); |
|
1090 |
||
1091 |
/** |
|
1092 |
* The BigInteger constant two. (Not exported.) |
|
1093 |
*/ |
|
1094 |
private static final BigInteger TWO = valueOf(2); |
|
1095 |
||
1096 |
/** |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1097 |
* The BigInteger constant -1. (Not exported.) |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1098 |
*/ |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1099 |
private static final BigInteger NEGATIVE_ONE = valueOf(-1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1100 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1101 |
/** |
2 | 1102 |
* The BigInteger constant ten. |
1103 |
* |
|
1104 |
* @since 1.5 |
|
1105 |
*/ |
|
1106 |
public static final BigInteger TEN = valueOf(10); |
|
1107 |
||
1108 |
// Arithmetic Operations |
|
1109 |
||
1110 |
/** |
|
1111 |
* Returns a BigInteger whose value is {@code (this + val)}. |
|
1112 |
* |
|
1113 |
* @param val value to be added to this BigInteger. |
|
1114 |
* @return {@code this + val} |
|
1115 |
*/ |
|
1116 |
public BigInteger add(BigInteger val) { |
|
1117 |
if (val.signum == 0) |
|
1118 |
return this; |
|
1119 |
if (signum == 0) |
|
1120 |
return val; |
|
1121 |
if (val.signum == signum) |
|
1122 |
return new BigInteger(add(mag, val.mag), signum); |
|
1123 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1124 |
int cmp = compareMagnitude(val); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1125 |
if (cmp == 0) |
2 | 1126 |
return ZERO; |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1127 |
int[] resultMag = (cmp > 0 ? subtract(mag, val.mag) |
2 | 1128 |
: subtract(val.mag, mag)); |
1129 |
resultMag = trustedStripLeadingZeroInts(resultMag); |
|
1130 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1131 |
return new BigInteger(resultMag, cmp == signum ? 1 : -1); |
2 | 1132 |
} |
1133 |
||
1134 |
/** |
|
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1135 |
* Package private methods used by BigDecimal code to add a BigInteger |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1136 |
* with a long. Assumes val is not equal to INFLATED. |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1137 |
*/ |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1138 |
BigInteger add(long val) { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1139 |
if (val == 0) |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1140 |
return this; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1141 |
if (signum == 0) |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1142 |
return valueOf(val); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1143 |
if (Long.signum(val) == signum) |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1144 |
return new BigInteger(add(mag, Math.abs(val)), signum); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1145 |
int cmp = compareMagnitude(val); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1146 |
if (cmp == 0) |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1147 |
return ZERO; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1148 |
int[] resultMag = (cmp > 0 ? subtract(mag, Math.abs(val)) : subtract(Math.abs(val), mag)); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1149 |
resultMag = trustedStripLeadingZeroInts(resultMag); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1150 |
return new BigInteger(resultMag, cmp == signum ? 1 : -1); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1151 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1152 |
|
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1153 |
/** |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1154 |
* Adds the contents of the int array x and long value val. This |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1155 |
* method allocates a new int array to hold the answer and returns |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1156 |
* a reference to that array. Assumes x.length > 0 and val is |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1157 |
* non-negative |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1158 |
*/ |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1159 |
private static int[] add(int[] x, long val) { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1160 |
int[] y; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1161 |
long sum = 0; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1162 |
int xIndex = x.length; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1163 |
int[] result; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1164 |
int highWord = (int)(val >>> 32); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1165 |
if (highWord == 0) { |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1166 |
result = new int[xIndex]; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1167 |
sum = (x[--xIndex] & LONG_MASK) + val; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1168 |
result[xIndex] = (int)sum; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1169 |
} else { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1170 |
if (xIndex == 1) { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1171 |
result = new int[2]; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1172 |
sum = val + (x[0] & LONG_MASK); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1173 |
result[1] = (int)sum; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1174 |
result[0] = (int)(sum >>> 32); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1175 |
return result; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1176 |
} else { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1177 |
result = new int[xIndex]; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1178 |
sum = (x[--xIndex] & LONG_MASK) + (val & LONG_MASK); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1179 |
result[xIndex] = (int)sum; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1180 |
sum = (x[--xIndex] & LONG_MASK) + (highWord & LONG_MASK) + (sum >>> 32); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1181 |
result[xIndex] = (int)sum; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1182 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1183 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1184 |
// Copy remainder of longer number while carry propagation is required |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1185 |
boolean carry = (sum >>> 32 != 0); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1186 |
while (xIndex > 0 && carry) |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1187 |
carry = ((result[--xIndex] = x[xIndex] + 1) == 0); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1188 |
// Copy remainder of longer number |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1189 |
while (xIndex > 0) |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1190 |
result[--xIndex] = x[xIndex]; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1191 |
// Grow result if necessary |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1192 |
if (carry) { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1193 |
int bigger[] = new int[result.length + 1]; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1194 |
System.arraycopy(result, 0, bigger, 1, result.length); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1195 |
bigger[0] = 0x01; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1196 |
return bigger; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1197 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1198 |
return result; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1199 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1200 |
|
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1201 |
/** |
2 | 1202 |
* Adds the contents of the int arrays x and y. This method allocates |
1203 |
* a new int array to hold the answer and returns a reference to that |
|
1204 |
* array. |
|
1205 |
*/ |
|
1206 |
private static int[] add(int[] x, int[] y) { |
|
1207 |
// If x is shorter, swap the two arrays |
|
1208 |
if (x.length < y.length) { |
|
1209 |
int[] tmp = x; |
|
1210 |
x = y; |
|
1211 |
y = tmp; |
|
1212 |
} |
|
1213 |
||
1214 |
int xIndex = x.length; |
|
1215 |
int yIndex = y.length; |
|
1216 |
int result[] = new int[xIndex]; |
|
1217 |
long sum = 0; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1218 |
if (yIndex == 1) { |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1219 |
sum = (x[--xIndex] & LONG_MASK) + (y[0] & LONG_MASK) ; |
2 | 1220 |
result[xIndex] = (int)sum; |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1221 |
} else { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1222 |
// Add common parts of both numbers |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1223 |
while (yIndex > 0) { |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1224 |
sum = (x[--xIndex] & LONG_MASK) + |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1225 |
(y[--yIndex] & LONG_MASK) + (sum >>> 32); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1226 |
result[xIndex] = (int)sum; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1227 |
} |
2 | 1228 |
} |
1229 |
// Copy remainder of longer number while carry propagation is required |
|
1230 |
boolean carry = (sum >>> 32 != 0); |
|
1231 |
while (xIndex > 0 && carry) |
|
1232 |
carry = ((result[--xIndex] = x[xIndex] + 1) == 0); |
|
1233 |
||
1234 |
// Copy remainder of longer number |
|
1235 |
while (xIndex > 0) |
|
1236 |
result[--xIndex] = x[xIndex]; |
|
1237 |
||
1238 |
// Grow result if necessary |
|
1239 |
if (carry) { |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1240 |
int bigger[] = new int[result.length + 1]; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1241 |
System.arraycopy(result, 0, bigger, 1, result.length); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1242 |
bigger[0] = 0x01; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1243 |
return bigger; |
2 | 1244 |
} |
1245 |
return result; |
|
1246 |
} |
|
1247 |
||
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1248 |
private static int[] subtract(long val, int[] little) { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1249 |
int highWord = (int)(val >>> 32); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1250 |
if (highWord == 0) { |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1251 |
int result[] = new int[1]; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1252 |
result[0] = (int)(val - (little[0] & LONG_MASK)); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1253 |
return result; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1254 |
} else { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1255 |
int result[] = new int[2]; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1256 |
if (little.length == 1) { |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1257 |
long difference = ((int)val & LONG_MASK) - (little[0] & LONG_MASK); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1258 |
result[1] = (int)difference; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1259 |
// Subtract remainder of longer number while borrow propagates |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1260 |
boolean borrow = (difference >> 32 != 0); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1261 |
if (borrow) { |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1262 |
result[0] = highWord - 1; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1263 |
} else { // Copy remainder of longer number |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1264 |
result[0] = highWord; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1265 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1266 |
return result; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1267 |
} else { // little.length == 2 |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1268 |
long difference = ((int)val & LONG_MASK) - (little[1] & LONG_MASK); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1269 |
result[1] = (int)difference; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1270 |
difference = (highWord & LONG_MASK) - (little[0] & LONG_MASK) + (difference >> 32); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1271 |
result[0] = (int)difference; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1272 |
return result; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1273 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1274 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1275 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1276 |
|
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1277 |
/** |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1278 |
* Subtracts the contents of the second argument (val) from the |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1279 |
* first (big). The first int array (big) must represent a larger number |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1280 |
* than the second. This method allocates the space necessary to hold the |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1281 |
* answer. |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1282 |
* assumes val >= 0 |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1283 |
*/ |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1284 |
private static int[] subtract(int[] big, long val) { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1285 |
int highWord = (int)(val >>> 32); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1286 |
int bigIndex = big.length; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1287 |
int result[] = new int[bigIndex]; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1288 |
long difference = 0; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1289 |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1290 |
if (highWord == 0) { |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1291 |
difference = (big[--bigIndex] & LONG_MASK) - val; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1292 |
result[bigIndex] = (int)difference; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1293 |
} else { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1294 |
difference = (big[--bigIndex] & LONG_MASK) - (val & LONG_MASK); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1295 |
result[bigIndex] = (int)difference; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1296 |
difference = (big[--bigIndex] & LONG_MASK) - (highWord & LONG_MASK) + (difference >> 32); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1297 |
result[bigIndex] = (int)difference; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1298 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1299 |
|
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1300 |
// Subtract remainder of longer number while borrow propagates |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1301 |
boolean borrow = (difference >> 32 != 0); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1302 |
while (bigIndex > 0 && borrow) |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1303 |
borrow = ((result[--bigIndex] = big[bigIndex] - 1) == -1); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1304 |
|
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1305 |
// Copy remainder of longer number |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1306 |
while (bigIndex > 0) |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1307 |
result[--bigIndex] = big[bigIndex]; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1308 |
|
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1309 |
return result; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1310 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1311 |
|
2 | 1312 |
/** |
1313 |
* Returns a BigInteger whose value is {@code (this - val)}. |
|
1314 |
* |
|
1315 |
* @param val value to be subtracted from this BigInteger. |
|
1316 |
* @return {@code this - val} |
|
1317 |
*/ |
|
1318 |
public BigInteger subtract(BigInteger val) { |
|
1319 |
if (val.signum == 0) |
|
1320 |
return this; |
|
1321 |
if (signum == 0) |
|
1322 |
return val.negate(); |
|
1323 |
if (val.signum != signum) |
|
1324 |
return new BigInteger(add(mag, val.mag), signum); |
|
1325 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1326 |
int cmp = compareMagnitude(val); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1327 |
if (cmp == 0) |
2 | 1328 |
return ZERO; |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1329 |
int[] resultMag = (cmp > 0 ? subtract(mag, val.mag) |
2 | 1330 |
: subtract(val.mag, mag)); |
1331 |
resultMag = trustedStripLeadingZeroInts(resultMag); |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1332 |
return new BigInteger(resultMag, cmp == signum ? 1 : -1); |
2 | 1333 |
} |
1334 |
||
1335 |
/** |
|
1336 |
* Subtracts the contents of the second int arrays (little) from the |
|
1337 |
* first (big). The first int array (big) must represent a larger number |
|
1338 |
* than the second. This method allocates the space necessary to hold the |
|
1339 |
* answer. |
|
1340 |
*/ |
|
1341 |
private static int[] subtract(int[] big, int[] little) { |
|
1342 |
int bigIndex = big.length; |
|
1343 |
int result[] = new int[bigIndex]; |
|
1344 |
int littleIndex = little.length; |
|
1345 |
long difference = 0; |
|
1346 |
||
1347 |
// Subtract common parts of both numbers |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1348 |
while (littleIndex > 0) { |
2 | 1349 |
difference = (big[--bigIndex] & LONG_MASK) - |
1350 |
(little[--littleIndex] & LONG_MASK) + |
|
1351 |
(difference >> 32); |
|
1352 |
result[bigIndex] = (int)difference; |
|
1353 |
} |
|
1354 |
||
1355 |
// Subtract remainder of longer number while borrow propagates |
|
1356 |
boolean borrow = (difference >> 32 != 0); |
|
1357 |
while (bigIndex > 0 && borrow) |
|
1358 |
borrow = ((result[--bigIndex] = big[bigIndex] - 1) == -1); |
|
1359 |
||
1360 |
// Copy remainder of longer number |
|
1361 |
while (bigIndex > 0) |
|
1362 |
result[--bigIndex] = big[bigIndex]; |
|
1363 |
||
1364 |
return result; |
|
1365 |
} |
|
1366 |
||
1367 |
/** |
|
1368 |
* Returns a BigInteger whose value is {@code (this * val)}. |
|
1369 |
* |
|
1370 |
* @param val value to be multiplied by this BigInteger. |
|
1371 |
* @return {@code this * val} |
|
1372 |
*/ |
|
1373 |
public BigInteger multiply(BigInteger val) { |
|
1374 |
if (val.signum == 0 || signum == 0) |
|
1375 |
return ZERO; |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1376 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1377 |
int xlen = mag.length; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1378 |
int ylen = val.mag.length; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1379 |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1380 |
if ((xlen < KARATSUBA_THRESHOLD) || (ylen < KARATSUBA_THRESHOLD)) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1381 |
int resultSign = signum == val.signum ? 1 : -1; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1382 |
if (val.mag.length == 1) { |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1383 |
return multiplyByInt(mag,val.mag[0], resultSign); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1384 |
} |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1385 |
if (mag.length == 1) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1386 |
return multiplyByInt(val.mag,mag[0], resultSign); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1387 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1388 |
int[] result = multiplyToLen(mag, xlen, |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1389 |
val.mag, ylen, null); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1390 |
result = trustedStripLeadingZeroInts(result); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1391 |
return new BigInteger(result, resultSign); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1392 |
} else { |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1393 |
if ((xlen < TOOM_COOK_THRESHOLD) && (ylen < TOOM_COOK_THRESHOLD)) { |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1394 |
return multiplyKaratsuba(this, val); |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1395 |
} else { |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1396 |
return multiplyToomCook3(this, val); |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1397 |
} |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1398 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1399 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1400 |
|
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1401 |
private static BigInteger multiplyByInt(int[] x, int y, int sign) { |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1402 |
if (Integer.bitCount(y) == 1) { |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1403 |
return new BigInteger(shiftLeft(x,Integer.numberOfTrailingZeros(y)), sign); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1404 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1405 |
int xlen = x.length; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1406 |
int[] rmag = new int[xlen + 1]; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1407 |
long carry = 0; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1408 |
long yl = y & LONG_MASK; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1409 |
int rstart = rmag.length - 1; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1410 |
for (int i = xlen - 1; i >= 0; i--) { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1411 |
long product = (x[i] & LONG_MASK) * yl + carry; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1412 |
rmag[rstart--] = (int)product; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1413 |
carry = product >>> 32; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1414 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1415 |
if (carry == 0L) { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1416 |
rmag = java.util.Arrays.copyOfRange(rmag, 1, rmag.length); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1417 |
} else { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1418 |
rmag[rstart] = (int)carry; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1419 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1420 |
return new BigInteger(rmag, sign); |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1421 |
} |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1422 |
|
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1423 |
/** |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1424 |
* Package private methods used by BigDecimal code to multiply a BigInteger |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1425 |
* with a long. Assumes v is not equal to INFLATED. |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1426 |
*/ |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1427 |
BigInteger multiply(long v) { |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1428 |
if (v == 0 || signum == 0) |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1429 |
return ZERO; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1430 |
if (v == BigDecimal.INFLATED) |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1431 |
return multiply(BigInteger.valueOf(v)); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1432 |
int rsign = (v > 0 ? signum : -signum); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1433 |
if (v < 0) |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1434 |
v = -v; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1435 |
long dh = v >>> 32; // higher order bits |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1436 |
long dl = v & LONG_MASK; // lower order bits |
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 |
int xlen = mag.length; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1439 |
int[] value = mag; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1440 |
int[] rmag = (dh == 0L) ? (new int[xlen + 1]) : (new int[xlen + 2]); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1441 |
long carry = 0; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1442 |
int rstart = rmag.length - 1; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1443 |
for (int i = xlen - 1; i >= 0; i--) { |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1444 |
long product = (value[i] & LONG_MASK) * dl + carry; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1445 |
rmag[rstart--] = (int)product; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1446 |
carry = product >>> 32; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1447 |
} |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1448 |
rmag[rstart] = (int)carry; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1449 |
if (dh != 0L) { |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1450 |
carry = 0; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1451 |
rstart = rmag.length - 2; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1452 |
for (int i = xlen - 1; i >= 0; i--) { |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1453 |
long product = (value[i] & LONG_MASK) * dh + |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1454 |
(rmag[rstart] & LONG_MASK) + carry; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1455 |
rmag[rstart--] = (int)product; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1456 |
carry = product >>> 32; |
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 |
rmag[0] = (int)carry; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1459 |
} |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1460 |
if (carry == 0L) |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1461 |
rmag = java.util.Arrays.copyOfRange(rmag, 1, rmag.length); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1462 |
return new BigInteger(rmag, rsign); |
2 | 1463 |
} |
1464 |
||
1465 |
/** |
|
1466 |
* Multiplies int arrays x and y to the specified lengths and places |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1467 |
* the result into z. There will be no leading zeros in the resultant array. |
2 | 1468 |
*/ |
1469 |
private int[] multiplyToLen(int[] x, int xlen, int[] y, int ylen, int[] z) { |
|
1470 |
int xstart = xlen - 1; |
|
1471 |
int ystart = ylen - 1; |
|
1472 |
||
1473 |
if (z == null || z.length < (xlen+ ylen)) |
|
1474 |
z = new int[xlen+ylen]; |
|
1475 |
||
1476 |
long carry = 0; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1477 |
for (int j=ystart, k=ystart+1+xstart; j >= 0; j--, k--) { |
2 | 1478 |
long product = (y[j] & LONG_MASK) * |
1479 |
(x[xstart] & LONG_MASK) + carry; |
|
1480 |
z[k] = (int)product; |
|
1481 |
carry = product >>> 32; |
|
1482 |
} |
|
1483 |
z[xstart] = (int)carry; |
|
1484 |
||
1485 |
for (int i = xstart-1; i >= 0; i--) { |
|
1486 |
carry = 0; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1487 |
for (int j=ystart, k=ystart+1+i; j >= 0; j--, k--) { |
2 | 1488 |
long product = (y[j] & LONG_MASK) * |
1489 |
(x[i] & LONG_MASK) + |
|
1490 |
(z[k] & LONG_MASK) + carry; |
|
1491 |
z[k] = (int)product; |
|
1492 |
carry = product >>> 32; |
|
1493 |
} |
|
1494 |
z[i] = (int)carry; |
|
1495 |
} |
|
1496 |
return z; |
|
1497 |
} |
|
1498 |
||
1499 |
/** |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1500 |
* Multiplies two BigIntegers using the Karatsuba multiplication |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1501 |
* algorithm. This is a recursive divide-and-conquer algorithm which is |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1502 |
* more efficient for large numbers than what is commonly called the |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1503 |
* "grade-school" algorithm used in multiplyToLen. If the numbers to be |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1504 |
* multiplied have length n, the "grade-school" algorithm has an |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1505 |
* asymptotic complexity of O(n^2). In contrast, the Karatsuba algorithm |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1506 |
* has complexity of O(n^(log2(3))), or O(n^1.585). It achieves this |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1507 |
* increased performance by doing 3 multiplies instead of 4 when |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1508 |
* evaluating the product. As it has some overhead, should be used when |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1509 |
* both numbers are larger than a certain threshold (found |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1510 |
* experimentally). |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1511 |
* |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1512 |
* See: http://en.wikipedia.org/wiki/Karatsuba_algorithm |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1513 |
*/ |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1514 |
private static BigInteger multiplyKaratsuba(BigInteger x, BigInteger y) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1515 |
int xlen = x.mag.length; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1516 |
int ylen = y.mag.length; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1517 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1518 |
// The number of ints in each half of the number. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1519 |
int half = (Math.max(xlen, ylen)+1) / 2; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1520 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1521 |
// xl and yl are the lower halves of x and y respectively, |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1522 |
// xh and yh are the upper halves. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1523 |
BigInteger xl = x.getLower(half); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1524 |
BigInteger xh = x.getUpper(half); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1525 |
BigInteger yl = y.getLower(half); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1526 |
BigInteger yh = y.getUpper(half); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1527 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1528 |
BigInteger p1 = xh.multiply(yh); // p1 = xh*yh |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1529 |
BigInteger p2 = xl.multiply(yl); // p2 = xl*yl |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1530 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1531 |
// p3=(xh+xl)*(yh+yl) |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1532 |
BigInteger p3 = xh.add(xl).multiply(yh.add(yl)); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1533 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1534 |
// result = p1 * 2^(32*2*half) + (p3 - p1 - p2) * 2^(32*half) + p2 |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1535 |
BigInteger result = p1.shiftLeft(32*half).add(p3.subtract(p1).subtract(p2)).shiftLeft(32*half).add(p2); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1536 |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1537 |
if (x.signum != y.signum) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1538 |
return result.negate(); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1539 |
} else { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1540 |
return result; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1541 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1542 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1543 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1544 |
/** |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1545 |
* Multiplies two BigIntegers using a 3-way Toom-Cook multiplication |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1546 |
* algorithm. This is a recursive divide-and-conquer algorithm which is |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1547 |
* more efficient for large numbers than what is commonly called the |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1548 |
* "grade-school" algorithm used in multiplyToLen. If the numbers to be |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1549 |
* multiplied have length n, the "grade-school" algorithm has an |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1550 |
* asymptotic complexity of O(n^2). In contrast, 3-way Toom-Cook has a |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1551 |
* complexity of about O(n^1.465). It achieves this increased asymptotic |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1552 |
* performance by breaking each number into three parts and by doing 5 |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1553 |
* multiplies instead of 9 when evaluating the product. Due to overhead |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1554 |
* (additions, shifts, and one division) in the Toom-Cook algorithm, it |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1555 |
* should only be used when both numbers are larger than a certain |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1556 |
* threshold (found experimentally). This threshold is generally larger |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1557 |
* than that for Karatsuba multiplication, so this algorithm is generally |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1558 |
* only used when numbers become significantly larger. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1559 |
* |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1560 |
* The algorithm used is the "optimal" 3-way Toom-Cook algorithm outlined |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1561 |
* by Marco Bodrato. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1562 |
* |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1563 |
* See: http://bodrato.it/toom-cook/ |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1564 |
* http://bodrato.it/papers/#WAIFI2007 |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1565 |
* |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1566 |
* "Towards Optimal Toom-Cook Multiplication for Univariate and |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1567 |
* Multivariate Polynomials in Characteristic 2 and 0." by Marco BODRATO; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1568 |
* In C.Carlet and B.Sunar, Eds., "WAIFI'07 proceedings", p. 116-133, |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1569 |
* LNCS #4547. Springer, Madrid, Spain, June 21-22, 2007. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1570 |
* |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1571 |
*/ |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1572 |
private static BigInteger multiplyToomCook3(BigInteger a, BigInteger b) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1573 |
int alen = a.mag.length; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1574 |
int blen = b.mag.length; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1575 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1576 |
int largest = Math.max(alen, blen); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1577 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1578 |
// k is the size (in ints) of the lower-order slices. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1579 |
int k = (largest+2)/3; // Equal to ceil(largest/3) |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1580 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1581 |
// r is the size (in ints) of the highest-order slice. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1582 |
int r = largest - 2*k; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1583 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1584 |
// Obtain slices of the numbers. a2 and b2 are the most significant |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1585 |
// bits of the numbers a and b, and a0 and b0 the least significant. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1586 |
BigInteger a0, a1, a2, b0, b1, b2; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1587 |
a2 = a.getToomSlice(k, r, 0, largest); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1588 |
a1 = a.getToomSlice(k, r, 1, largest); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1589 |
a0 = a.getToomSlice(k, r, 2, largest); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1590 |
b2 = b.getToomSlice(k, r, 0, largest); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1591 |
b1 = b.getToomSlice(k, r, 1, largest); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1592 |
b0 = b.getToomSlice(k, r, 2, largest); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1593 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1594 |
BigInteger v0, v1, v2, vm1, vinf, t1, t2, tm1, da1, db1; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1595 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1596 |
v0 = a0.multiply(b0); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1597 |
da1 = a2.add(a0); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1598 |
db1 = b2.add(b0); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1599 |
vm1 = da1.subtract(a1).multiply(db1.subtract(b1)); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1600 |
da1 = da1.add(a1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1601 |
db1 = db1.add(b1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1602 |
v1 = da1.multiply(db1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1603 |
v2 = da1.add(a2).shiftLeft(1).subtract(a0).multiply( |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1604 |
db1.add(b2).shiftLeft(1).subtract(b0)); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1605 |
vinf = a2.multiply(b2); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1606 |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1607 |
// The algorithm requires two divisions by 2 and one by 3. |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1608 |
// All divisions are known to be exact, that is, they do not produce |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1609 |
// remainders, and all results are positive. The divisions by 2 are |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1610 |
// implemented as right shifts which are relatively efficient, leaving |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1611 |
// only an exact division by 3, which is done by a specialized |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1612 |
// linear-time algorithm. |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1613 |
t2 = v2.subtract(vm1).exactDivideBy3(); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1614 |
tm1 = v1.subtract(vm1).shiftRight(1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1615 |
t1 = v1.subtract(v0); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1616 |
t2 = t2.subtract(t1).shiftRight(1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1617 |
t1 = t1.subtract(tm1).subtract(vinf); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1618 |
t2 = t2.subtract(vinf.shiftLeft(1)); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1619 |
tm1 = tm1.subtract(t2); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1620 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1621 |
// Number of bits to shift left. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1622 |
int ss = k*32; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1623 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1624 |
BigInteger result = vinf.shiftLeft(ss).add(t2).shiftLeft(ss).add(t1).shiftLeft(ss).add(tm1).shiftLeft(ss).add(v0); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1625 |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1626 |
if (a.signum != b.signum) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1627 |
return result.negate(); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1628 |
} else { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1629 |
return result; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1630 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1631 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1632 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1633 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1634 |
/** |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1635 |
* Returns a slice of a BigInteger for use in Toom-Cook multiplication. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1636 |
* |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1637 |
* @param lowerSize The size of the lower-order bit slices. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1638 |
* @param upperSize The size of the higher-order bit slices. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1639 |
* @param slice The index of which slice is requested, which must be a |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1640 |
* number from 0 to size-1. Slice 0 is the highest-order bits, and slice |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1641 |
* size-1 are the lowest-order bits. Slice 0 may be of different size than |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1642 |
* the other slices. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1643 |
* @param fullsize The size of the larger integer array, used to align |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1644 |
* slices to the appropriate position when multiplying different-sized |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1645 |
* numbers. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1646 |
*/ |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1647 |
private BigInteger getToomSlice(int lowerSize, int upperSize, int slice, |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1648 |
int fullsize) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1649 |
int start, end, sliceSize, len, offset; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1650 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1651 |
len = mag.length; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1652 |
offset = fullsize - len; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1653 |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1654 |
if (slice == 0) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1655 |
start = 0 - offset; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1656 |
end = upperSize - 1 - offset; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1657 |
} else { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1658 |
start = upperSize + (slice-1)*lowerSize - offset; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1659 |
end = start + lowerSize - 1; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1660 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1661 |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1662 |
if (start < 0) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1663 |
start = 0; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1664 |
} |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1665 |
if (end < 0) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1666 |
return ZERO; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1667 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1668 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1669 |
sliceSize = (end-start) + 1; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1670 |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1671 |
if (sliceSize <= 0) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1672 |
return ZERO; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1673 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1674 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1675 |
// While performing Toom-Cook, all slices are positive and |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1676 |
// the sign is adjusted when the final number is composed. |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1677 |
if (start == 0 && sliceSize >= len) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1678 |
return this.abs(); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1679 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1680 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1681 |
int intSlice[] = new int[sliceSize]; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1682 |
System.arraycopy(mag, start, intSlice, 0, sliceSize); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1683 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1684 |
return new BigInteger(trustedStripLeadingZeroInts(intSlice), 1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1685 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1686 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1687 |
/** |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1688 |
* Does an exact division (that is, the remainder is known to be zero) |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1689 |
* of the specified number by 3. This is used in Toom-Cook |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1690 |
* multiplication. This is an efficient algorithm that runs in linear |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1691 |
* time. If the argument is not exactly divisible by 3, results are |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1692 |
* undefined. Note that this is expected to be called with positive |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1693 |
* arguments only. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1694 |
*/ |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1695 |
private BigInteger exactDivideBy3() { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1696 |
int len = mag.length; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1697 |
int[] result = new int[len]; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1698 |
long x, w, q, borrow; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1699 |
borrow = 0L; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1700 |
for (int i=len-1; i >= 0; i--) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1701 |
x = (mag[i] & LONG_MASK); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1702 |
w = x - borrow; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1703 |
if (borrow > x) { // Did we make the number go negative? |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1704 |
borrow = 1L; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1705 |
} else { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1706 |
borrow = 0L; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1707 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1708 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1709 |
// 0xAAAAAAAB is the modular inverse of 3 (mod 2^32). Thus, |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1710 |
// the effect of this is to divide by 3 (mod 2^32). |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1711 |
// This is much faster than division on most architectures. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1712 |
q = (w * 0xAAAAAAABL) & LONG_MASK; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1713 |
result[i] = (int) q; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1714 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1715 |
// Now check the borrow. The second check can of course be |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1716 |
// eliminated if the first fails. |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1717 |
if (q >= 0x55555556L) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1718 |
borrow++; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1719 |
if (q >= 0xAAAAAAABL) |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1720 |
borrow++; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1721 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1722 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1723 |
result = trustedStripLeadingZeroInts(result); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1724 |
return new BigInteger(result, signum); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1725 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1726 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1727 |
/** |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1728 |
* Returns a new BigInteger representing n lower ints of the number. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1729 |
* This is used by Karatsuba multiplication and Karatsuba squaring. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1730 |
*/ |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1731 |
private BigInteger getLower(int n) { |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1732 |
int len = mag.length; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1733 |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1734 |
if (len <= n) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1735 |
return this; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1736 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1737 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1738 |
int lowerInts[] = new int[n]; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1739 |
System.arraycopy(mag, len-n, lowerInts, 0, n); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1740 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1741 |
return new BigInteger(trustedStripLeadingZeroInts(lowerInts), 1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1742 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1743 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1744 |
/** |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1745 |
* Returns a new BigInteger representing mag.length-n upper |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1746 |
* ints of the number. This is used by Karatsuba multiplication and |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1747 |
* Karatsuba squaring. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1748 |
*/ |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1749 |
private BigInteger getUpper(int n) { |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1750 |
int len = mag.length; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1751 |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1752 |
if (len <= n) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1753 |
return ZERO; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1754 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1755 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1756 |
int upperLen = len - n; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1757 |
int upperInts[] = new int[upperLen]; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1758 |
System.arraycopy(mag, 0, upperInts, 0, upperLen); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1759 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1760 |
return new BigInteger(trustedStripLeadingZeroInts(upperInts), 1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1761 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1762 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1763 |
// Squaring |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1764 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1765 |
/** |
2 | 1766 |
* Returns a BigInteger whose value is {@code (this<sup>2</sup>)}. |
1767 |
* |
|
1768 |
* @return {@code this<sup>2</sup>} |
|
1769 |
*/ |
|
1770 |
private BigInteger square() { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1771 |
if (signum == 0) { |
2 | 1772 |
return ZERO; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1773 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1774 |
int len = mag.length; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1775 |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1776 |
if (len < KARATSUBA_SQUARE_THRESHOLD) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1777 |
int[] z = squareToLen(mag, len, null); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1778 |
return new BigInteger(trustedStripLeadingZeroInts(z), 1); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1779 |
} else { |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1780 |
if (len < TOOM_COOK_SQUARE_THRESHOLD) { |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1781 |
return squareKaratsuba(); |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1782 |
} else { |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1783 |
return squareToomCook3(); |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1784 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1785 |
} |
2 | 1786 |
} |
1787 |
||
1788 |
/** |
|
1789 |
* Squares the contents of the int array x. The result is placed into the |
|
1790 |
* int array z. The contents of x are not changed. |
|
1791 |
*/ |
|
1792 |
private static final int[] squareToLen(int[] x, int len, int[] z) { |
|
1793 |
/* |
|
1794 |
* The algorithm used here is adapted from Colin Plumb's C library. |
|
1795 |
* Technique: Consider the partial products in the multiplication |
|
1796 |
* of "abcde" by itself: |
|
1797 |
* |
|
1798 |
* a b c d e |
|
1799 |
* * a b c d e |
|
1800 |
* ================== |
|
1801 |
* ae be ce de ee |
|
1802 |
* ad bd cd dd de |
|
1803 |
* ac bc cc cd ce |
|
1804 |
* ab bb bc bd be |
|
1805 |
* aa ab ac ad ae |
|
1806 |
* |
|
1807 |
* Note that everything above the main diagonal: |
|
1808 |
* ae be ce de = (abcd) * e |
|
1809 |
* ad bd cd = (abc) * d |
|
1810 |
* ac bc = (ab) * c |
|
1811 |
* ab = (a) * b |
|
1812 |
* |
|
1813 |
* is a copy of everything below the main diagonal: |
|
1814 |
* de |
|
1815 |
* cd ce |
|
1816 |
* bc bd be |
|
1817 |
* ab ac ad ae |
|
1818 |
* |
|
1819 |
* Thus, the sum is 2 * (off the diagonal) + diagonal. |
|
1820 |
* |
|
1821 |
* This is accumulated beginning with the diagonal (which |
|
1822 |
* consist of the squares of the digits of the input), which is then |
|
1823 |
* divided by two, the off-diagonal added, and multiplied by two |
|
1824 |
* again. The low bit is simply a copy of the low bit of the |
|
1825 |
* input, so it doesn't need special care. |
|
1826 |
*/ |
|
1827 |
int zlen = len << 1; |
|
1828 |
if (z == null || z.length < zlen) |
|
1829 |
z = new int[zlen]; |
|
1830 |
||
1831 |
// Store the squares, right shifted one bit (i.e., divided by 2) |
|
1832 |
int lastProductLowWord = 0; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1833 |
for (int j=0, i=0; j < len; j++) { |
2 | 1834 |
long piece = (x[j] & LONG_MASK); |
1835 |
long product = piece * piece; |
|
1836 |
z[i++] = (lastProductLowWord << 31) | (int)(product >>> 33); |
|
1837 |
z[i++] = (int)(product >>> 1); |
|
1838 |
lastProductLowWord = (int)product; |
|
1839 |
} |
|
1840 |
||
1841 |
// Add in off-diagonal sums |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1842 |
for (int i=len, offset=1; i > 0; i--, offset+=2) { |
2 | 1843 |
int t = x[i-1]; |
1844 |
t = mulAdd(z, x, offset, i-1, t); |
|
1845 |
addOne(z, offset-1, i, t); |
|
1846 |
} |
|
1847 |
||
1848 |
// Shift back up and set low bit |
|
1849 |
primitiveLeftShift(z, zlen, 1); |
|
1850 |
z[zlen-1] |= x[len-1] & 1; |
|
1851 |
||
1852 |
return z; |
|
1853 |
} |
|
1854 |
||
1855 |
/** |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1856 |
* Squares a BigInteger using the Karatsuba squaring algorithm. It should |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1857 |
* be used when both numbers are larger than a certain threshold (found |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1858 |
* experimentally). It is a recursive divide-and-conquer algorithm that |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1859 |
* has better asymptotic performance than the algorithm used in |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1860 |
* squareToLen. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1861 |
*/ |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1862 |
private BigInteger squareKaratsuba() { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1863 |
int half = (mag.length+1) / 2; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1864 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1865 |
BigInteger xl = getLower(half); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1866 |
BigInteger xh = getUpper(half); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1867 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1868 |
BigInteger xhs = xh.square(); // xhs = xh^2 |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1869 |
BigInteger xls = xl.square(); // xls = xl^2 |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1870 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1871 |
// xh^2 << 64 + (((xl+xh)^2 - (xh^2 + xl^2)) << 32) + xl^2 |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1872 |
return xhs.shiftLeft(half*32).add(xl.add(xh).square().subtract(xhs.add(xls))).shiftLeft(half*32).add(xls); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1873 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1874 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1875 |
/** |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1876 |
* Squares a BigInteger using the 3-way Toom-Cook squaring algorithm. It |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1877 |
* should be used when both numbers are larger than a certain threshold |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1878 |
* (found experimentally). It is a recursive divide-and-conquer algorithm |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1879 |
* that has better asymptotic performance than the algorithm used in |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1880 |
* squareToLen or squareKaratsuba. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1881 |
*/ |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1882 |
private BigInteger squareToomCook3() { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1883 |
int len = mag.length; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1884 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1885 |
// k is the size (in ints) of the lower-order slices. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1886 |
int k = (len+2)/3; // Equal to ceil(largest/3) |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1887 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1888 |
// r is the size (in ints) of the highest-order slice. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1889 |
int r = len - 2*k; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1890 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1891 |
// Obtain slices of the numbers. a2 is the most significant |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1892 |
// bits of the number, and a0 the least significant. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1893 |
BigInteger a0, a1, a2; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1894 |
a2 = getToomSlice(k, r, 0, len); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1895 |
a1 = getToomSlice(k, r, 1, len); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1896 |
a0 = getToomSlice(k, r, 2, len); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1897 |
BigInteger v0, v1, v2, vm1, vinf, t1, t2, tm1, da1; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1898 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1899 |
v0 = a0.square(); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1900 |
da1 = a2.add(a0); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1901 |
vm1 = da1.subtract(a1).square(); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1902 |
da1 = da1.add(a1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1903 |
v1 = da1.square(); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1904 |
vinf = a2.square(); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1905 |
v2 = da1.add(a2).shiftLeft(1).subtract(a0).square(); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1906 |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1907 |
// The algorithm requires two divisions by 2 and one by 3. |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1908 |
// All divisions are known to be exact, that is, they do not produce |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1909 |
// remainders, and all results are positive. The divisions by 2 are |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1910 |
// implemented as right shifts which are relatively efficient, leaving |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1911 |
// only a division by 3. |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1912 |
// The division by 3 is done by an optimized algorithm for this case. |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1913 |
t2 = v2.subtract(vm1).exactDivideBy3(); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1914 |
tm1 = v1.subtract(vm1).shiftRight(1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1915 |
t1 = v1.subtract(v0); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1916 |
t2 = t2.subtract(t1).shiftRight(1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1917 |
t1 = t1.subtract(tm1).subtract(vinf); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1918 |
t2 = t2.subtract(vinf.shiftLeft(1)); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1919 |
tm1 = tm1.subtract(t2); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1920 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1921 |
// Number of bits to shift left. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1922 |
int ss = k*32; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1923 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1924 |
return vinf.shiftLeft(ss).add(t2).shiftLeft(ss).add(t1).shiftLeft(ss).add(tm1).shiftLeft(ss).add(v0); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1925 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1926 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1927 |
// Division |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1928 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
1929 |
/** |
2 | 1930 |
* Returns a BigInteger whose value is {@code (this / val)}. |
1931 |
* |
|
1932 |
* @param val value by which this BigInteger is to be divided. |
|
1933 |
* @return {@code this / val} |
|
4160
bda0a85afcb7
6560935: BigInteger.modPow() throws ArithmeticException for negative exponent
darcy
parents:
4151
diff
changeset
|
1934 |
* @throws ArithmeticException if {@code val} is zero. |
2 | 1935 |
*/ |
1936 |
public BigInteger divide(BigInteger val) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1937 |
if (mag.length < BURNIKEL_ZIEGLER_THRESHOLD || |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1938 |
val.mag.length < BURNIKEL_ZIEGLER_THRESHOLD) { |
19060 | 1939 |
return divideKnuth(val); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1940 |
} else { |
19060 | 1941 |
return divideBurnikelZiegler(val); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1942 |
} |
19060 | 1943 |
} |
1944 |
||
1945 |
/** |
|
1946 |
* Returns a BigInteger whose value is {@code (this / val)} using an O(n^2) algorithm from Knuth. |
|
1947 |
* |
|
1948 |
* @param val value by which this BigInteger is to be divided. |
|
1949 |
* @return {@code this / val} |
|
1950 |
* @throws ArithmeticException if {@code val} is zero. |
|
1951 |
* @see MutableBigInteger#divideKnuth(MutableBigInteger, MutableBigInteger, boolean) |
|
1952 |
*/ |
|
1953 |
private BigInteger divideKnuth(BigInteger val) { |
|
2 | 1954 |
MutableBigInteger q = new MutableBigInteger(), |
1955 |
a = new MutableBigInteger(this.mag), |
|
1956 |
b = new MutableBigInteger(val.mag); |
|
1957 |
||
19060 | 1958 |
a.divideKnuth(b, q, false); |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
1959 |
return q.toBigInteger(this.signum * val.signum); |
2 | 1960 |
} |
1961 |
||
1962 |
/** |
|
1963 |
* Returns an array of two BigIntegers containing {@code (this / val)} |
|
1964 |
* followed by {@code (this % val)}. |
|
1965 |
* |
|
1966 |
* @param val value by which this BigInteger is to be divided, and the |
|
1967 |
* remainder computed. |
|
1968 |
* @return an array of two BigIntegers: the quotient {@code (this / val)} |
|
1969 |
* is the initial element, and the remainder {@code (this % val)} |
|
1970 |
* is the final element. |
|
4160
bda0a85afcb7
6560935: BigInteger.modPow() throws ArithmeticException for negative exponent
darcy
parents:
4151
diff
changeset
|
1971 |
* @throws ArithmeticException if {@code val} is zero. |
2 | 1972 |
*/ |
1973 |
public BigInteger[] divideAndRemainder(BigInteger val) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1974 |
if (mag.length < BURNIKEL_ZIEGLER_THRESHOLD || |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1975 |
val.mag.length < BURNIKEL_ZIEGLER_THRESHOLD) { |
19060 | 1976 |
return divideAndRemainderKnuth(val); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1977 |
} else { |
19060 | 1978 |
return divideAndRemainderBurnikelZiegler(val); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1979 |
} |
19060 | 1980 |
} |
1981 |
||
1982 |
/** Long division */ |
|
1983 |
private BigInteger[] divideAndRemainderKnuth(BigInteger val) { |
|
2 | 1984 |
BigInteger[] result = new BigInteger[2]; |
1985 |
MutableBigInteger q = new MutableBigInteger(), |
|
1986 |
a = new MutableBigInteger(this.mag), |
|
1987 |
b = new MutableBigInteger(val.mag); |
|
19060 | 1988 |
MutableBigInteger r = a.divideKnuth(b, q); |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1989 |
result[0] = q.toBigInteger(this.signum == val.signum ? 1 : -1); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
1990 |
result[1] = r.toBigInteger(this.signum); |
2 | 1991 |
return result; |
1992 |
} |
|
1993 |
||
1994 |
/** |
|
1995 |
* Returns a BigInteger whose value is {@code (this % val)}. |
|
1996 |
* |
|
1997 |
* @param val value by which this BigInteger is to be divided, and the |
|
1998 |
* remainder computed. |
|
1999 |
* @return {@code this % val} |
|
4160
bda0a85afcb7
6560935: BigInteger.modPow() throws ArithmeticException for negative exponent
darcy
parents:
4151
diff
changeset
|
2000 |
* @throws ArithmeticException if {@code val} is zero. |
2 | 2001 |
*/ |
2002 |
public BigInteger remainder(BigInteger val) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2003 |
if (mag.length < BURNIKEL_ZIEGLER_THRESHOLD || |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2004 |
val.mag.length < BURNIKEL_ZIEGLER_THRESHOLD) { |
19060 | 2005 |
return remainderKnuth(val); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2006 |
} else { |
19060 | 2007 |
return remainderBurnikelZiegler(val); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2008 |
} |
19060 | 2009 |
} |
2010 |
||
2011 |
/** Long division */ |
|
2012 |
private BigInteger remainderKnuth(BigInteger val) { |
|
2 | 2013 |
MutableBigInteger q = new MutableBigInteger(), |
2014 |
a = new MutableBigInteger(this.mag), |
|
2015 |
b = new MutableBigInteger(val.mag); |
|
2016 |
||
19060 | 2017 |
return a.divideKnuth(b, q).toBigInteger(this.signum); |
2018 |
} |
|
2019 |
||
2020 |
/** |
|
2021 |
* Calculates {@code this / val} using the Burnikel-Ziegler algorithm. |
|
2022 |
* @param val the divisor |
|
2023 |
* @return {@code this / val} |
|
2024 |
*/ |
|
2025 |
private BigInteger divideBurnikelZiegler(BigInteger val) { |
|
2026 |
return divideAndRemainderBurnikelZiegler(val)[0]; |
|
2027 |
} |
|
2028 |
||
2029 |
/** |
|
2030 |
* Calculates {@code this % val} using the Burnikel-Ziegler algorithm. |
|
2031 |
* @param val the divisor |
|
2032 |
* @return {@code this % val} |
|
2033 |
*/ |
|
2034 |
private BigInteger remainderBurnikelZiegler(BigInteger val) { |
|
2035 |
return divideAndRemainderBurnikelZiegler(val)[1]; |
|
2036 |
} |
|
2037 |
||
2038 |
/** |
|
2039 |
* Computes {@code this / val} and {@code this % val} using the |
|
2040 |
* Burnikel-Ziegler algorithm. |
|
2041 |
* @param val the divisor |
|
2042 |
* @return an array containing the quotient and remainder |
|
2043 |
*/ |
|
2044 |
private BigInteger[] divideAndRemainderBurnikelZiegler(BigInteger val) { |
|
2045 |
MutableBigInteger q = new MutableBigInteger(); |
|
2046 |
MutableBigInteger r = new MutableBigInteger(this).divideAndRemainderBurnikelZiegler(new MutableBigInteger(val), q); |
|
2047 |
BigInteger qBigInt = q.isZero() ? ZERO : q.toBigInteger(signum*val.signum); |
|
2048 |
BigInteger rBigInt = r.isZero() ? ZERO : r.toBigInteger(signum); |
|
2049 |
return new BigInteger[] {qBigInt, rBigInt}; |
|
2 | 2050 |
} |
2051 |
||
2052 |
/** |
|
2053 |
* Returns a BigInteger whose value is <tt>(this<sup>exponent</sup>)</tt>. |
|
2054 |
* Note that {@code exponent} is an integer rather than a BigInteger. |
|
2055 |
* |
|
2056 |
* @param exponent exponent to which this BigInteger is to be raised. |
|
2057 |
* @return <tt>this<sup>exponent</sup></tt> |
|
2058 |
* @throws ArithmeticException {@code exponent} is negative. (This would |
|
2059 |
* cause the operation to yield a non-integer value.) |
|
2060 |
*/ |
|
2061 |
public BigInteger pow(int exponent) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2062 |
if (exponent < 0) { |
2 | 2063 |
throw new ArithmeticException("Negative exponent"); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2064 |
} |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2065 |
if (signum == 0) { |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2066 |
return (exponent == 0 ? ONE : this); |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2067 |
} |
2 | 2068 |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2069 |
BigInteger partToSquare = this.abs(); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2070 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2071 |
// Factor out powers of two from the base, as the exponentiation of |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2072 |
// these can be done by left shifts only. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2073 |
// The remaining part can then be exponentiated faster. The |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2074 |
// powers of two will be multiplied back at the end. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2075 |
int powersOfTwo = partToSquare.getLowestSetBit(); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2076 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2077 |
int remainingBits; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2078 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2079 |
// Factor the powers of two out quickly by shifting right, if needed. |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2080 |
if (powersOfTwo > 0) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2081 |
partToSquare = partToSquare.shiftRight(powersOfTwo); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2082 |
remainingBits = partToSquare.bitLength(); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2083 |
if (remainingBits == 1) { // Nothing left but +/- 1? |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2084 |
if (signum < 0 && (exponent&1) == 1) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2085 |
return NEGATIVE_ONE.shiftLeft(powersOfTwo*exponent); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2086 |
} else { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2087 |
return ONE.shiftLeft(powersOfTwo*exponent); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2088 |
} |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2089 |
} |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2090 |
} else { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2091 |
remainingBits = partToSquare.bitLength(); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2092 |
if (remainingBits == 1) { // Nothing left but +/- 1? |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2093 |
if (signum < 0 && (exponent&1) == 1) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2094 |
return NEGATIVE_ONE; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2095 |
} else { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2096 |
return ONE; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2097 |
} |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2098 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2099 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2100 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2101 |
// This is a quick way to approximate the size of the result, |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2102 |
// similar to doing log2[n] * exponent. This will give an upper bound |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2103 |
// of how big the result can be, and which algorithm to use. |
19585
b57abf89019f
6378503: In java.math.BigDecimal, division by one returns zero
bpb
parents:
19061
diff
changeset
|
2104 |
long scaleFactor = (long)remainingBits * exponent; |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2105 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2106 |
// Use slightly different algorithms for small and large operands. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2107 |
// See if the result will safely fit into a long. (Largest 2^63-1) |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2108 |
if (partToSquare.mag.length == 1 && scaleFactor <= 62) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2109 |
// Small number algorithm. Everything fits into a long. |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2110 |
int newSign = (signum <0 && (exponent&1) == 1 ? -1 : 1); |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2111 |
long result = 1; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2112 |
long baseToPow2 = partToSquare.mag[0] & LONG_MASK; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2113 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2114 |
int workingExponent = exponent; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2115 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2116 |
// Perform exponentiation using repeated squaring trick |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2117 |
while (workingExponent != 0) { |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2118 |
if ((workingExponent & 1) == 1) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2119 |
result = result * baseToPow2; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2120 |
} |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2121 |
|
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2122 |
if ((workingExponent >>>= 1) != 0) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2123 |
baseToPow2 = baseToPow2 * baseToPow2; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2124 |
} |
2 | 2125 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2126 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2127 |
// Multiply back the powers of two (quickly, by shifting left) |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2128 |
if (powersOfTwo > 0) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2129 |
int bitsToShift = powersOfTwo*exponent; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2130 |
if (bitsToShift + scaleFactor <= 62) { // Fits in long? |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2131 |
return valueOf((result << bitsToShift) * newSign); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2132 |
} else { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2133 |
return valueOf(result*newSign).shiftLeft(bitsToShift); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2134 |
} |
2 | 2135 |
} |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2136 |
else { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2137 |
return valueOf(result*newSign); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2138 |
} |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2139 |
} else { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2140 |
// Large number algorithm. This is basically identical to |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2141 |
// the algorithm above, but calls multiply() and square() |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2142 |
// which may use more efficient algorithms for large numbers. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2143 |
BigInteger answer = ONE; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2144 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2145 |
int workingExponent = exponent; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2146 |
// Perform exponentiation using repeated squaring trick |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2147 |
while (workingExponent != 0) { |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2148 |
if ((workingExponent & 1) == 1) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2149 |
answer = answer.multiply(partToSquare); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2150 |
} |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2151 |
|
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2152 |
if ((workingExponent >>>= 1) != 0) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2153 |
partToSquare = partToSquare.square(); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2154 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2155 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2156 |
// Multiply back the (exponentiated) powers of two (quickly, |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2157 |
// by shifting left) |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2158 |
if (powersOfTwo > 0) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2159 |
answer = answer.shiftLeft(powersOfTwo*exponent); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2160 |
} |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2161 |
|
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2162 |
if (signum < 0 && (exponent&1) == 1) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2163 |
return answer.negate(); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2164 |
} else { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2165 |
return answer; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2166 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2167 |
} |
2 | 2168 |
} |
2169 |
||
2170 |
/** |
|
2171 |
* Returns a BigInteger whose value is the greatest common divisor of |
|
2172 |
* {@code abs(this)} and {@code abs(val)}. Returns 0 if |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2173 |
* {@code this == 0 && val == 0}. |
2 | 2174 |
* |
2175 |
* @param val value with which the GCD is to be computed. |
|
2176 |
* @return {@code GCD(abs(this), abs(val))} |
|
2177 |
*/ |
|
2178 |
public BigInteger gcd(BigInteger val) { |
|
2179 |
if (val.signum == 0) |
|
2180 |
return this.abs(); |
|
2181 |
else if (this.signum == 0) |
|
2182 |
return val.abs(); |
|
2183 |
||
2184 |
MutableBigInteger a = new MutableBigInteger(this); |
|
2185 |
MutableBigInteger b = new MutableBigInteger(val); |
|
2186 |
||
2187 |
MutableBigInteger result = a.hybridGCD(b); |
|
2188 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2189 |
return result.toBigInteger(1); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2190 |
} |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2191 |
|
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2192 |
/** |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2193 |
* Package private method to return bit length for an integer. |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2194 |
*/ |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2195 |
static int bitLengthForInt(int n) { |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2196 |
return 32 - Integer.numberOfLeadingZeros(n); |
2 | 2197 |
} |
2198 |
||
2199 |
/** |
|
2200 |
* Left shift int array a up to len by n bits. Returns the array that |
|
2201 |
* results from the shift since space may have to be reallocated. |
|
2202 |
*/ |
|
2203 |
private static int[] leftShift(int[] a, int len, int n) { |
|
2204 |
int nInts = n >>> 5; |
|
2205 |
int nBits = n&0x1F; |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2206 |
int bitsInHighWord = bitLengthForInt(a[0]); |
2 | 2207 |
|
2208 |
// If shift can be done without recopy, do so |
|
2209 |
if (n <= (32-bitsInHighWord)) { |
|
2210 |
primitiveLeftShift(a, len, nBits); |
|
2211 |
return a; |
|
2212 |
} else { // Array must be resized |
|
2213 |
if (nBits <= (32-bitsInHighWord)) { |
|
2214 |
int result[] = new int[nInts+len]; |
|
10425
7903cf45f96f
6989067: BigInteger's array copiers should be converted to System.arraycopy()
darcy
parents:
10423
diff
changeset
|
2215 |
System.arraycopy(a, 0, result, 0, len); |
2 | 2216 |
primitiveLeftShift(result, result.length, nBits); |
2217 |
return result; |
|
2218 |
} else { |
|
2219 |
int result[] = new int[nInts+len+1]; |
|
10425
7903cf45f96f
6989067: BigInteger's array copiers should be converted to System.arraycopy()
darcy
parents:
10423
diff
changeset
|
2220 |
System.arraycopy(a, 0, result, 0, len); |
2 | 2221 |
primitiveRightShift(result, result.length, 32 - nBits); |
2222 |
return result; |
|
2223 |
} |
|
2224 |
} |
|
2225 |
} |
|
2226 |
||
2227 |
// shifts a up to len right n bits assumes no leading zeros, 0<n<32 |
|
2228 |
static void primitiveRightShift(int[] a, int len, int n) { |
|
2229 |
int n2 = 32 - n; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2230 |
for (int i=len-1, c=a[i]; i > 0; i--) { |
2 | 2231 |
int b = c; |
2232 |
c = a[i-1]; |
|
2233 |
a[i] = (c << n2) | (b >>> n); |
|
2234 |
} |
|
2235 |
a[0] >>>= n; |
|
2236 |
} |
|
2237 |
||
2238 |
// shifts a up to len left n bits assumes no leading zeros, 0<=n<32 |
|
2239 |
static void primitiveLeftShift(int[] a, int len, int n) { |
|
2240 |
if (len == 0 || n == 0) |
|
2241 |
return; |
|
2242 |
||
2243 |
int n2 = 32 - n; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2244 |
for (int i=0, c=a[i], m=i+len-1; i < m; i++) { |
2 | 2245 |
int b = c; |
2246 |
c = a[i+1]; |
|
2247 |
a[i] = (b << n) | (c >>> n2); |
|
2248 |
} |
|
2249 |
a[len-1] <<= n; |
|
2250 |
} |
|
2251 |
||
2252 |
/** |
|
2253 |
* Calculate bitlength of contents of the first len elements an int array, |
|
2254 |
* assuming there are no leading zero ints. |
|
2255 |
*/ |
|
2256 |
private static int bitLength(int[] val, int len) { |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2257 |
if (len == 0) |
2 | 2258 |
return 0; |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2259 |
return ((len - 1) << 5) + bitLengthForInt(val[0]); |
2 | 2260 |
} |
2261 |
||
2262 |
/** |
|
2263 |
* Returns a BigInteger whose value is the absolute value of this |
|
2264 |
* BigInteger. |
|
2265 |
* |
|
2266 |
* @return {@code abs(this)} |
|
2267 |
*/ |
|
2268 |
public BigInteger abs() { |
|
2269 |
return (signum >= 0 ? this : this.negate()); |
|
2270 |
} |
|
2271 |
||
2272 |
/** |
|
2273 |
* Returns a BigInteger whose value is {@code (-this)}. |
|
2274 |
* |
|
2275 |
* @return {@code -this} |
|
2276 |
*/ |
|
2277 |
public BigInteger negate() { |
|
2278 |
return new BigInteger(this.mag, -this.signum); |
|
2279 |
} |
|
2280 |
||
2281 |
/** |
|
2282 |
* Returns the signum function of this BigInteger. |
|
2283 |
* |
|
2284 |
* @return -1, 0 or 1 as the value of this BigInteger is negative, zero or |
|
2285 |
* positive. |
|
2286 |
*/ |
|
2287 |
public int signum() { |
|
2288 |
return this.signum; |
|
2289 |
} |
|
2290 |
||
2291 |
// Modular Arithmetic Operations |
|
2292 |
||
2293 |
/** |
|
2294 |
* Returns a BigInteger whose value is {@code (this mod m}). This method |
|
2295 |
* differs from {@code remainder} in that it always returns a |
|
2296 |
* <i>non-negative</i> BigInteger. |
|
2297 |
* |
|
2298 |
* @param m the modulus. |
|
2299 |
* @return {@code this mod m} |
|
4160
bda0a85afcb7
6560935: BigInteger.modPow() throws ArithmeticException for negative exponent
darcy
parents:
4151
diff
changeset
|
2300 |
* @throws ArithmeticException {@code m} ≤ 0 |
2 | 2301 |
* @see #remainder |
2302 |
*/ |
|
2303 |
public BigInteger mod(BigInteger m) { |
|
2304 |
if (m.signum <= 0) |
|
2305 |
throw new ArithmeticException("BigInteger: modulus not positive"); |
|
2306 |
||
2307 |
BigInteger result = this.remainder(m); |
|
2308 |
return (result.signum >= 0 ? result : result.add(m)); |
|
2309 |
} |
|
2310 |
||
2311 |
/** |
|
2312 |
* Returns a BigInteger whose value is |
|
2313 |
* <tt>(this<sup>exponent</sup> mod m)</tt>. (Unlike {@code pow}, this |
|
2314 |
* method permits negative exponents.) |
|
2315 |
* |
|
2316 |
* @param exponent the exponent. |
|
2317 |
* @param m the modulus. |
|
2318 |
* @return <tt>this<sup>exponent</sup> mod m</tt> |
|
4160
bda0a85afcb7
6560935: BigInteger.modPow() throws ArithmeticException for negative exponent
darcy
parents:
4151
diff
changeset
|
2319 |
* @throws ArithmeticException {@code m} ≤ 0 or the exponent is |
bda0a85afcb7
6560935: BigInteger.modPow() throws ArithmeticException for negative exponent
darcy
parents:
4151
diff
changeset
|
2320 |
* negative and this BigInteger is not <i>relatively |
bda0a85afcb7
6560935: BigInteger.modPow() throws ArithmeticException for negative exponent
darcy
parents:
4151
diff
changeset
|
2321 |
* prime</i> to {@code m}. |
2 | 2322 |
* @see #modInverse |
2323 |
*/ |
|
2324 |
public BigInteger modPow(BigInteger exponent, BigInteger m) { |
|
2325 |
if (m.signum <= 0) |
|
2326 |
throw new ArithmeticException("BigInteger: modulus not positive"); |
|
2327 |
||
2328 |
// Trivial cases |
|
2329 |
if (exponent.signum == 0) |
|
2330 |
return (m.equals(ONE) ? ZERO : ONE); |
|
2331 |
||
2332 |
if (this.equals(ONE)) |
|
2333 |
return (m.equals(ONE) ? ZERO : ONE); |
|
2334 |
||
2335 |
if (this.equals(ZERO) && exponent.signum >= 0) |
|
2336 |
return ZERO; |
|
2337 |
||
2338 |
if (this.equals(negConst[1]) && (!exponent.testBit(0))) |
|
2339 |
return (m.equals(ONE) ? ZERO : ONE); |
|
2340 |
||
2341 |
boolean invertResult; |
|
2342 |
if ((invertResult = (exponent.signum < 0))) |
|
2343 |
exponent = exponent.negate(); |
|
2344 |
||
2345 |
BigInteger base = (this.signum < 0 || this.compareTo(m) >= 0 |
|
2346 |
? this.mod(m) : this); |
|
2347 |
BigInteger result; |
|
2348 |
if (m.testBit(0)) { // odd modulus |
|
2349 |
result = base.oddModPow(exponent, m); |
|
2350 |
} else { |
|
2351 |
/* |
|
2352 |
* Even modulus. Tear it into an "odd part" (m1) and power of two |
|
2353 |
* (m2), exponentiate mod m1, manually exponentiate mod m2, and |
|
2354 |
* use Chinese Remainder Theorem to combine results. |
|
2355 |
*/ |
|
2356 |
||
2357 |
// Tear m apart into odd part (m1) and power of 2 (m2) |
|
2358 |
int p = m.getLowestSetBit(); // Max pow of 2 that divides m |
|
2359 |
||
2360 |
BigInteger m1 = m.shiftRight(p); // m/2**p |
|
2361 |
BigInteger m2 = ONE.shiftLeft(p); // 2**p |
|
2362 |
||
2363 |
// Calculate new base from m1 |
|
2364 |
BigInteger base2 = (this.signum < 0 || this.compareTo(m1) >= 0 |
|
2365 |
? this.mod(m1) : this); |
|
2366 |
||
2367 |
// Caculate (base ** exponent) mod m1. |
|
2368 |
BigInteger a1 = (m1.equals(ONE) ? ZERO : |
|
2369 |
base2.oddModPow(exponent, m1)); |
|
2370 |
||
2371 |
// Calculate (this ** exponent) mod m2 |
|
2372 |
BigInteger a2 = base.modPow2(exponent, p); |
|
2373 |
||
2374 |
// Combine results using Chinese Remainder Theorem |
|
2375 |
BigInteger y1 = m2.modInverse(m1); |
|
2376 |
BigInteger y2 = m1.modInverse(m2); |
|
2377 |
||
2378 |
result = a1.multiply(m2).multiply(y1).add |
|
2379 |
(a2.multiply(m1).multiply(y2)).mod(m); |
|
2380 |
} |
|
2381 |
||
2382 |
return (invertResult ? result.modInverse(m) : result); |
|
2383 |
} |
|
2384 |
||
2385 |
static int[] bnExpModThreshTable = {7, 25, 81, 241, 673, 1793, |
|
2386 |
Integer.MAX_VALUE}; // Sentinel |
|
2387 |
||
2388 |
/** |
|
2389 |
* Returns a BigInteger whose value is x to the power of y mod z. |
|
2390 |
* Assumes: z is odd && x < z. |
|
2391 |
*/ |
|
2392 |
private BigInteger oddModPow(BigInteger y, BigInteger z) { |
|
2393 |
/* |
|
2394 |
* The algorithm is adapted from Colin Plumb's C library. |
|
2395 |
* |
|
2396 |
* The window algorithm: |
|
2397 |
* The idea is to keep a running product of b1 = n^(high-order bits of exp) |
|
2398 |
* and then keep appending exponent bits to it. The following patterns |
|
2399 |
* apply to a 3-bit window (k = 3): |
|
2400 |
* To append 0: square |
|
2401 |
* To append 1: square, multiply by n^1 |
|
2402 |
* To append 10: square, multiply by n^1, square |
|
2403 |
* To append 11: square, square, multiply by n^3 |
|
2404 |
* To append 100: square, multiply by n^1, square, square |
|
2405 |
* To append 101: square, square, square, multiply by n^5 |
|
2406 |
* To append 110: square, square, multiply by n^3, square |
|
2407 |
* To append 111: square, square, square, multiply by n^7 |
|
2408 |
* |
|
2409 |
* Since each pattern involves only one multiply, the longer the pattern |
|
2410 |
* the better, except that a 0 (no multiplies) can be appended directly. |
|
2411 |
* We precompute a table of odd powers of n, up to 2^k, and can then |
|
2412 |
* multiply k bits of exponent at a time. Actually, assuming random |
|
2413 |
* exponents, there is on average one zero bit between needs to |
|
2414 |
* multiply (1/2 of the time there's none, 1/4 of the time there's 1, |
|
2415 |
* 1/8 of the time, there's 2, 1/32 of the time, there's 3, etc.), so |
|
2416 |
* you have to do one multiply per k+1 bits of exponent. |
|
2417 |
* |
|
2418 |
* The loop walks down the exponent, squaring the result buffer as |
|
2419 |
* it goes. There is a wbits+1 bit lookahead buffer, buf, that is |
|
2420 |
* filled with the upcoming exponent bits. (What is read after the |
|
2421 |
* end of the exponent is unimportant, but it is filled with zero here.) |
|
2422 |
* When the most-significant bit of this buffer becomes set, i.e. |
|
2423 |
* (buf & tblmask) != 0, we have to decide what pattern to multiply |
|
2424 |
* by, and when to do it. We decide, remember to do it in future |
|
2425 |
* after a suitable number of squarings have passed (e.g. a pattern |
|
2426 |
* of "100" in the buffer requires that we multiply by n^1 immediately; |
|
2427 |
* a pattern of "110" calls for multiplying by n^3 after one more |
|
2428 |
* squaring), clear the buffer, and continue. |
|
2429 |
* |
|
2430 |
* When we start, there is one more optimization: the result buffer |
|
2431 |
* is implcitly one, so squaring it or multiplying by it can be |
|
2432 |
* optimized away. Further, if we start with a pattern like "100" |
|
2433 |
* in the lookahead window, rather than placing n into the buffer |
|
2434 |
* and then starting to square it, we have already computed n^2 |
|
2435 |
* to compute the odd-powers table, so we can place that into |
|
2436 |
* the buffer and save a squaring. |
|
2437 |
* |
|
2438 |
* This means that if you have a k-bit window, to compute n^z, |
|
2439 |
* where z is the high k bits of the exponent, 1/2 of the time |
|
2440 |
* it requires no squarings. 1/4 of the time, it requires 1 |
|
2441 |
* squaring, ... 1/2^(k-1) of the time, it reqires k-2 squarings. |
|
2442 |
* And the remaining 1/2^(k-1) of the time, the top k bits are a |
|
2443 |
* 1 followed by k-1 0 bits, so it again only requires k-2 |
|
2444 |
* squarings, not k-1. The average of these is 1. Add that |
|
2445 |
* to the one squaring we have to do to compute the table, |
|
2446 |
* and you'll see that a k-bit window saves k-2 squarings |
|
2447 |
* as well as reducing the multiplies. (It actually doesn't |
|
2448 |
* hurt in the case k = 1, either.) |
|
2449 |
*/ |
|
2450 |
// Special case for exponent of one |
|
2451 |
if (y.equals(ONE)) |
|
2452 |
return this; |
|
2453 |
||
2454 |
// Special case for base of zero |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2455 |
if (signum == 0) |
2 | 2456 |
return ZERO; |
2457 |
||
2458 |
int[] base = mag.clone(); |
|
2459 |
int[] exp = y.mag; |
|
2460 |
int[] mod = z.mag; |
|
2461 |
int modLen = mod.length; |
|
2462 |
||
2463 |
// Select an appropriate window size |
|
2464 |
int wbits = 0; |
|
2465 |
int ebits = bitLength(exp, exp.length); |
|
2466 |
// if exponent is 65537 (0x10001), use minimum window size |
|
2467 |
if ((ebits != 17) || (exp[0] != 65537)) { |
|
2468 |
while (ebits > bnExpModThreshTable[wbits]) { |
|
2469 |
wbits++; |
|
2470 |
} |
|
2471 |
} |
|
2472 |
||
2473 |
// Calculate appropriate table size |
|
2474 |
int tblmask = 1 << wbits; |
|
2475 |
||
2476 |
// Allocate table for precomputed odd powers of base in Montgomery form |
|
2477 |
int[][] table = new int[tblmask][]; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2478 |
for (int i=0; i < tblmask; i++) |
2 | 2479 |
table[i] = new int[modLen]; |
2480 |
||
2481 |
// Compute the modular inverse |
|
2482 |
int inv = -MutableBigInteger.inverseMod32(mod[modLen-1]); |
|
2483 |
||
2484 |
// Convert base to Montgomery form |
|
2485 |
int[] a = leftShift(base, base.length, modLen << 5); |
|
2486 |
||
2487 |
MutableBigInteger q = new MutableBigInteger(), |
|
2488 |
a2 = new MutableBigInteger(a), |
|
2489 |
b2 = new MutableBigInteger(mod); |
|
2490 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2491 |
MutableBigInteger r= a2.divide(b2, q); |
2 | 2492 |
table[0] = r.toIntArray(); |
2493 |
||
2494 |
// Pad table[0] with leading zeros so its length is at least modLen |
|
2495 |
if (table[0].length < modLen) { |
|
2496 |
int offset = modLen - table[0].length; |
|
2497 |
int[] t2 = new int[modLen]; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2498 |
for (int i=0; i < table[0].length; i++) |
2 | 2499 |
t2[i+offset] = table[0][i]; |
2500 |
table[0] = t2; |
|
2501 |
} |
|
2502 |
||
2503 |
// Set b to the square of the base |
|
2504 |
int[] b = squareToLen(table[0], modLen, null); |
|
2505 |
b = montReduce(b, mod, modLen, inv); |
|
2506 |
||
2507 |
// Set t to high half of b |
|
10425
7903cf45f96f
6989067: BigInteger's array copiers should be converted to System.arraycopy()
darcy
parents:
10423
diff
changeset
|
2508 |
int[] t = Arrays.copyOf(b, modLen); |
2 | 2509 |
|
2510 |
// Fill in the table with odd powers of the base |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2511 |
for (int i=1; i < tblmask; i++) { |
2 | 2512 |
int[] prod = multiplyToLen(t, modLen, table[i-1], modLen, null); |
2513 |
table[i] = montReduce(prod, mod, modLen, inv); |
|
2514 |
} |
|
2515 |
||
2516 |
// Pre load the window that slides over the exponent |
|
2517 |
int bitpos = 1 << ((ebits-1) & (32-1)); |
|
2518 |
||
2519 |
int buf = 0; |
|
2520 |
int elen = exp.length; |
|
2521 |
int eIndex = 0; |
|
2522 |
for (int i = 0; i <= wbits; i++) { |
|
2523 |
buf = (buf << 1) | (((exp[eIndex] & bitpos) != 0)?1:0); |
|
2524 |
bitpos >>>= 1; |
|
2525 |
if (bitpos == 0) { |
|
2526 |
eIndex++; |
|
2527 |
bitpos = 1 << (32-1); |
|
2528 |
elen--; |
|
2529 |
} |
|
2530 |
} |
|
2531 |
||
2532 |
int multpos = ebits; |
|
2533 |
||
2534 |
// The first iteration, which is hoisted out of the main loop |
|
2535 |
ebits--; |
|
2536 |
boolean isone = true; |
|
2537 |
||
2538 |
multpos = ebits - wbits; |
|
2539 |
while ((buf & 1) == 0) { |
|
2540 |
buf >>>= 1; |
|
2541 |
multpos++; |
|
2542 |
} |
|
2543 |
||
2544 |
int[] mult = table[buf >>> 1]; |
|
2545 |
||
2546 |
buf = 0; |
|
2547 |
if (multpos == ebits) |
|
2548 |
isone = false; |
|
2549 |
||
2550 |
// The main loop |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2551 |
while (true) { |
2 | 2552 |
ebits--; |
2553 |
// Advance the window |
|
2554 |
buf <<= 1; |
|
2555 |
||
2556 |
if (elen != 0) { |
|
2557 |
buf |= ((exp[eIndex] & bitpos) != 0) ? 1 : 0; |
|
2558 |
bitpos >>>= 1; |
|
2559 |
if (bitpos == 0) { |
|
2560 |
eIndex++; |
|
2561 |
bitpos = 1 << (32-1); |
|
2562 |
elen--; |
|
2563 |
} |
|
2564 |
} |
|
2565 |
||
2566 |
// Examine the window for pending multiplies |
|
2567 |
if ((buf & tblmask) != 0) { |
|
2568 |
multpos = ebits - wbits; |
|
2569 |
while ((buf & 1) == 0) { |
|
2570 |
buf >>>= 1; |
|
2571 |
multpos++; |
|
2572 |
} |
|
2573 |
mult = table[buf >>> 1]; |
|
2574 |
buf = 0; |
|
2575 |
} |
|
2576 |
||
2577 |
// Perform multiply |
|
2578 |
if (ebits == multpos) { |
|
2579 |
if (isone) { |
|
2580 |
b = mult.clone(); |
|
2581 |
isone = false; |
|
2582 |
} else { |
|
2583 |
t = b; |
|
2584 |
a = multiplyToLen(t, modLen, mult, modLen, a); |
|
2585 |
a = montReduce(a, mod, modLen, inv); |
|
2586 |
t = a; a = b; b = t; |
|
2587 |
} |
|
2588 |
} |
|
2589 |
||
2590 |
// Check if done |
|
2591 |
if (ebits == 0) |
|
2592 |
break; |
|
2593 |
||
2594 |
// Square the input |
|
2595 |
if (!isone) { |
|
2596 |
t = b; |
|
2597 |
a = squareToLen(t, modLen, a); |
|
2598 |
a = montReduce(a, mod, modLen, inv); |
|
2599 |
t = a; a = b; b = t; |
|
2600 |
} |
|
2601 |
} |
|
2602 |
||
2603 |
// Convert result out of Montgomery form and return |
|
2604 |
int[] t2 = new int[2*modLen]; |
|
10425
7903cf45f96f
6989067: BigInteger's array copiers should be converted to System.arraycopy()
darcy
parents:
10423
diff
changeset
|
2605 |
System.arraycopy(b, 0, t2, modLen, modLen); |
2 | 2606 |
|
2607 |
b = montReduce(t2, mod, modLen, inv); |
|
2608 |
||
10425
7903cf45f96f
6989067: BigInteger's array copiers should be converted to System.arraycopy()
darcy
parents:
10423
diff
changeset
|
2609 |
t2 = Arrays.copyOf(b, modLen); |
2 | 2610 |
|
2611 |
return new BigInteger(1, t2); |
|
2612 |
} |
|
2613 |
||
2614 |
/** |
|
2615 |
* Montgomery reduce n, modulo mod. This reduces modulo mod and divides |
|
2616 |
* by 2^(32*mlen). Adapted from Colin Plumb's C library. |
|
2617 |
*/ |
|
2618 |
private static int[] montReduce(int[] n, int[] mod, int mlen, int inv) { |
|
2619 |
int c=0; |
|
2620 |
int len = mlen; |
|
2621 |
int offset=0; |
|
2622 |
||
2623 |
do { |
|
2624 |
int nEnd = n[n.length-1-offset]; |
|
2625 |
int carry = mulAdd(n, mod, offset, mlen, inv * nEnd); |
|
2626 |
c += addOne(n, offset, mlen, carry); |
|
2627 |
offset++; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2628 |
} while (--len > 0); |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2629 |
|
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2630 |
while (c > 0) |
2 | 2631 |
c += subN(n, mod, mlen); |
2632 |
||
2633 |
while (intArrayCmpToLen(n, mod, mlen) >= 0) |
|
2634 |
subN(n, mod, mlen); |
|
2635 |
||
2636 |
return n; |
|
2637 |
} |
|
2638 |
||
2639 |
||
2640 |
/* |
|
2641 |
* Returns -1, 0 or +1 as big-endian unsigned int array arg1 is less than, |
|
2642 |
* equal to, or greater than arg2 up to length len. |
|
2643 |
*/ |
|
2644 |
private static int intArrayCmpToLen(int[] arg1, int[] arg2, int len) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2645 |
for (int i=0; i < len; i++) { |
2 | 2646 |
long b1 = arg1[i] & LONG_MASK; |
2647 |
long b2 = arg2[i] & LONG_MASK; |
|
2648 |
if (b1 < b2) |
|
2649 |
return -1; |
|
2650 |
if (b1 > b2) |
|
2651 |
return 1; |
|
2652 |
} |
|
2653 |
return 0; |
|
2654 |
} |
|
2655 |
||
2656 |
/** |
|
2657 |
* Subtracts two numbers of same length, returning borrow. |
|
2658 |
*/ |
|
2659 |
private static int subN(int[] a, int[] b, int len) { |
|
2660 |
long sum = 0; |
|
2661 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2662 |
while (--len >= 0) { |
2 | 2663 |
sum = (a[len] & LONG_MASK) - |
2664 |
(b[len] & LONG_MASK) + (sum >> 32); |
|
2665 |
a[len] = (int)sum; |
|
2666 |
} |
|
2667 |
||
2668 |
return (int)(sum >> 32); |
|
2669 |
} |
|
2670 |
||
2671 |
/** |
|
2672 |
* Multiply an array by one word k and add to result, return the carry |
|
2673 |
*/ |
|
2674 |
static int mulAdd(int[] out, int[] in, int offset, int len, int k) { |
|
2675 |
long kLong = k & LONG_MASK; |
|
2676 |
long carry = 0; |
|
2677 |
||
2678 |
offset = out.length-offset - 1; |
|
2679 |
for (int j=len-1; j >= 0; j--) { |
|
2680 |
long product = (in[j] & LONG_MASK) * kLong + |
|
2681 |
(out[offset] & LONG_MASK) + carry; |
|
2682 |
out[offset--] = (int)product; |
|
2683 |
carry = product >>> 32; |
|
2684 |
} |
|
2685 |
return (int)carry; |
|
2686 |
} |
|
2687 |
||
2688 |
/** |
|
2689 |
* Add one word to the number a mlen words into a. Return the resulting |
|
2690 |
* carry. |
|
2691 |
*/ |
|
2692 |
static int addOne(int[] a, int offset, int mlen, int carry) { |
|
2693 |
offset = a.length-1-mlen-offset; |
|
2694 |
long t = (a[offset] & LONG_MASK) + (carry & LONG_MASK); |
|
2695 |
||
2696 |
a[offset] = (int)t; |
|
2697 |
if ((t >>> 32) == 0) |
|
2698 |
return 0; |
|
2699 |
while (--mlen >= 0) { |
|
2700 |
if (--offset < 0) { // Carry out of number |
|
2701 |
return 1; |
|
2702 |
} else { |
|
2703 |
a[offset]++; |
|
2704 |
if (a[offset] != 0) |
|
2705 |
return 0; |
|
2706 |
} |
|
2707 |
} |
|
2708 |
return 1; |
|
2709 |
} |
|
2710 |
||
2711 |
/** |
|
2712 |
* Returns a BigInteger whose value is (this ** exponent) mod (2**p) |
|
2713 |
*/ |
|
2714 |
private BigInteger modPow2(BigInteger exponent, int p) { |
|
2715 |
/* |
|
2716 |
* Perform exponentiation using repeated squaring trick, chopping off |
|
2717 |
* high order bits as indicated by modulus. |
|
2718 |
*/ |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
2719 |
BigInteger result = ONE; |
2 | 2720 |
BigInteger baseToPow2 = this.mod2(p); |
2721 |
int expOffset = 0; |
|
2722 |
||
2723 |
int limit = exponent.bitLength(); |
|
2724 |
||
2725 |
if (this.testBit(0)) |
|
2726 |
limit = (p-1) < limit ? (p-1) : limit; |
|
2727 |
||
2728 |
while (expOffset < limit) { |
|
2729 |
if (exponent.testBit(expOffset)) |
|
2730 |
result = result.multiply(baseToPow2).mod2(p); |
|
2731 |
expOffset++; |
|
2732 |
if (expOffset < limit) |
|
2733 |
baseToPow2 = baseToPow2.square().mod2(p); |
|
2734 |
} |
|
2735 |
||
2736 |
return result; |
|
2737 |
} |
|
2738 |
||
2739 |
/** |
|
2740 |
* Returns a BigInteger whose value is this mod(2**p). |
|
2741 |
* Assumes that this {@code BigInteger >= 0} and {@code p > 0}. |
|
2742 |
*/ |
|
2743 |
private BigInteger mod2(int p) { |
|
2744 |
if (bitLength() <= p) |
|
2745 |
return this; |
|
2746 |
||
2747 |
// Copy remaining ints of mag |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2748 |
int numInts = (p + 31) >>> 5; |
2 | 2749 |
int[] mag = new int[numInts]; |
10425
7903cf45f96f
6989067: BigInteger's array copiers should be converted to System.arraycopy()
darcy
parents:
10423
diff
changeset
|
2750 |
System.arraycopy(this.mag, (this.mag.length - numInts), mag, 0, numInts); |
2 | 2751 |
|
2752 |
// Mask out any excess bits |
|
2753 |
int excessBits = (numInts << 5) - p; |
|
2754 |
mag[0] &= (1L << (32-excessBits)) - 1; |
|
2755 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2756 |
return (mag[0] == 0 ? new BigInteger(1, mag) : new BigInteger(mag, 1)); |
2 | 2757 |
} |
2758 |
||
2759 |
/** |
|
2760 |
* Returns a BigInteger whose value is {@code (this}<sup>-1</sup> {@code mod m)}. |
|
2761 |
* |
|
2762 |
* @param m the modulus. |
|
2763 |
* @return {@code this}<sup>-1</sup> {@code mod m}. |
|
4160
bda0a85afcb7
6560935: BigInteger.modPow() throws ArithmeticException for negative exponent
darcy
parents:
4151
diff
changeset
|
2764 |
* @throws ArithmeticException {@code m} ≤ 0, or this BigInteger |
2 | 2765 |
* has no multiplicative inverse mod m (that is, this BigInteger |
2766 |
* is not <i>relatively prime</i> to m). |
|
2767 |
*/ |
|
2768 |
public BigInteger modInverse(BigInteger m) { |
|
2769 |
if (m.signum != 1) |
|
2770 |
throw new ArithmeticException("BigInteger: modulus not positive"); |
|
2771 |
||
2772 |
if (m.equals(ONE)) |
|
2773 |
return ZERO; |
|
2774 |
||
2775 |
// Calculate (this mod m) |
|
2776 |
BigInteger modVal = this; |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2777 |
if (signum < 0 || (this.compareMagnitude(m) >= 0)) |
2 | 2778 |
modVal = this.mod(m); |
2779 |
||
2780 |
if (modVal.equals(ONE)) |
|
2781 |
return ONE; |
|
2782 |
||
2783 |
MutableBigInteger a = new MutableBigInteger(modVal); |
|
2784 |
MutableBigInteger b = new MutableBigInteger(m); |
|
2785 |
||
2786 |
MutableBigInteger result = a.mutableModInverse(b); |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
2787 |
return result.toBigInteger(1); |
2 | 2788 |
} |
2789 |
||
2790 |
// Shift Operations |
|
2791 |
||
2792 |
/** |
|
2793 |
* Returns a BigInteger whose value is {@code (this << n)}. |
|
2794 |
* The shift distance, {@code n}, may be negative, in which case |
|
2795 |
* this method performs a right shift. |
|
2796 |
* (Computes <tt>floor(this * 2<sup>n</sup>)</tt>.) |
|
2797 |
* |
|
2798 |
* @param n shift distance, in bits. |
|
2799 |
* @return {@code this << n} |
|
4151
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2800 |
* @throws ArithmeticException if the shift distance is {@code |
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2801 |
* Integer.MIN_VALUE}. |
2 | 2802 |
* @see #shiftRight |
2803 |
*/ |
|
2804 |
public BigInteger shiftLeft(int n) { |
|
2805 |
if (signum == 0) |
|
2806 |
return ZERO; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2807 |
if (n == 0) |
2 | 2808 |
return this; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2809 |
if (n < 0) { |
4151
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2810 |
if (n == Integer.MIN_VALUE) { |
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2811 |
throw new ArithmeticException("Shift distance of Integer.MIN_VALUE not supported."); |
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2812 |
} else { |
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2813 |
return shiftRight(-n); |
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2814 |
} |
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2815 |
} |
10425
7903cf45f96f
6989067: BigInteger's array copiers should be converted to System.arraycopy()
darcy
parents:
10423
diff
changeset
|
2816 |
int[] newMag = shiftLeft(mag, n); |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
2817 |
|
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
2818 |
return new BigInteger(newMag, signum); |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
2819 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
2820 |
|
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
2821 |
private static int[] shiftLeft(int[] mag, int n) { |
2 | 2822 |
int nInts = n >>> 5; |
2823 |
int nBits = n & 0x1f; |
|
2824 |
int magLen = mag.length; |
|
2825 |
int newMag[] = null; |
|
2826 |
||
2827 |
if (nBits == 0) { |
|
2828 |
newMag = new int[magLen + nInts]; |
|
10425
7903cf45f96f
6989067: BigInteger's array copiers should be converted to System.arraycopy()
darcy
parents:
10423
diff
changeset
|
2829 |
System.arraycopy(mag, 0, newMag, 0, magLen); |
2 | 2830 |
} else { |
2831 |
int i = 0; |
|
2832 |
int nBits2 = 32 - nBits; |
|
2833 |
int highBits = mag[0] >>> nBits2; |
|
2834 |
if (highBits != 0) { |
|
2835 |
newMag = new int[magLen + nInts + 1]; |
|
2836 |
newMag[i++] = highBits; |
|
2837 |
} else { |
|
2838 |
newMag = new int[magLen + nInts]; |
|
2839 |
} |
|
2840 |
int j=0; |
|
2841 |
while (j < magLen-1) |
|
2842 |
newMag[i++] = mag[j++] << nBits | mag[j] >>> nBits2; |
|
2843 |
newMag[i] = mag[j] << nBits; |
|
2844 |
} |
|
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
2845 |
return newMag; |
2 | 2846 |
} |
2847 |
||
2848 |
/** |
|
2849 |
* Returns a BigInteger whose value is {@code (this >> n)}. Sign |
|
2850 |
* extension is performed. The shift distance, {@code n}, may be |
|
2851 |
* negative, in which case this method performs a left shift. |
|
2852 |
* (Computes <tt>floor(this / 2<sup>n</sup>)</tt>.) |
|
2853 |
* |
|
2854 |
* @param n shift distance, in bits. |
|
2855 |
* @return {@code this >> n} |
|
4151
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2856 |
* @throws ArithmeticException if the shift distance is {@code |
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2857 |
* Integer.MIN_VALUE}. |
2 | 2858 |
* @see #shiftLeft |
2859 |
*/ |
|
2860 |
public BigInteger shiftRight(int n) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2861 |
if (n == 0) |
2 | 2862 |
return this; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2863 |
if (n < 0) { |
4151
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2864 |
if (n == Integer.MIN_VALUE) { |
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2865 |
throw new ArithmeticException("Shift distance of Integer.MIN_VALUE not supported."); |
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2866 |
} else { |
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2867 |
return shiftLeft(-n); |
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2868 |
} |
db7d59c0b0e6
6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError
darcy
parents:
2922
diff
changeset
|
2869 |
} |
2 | 2870 |
|
2871 |
int nInts = n >>> 5; |
|
2872 |
int nBits = n & 0x1f; |
|
2873 |
int magLen = mag.length; |
|
2874 |
int newMag[] = null; |
|
2875 |
||
2876 |
// Special case: entire contents shifted off the end |
|
2877 |
if (nInts >= magLen) |
|
2878 |
return (signum >= 0 ? ZERO : negConst[1]); |
|
2879 |
||
2880 |
if (nBits == 0) { |
|
2881 |
int newMagLen = magLen - nInts; |
|
10425
7903cf45f96f
6989067: BigInteger's array copiers should be converted to System.arraycopy()
darcy
parents:
10423
diff
changeset
|
2882 |
newMag = Arrays.copyOf(mag, newMagLen); |
2 | 2883 |
} else { |
2884 |
int i = 0; |
|
2885 |
int highBits = mag[0] >>> nBits; |
|
2886 |
if (highBits != 0) { |
|
2887 |
newMag = new int[magLen - nInts]; |
|
2888 |
newMag[i++] = highBits; |
|
2889 |
} else { |
|
2890 |
newMag = new int[magLen - nInts -1]; |
|
2891 |
} |
|
2892 |
||
2893 |
int nBits2 = 32 - nBits; |
|
2894 |
int j=0; |
|
2895 |
while (j < magLen - nInts - 1) |
|
2896 |
newMag[i++] = (mag[j++] << nBits2) | (mag[j] >>> nBits); |
|
2897 |
} |
|
2898 |
||
2899 |
if (signum < 0) { |
|
2900 |
// Find out whether any one-bits were shifted off the end. |
|
2901 |
boolean onesLost = false; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2902 |
for (int i=magLen-1, j=magLen-nInts; i >= j && !onesLost; i--) |
2 | 2903 |
onesLost = (mag[i] != 0); |
2904 |
if (!onesLost && nBits != 0) |
|
2905 |
onesLost = (mag[magLen - nInts - 1] << (32 - nBits) != 0); |
|
2906 |
||
2907 |
if (onesLost) |
|
2908 |
newMag = javaIncrement(newMag); |
|
2909 |
} |
|
2910 |
||
2911 |
return new BigInteger(newMag, signum); |
|
2912 |
} |
|
2913 |
||
2914 |
int[] javaIncrement(int[] val) { |
|
2915 |
int lastSum = 0; |
|
2916 |
for (int i=val.length-1; i >= 0 && lastSum == 0; i--) |
|
2917 |
lastSum = (val[i] += 1); |
|
2918 |
if (lastSum == 0) { |
|
2919 |
val = new int[val.length+1]; |
|
2920 |
val[0] = 1; |
|
2921 |
} |
|
2922 |
return val; |
|
2923 |
} |
|
2924 |
||
2925 |
// Bitwise Operations |
|
2926 |
||
2927 |
/** |
|
2928 |
* Returns a BigInteger whose value is {@code (this & val)}. (This |
|
2929 |
* method returns a negative BigInteger if and only if this and val are |
|
2930 |
* both negative.) |
|
2931 |
* |
|
2932 |
* @param val value to be AND'ed with this BigInteger. |
|
2933 |
* @return {@code this & val} |
|
2934 |
*/ |
|
2935 |
public BigInteger and(BigInteger val) { |
|
2936 |
int[] result = new int[Math.max(intLength(), val.intLength())]; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2937 |
for (int i=0; i < result.length; i++) |
2 | 2938 |
result[i] = (getInt(result.length-i-1) |
2939 |
& val.getInt(result.length-i-1)); |
|
2940 |
||
2941 |
return valueOf(result); |
|
2942 |
} |
|
2943 |
||
2944 |
/** |
|
2945 |
* Returns a BigInteger whose value is {@code (this | val)}. (This method |
|
2946 |
* returns a negative BigInteger if and only if either this or val is |
|
2947 |
* negative.) |
|
2948 |
* |
|
2949 |
* @param val value to be OR'ed with this BigInteger. |
|
2950 |
* @return {@code this | val} |
|
2951 |
*/ |
|
2952 |
public BigInteger or(BigInteger val) { |
|
2953 |
int[] result = new int[Math.max(intLength(), val.intLength())]; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2954 |
for (int i=0; i < result.length; i++) |
2 | 2955 |
result[i] = (getInt(result.length-i-1) |
2956 |
| val.getInt(result.length-i-1)); |
|
2957 |
||
2958 |
return valueOf(result); |
|
2959 |
} |
|
2960 |
||
2961 |
/** |
|
2962 |
* Returns a BigInteger whose value is {@code (this ^ val)}. (This method |
|
2963 |
* returns a negative BigInteger if and only if exactly one of this and |
|
2964 |
* val are negative.) |
|
2965 |
* |
|
2966 |
* @param val value to be XOR'ed with this BigInteger. |
|
2967 |
* @return {@code this ^ val} |
|
2968 |
*/ |
|
2969 |
public BigInteger xor(BigInteger val) { |
|
2970 |
int[] result = new int[Math.max(intLength(), val.intLength())]; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2971 |
for (int i=0; i < result.length; i++) |
2 | 2972 |
result[i] = (getInt(result.length-i-1) |
2973 |
^ val.getInt(result.length-i-1)); |
|
2974 |
||
2975 |
return valueOf(result); |
|
2976 |
} |
|
2977 |
||
2978 |
/** |
|
2979 |
* Returns a BigInteger whose value is {@code (~this)}. (This method |
|
2980 |
* returns a negative value if and only if this BigInteger is |
|
2981 |
* non-negative.) |
|
2982 |
* |
|
2983 |
* @return {@code ~this} |
|
2984 |
*/ |
|
2985 |
public BigInteger not() { |
|
2986 |
int[] result = new int[intLength()]; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
2987 |
for (int i=0; i < result.length; i++) |
2 | 2988 |
result[i] = ~getInt(result.length-i-1); |
2989 |
||
2990 |
return valueOf(result); |
|
2991 |
} |
|
2992 |
||
2993 |
/** |
|
2994 |
* Returns a BigInteger whose value is {@code (this & ~val)}. This |
|
2995 |
* method, which is equivalent to {@code and(val.not())}, is provided as |
|
2996 |
* a convenience for masking operations. (This method returns a negative |
|
2997 |
* BigInteger if and only if {@code this} is negative and {@code val} is |
|
2998 |
* positive.) |
|
2999 |
* |
|
3000 |
* @param val value to be complemented and AND'ed with this BigInteger. |
|
3001 |
* @return {@code this & ~val} |
|
3002 |
*/ |
|
3003 |
public BigInteger andNot(BigInteger val) { |
|
3004 |
int[] result = new int[Math.max(intLength(), val.intLength())]; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3005 |
for (int i=0; i < result.length; i++) |
2 | 3006 |
result[i] = (getInt(result.length-i-1) |
3007 |
& ~val.getInt(result.length-i-1)); |
|
3008 |
||
3009 |
return valueOf(result); |
|
3010 |
} |
|
3011 |
||
3012 |
||
3013 |
// Single Bit Operations |
|
3014 |
||
3015 |
/** |
|
3016 |
* Returns {@code true} if and only if the designated bit is set. |
|
3017 |
* (Computes {@code ((this & (1<<n)) != 0)}.) |
|
3018 |
* |
|
3019 |
* @param n index of bit to test. |
|
3020 |
* @return {@code true} if and only if the designated bit is set. |
|
3021 |
* @throws ArithmeticException {@code n} is negative. |
|
3022 |
*/ |
|
3023 |
public boolean testBit(int n) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3024 |
if (n < 0) |
2 | 3025 |
throw new ArithmeticException("Negative bit address"); |
3026 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3027 |
return (getInt(n >>> 5) & (1 << (n & 31))) != 0; |
2 | 3028 |
} |
3029 |
||
3030 |
/** |
|
3031 |
* Returns a BigInteger whose value is equivalent to this BigInteger |
|
3032 |
* with the designated bit set. (Computes {@code (this | (1<<n))}.) |
|
3033 |
* |
|
3034 |
* @param n index of bit to set. |
|
3035 |
* @return {@code this | (1<<n)} |
|
3036 |
* @throws ArithmeticException {@code n} is negative. |
|
3037 |
*/ |
|
3038 |
public BigInteger setBit(int n) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3039 |
if (n < 0) |
2 | 3040 |
throw new ArithmeticException("Negative bit address"); |
3041 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3042 |
int intNum = n >>> 5; |
2 | 3043 |
int[] result = new int[Math.max(intLength(), intNum+2)]; |
3044 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3045 |
for (int i=0; i < result.length; i++) |
2 | 3046 |
result[result.length-i-1] = getInt(i); |
3047 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3048 |
result[result.length-intNum-1] |= (1 << (n & 31)); |
2 | 3049 |
|
3050 |
return valueOf(result); |
|
3051 |
} |
|
3052 |
||
3053 |
/** |
|
3054 |
* Returns a BigInteger whose value is equivalent to this BigInteger |
|
3055 |
* with the designated bit cleared. |
|
3056 |
* (Computes {@code (this & ~(1<<n))}.) |
|
3057 |
* |
|
3058 |
* @param n index of bit to clear. |
|
3059 |
* @return {@code this & ~(1<<n)} |
|
3060 |
* @throws ArithmeticException {@code n} is negative. |
|
3061 |
*/ |
|
3062 |
public BigInteger clearBit(int n) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3063 |
if (n < 0) |
2 | 3064 |
throw new ArithmeticException("Negative bit address"); |
3065 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3066 |
int intNum = n >>> 5; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3067 |
int[] result = new int[Math.max(intLength(), ((n + 1) >>> 5) + 1)]; |
2 | 3068 |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3069 |
for (int i=0; i < result.length; i++) |
2 | 3070 |
result[result.length-i-1] = getInt(i); |
3071 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3072 |
result[result.length-intNum-1] &= ~(1 << (n & 31)); |
2 | 3073 |
|
3074 |
return valueOf(result); |
|
3075 |
} |
|
3076 |
||
3077 |
/** |
|
3078 |
* Returns a BigInteger whose value is equivalent to this BigInteger |
|
3079 |
* with the designated bit flipped. |
|
3080 |
* (Computes {@code (this ^ (1<<n))}.) |
|
3081 |
* |
|
3082 |
* @param n index of bit to flip. |
|
3083 |
* @return {@code this ^ (1<<n)} |
|
3084 |
* @throws ArithmeticException {@code n} is negative. |
|
3085 |
*/ |
|
3086 |
public BigInteger flipBit(int n) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3087 |
if (n < 0) |
2 | 3088 |
throw new ArithmeticException("Negative bit address"); |
3089 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3090 |
int intNum = n >>> 5; |
2 | 3091 |
int[] result = new int[Math.max(intLength(), intNum+2)]; |
3092 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3093 |
for (int i=0; i < result.length; i++) |
2 | 3094 |
result[result.length-i-1] = getInt(i); |
3095 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3096 |
result[result.length-intNum-1] ^= (1 << (n & 31)); |
2 | 3097 |
|
3098 |
return valueOf(result); |
|
3099 |
} |
|
3100 |
||
3101 |
/** |
|
3102 |
* Returns the index of the rightmost (lowest-order) one bit in this |
|
3103 |
* BigInteger (the number of zero bits to the right of the rightmost |
|
3104 |
* one bit). Returns -1 if this BigInteger contains no one bits. |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3105 |
* (Computes {@code (this == 0? -1 : log2(this & -this))}.) |
2 | 3106 |
* |
3107 |
* @return index of the rightmost one bit in this BigInteger. |
|
3108 |
*/ |
|
3109 |
public int getLowestSetBit() { |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3110 |
@SuppressWarnings("deprecation") int lsb = lowestSetBit - 2; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3111 |
if (lsb == -2) { // lowestSetBit not initialized yet |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3112 |
lsb = 0; |
2 | 3113 |
if (signum == 0) { |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3114 |
lsb -= 1; |
2 | 3115 |
} else { |
3116 |
// Search for lowest order nonzero int |
|
3117 |
int i,b; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3118 |
for (i=0; (b = getInt(i)) == 0; i++) |
2 | 3119 |
; |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3120 |
lsb += (i << 5) + Integer.numberOfTrailingZeros(b); |
2 | 3121 |
} |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3122 |
lowestSetBit = lsb + 2; |
2 | 3123 |
} |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3124 |
return lsb; |
2 | 3125 |
} |
3126 |
||
3127 |
||
3128 |
// Miscellaneous Bit Operations |
|
3129 |
||
3130 |
/** |
|
3131 |
* Returns the number of bits in the minimal two's-complement |
|
3132 |
* representation of this BigInteger, <i>excluding</i> a sign bit. |
|
3133 |
* For positive BigIntegers, this is equivalent to the number of bits in |
|
3134 |
* the ordinary binary representation. (Computes |
|
3135 |
* {@code (ceil(log2(this < 0 ? -this : this+1)))}.) |
|
3136 |
* |
|
3137 |
* @return number of bits in the minimal two's-complement |
|
3138 |
* representation of this BigInteger, <i>excluding</i> a sign bit. |
|
3139 |
*/ |
|
3140 |
public int bitLength() { |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3141 |
@SuppressWarnings("deprecation") int n = bitLength - 1; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3142 |
if (n == -1) { // bitLength not initialized yet |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3143 |
int[] m = mag; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3144 |
int len = m.length; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3145 |
if (len == 0) { |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3146 |
n = 0; // offset by one to initialize |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3147 |
} else { |
2 | 3148 |
// Calculate the bit length of the magnitude |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3149 |
int magBitLength = ((len - 1) << 5) + bitLengthForInt(mag[0]); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3150 |
if (signum < 0) { |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3151 |
// Check if magnitude is a power of two |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3152 |
boolean pow2 = (Integer.bitCount(mag[0]) == 1); |
10425
7903cf45f96f
6989067: BigInteger's array copiers should be converted to System.arraycopy()
darcy
parents:
10423
diff
changeset
|
3153 |
for (int i=1; i< len && pow2; i++) |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3154 |
pow2 = (mag[i] == 0); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3155 |
|
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3156 |
n = (pow2 ? magBitLength -1 : magBitLength); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3157 |
} else { |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3158 |
n = magBitLength; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3159 |
} |
2 | 3160 |
} |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3161 |
bitLength = n + 1; |
2 | 3162 |
} |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3163 |
return n; |
2 | 3164 |
} |
3165 |
||
3166 |
/** |
|
3167 |
* Returns the number of bits in the two's complement representation |
|
3168 |
* of this BigInteger that differ from its sign bit. This method is |
|
3169 |
* useful when implementing bit-vector style sets atop BigIntegers. |
|
3170 |
* |
|
3171 |
* @return number of bits in the two's complement representation |
|
3172 |
* of this BigInteger that differ from its sign bit. |
|
3173 |
*/ |
|
3174 |
public int bitCount() { |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3175 |
@SuppressWarnings("deprecation") int bc = bitCount - 1; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3176 |
if (bc == -1) { // bitCount not initialized yet |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3177 |
bc = 0; // offset by one to initialize |
2 | 3178 |
// Count the bits in the magnitude |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3179 |
for (int i=0; i < mag.length; i++) |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3180 |
bc += Integer.bitCount(mag[i]); |
2 | 3181 |
if (signum < 0) { |
3182 |
// Count the trailing zeros in the magnitude |
|
3183 |
int magTrailingZeroCount = 0, j; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3184 |
for (j=mag.length-1; mag[j] == 0; j--) |
2 | 3185 |
magTrailingZeroCount += 32; |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3186 |
magTrailingZeroCount += Integer.numberOfTrailingZeros(mag[j]); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3187 |
bc += magTrailingZeroCount - 1; |
2 | 3188 |
} |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3189 |
bitCount = bc + 1; |
2 | 3190 |
} |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3191 |
return bc; |
2 | 3192 |
} |
3193 |
||
3194 |
// Primality Testing |
|
3195 |
||
3196 |
/** |
|
3197 |
* Returns {@code true} if this BigInteger is probably prime, |
|
3198 |
* {@code false} if it's definitely composite. If |
|
4160
bda0a85afcb7
6560935: BigInteger.modPow() throws ArithmeticException for negative exponent
darcy
parents:
4151
diff
changeset
|
3199 |
* {@code certainty} is ≤ 0, {@code true} is |
2 | 3200 |
* returned. |
3201 |
* |
|
3202 |
* @param certainty a measure of the uncertainty that the caller is |
|
3203 |
* willing to tolerate: if the call returns {@code true} |
|
3204 |
* the probability that this BigInteger is prime exceeds |
|
3205 |
* (1 - 1/2<sup>{@code certainty}</sup>). The execution time of |
|
3206 |
* this method is proportional to the value of this parameter. |
|
3207 |
* @return {@code true} if this BigInteger is probably prime, |
|
3208 |
* {@code false} if it's definitely composite. |
|
3209 |
*/ |
|
3210 |
public boolean isProbablePrime(int certainty) { |
|
3211 |
if (certainty <= 0) |
|
3212 |
return true; |
|
3213 |
BigInteger w = this.abs(); |
|
3214 |
if (w.equals(TWO)) |
|
3215 |
return true; |
|
3216 |
if (!w.testBit(0) || w.equals(ONE)) |
|
3217 |
return false; |
|
3218 |
||
3219 |
return w.primeToCertainty(certainty, null); |
|
3220 |
} |
|
3221 |
||
3222 |
// Comparison Operations |
|
3223 |
||
3224 |
/** |
|
3225 |
* Compares this BigInteger with the specified BigInteger. This |
|
3226 |
* method is provided in preference to individual methods for each |
|
3227 |
* of the six boolean comparison operators ({@literal <}, ==, |
|
3228 |
* {@literal >}, {@literal >=}, !=, {@literal <=}). The suggested |
|
3229 |
* idiom for performing these comparisons is: {@code |
|
3230 |
* (x.compareTo(y)} <<i>op</i>> {@code 0)}, where |
|
3231 |
* <<i>op</i>> is one of the six comparison operators. |
|
3232 |
* |
|
3233 |
* @param val BigInteger to which this BigInteger is to be compared. |
|
3234 |
* @return -1, 0 or 1 as this BigInteger is numerically less than, equal |
|
3235 |
* to, or greater than {@code val}. |
|
3236 |
*/ |
|
3237 |
public int compareTo(BigInteger val) { |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3238 |
if (signum == val.signum) { |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3239 |
switch (signum) { |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3240 |
case 1: |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3241 |
return compareMagnitude(val); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3242 |
case -1: |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3243 |
return val.compareMagnitude(this); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3244 |
default: |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3245 |
return 0; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3246 |
} |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3247 |
} |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3248 |
return signum > val.signum ? 1 : -1; |
2 | 3249 |
} |
3250 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3251 |
/** |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3252 |
* Compares the magnitude array of this BigInteger with the specified |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3253 |
* BigInteger's. This is the version of compareTo ignoring sign. |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3254 |
* |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3255 |
* @param val BigInteger whose magnitude array to be compared. |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3256 |
* @return -1, 0 or 1 as this magnitude array is less than, equal to or |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3257 |
* greater than the magnitude aray for the specified BigInteger's. |
2 | 3258 |
*/ |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3259 |
final int compareMagnitude(BigInteger val) { |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3260 |
int[] m1 = mag; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3261 |
int len1 = m1.length; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3262 |
int[] m2 = val.mag; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3263 |
int len2 = m2.length; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3264 |
if (len1 < len2) |
2 | 3265 |
return -1; |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3266 |
if (len1 > len2) |
2 | 3267 |
return 1; |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3268 |
for (int i = 0; i < len1; i++) { |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3269 |
int a = m1[i]; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3270 |
int b = m2[i]; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3271 |
if (a != b) |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3272 |
return ((a & LONG_MASK) < (b & LONG_MASK)) ? -1 : 1; |
2 | 3273 |
} |
3274 |
return 0; |
|
3275 |
} |
|
3276 |
||
3277 |
/** |
|
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3278 |
* Version of compareMagnitude that compares magnitude with long value. |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3279 |
* val can't be Long.MIN_VALUE. |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3280 |
*/ |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3281 |
final int compareMagnitude(long val) { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3282 |
assert val != Long.MIN_VALUE; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3283 |
int[] m1 = mag; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3284 |
int len = m1.length; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3285 |
if (len > 2) { |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3286 |
return 1; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3287 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3288 |
if (val < 0) { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3289 |
val = -val; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3290 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3291 |
int highWord = (int)(val >>> 32); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3292 |
if (highWord == 0) { |
10423
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3293 |
if (len < 1) |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3294 |
return -1; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3295 |
if (len > 1) |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3296 |
return 1; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3297 |
int a = m1[0]; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3298 |
int b = (int)val; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3299 |
if (a != b) { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3300 |
return ((a & LONG_MASK) < (b & LONG_MASK))? -1 : 1; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3301 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3302 |
return 0; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3303 |
} else { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3304 |
if (len < 2) |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3305 |
return -1; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3306 |
int a = m1[0]; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3307 |
int b = highWord; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3308 |
if (a != b) { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3309 |
return ((a & LONG_MASK) < (b & LONG_MASK))? -1 : 1; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3310 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3311 |
a = m1[1]; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3312 |
b = (int)val; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3313 |
if (a != b) { |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3314 |
return ((a & LONG_MASK) < (b & LONG_MASK))? -1 : 1; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3315 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3316 |
return 0; |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3317 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3318 |
} |
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3319 |
|
2c852092a4e5
7082971: More performance tuning of BigDecimal and other java.math classes
darcy
parents:
9266
diff
changeset
|
3320 |
/** |
2 | 3321 |
* Compares this BigInteger with the specified Object for equality. |
3322 |
* |
|
3323 |
* @param x Object to which this BigInteger is to be compared. |
|
3324 |
* @return {@code true} if and only if the specified Object is a |
|
3325 |
* BigInteger whose value is numerically equal to this BigInteger. |
|
3326 |
*/ |
|
3327 |
public boolean equals(Object x) { |
|
3328 |
// This test is just an optimization, which may or may not help |
|
3329 |
if (x == this) |
|
3330 |
return true; |
|
3331 |
||
3332 |
if (!(x instanceof BigInteger)) |
|
3333 |
return false; |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3334 |
|
2 | 3335 |
BigInteger xInt = (BigInteger) x; |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3336 |
if (xInt.signum != signum) |
2 | 3337 |
return false; |
3338 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3339 |
int[] m = mag; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3340 |
int len = m.length; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3341 |
int[] xm = xInt.mag; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3342 |
if (len != xm.length) |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3343 |
return false; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3344 |
|
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3345 |
for (int i = 0; i < len; i++) |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3346 |
if (xm[i] != m[i]) |
2 | 3347 |
return false; |
3348 |
||
3349 |
return true; |
|
3350 |
} |
|
3351 |
||
3352 |
/** |
|
3353 |
* Returns the minimum of this BigInteger and {@code val}. |
|
3354 |
* |
|
3355 |
* @param val value with which the minimum is to be computed. |
|
3356 |
* @return the BigInteger whose value is the lesser of this BigInteger and |
|
3357 |
* {@code val}. If they are equal, either may be returned. |
|
3358 |
*/ |
|
3359 |
public BigInteger min(BigInteger val) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3360 |
return (compareTo(val) < 0 ? this : val); |
2 | 3361 |
} |
3362 |
||
3363 |
/** |
|
3364 |
* Returns the maximum of this BigInteger and {@code val}. |
|
3365 |
* |
|
3366 |
* @param val value with which the maximum is to be computed. |
|
3367 |
* @return the BigInteger whose value is the greater of this and |
|
3368 |
* {@code val}. If they are equal, either may be returned. |
|
3369 |
*/ |
|
3370 |
public BigInteger max(BigInteger val) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3371 |
return (compareTo(val) > 0 ? this : val); |
2 | 3372 |
} |
3373 |
||
3374 |
||
3375 |
// Hash Function |
|
3376 |
||
3377 |
/** |
|
3378 |
* Returns the hash code for this BigInteger. |
|
3379 |
* |
|
3380 |
* @return hash code for this BigInteger. |
|
3381 |
*/ |
|
3382 |
public int hashCode() { |
|
3383 |
int hashCode = 0; |
|
3384 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3385 |
for (int i=0; i < mag.length; i++) |
2 | 3386 |
hashCode = (int)(31*hashCode + (mag[i] & LONG_MASK)); |
3387 |
||
3388 |
return hashCode * signum; |
|
3389 |
} |
|
3390 |
||
3391 |
/** |
|
3392 |
* Returns the String representation of this BigInteger in the |
|
3393 |
* given radix. If the radix is outside the range from {@link |
|
3394 |
* Character#MIN_RADIX} to {@link Character#MAX_RADIX} inclusive, |
|
3395 |
* it will default to 10 (as is the case for |
|
3396 |
* {@code Integer.toString}). The digit-to-character mapping |
|
3397 |
* provided by {@code Character.forDigit} is used, and a minus |
|
3398 |
* sign is prepended if appropriate. (This representation is |
|
3399 |
* compatible with the {@link #BigInteger(String, int) (String, |
|
3400 |
* int)} constructor.) |
|
3401 |
* |
|
3402 |
* @param radix radix of the String representation. |
|
3403 |
* @return String representation of this BigInteger in the given radix. |
|
3404 |
* @see Integer#toString |
|
3405 |
* @see Character#forDigit |
|
3406 |
* @see #BigInteger(java.lang.String, int) |
|
3407 |
*/ |
|
3408 |
public String toString(int radix) { |
|
3409 |
if (signum == 0) |
|
3410 |
return "0"; |
|
3411 |
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) |
|
3412 |
radix = 10; |
|
3413 |
||
18548 | 3414 |
// If it's small enough, use smallToString. |
3415 |
if (mag.length <= SCHOENHAGE_BASE_CONVERSION_THRESHOLD) |
|
3416 |
return smallToString(radix); |
|
3417 |
||
3418 |
// Otherwise use recursive toString, which requires positive arguments. |
|
3419 |
// The results will be concatenated into this StringBuilder |
|
3420 |
StringBuilder sb = new StringBuilder(); |
|
3421 |
if (signum < 0) { |
|
3422 |
toString(this.negate(), sb, radix, 0); |
|
3423 |
sb.insert(0, '-'); |
|
3424 |
} |
|
3425 |
else |
|
3426 |
toString(this, sb, radix, 0); |
|
3427 |
||
3428 |
return sb.toString(); |
|
3429 |
} |
|
3430 |
||
3431 |
/** This method is used to perform toString when arguments are small. */ |
|
3432 |
private String smallToString(int radix) { |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3433 |
if (signum == 0) { |
18548 | 3434 |
return "0"; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3435 |
} |
18548 | 3436 |
|
2 | 3437 |
// Compute upper bound on number of digit groups and allocate space |
3438 |
int maxNumDigitGroups = (4*mag.length + 6)/7; |
|
3439 |
String digitGroup[] = new String[maxNumDigitGroups]; |
|
3440 |
||
3441 |
// Translate number to string, a digit group at a time |
|
3442 |
BigInteger tmp = this.abs(); |
|
3443 |
int numGroups = 0; |
|
3444 |
while (tmp.signum != 0) { |
|
3445 |
BigInteger d = longRadix[radix]; |
|
3446 |
||
3447 |
MutableBigInteger q = new MutableBigInteger(), |
|
3448 |
a = new MutableBigInteger(tmp.mag), |
|
3449 |
b = new MutableBigInteger(d.mag); |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3450 |
MutableBigInteger r = a.divide(b, q); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3451 |
BigInteger q2 = q.toBigInteger(tmp.signum * d.signum); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3452 |
BigInteger r2 = r.toBigInteger(tmp.signum * d.signum); |
2 | 3453 |
|
3454 |
digitGroup[numGroups++] = Long.toString(r2.longValue(), radix); |
|
3455 |
tmp = q2; |
|
3456 |
} |
|
3457 |
||
3458 |
// Put sign (if any) and first digit group into result buffer |
|
3459 |
StringBuilder buf = new StringBuilder(numGroups*digitsPerLong[radix]+1); |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3460 |
if (signum < 0) { |
2 | 3461 |
buf.append('-'); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3462 |
} |
2 | 3463 |
buf.append(digitGroup[numGroups-1]); |
3464 |
||
3465 |
// Append remaining digit groups padded with leading zeros |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3466 |
for (int i=numGroups-2; i >= 0; i--) { |
2 | 3467 |
// Prepend (any) leading zeros for this digit group |
3468 |
int numLeadingZeros = digitsPerLong[radix]-digitGroup[i].length(); |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3469 |
if (numLeadingZeros != 0) { |
2 | 3470 |
buf.append(zeros[numLeadingZeros]); |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3471 |
} |
2 | 3472 |
buf.append(digitGroup[i]); |
3473 |
} |
|
3474 |
return buf.toString(); |
|
3475 |
} |
|
3476 |
||
18548 | 3477 |
/** |
3478 |
* Converts the specified BigInteger to a string and appends to |
|
19060 | 3479 |
* {@code sb}. This implements the recursive Schoenhage algorithm |
18548 | 3480 |
* for base conversions. |
3481 |
* <p/> |
|
3482 |
* See Knuth, Donald, _The Art of Computer Programming_, Vol. 2, |
|
3483 |
* Answers to Exercises (4.4) Question 14. |
|
3484 |
* |
|
3485 |
* @param u The number to convert to a string. |
|
3486 |
* @param sb The StringBuilder that will be appended to in place. |
|
3487 |
* @param radix The base to convert to. |
|
3488 |
* @param digits The minimum number of digits to pad to. |
|
3489 |
*/ |
|
3490 |
private static void toString(BigInteger u, StringBuilder sb, int radix, |
|
3491 |
int digits) { |
|
3492 |
/* If we're smaller than a certain threshold, use the smallToString |
|
3493 |
method, padding with leading zeroes when necessary. */ |
|
3494 |
if (u.mag.length <= SCHOENHAGE_BASE_CONVERSION_THRESHOLD) { |
|
3495 |
String s = u.smallToString(radix); |
|
3496 |
||
3497 |
// Pad with internal zeros if necessary. |
|
3498 |
// Don't pad if we're at the beginning of the string. |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3499 |
if ((s.length() < digits) && (sb.length() > 0)) { |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3500 |
for (int i=s.length(); i < digits; i++) { // May be a faster way to |
18548 | 3501 |
sb.append('0'); // do this? |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3502 |
} |
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3503 |
} |
18548 | 3504 |
|
3505 |
sb.append(s); |
|
3506 |
return; |
|
3507 |
} |
|
3508 |
||
3509 |
int b, n; |
|
3510 |
b = u.bitLength(); |
|
3511 |
||
3512 |
// Calculate a value for n in the equation radix^(2^n) = u |
|
3513 |
// and subtract 1 from that value. This is used to find the |
|
3514 |
// cache index that contains the best value to divide u. |
|
3515 |
n = (int) Math.round(Math.log(b * LOG_TWO / logCache[radix]) / LOG_TWO - 1.0); |
|
3516 |
BigInteger v = getRadixConversionCache(radix, n); |
|
3517 |
BigInteger[] results; |
|
3518 |
results = u.divideAndRemainder(v); |
|
3519 |
||
3520 |
int expectedDigits = 1 << n; |
|
3521 |
||
3522 |
// Now recursively build the two halves of each number. |
|
3523 |
toString(results[0], sb, radix, digits-expectedDigits); |
|
3524 |
toString(results[1], sb, radix, expectedDigits); |
|
3525 |
} |
|
3526 |
||
3527 |
/** |
|
3528 |
* Returns the value radix^(2^exponent) from the cache. |
|
3529 |
* If this value doesn't already exist in the cache, it is added. |
|
3530 |
* <p/> |
|
3531 |
* This could be changed to a more complicated caching method using |
|
19060 | 3532 |
* {@code Future}. |
18548 | 3533 |
*/ |
18588
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3534 |
private static BigInteger getRadixConversionCache(int radix, int exponent) { |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3535 |
BigInteger[] cacheLine = powerCache[radix]; // volatile read |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3536 |
if (exponent < cacheLine.length) { |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3537 |
return cacheLine[exponent]; |
18548 | 3538 |
} |
18588
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3539 |
|
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3540 |
int oldLength = cacheLine.length; |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3541 |
cacheLine = Arrays.copyOf(cacheLine, exponent + 1); |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3542 |
for (int i = oldLength; i <= exponent; i++) { |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3543 |
cacheLine[i] = cacheLine[i - 1].pow(2); |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3544 |
} |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3545 |
|
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3546 |
BigInteger[][] pc = powerCache; // volatile read again |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3547 |
if (exponent >= pc[radix].length) { |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3548 |
pc = pc.clone(); |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3549 |
pc[radix] = cacheLine; |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3550 |
powerCache = pc; // volatile write, publish |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3551 |
} |
8aa1d5a9d447
8017540: Improve multi-threaded contention behavior of radix conversion cache
bpb
parents:
18548
diff
changeset
|
3552 |
return cacheLine[exponent]; |
18548 | 3553 |
} |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
3554 |
|
2 | 3555 |
/* zero[i] is a string of i consecutive zeros. */ |
3556 |
private static String zeros[] = new String[64]; |
|
3557 |
static { |
|
3558 |
zeros[63] = |
|
3559 |
"000000000000000000000000000000000000000000000000000000000000000"; |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3560 |
for (int i=0; i < 63; i++) |
2 | 3561 |
zeros[i] = zeros[63].substring(0, i); |
3562 |
} |
|
3563 |
||
3564 |
/** |
|
3565 |
* Returns the decimal String representation of this BigInteger. |
|
3566 |
* The digit-to-character mapping provided by |
|
3567 |
* {@code Character.forDigit} is used, and a minus sign is |
|
3568 |
* prepended if appropriate. (This representation is compatible |
|
3569 |
* with the {@link #BigInteger(String) (String)} constructor, and |
|
3570 |
* allows for String concatenation with Java's + operator.) |
|
3571 |
* |
|
3572 |
* @return decimal String representation of this BigInteger. |
|
3573 |
* @see Character#forDigit |
|
3574 |
* @see #BigInteger(java.lang.String) |
|
3575 |
*/ |
|
3576 |
public String toString() { |
|
3577 |
return toString(10); |
|
3578 |
} |
|
3579 |
||
3580 |
/** |
|
3581 |
* Returns a byte array containing the two's-complement |
|
3582 |
* representation of this BigInteger. The byte array will be in |
|
3583 |
* <i>big-endian</i> byte-order: the most significant byte is in |
|
3584 |
* the zeroth element. The array will contain the minimum number |
|
3585 |
* of bytes required to represent this BigInteger, including at |
|
3586 |
* least one sign bit, which is {@code (ceil((this.bitLength() + |
|
3587 |
* 1)/8))}. (This representation is compatible with the |
|
3588 |
* {@link #BigInteger(byte[]) (byte[])} constructor.) |
|
3589 |
* |
|
3590 |
* @return a byte array containing the two's-complement representation of |
|
3591 |
* this BigInteger. |
|
3592 |
* @see #BigInteger(byte[]) |
|
3593 |
*/ |
|
3594 |
public byte[] toByteArray() { |
|
3595 |
int byteLen = bitLength()/8 + 1; |
|
3596 |
byte[] byteArray = new byte[byteLen]; |
|
3597 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3598 |
for (int i=byteLen-1, bytesCopied=4, nextInt=0, intIndex=0; i >= 0; i--) { |
2 | 3599 |
if (bytesCopied == 4) { |
3600 |
nextInt = getInt(intIndex++); |
|
3601 |
bytesCopied = 1; |
|
3602 |
} else { |
|
3603 |
nextInt >>>= 8; |
|
3604 |
bytesCopied++; |
|
3605 |
} |
|
3606 |
byteArray[i] = (byte)nextInt; |
|
3607 |
} |
|
3608 |
return byteArray; |
|
3609 |
} |
|
3610 |
||
3611 |
/** |
|
3612 |
* Converts this BigInteger to an {@code int}. This |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3613 |
* conversion is analogous to a |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3614 |
* <i>narrowing primitive conversion</i> from {@code long} to |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3615 |
* {@code int} as defined in section 5.1.3 of |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3616 |
* <cite>The Java™ Language Specification</cite>: |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3617 |
* if this BigInteger is too big to fit in an |
2 | 3618 |
* {@code int}, only the low-order 32 bits are returned. |
3619 |
* Note that this conversion can lose information about the |
|
3620 |
* overall magnitude of the BigInteger value as well as return a |
|
3621 |
* result with the opposite sign. |
|
3622 |
* |
|
3623 |
* @return this BigInteger converted to an {@code int}. |
|
10588
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
3624 |
* @see #intValueExact() |
2 | 3625 |
*/ |
3626 |
public int intValue() { |
|
3627 |
int result = 0; |
|
3628 |
result = getInt(0); |
|
3629 |
return result; |
|
3630 |
} |
|
3631 |
||
3632 |
/** |
|
3633 |
* Converts this BigInteger to a {@code long}. This |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3634 |
* conversion is analogous to a |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3635 |
* <i>narrowing primitive conversion</i> from {@code long} to |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3636 |
* {@code int} as defined in section 5.1.3 of |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3637 |
* <cite>The Java™ Language Specification</cite>: |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3638 |
* if this BigInteger is too big to fit in a |
2 | 3639 |
* {@code long}, only the low-order 64 bits are returned. |
3640 |
* Note that this conversion can lose information about the |
|
3641 |
* overall magnitude of the BigInteger value as well as return a |
|
3642 |
* result with the opposite sign. |
|
3643 |
* |
|
3644 |
* @return this BigInteger converted to a {@code long}. |
|
10588
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
3645 |
* @see #longValueExact() |
2 | 3646 |
*/ |
3647 |
public long longValue() { |
|
3648 |
long result = 0; |
|
3649 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3650 |
for (int i=1; i >= 0; i--) |
2 | 3651 |
result = (result << 32) + (getInt(i) & LONG_MASK); |
3652 |
return result; |
|
3653 |
} |
|
3654 |
||
3655 |
/** |
|
3656 |
* Converts this BigInteger to a {@code float}. This |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3657 |
* conversion is similar to the |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3658 |
* <i>narrowing primitive conversion</i> from {@code double} to |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3659 |
* {@code float} as defined in section 5.1.3 of |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3660 |
* <cite>The Java™ Language Specification</cite>: |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3661 |
* if this BigInteger has too great a magnitude |
2 | 3662 |
* to represent as a {@code float}, it will be converted to |
3663 |
* {@link Float#NEGATIVE_INFINITY} or {@link |
|
3664 |
* Float#POSITIVE_INFINITY} as appropriate. Note that even when |
|
3665 |
* the return value is finite, this conversion can lose |
|
3666 |
* information about the precision of the BigInteger value. |
|
3667 |
* |
|
3668 |
* @return this BigInteger converted to a {@code float}. |
|
3669 |
*/ |
|
3670 |
public float floatValue() { |
|
18538
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3671 |
if (signum == 0) { |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3672 |
return 0.0f; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3673 |
} |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3674 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3675 |
int exponent = ((mag.length - 1) << 5) + bitLengthForInt(mag[0]) - 1; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3676 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3677 |
// exponent == floor(log2(abs(this))) |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3678 |
if (exponent < Long.SIZE - 1) { |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3679 |
return longValue(); |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3680 |
} else if (exponent > Float.MAX_EXPONENT) { |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3681 |
return signum > 0 ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3682 |
} |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3683 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3684 |
/* |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3685 |
* We need the top SIGNIFICAND_WIDTH bits, including the "implicit" |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3686 |
* one bit. To make rounding easier, we pick out the top |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3687 |
* SIGNIFICAND_WIDTH + 1 bits, so we have one to help us round up or |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3688 |
* down. twiceSignifFloor will contain the top SIGNIFICAND_WIDTH + 1 |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3689 |
* bits, and signifFloor the top SIGNIFICAND_WIDTH. |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3690 |
* |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3691 |
* It helps to consider the real number signif = abs(this) * |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3692 |
* 2^(SIGNIFICAND_WIDTH - 1 - exponent). |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3693 |
*/ |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3694 |
int shift = exponent - FloatConsts.SIGNIFICAND_WIDTH; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3695 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3696 |
int twiceSignifFloor; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3697 |
// twiceSignifFloor will be == abs().shiftRight(shift).intValue() |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3698 |
// We do the shift into an int directly to improve performance. |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3699 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3700 |
int nBits = shift & 0x1f; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3701 |
int nBits2 = 32 - nBits; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3702 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3703 |
if (nBits == 0) { |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3704 |
twiceSignifFloor = mag[0]; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3705 |
} else { |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3706 |
twiceSignifFloor = mag[0] >>> nBits; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3707 |
if (twiceSignifFloor == 0) { |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3708 |
twiceSignifFloor = (mag[0] << nBits2) | (mag[1] >>> nBits); |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3709 |
} |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3710 |
} |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3711 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3712 |
int signifFloor = twiceSignifFloor >> 1; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3713 |
signifFloor &= FloatConsts.SIGNIF_BIT_MASK; // remove the implied bit |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3714 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3715 |
/* |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3716 |
* We round up if either the fractional part of signif is strictly |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3717 |
* greater than 0.5 (which is true if the 0.5 bit is set and any lower |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3718 |
* bit is set), or if the fractional part of signif is >= 0.5 and |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3719 |
* signifFloor is odd (which is true if both the 0.5 bit and the 1 bit |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3720 |
* are set). This is equivalent to the desired HALF_EVEN rounding. |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3721 |
*/ |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3722 |
boolean increment = (twiceSignifFloor & 1) != 0 |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3723 |
&& ((signifFloor & 1) != 0 || abs().getLowestSetBit() < shift); |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3724 |
int signifRounded = increment ? signifFloor + 1 : signifFloor; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3725 |
int bits = ((exponent + FloatConsts.EXP_BIAS)) |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3726 |
<< (FloatConsts.SIGNIFICAND_WIDTH - 1); |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3727 |
bits += signifRounded; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3728 |
/* |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3729 |
* If signifRounded == 2^24, we'd need to set all of the significand |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3730 |
* bits to zero and add 1 to the exponent. This is exactly the behavior |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3731 |
* we get from just adding signifRounded to bits directly. If the |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3732 |
* exponent is Float.MAX_EXPONENT, we round up (correctly) to |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3733 |
* Float.POSITIVE_INFINITY. |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3734 |
*/ |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3735 |
bits |= signum & FloatConsts.SIGN_BIT_MASK; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3736 |
return Float.intBitsToFloat(bits); |
2 | 3737 |
} |
3738 |
||
3739 |
/** |
|
3740 |
* Converts this BigInteger to a {@code double}. This |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3741 |
* conversion is similar to the |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3742 |
* <i>narrowing primitive conversion</i> from {@code double} to |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3743 |
* {@code float} as defined in section 5.1.3 of |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3744 |
* <cite>The Java™ Language Specification</cite>: |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
3745 |
* if this BigInteger has too great a magnitude |
2 | 3746 |
* to represent as a {@code double}, it will be converted to |
3747 |
* {@link Double#NEGATIVE_INFINITY} or {@link |
|
3748 |
* Double#POSITIVE_INFINITY} as appropriate. Note that even when |
|
3749 |
* the return value is finite, this conversion can lose |
|
3750 |
* information about the precision of the BigInteger value. |
|
3751 |
* |
|
3752 |
* @return this BigInteger converted to a {@code double}. |
|
3753 |
*/ |
|
3754 |
public double doubleValue() { |
|
18538
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3755 |
if (signum == 0) { |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3756 |
return 0.0; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3757 |
} |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3758 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3759 |
int exponent = ((mag.length - 1) << 5) + bitLengthForInt(mag[0]) - 1; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3760 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3761 |
// exponent == floor(log2(abs(this))Double) |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3762 |
if (exponent < Long.SIZE - 1) { |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3763 |
return longValue(); |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3764 |
} else if (exponent > Double.MAX_EXPONENT) { |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3765 |
return signum > 0 ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3766 |
} |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3767 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3768 |
/* |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3769 |
* We need the top SIGNIFICAND_WIDTH bits, including the "implicit" |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3770 |
* one bit. To make rounding easier, we pick out the top |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3771 |
* SIGNIFICAND_WIDTH + 1 bits, so we have one to help us round up or |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3772 |
* down. twiceSignifFloor will contain the top SIGNIFICAND_WIDTH + 1 |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3773 |
* bits, and signifFloor the top SIGNIFICAND_WIDTH. |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3774 |
* |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3775 |
* It helps to consider the real number signif = abs(this) * |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3776 |
* 2^(SIGNIFICAND_WIDTH - 1 - exponent). |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3777 |
*/ |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3778 |
int shift = exponent - DoubleConsts.SIGNIFICAND_WIDTH; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3779 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3780 |
long twiceSignifFloor; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3781 |
// twiceSignifFloor will be == abs().shiftRight(shift).longValue() |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3782 |
// We do the shift into a long directly to improve performance. |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3783 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3784 |
int nBits = shift & 0x1f; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3785 |
int nBits2 = 32 - nBits; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3786 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3787 |
int highBits; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3788 |
int lowBits; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3789 |
if (nBits == 0) { |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3790 |
highBits = mag[0]; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3791 |
lowBits = mag[1]; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3792 |
} else { |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3793 |
highBits = mag[0] >>> nBits; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3794 |
lowBits = (mag[0] << nBits2) | (mag[1] >>> nBits); |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3795 |
if (highBits == 0) { |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3796 |
highBits = lowBits; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3797 |
lowBits = (mag[1] << nBits2) | (mag[2] >>> nBits); |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3798 |
} |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3799 |
} |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3800 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3801 |
twiceSignifFloor = ((highBits & LONG_MASK) << 32) |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3802 |
| (lowBits & LONG_MASK); |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3803 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3804 |
long signifFloor = twiceSignifFloor >> 1; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3805 |
signifFloor &= DoubleConsts.SIGNIF_BIT_MASK; // remove the implied bit |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3806 |
|
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3807 |
/* |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3808 |
* We round up if either the fractional part of signif is strictly |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3809 |
* greater than 0.5 (which is true if the 0.5 bit is set and any lower |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3810 |
* bit is set), or if the fractional part of signif is >= 0.5 and |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3811 |
* signifFloor is odd (which is true if both the 0.5 bit and the 1 bit |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3812 |
* are set). This is equivalent to the desired HALF_EVEN rounding. |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3813 |
*/ |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3814 |
boolean increment = (twiceSignifFloor & 1) != 0 |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3815 |
&& ((signifFloor & 1) != 0 || abs().getLowestSetBit() < shift); |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3816 |
long signifRounded = increment ? signifFloor + 1 : signifFloor; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3817 |
long bits = (long) ((exponent + DoubleConsts.EXP_BIAS)) |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3818 |
<< (DoubleConsts.SIGNIFICAND_WIDTH - 1); |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3819 |
bits += signifRounded; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3820 |
/* |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3821 |
* If signifRounded == 2^53, we'd need to set all of the significand |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3822 |
* bits to zero and add 1 to the exponent. This is exactly the behavior |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3823 |
* we get from just adding signifRounded to bits directly. If the |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3824 |
* exponent is Double.MAX_EXPONENT, we round up (correctly) to |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3825 |
* Double.POSITIVE_INFINITY. |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3826 |
*/ |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3827 |
bits |= signum & DoubleConsts.SIGN_BIT_MASK; |
642cc17315fd
7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
18286
diff
changeset
|
3828 |
return Double.longBitsToDouble(bits); |
2 | 3829 |
} |
3830 |
||
3831 |
/** |
|
3832 |
* Returns a copy of the input array stripped of any leading zero bytes. |
|
3833 |
*/ |
|
3834 |
private static int[] stripLeadingZeroInts(int val[]) { |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3835 |
int vlen = val.length; |
2 | 3836 |
int keep; |
3837 |
||
3838 |
// Find first nonzero byte |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3839 |
for (keep = 0; keep < vlen && val[keep] == 0; keep++) |
2 | 3840 |
; |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3841 |
return java.util.Arrays.copyOfRange(val, keep, vlen); |
2 | 3842 |
} |
3843 |
||
3844 |
/** |
|
3845 |
* Returns the input array stripped of any leading zero bytes. |
|
3846 |
* Since the source is trusted the copying may be skipped. |
|
3847 |
*/ |
|
3848 |
private static int[] trustedStripLeadingZeroInts(int val[]) { |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3849 |
int vlen = val.length; |
2 | 3850 |
int keep; |
3851 |
||
3852 |
// Find first nonzero byte |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3853 |
for (keep = 0; keep < vlen && val[keep] == 0; keep++) |
2 | 3854 |
; |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3855 |
return keep == 0 ? val : java.util.Arrays.copyOfRange(val, keep, vlen); |
2 | 3856 |
} |
3857 |
||
3858 |
/** |
|
3859 |
* Returns a copy of the input array stripped of any leading zero bytes. |
|
3860 |
*/ |
|
3861 |
private static int[] stripLeadingZeroBytes(byte a[]) { |
|
3862 |
int byteLength = a.length; |
|
3863 |
int keep; |
|
3864 |
||
3865 |
// Find first nonzero byte |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3866 |
for (keep = 0; keep < byteLength && a[keep] == 0; keep++) |
2 | 3867 |
; |
3868 |
||
3869 |
// Allocate new array and copy relevant part of input array |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3870 |
int intLength = ((byteLength - keep) + 3) >>> 2; |
2 | 3871 |
int[] result = new int[intLength]; |
3872 |
int b = byteLength - 1; |
|
3873 |
for (int i = intLength-1; i >= 0; i--) { |
|
3874 |
result[i] = a[b--] & 0xff; |
|
3875 |
int bytesRemaining = b - keep + 1; |
|
3876 |
int bytesToTransfer = Math.min(3, bytesRemaining); |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
3877 |
for (int j=8; j <= (bytesToTransfer << 3); j += 8) |
2 | 3878 |
result[i] |= ((a[b--] & 0xff) << j); |
3879 |
} |
|
3880 |
return result; |
|
3881 |
} |
|
3882 |
||
3883 |
/** |
|
3884 |
* Takes an array a representing a negative 2's-complement number and |
|
3885 |
* returns the minimal (no leading zero bytes) unsigned whose value is -a. |
|
3886 |
*/ |
|
3887 |
private static int[] makePositive(byte a[]) { |
|
3888 |
int keep, k; |
|
3889 |
int byteLength = a.length; |
|
3890 |
||
3891 |
// Find first non-sign (0xff) byte of input |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3892 |
for (keep=0; keep < byteLength && a[keep] == -1; keep++) |
2 | 3893 |
; |
3894 |
||
3895 |
||
3896 |
/* Allocate output array. If all non-sign bytes are 0x00, we must |
|
3897 |
* allocate space for one extra output byte. */ |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3898 |
for (k=keep; k < byteLength && a[k] == 0; k++) |
2 | 3899 |
; |
3900 |
||
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3901 |
int extraByte = (k == byteLength) ? 1 : 0; |
2 | 3902 |
int intLength = ((byteLength - keep + extraByte) + 3)/4; |
3903 |
int result[] = new int[intLength]; |
|
3904 |
||
3905 |
/* Copy one's complement of input into output, leaving extra |
|
3906 |
* byte (if it exists) == 0x00 */ |
|
3907 |
int b = byteLength - 1; |
|
3908 |
for (int i = intLength-1; i >= 0; i--) { |
|
3909 |
result[i] = a[b--] & 0xff; |
|
3910 |
int numBytesToTransfer = Math.min(3, b-keep+1); |
|
3911 |
if (numBytesToTransfer < 0) |
|
3912 |
numBytesToTransfer = 0; |
|
3913 |
for (int j=8; j <= 8*numBytesToTransfer; j += 8) |
|
3914 |
result[i] |= ((a[b--] & 0xff) << j); |
|
3915 |
||
3916 |
// Mask indicates which bits must be complemented |
|
3917 |
int mask = -1 >>> (8*(3-numBytesToTransfer)); |
|
3918 |
result[i] = ~result[i] & mask; |
|
3919 |
} |
|
3920 |
||
3921 |
// Add one to one's complement to generate two's complement |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3922 |
for (int i=result.length-1; i >= 0; i--) { |
2 | 3923 |
result[i] = (int)((result[i] & LONG_MASK) + 1); |
3924 |
if (result[i] != 0) |
|
3925 |
break; |
|
3926 |
} |
|
3927 |
||
3928 |
return result; |
|
3929 |
} |
|
3930 |
||
3931 |
/** |
|
3932 |
* Takes an array a representing a negative 2's-complement number and |
|
3933 |
* returns the minimal (no leading zero ints) unsigned whose value is -a. |
|
3934 |
*/ |
|
3935 |
private static int[] makePositive(int a[]) { |
|
3936 |
int keep, j; |
|
3937 |
||
3938 |
// Find first non-sign (0xffffffff) int of input |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3939 |
for (keep=0; keep < a.length && a[keep] == -1; keep++) |
2 | 3940 |
; |
3941 |
||
3942 |
/* Allocate output array. If all non-sign ints are 0x00, we must |
|
3943 |
* allocate space for one extra output int. */ |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3944 |
for (j=keep; j < a.length && a[j] == 0; j++) |
2 | 3945 |
; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3946 |
int extraInt = (j == a.length ? 1 : 0); |
2 | 3947 |
int result[] = new int[a.length - keep + extraInt]; |
3948 |
||
3949 |
/* Copy one's complement of input into output, leaving extra |
|
3950 |
* int (if it exists) == 0x00 */ |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3951 |
for (int i = keep; i < a.length; i++) |
2 | 3952 |
result[i - keep + extraInt] = ~a[i]; |
3953 |
||
3954 |
// Add one to one's complement to generate two's complement |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
3955 |
for (int i=result.length-1; ++result[i] == 0; i--) |
2 | 3956 |
; |
3957 |
||
3958 |
return result; |
|
3959 |
} |
|
3960 |
||
3961 |
/* |
|
3962 |
* The following two arrays are used for fast String conversions. Both |
|
3963 |
* are indexed by radix. The first is the number of digits of the given |
|
3964 |
* radix that can fit in a Java long without "going negative", i.e., the |
|
3965 |
* highest integer n such that radix**n < 2**63. The second is the |
|
3966 |
* "long radix" that tears each number into "long digits", each of which |
|
3967 |
* consists of the number of digits in the corresponding element in |
|
3968 |
* digitsPerLong (longRadix[i] = i**digitPerLong[i]). Both arrays have |
|
3969 |
* nonsense values in their 0 and 1 elements, as radixes 0 and 1 are not |
|
3970 |
* used. |
|
3971 |
*/ |
|
3972 |
private static int digitsPerLong[] = {0, 0, |
|
3973 |
62, 39, 31, 27, 24, 22, 20, 19, 18, 18, 17, 17, 16, 16, 15, 15, 15, 14, |
|
3974 |
14, 14, 14, 13, 13, 13, 13, 13, 13, 12, 12, 12, 12, 12, 12, 12, 12}; |
|
3975 |
||
3976 |
private static BigInteger longRadix[] = {null, null, |
|
3977 |
valueOf(0x4000000000000000L), valueOf(0x383d9170b85ff80bL), |
|
3978 |
valueOf(0x4000000000000000L), valueOf(0x6765c793fa10079dL), |
|
3979 |
valueOf(0x41c21cb8e1000000L), valueOf(0x3642798750226111L), |
|
3980 |
valueOf(0x1000000000000000L), valueOf(0x12bf307ae81ffd59L), |
|
3981 |
valueOf( 0xde0b6b3a7640000L), valueOf(0x4d28cb56c33fa539L), |
|
3982 |
valueOf(0x1eca170c00000000L), valueOf(0x780c7372621bd74dL), |
|
3983 |
valueOf(0x1e39a5057d810000L), valueOf(0x5b27ac993df97701L), |
|
3984 |
valueOf(0x1000000000000000L), valueOf(0x27b95e997e21d9f1L), |
|
3985 |
valueOf(0x5da0e1e53c5c8000L), valueOf( 0xb16a458ef403f19L), |
|
3986 |
valueOf(0x16bcc41e90000000L), valueOf(0x2d04b7fdd9c0ef49L), |
|
3987 |
valueOf(0x5658597bcaa24000L), valueOf( 0x6feb266931a75b7L), |
|
3988 |
valueOf( 0xc29e98000000000L), valueOf(0x14adf4b7320334b9L), |
|
3989 |
valueOf(0x226ed36478bfa000L), valueOf(0x383d9170b85ff80bL), |
|
3990 |
valueOf(0x5a3c23e39c000000L), valueOf( 0x4e900abb53e6b71L), |
|
3991 |
valueOf( 0x7600ec618141000L), valueOf( 0xaee5720ee830681L), |
|
3992 |
valueOf(0x1000000000000000L), valueOf(0x172588ad4f5f0981L), |
|
3993 |
valueOf(0x211e44f7d02c1000L), valueOf(0x2ee56725f06e5c71L), |
|
3994 |
valueOf(0x41c21cb8e1000000L)}; |
|
3995 |
||
3996 |
/* |
|
3997 |
* These two arrays are the integer analogue of above. |
|
3998 |
*/ |
|
3999 |
private static int digitsPerInt[] = {0, 0, 30, 19, 15, 13, 11, |
|
4000 |
11, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, |
|
4001 |
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5}; |
|
4002 |
||
4003 |
private static int intRadix[] = {0, 0, |
|
4004 |
0x40000000, 0x4546b3db, 0x40000000, 0x48c27395, 0x159fd800, |
|
4005 |
0x75db9c97, 0x40000000, 0x17179149, 0x3b9aca00, 0xcc6db61, |
|
4006 |
0x19a10000, 0x309f1021, 0x57f6c100, 0xa2f1b6f, 0x10000000, |
|
4007 |
0x18754571, 0x247dbc80, 0x3547667b, 0x4c4b4000, 0x6b5a6e1d, |
|
4008 |
0x6c20a40, 0x8d2d931, 0xb640000, 0xe8d4a51, 0x1269ae40, |
|
4009 |
0x17179149, 0x1cb91000, 0x23744899, 0x2b73a840, 0x34e63b41, |
|
4010 |
0x40000000, 0x4cfa3cc1, 0x5c13d840, 0x6d91b519, 0x39aa400 |
|
4011 |
}; |
|
4012 |
||
4013 |
/** |
|
4014 |
* These routines provide access to the two's complement representation |
|
4015 |
* of BigIntegers. |
|
4016 |
*/ |
|
4017 |
||
4018 |
/** |
|
4019 |
* Returns the length of the two's complement representation in ints, |
|
4020 |
* including space for at least one sign bit. |
|
4021 |
*/ |
|
4022 |
private int intLength() { |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4023 |
return (bitLength() >>> 5) + 1; |
2 | 4024 |
} |
4025 |
||
4026 |
/* Returns sign bit */ |
|
4027 |
private int signBit() { |
|
4028 |
return signum < 0 ? 1 : 0; |
|
4029 |
} |
|
4030 |
||
4031 |
/* Returns an int of sign bits */ |
|
4032 |
private int signInt() { |
|
4033 |
return signum < 0 ? -1 : 0; |
|
4034 |
} |
|
4035 |
||
4036 |
/** |
|
4037 |
* Returns the specified int of the little-endian two's complement |
|
4038 |
* representation (int 0 is the least significant). The int number can |
|
4039 |
* be arbitrarily high (values are logically preceded by infinitely many |
|
4040 |
* sign ints). |
|
4041 |
*/ |
|
4042 |
private int getInt(int n) { |
|
4043 |
if (n < 0) |
|
4044 |
return 0; |
|
4045 |
if (n >= mag.length) |
|
4046 |
return signInt(); |
|
4047 |
||
4048 |
int magInt = mag[mag.length-n-1]; |
|
4049 |
||
4050 |
return (signum >= 0 ? magInt : |
|
4051 |
(n <= firstNonzeroIntNum() ? -magInt : ~magInt)); |
|
4052 |
} |
|
4053 |
||
4054 |
/** |
|
4055 |
* Returns the index of the int that contains the first nonzero int in the |
|
4056 |
* little-endian binary representation of the magnitude (int 0 is the |
|
4057 |
* least significant). If the magnitude is zero, return value is undefined. |
|
4058 |
*/ |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4059 |
private int firstNonzeroIntNum() { |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4060 |
int fn = firstNonzeroIntNum - 2; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4061 |
if (fn == -2) { // firstNonzeroIntNum not initialized yet |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4062 |
fn = 0; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4063 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4064 |
// Search for the first nonzero int |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4065 |
int i; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4066 |
int mlen = mag.length; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4067 |
for (i = mlen - 1; i >= 0 && mag[i] == 0; i--) |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4068 |
; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4069 |
fn = mlen - i - 1; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4070 |
firstNonzeroIntNum = fn + 2; // offset by two to initialize |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4071 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4072 |
return fn; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
10588
diff
changeset
|
4073 |
} |
2 | 4074 |
|
4075 |
/** use serialVersionUID from JDK 1.1. for interoperability */ |
|
4076 |
private static final long serialVersionUID = -8287574255936472291L; |
|
4077 |
||
4078 |
/** |
|
4079 |
* Serializable fields for BigInteger. |
|
4080 |
* |
|
4081 |
* @serialField signum int |
|
4082 |
* signum of this BigInteger. |
|
4083 |
* @serialField magnitude int[] |
|
4084 |
* magnitude array of this BigInteger. |
|
4085 |
* @serialField bitCount int |
|
4086 |
* number of bits in this BigInteger |
|
4087 |
* @serialField bitLength int |
|
4088 |
* the number of bits in the minimal two's-complement |
|
4089 |
* representation of this BigInteger |
|
4090 |
* @serialField lowestSetBit int |
|
4091 |
* lowest set bit in the twos complement representation |
|
4092 |
*/ |
|
4093 |
private static final ObjectStreamField[] serialPersistentFields = { |
|
4094 |
new ObjectStreamField("signum", Integer.TYPE), |
|
4095 |
new ObjectStreamField("magnitude", byte[].class), |
|
4096 |
new ObjectStreamField("bitCount", Integer.TYPE), |
|
4097 |
new ObjectStreamField("bitLength", Integer.TYPE), |
|
4098 |
new ObjectStreamField("firstNonzeroByteNum", Integer.TYPE), |
|
4099 |
new ObjectStreamField("lowestSetBit", Integer.TYPE) |
|
4100 |
}; |
|
4101 |
||
4102 |
/** |
|
4103 |
* Reconstitute the {@code BigInteger} instance from a stream (that is, |
|
4104 |
* deserialize it). The magnitude is read in as an array of bytes |
|
4105 |
* for historical reasons, but it is converted to an array of ints |
|
4106 |
* and the byte array is discarded. |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4107 |
* Note: |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4108 |
* The current convention is to initialize the cache fields, bitCount, |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4109 |
* bitLength and lowestSetBit, to 0 rather than some other marker value. |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4110 |
* Therefore, no explicit action to set these fields needs to be taken in |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4111 |
* readObject because those fields already have a 0 value be default since |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4112 |
* defaultReadObject is not being used. |
2 | 4113 |
*/ |
4114 |
private void readObject(java.io.ObjectInputStream s) |
|
4115 |
throws java.io.IOException, ClassNotFoundException { |
|
4116 |
/* |
|
4117 |
* In order to maintain compatibility with previous serialized forms, |
|
4118 |
* the magnitude of a BigInteger is serialized as an array of bytes. |
|
4119 |
* The magnitude field is used as a temporary store for the byte array |
|
4120 |
* that is deserialized. The cached computation fields should be |
|
4121 |
* transient but are serialized for compatibility reasons. |
|
4122 |
*/ |
|
4123 |
||
4124 |
// prepare to read the alternate persistent fields |
|
4125 |
ObjectInputStream.GetField fields = s.readFields(); |
|
4126 |
||
4127 |
// Read the alternate persistent fields that we care about |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4128 |
int sign = fields.get("signum", -2); |
2 | 4129 |
byte[] magnitude = (byte[])fields.get("magnitude", null); |
4130 |
||
4131 |
// Validate signum |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4132 |
if (sign < -1 || sign > 1) { |
2 | 4133 |
String message = "BigInteger: Invalid signum value"; |
4134 |
if (fields.defaulted("signum")) |
|
4135 |
message = "BigInteger: Signum not present in stream"; |
|
4136 |
throw new java.io.StreamCorruptedException(message); |
|
4137 |
} |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4138 |
if ((magnitude.length == 0) != (sign == 0)) { |
2 | 4139 |
String message = "BigInteger: signum-magnitude mismatch"; |
4140 |
if (fields.defaulted("magnitude")) |
|
4141 |
message = "BigInteger: Magnitude not present in stream"; |
|
4142 |
throw new java.io.StreamCorruptedException(message); |
|
4143 |
} |
|
4144 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4145 |
// Commit final fields via Unsafe |
10431
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4146 |
UnsafeHolder.putSign(this, sign); |
2 | 4147 |
|
4148 |
// Calculate mag field from magnitude and discard magnitude |
|
10431
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4149 |
UnsafeHolder.putMag(this, stripLeadingZeroBytes(magnitude)); |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4150 |
} |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4151 |
|
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4152 |
// Support for resetting final fields while deserializing |
10431
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4153 |
private static class UnsafeHolder { |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4154 |
private static final sun.misc.Unsafe unsafe; |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4155 |
private static final long signumOffset; |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4156 |
private static final long magOffset; |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4157 |
static { |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4158 |
try { |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4159 |
unsafe = sun.misc.Unsafe.getUnsafe(); |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4160 |
signumOffset = unsafe.objectFieldOffset |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4161 |
(BigInteger.class.getDeclaredField("signum")); |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4162 |
magOffset = unsafe.objectFieldOffset |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4163 |
(BigInteger.class.getDeclaredField("mag")); |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4164 |
} catch (Exception ex) { |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4165 |
throw new ExceptionInInitializerError(ex); |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4166 |
} |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4167 |
} |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4168 |
|
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4169 |
static void putSign(BigInteger bi, int sign) { |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4170 |
unsafe.putIntVolatile(bi, signumOffset, sign); |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4171 |
} |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4172 |
|
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4173 |
static void putMag(BigInteger bi, int[] magnitude) { |
448fc54a8e23
6838776: Defer initialization of static fields in java.math.BigInteger
darcy
parents:
10425
diff
changeset
|
4174 |
unsafe.putObjectVolatile(bi, magOffset, magnitude); |
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4175 |
} |
2 | 4176 |
} |
4177 |
||
4178 |
/** |
|
4179 |
* Save the {@code BigInteger} instance to a stream. |
|
4180 |
* The magnitude of a BigInteger is serialized as a byte array for |
|
4181 |
* historical reasons. |
|
4182 |
* |
|
4183 |
* @serialData two necessary fields are written as well as obsolete |
|
4184 |
* fields for compatibility with older versions. |
|
4185 |
*/ |
|
4186 |
private void writeObject(ObjectOutputStream s) throws IOException { |
|
4187 |
// set the values of the Serializable fields |
|
4188 |
ObjectOutputStream.PutField fields = s.putFields(); |
|
4189 |
fields.put("signum", signum); |
|
4190 |
fields.put("magnitude", magSerializedForm()); |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4191 |
// The values written for cached fields are compatible with older |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4192 |
// versions, but are ignored in readObject so don't otherwise matter. |
2 | 4193 |
fields.put("bitCount", -1); |
4194 |
fields.put("bitLength", -1); |
|
4195 |
fields.put("lowestSetBit", -2); |
|
4196 |
fields.put("firstNonzeroByteNum", -2); |
|
4197 |
||
4198 |
// save them |
|
4199 |
s.writeFields(); |
|
4200 |
} |
|
4201 |
||
4202 |
/** |
|
4203 |
* Returns the mag array as an array of bytes. |
|
4204 |
*/ |
|
4205 |
private byte[] magSerializedForm() { |
|
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4206 |
int len = mag.length; |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4207 |
|
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4208 |
int bitLen = (len == 0 ? 0 : ((len - 1) << 5) + bitLengthForInt(mag[0])); |
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4209 |
int byteLen = (bitLen + 7) >>> 3; |
2 | 4210 |
byte[] result = new byte[byteLen]; |
4211 |
||
2922
dd6d609861f0
6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents:
2
diff
changeset
|
4212 |
for (int i = byteLen - 1, bytesCopied = 4, intIndex = len - 1, nextInt = 0; |
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
4213 |
i >= 0; i--) { |
2 | 4214 |
if (bytesCopied == 4) { |
4215 |
nextInt = mag[intIndex--]; |
|
4216 |
bytesCopied = 1; |
|
4217 |
} else { |
|
4218 |
nextInt >>>= 8; |
|
4219 |
bytesCopied++; |
|
4220 |
} |
|
4221 |
result[i] = (byte)nextInt; |
|
4222 |
} |
|
4223 |
return result; |
|
4224 |
} |
|
10588
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4225 |
|
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4226 |
/** |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4227 |
* Converts this {@code BigInteger} to a {@code long}, checking |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4228 |
* for lost information. If the value of this {@code BigInteger} |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4229 |
* is out of the range of the {@code long} type, then an |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4230 |
* {@code ArithmeticException} is thrown. |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4231 |
* |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4232 |
* @return this {@code BigInteger} converted to a {@code long}. |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4233 |
* @throws ArithmeticException if the value of {@code this} will |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4234 |
* not exactly fit in a {@code long}. |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4235 |
* @see BigInteger#longValue |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4236 |
* @since 1.8 |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4237 |
*/ |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4238 |
public long longValueExact() { |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4239 |
if (mag.length <= 2 && bitLength() <= 63) |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4240 |
return longValue(); |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4241 |
else |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4242 |
throw new ArithmeticException("BigInteger out of long range"); |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4243 |
} |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4244 |
|
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4245 |
/** |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4246 |
* Converts this {@code BigInteger} to an {@code int}, checking |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4247 |
* for lost information. If the value of this {@code BigInteger} |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4248 |
* is out of the range of the {@code int} type, then an |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4249 |
* {@code ArithmeticException} is thrown. |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4250 |
* |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4251 |
* @return this {@code BigInteger} converted to an {@code int}. |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4252 |
* @throws ArithmeticException if the value of {@code this} will |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4253 |
* not exactly fit in a {@code int}. |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4254 |
* @see BigInteger#intValue |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4255 |
* @since 1.8 |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4256 |
*/ |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4257 |
public int intValueExact() { |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4258 |
if (mag.length <= 1 && bitLength() <= 31) |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4259 |
return intValue(); |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4260 |
else |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4261 |
throw new ArithmeticException("BigInteger out of int range"); |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4262 |
} |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4263 |
|
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4264 |
/** |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4265 |
* Converts this {@code BigInteger} to a {@code short}, checking |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4266 |
* for lost information. If the value of this {@code BigInteger} |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4267 |
* is out of the range of the {@code short} type, then an |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4268 |
* {@code ArithmeticException} is thrown. |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4269 |
* |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4270 |
* @return this {@code BigInteger} converted to a {@code short}. |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4271 |
* @throws ArithmeticException if the value of {@code this} will |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4272 |
* not exactly fit in a {@code short}. |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4273 |
* @see BigInteger#shortValue |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4274 |
* @since 1.8 |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4275 |
*/ |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4276 |
public short shortValueExact() { |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4277 |
if (mag.length <= 1 && bitLength() <= 31) { |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4278 |
int value = intValue(); |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4279 |
if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4280 |
return shortValue(); |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4281 |
} |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4282 |
throw new ArithmeticException("BigInteger out of short range"); |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4283 |
} |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4284 |
|
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4285 |
/** |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4286 |
* Converts this {@code BigInteger} to a {@code byte}, checking |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4287 |
* for lost information. If the value of this {@code BigInteger} |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4288 |
* is out of the range of the {@code byte} type, then an |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4289 |
* {@code ArithmeticException} is thrown. |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4290 |
* |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4291 |
* @return this {@code BigInteger} converted to a {@code byte}. |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4292 |
* @throws ArithmeticException if the value of {@code this} will |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4293 |
* not exactly fit in a {@code byte}. |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4294 |
* @see BigInteger#byteValue |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4295 |
* @since 1.8 |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4296 |
*/ |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4297 |
public byte byteValueExact() { |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4298 |
if (mag.length <= 1 && bitLength() <= 31) { |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4299 |
int value = intValue(); |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4300 |
if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4301 |
return byteValue(); |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4302 |
} |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4303 |
throw new ArithmeticException("BigInteger out of byte range"); |
d8173a78bdca
6879143: java.math.BigInteger misses the xxxValueExact methods
darcy
parents:
10431
diff
changeset
|
4304 |
} |
2 | 4305 |
} |