1 /*
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
3423 // Add dot if previously determined to be necessary.
3424 if (needDot) {
3425 sb.append('.');
3426 }
3427
3428 // Add zeros.
3429 trailingZeros(sb, prec - outPrec);
3430 }
3431
3432 // Method assumes that d > 0.
3433 private String hexDouble(double d, int prec) {
3434 // Let Double.toHexString handle simple cases
3435 if (!Double.isFinite(d) || d == 0.0 || prec == 0 || prec >= 13) {
3436 // remove "0x"
3437 return Double.toHexString(d).substring(2);
3438 } else {
3439 assert(prec >= 1 && prec <= 12);
3440
3441 int exponent = Math.getExponent(d);
3442 boolean subnormal
3443 = (exponent == DoubleConsts.MIN_EXPONENT - 1);
3444
3445 // If this is subnormal input so normalize (could be faster to
3446 // do as integer operation).
3447 if (subnormal) {
3448 scaleUp = Math.scalb(1.0, 54);
3449 d *= scaleUp;
3450 // Calculate the exponent. This is not just exponent + 54
3451 // since the former is not the normalized exponent.
3452 exponent = Math.getExponent(d);
3453 assert exponent >= DoubleConsts.MIN_EXPONENT &&
3454 exponent <= DoubleConsts.MAX_EXPONENT: exponent;
3455 }
3456
3457 int precision = 1 + prec*4;
3458 int shiftDistance
3459 = DoubleConsts.SIGNIFICAND_WIDTH - precision;
3460 assert(shiftDistance >= 1 && shiftDistance < DoubleConsts.SIGNIFICAND_WIDTH);
3461
3462 long doppel = Double.doubleToLongBits(d);
3463 // Deterime the number of bits to keep.
3464 long newSignif
3465 = (doppel & (DoubleConsts.EXP_BIT_MASK
3466 | DoubleConsts.SIGNIF_BIT_MASK))
3467 >> shiftDistance;
3468 // Bits to round away.
3469 long roundingBits = doppel & ~(~0L << shiftDistance);
3470
3471 // To decide how to round, look at the low-order bit of the
3472 // working significand, the highest order discarded bit (the
3473 // round bit) and whether any of the lower order discarded bits
3474 // are nonzero (the sticky bit).
|
1 /*
2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
3423 // Add dot if previously determined to be necessary.
3424 if (needDot) {
3425 sb.append('.');
3426 }
3427
3428 // Add zeros.
3429 trailingZeros(sb, prec - outPrec);
3430 }
3431
3432 // Method assumes that d > 0.
3433 private String hexDouble(double d, int prec) {
3434 // Let Double.toHexString handle simple cases
3435 if (!Double.isFinite(d) || d == 0.0 || prec == 0 || prec >= 13) {
3436 // remove "0x"
3437 return Double.toHexString(d).substring(2);
3438 } else {
3439 assert(prec >= 1 && prec <= 12);
3440
3441 int exponent = Math.getExponent(d);
3442 boolean subnormal
3443 = (exponent == Double.MIN_EXPONENT - 1);
3444
3445 // If this is subnormal input so normalize (could be faster to
3446 // do as integer operation).
3447 if (subnormal) {
3448 scaleUp = Math.scalb(1.0, 54);
3449 d *= scaleUp;
3450 // Calculate the exponent. This is not just exponent + 54
3451 // since the former is not the normalized exponent.
3452 exponent = Math.getExponent(d);
3453 assert exponent >= Double.MIN_EXPONENT &&
3454 exponent <= Double.MAX_EXPONENT: exponent;
3455 }
3456
3457 int precision = 1 + prec*4;
3458 int shiftDistance
3459 = DoubleConsts.SIGNIFICAND_WIDTH - precision;
3460 assert(shiftDistance >= 1 && shiftDistance < DoubleConsts.SIGNIFICAND_WIDTH);
3461
3462 long doppel = Double.doubleToLongBits(d);
3463 // Deterime the number of bits to keep.
3464 long newSignif
3465 = (doppel & (DoubleConsts.EXP_BIT_MASK
3466 | DoubleConsts.SIGNIF_BIT_MASK))
3467 >> shiftDistance;
3468 // Bits to round away.
3469 long roundingBits = doppel & ~(~0L << shiftDistance);
3470
3471 // To decide how to round, look at the low-order bit of the
3472 // working significand, the highest order discarded bit (the
3473 // round bit) and whether any of the lower order discarded bits
3474 // are nonzero (the sticky bit).
|