From 90aebf1cf930c126e830e70f2c9afc29b79473e6 Mon Sep 17 00:00:00 2001 From: Louis Wasserman Date: Fri, 21 Jun 2013 11:50:45 -0700 Subject: [PATCH] 7131192: BigInteger.doubleValue() is depressingly slow In doubleValue() and floatValue() replace converting to String and parsing to Double or Float with direct conversion into IEEE 754 bits. Reviewed-by: bpb, drchase, martin --- .../share/classes/java/math/BigInteger.java | 146 +++++++++++++++++- .../BigInteger/PrimitiveConversionTests.java | 105 +++++++++++++ 2 files changed, 247 insertions(+), 4 deletions(-) create mode 100644 jdk/test/java/math/BigInteger/PrimitiveConversionTests.java diff --git a/jdk/src/share/classes/java/math/BigInteger.java b/jdk/src/share/classes/java/math/BigInteger.java index 4c8b7816d08..8972ac76ef9 100644 --- a/jdk/src/share/classes/java/math/BigInteger.java +++ b/jdk/src/share/classes/java/math/BigInteger.java @@ -35,6 +35,8 @@ import java.io.ObjectOutputStream; import java.io.ObjectStreamField; import java.util.Arrays; import java.util.Random; +import sun.misc.DoubleConsts; +import sun.misc.FloatConsts; /** * Immutable arbitrary-precision integers. All operations behave as if @@ -3452,8 +3454,72 @@ public class BigInteger extends Number implements Comparable { * @return this BigInteger converted to a {@code float}. */ public float floatValue() { - // Somewhat inefficient, but guaranteed to work. - return Float.parseFloat(this.toString()); + if (signum == 0) { + return 0.0f; + } + + int exponent = ((mag.length - 1) << 5) + bitLengthForInt(mag[0]) - 1; + + // exponent == floor(log2(abs(this))) + if (exponent < Long.SIZE - 1) { + return longValue(); + } else if (exponent > Float.MAX_EXPONENT) { + return signum > 0 ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY; + } + + /* + * We need the top SIGNIFICAND_WIDTH bits, including the "implicit" + * one bit. To make rounding easier, we pick out the top + * SIGNIFICAND_WIDTH + 1 bits, so we have one to help us round up or + * down. twiceSignifFloor will contain the top SIGNIFICAND_WIDTH + 1 + * bits, and signifFloor the top SIGNIFICAND_WIDTH. + * + * It helps to consider the real number signif = abs(this) * + * 2^(SIGNIFICAND_WIDTH - 1 - exponent). + */ + int shift = exponent - FloatConsts.SIGNIFICAND_WIDTH; + + int twiceSignifFloor; + // twiceSignifFloor will be == abs().shiftRight(shift).intValue() + // We do the shift into an int directly to improve performance. + + int nBits = shift & 0x1f; + int nBits2 = 32 - nBits; + + if (nBits == 0) { + twiceSignifFloor = mag[0]; + } else { + twiceSignifFloor = mag[0] >>> nBits; + if (twiceSignifFloor == 0) { + twiceSignifFloor = (mag[0] << nBits2) | (mag[1] >>> nBits); + } + } + + int signifFloor = twiceSignifFloor >> 1; + signifFloor &= FloatConsts.SIGNIF_BIT_MASK; // remove the implied bit + + /* + * We round up if either the fractional part of signif is strictly + * greater than 0.5 (which is true if the 0.5 bit is set and any lower + * bit is set), or if the fractional part of signif is >= 0.5 and + * signifFloor is odd (which is true if both the 0.5 bit and the 1 bit + * are set). This is equivalent to the desired HALF_EVEN rounding. + */ + boolean increment = (twiceSignifFloor & 1) != 0 + && ((signifFloor & 1) != 0 || abs().getLowestSetBit() < shift); + int signifRounded = increment ? signifFloor + 1 : signifFloor; + int bits = ((exponent + FloatConsts.EXP_BIAS)) + << (FloatConsts.SIGNIFICAND_WIDTH - 1); + bits += signifRounded; + /* + * If signifRounded == 2^24, we'd need to set all of the significand + * bits to zero and add 1 to the exponent. This is exactly the behavior + * we get from just adding signifRounded to bits directly. If the + * exponent is Float.MAX_EXPONENT, we round up (correctly) to + * Float.POSITIVE_INFINITY. + */ + bits |= signum & FloatConsts.SIGN_BIT_MASK; + return Float.intBitsToFloat(bits); } /** @@ -3472,8 +3538,80 @@ public class BigInteger extends Number implements Comparable { * @return this BigInteger converted to a {@code double}. */ public double doubleValue() { - // Somewhat inefficient, but guaranteed to work. - return Double.parseDouble(this.toString()); + if (signum == 0) { + return 0.0; + } + + int exponent = ((mag.length - 1) << 5) + bitLengthForInt(mag[0]) - 1; + + // exponent == floor(log2(abs(this))Double) + if (exponent < Long.SIZE - 1) { + return longValue(); + } else if (exponent > Double.MAX_EXPONENT) { + return signum > 0 ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY; + } + + /* + * We need the top SIGNIFICAND_WIDTH bits, including the "implicit" + * one bit. To make rounding easier, we pick out the top + * SIGNIFICAND_WIDTH + 1 bits, so we have one to help us round up or + * down. twiceSignifFloor will contain the top SIGNIFICAND_WIDTH + 1 + * bits, and signifFloor the top SIGNIFICAND_WIDTH. + * + * It helps to consider the real number signif = abs(this) * + * 2^(SIGNIFICAND_WIDTH - 1 - exponent). + */ + int shift = exponent - DoubleConsts.SIGNIFICAND_WIDTH; + + long twiceSignifFloor; + // twiceSignifFloor will be == abs().shiftRight(shift).longValue() + // We do the shift into a long directly to improve performance. + + int nBits = shift & 0x1f; + int nBits2 = 32 - nBits; + + int highBits; + int lowBits; + if (nBits == 0) { + highBits = mag[0]; + lowBits = mag[1]; + } else { + highBits = mag[0] >>> nBits; + lowBits = (mag[0] << nBits2) | (mag[1] >>> nBits); + if (highBits == 0) { + highBits = lowBits; + lowBits = (mag[1] << nBits2) | (mag[2] >>> nBits); + } + } + + twiceSignifFloor = ((highBits & LONG_MASK) << 32) + | (lowBits & LONG_MASK); + + long signifFloor = twiceSignifFloor >> 1; + signifFloor &= DoubleConsts.SIGNIF_BIT_MASK; // remove the implied bit + + /* + * We round up if either the fractional part of signif is strictly + * greater than 0.5 (which is true if the 0.5 bit is set and any lower + * bit is set), or if the fractional part of signif is >= 0.5 and + * signifFloor is odd (which is true if both the 0.5 bit and the 1 bit + * are set). This is equivalent to the desired HALF_EVEN rounding. + */ + boolean increment = (twiceSignifFloor & 1) != 0 + && ((signifFloor & 1) != 0 || abs().getLowestSetBit() < shift); + long signifRounded = increment ? signifFloor + 1 : signifFloor; + long bits = (long) ((exponent + DoubleConsts.EXP_BIAS)) + << (DoubleConsts.SIGNIFICAND_WIDTH - 1); + bits += signifRounded; + /* + * If signifRounded == 2^53, we'd need to set all of the significand + * bits to zero and add 1 to the exponent. This is exactly the behavior + * we get from just adding signifRounded to bits directly. If the + * exponent is Double.MAX_EXPONENT, we round up (correctly) to + * Double.POSITIVE_INFINITY. + */ + bits |= signum & DoubleConsts.SIGN_BIT_MASK; + return Double.longBitsToDouble(bits); } /** diff --git a/jdk/test/java/math/BigInteger/PrimitiveConversionTests.java b/jdk/test/java/math/BigInteger/PrimitiveConversionTests.java new file mode 100644 index 00000000000..d459fe54cf0 --- /dev/null +++ b/jdk/test/java/math/BigInteger/PrimitiveConversionTests.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import static java.math.BigInteger.ONE; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +/** + * @test + * @bug 7131192 + * @summary This test ensures that BigInteger.floatValue() and + * BigInteger.doubleValue() behave correctly. + * @author Louis Wasserman + */ +public class PrimitiveConversionTests { + static final List ALL_BIGINTEGER_CANDIDATES; + + static { + List samples = new ArrayList<>(); + // Now add values near 2^N for lots of values of N. + for (int exponent : Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 31, 32, 33, + 34, 62, 63, 64, 65, 71, 72, 73, 79, 80, 81, 255, 256, 257, 511, + 512, 513, Double.MAX_EXPONENT - 1, Double.MAX_EXPONENT, + Double.MAX_EXPONENT + 1, 2000, 2001, 2002)) { + BigInteger x = ONE.shiftLeft(exponent); + for (BigInteger y : Arrays.asList(x, x.add(ONE), x.subtract(ONE))) { + samples.add(y); + samples.add(y.negate()); + } + } + + Random rng = new Random(1234567); + for (int i = 0; i < 2000; i++) { + samples.add(new BigInteger(rng.nextInt(2000), rng)); + } + + ALL_BIGINTEGER_CANDIDATES = Collections.unmodifiableList(samples); + } + + public static int testDoubleValue() { + int failures = 0; + for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) { + double expected = Double.parseDouble(big.toString()); + double actual = big.doubleValue(); + + // should be bitwise identical + if (Double.doubleToRawLongBits(expected) != Double + .doubleToRawLongBits(actual)) { + System.out.println(big); + failures++; + } + } + return failures; + } + + public static int testFloatValue() { + int failures = 0; + for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) { + float expected = Float.parseFloat(big.toString()); + float actual = big.floatValue(); + + // should be bitwise identical + if (Float.floatToRawIntBits(expected) != Float + .floatToRawIntBits(actual)) { + System.out.println(big + " " + expected + " " + actual); + failures++; + } + } + return failures; + } + + public static void main(String[] args) { + int failures = testDoubleValue(); + failures += testFloatValue(); + if (failures > 0) { + throw new RuntimeException("Incurred " + failures + + " failures while testing primitive conversions."); + } + } +}