1/*
2 * Copyright (C) 2011 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.math;
18
19import static com.google.common.base.Preconditions.checkArgument;
20import static java.lang.Double.MAX_EXPONENT;
21import static java.lang.Double.MIN_EXPONENT;
22import static java.lang.Double.POSITIVE_INFINITY;
23import static java.lang.Double.doubleToRawLongBits;
24import static java.lang.Double.isNaN;
25import static java.lang.Double.longBitsToDouble;
26import static java.lang.Math.getExponent;
27
28import java.math.BigInteger;
29
30/**
31 * Utilities for {@code double} primitives.
32 *
33 * @author Louis Wasserman
34 */
35final class DoubleUtils {
36  private DoubleUtils() {
37  }
38
39  static double nextDown(double d) {
40    return -Math.nextUp(-d);
41  }
42
43  // The mask for the significand, according to the {@link
44  // Double#doubleToRawLongBits(double)} spec.
45  static final long SIGNIFICAND_MASK = 0x000fffffffffffffL;
46
47  // The mask for the exponent, according to the {@link
48  // Double#doubleToRawLongBits(double)} spec.
49  static final long EXPONENT_MASK = 0x7ff0000000000000L;
50
51  // The mask for the sign, according to the {@link
52  // Double#doubleToRawLongBits(double)} spec.
53  static final long SIGN_MASK = 0x8000000000000000L;
54
55  static final int SIGNIFICAND_BITS = 52;
56
57  static final int EXPONENT_BIAS = 1023;
58
59  /**
60   * The implicit 1 bit that is omitted in significands of normal doubles.
61   */
62  static final long IMPLICIT_BIT = SIGNIFICAND_MASK + 1;
63
64  static long getSignificand(double d) {
65    checkArgument(isFinite(d), "not a normal value");
66    int exponent = getExponent(d);
67    long bits = doubleToRawLongBits(d);
68    bits &= SIGNIFICAND_MASK;
69    return (exponent == MIN_EXPONENT - 1)
70        ? bits << 1
71        : bits | IMPLICIT_BIT;
72  }
73
74  static boolean isFinite(double d) {
75    return getExponent(d) <= MAX_EXPONENT;
76  }
77
78  static boolean isNormal(double d) {
79    return getExponent(d) >= MIN_EXPONENT;
80  }
81
82  /*
83   * Returns x scaled by a power of 2 such that it is in the range [1, 2). Assumes x is positive,
84   * normal, and finite.
85   */
86  static double scaleNormalize(double x) {
87    long significand = doubleToRawLongBits(x) & SIGNIFICAND_MASK;
88    return longBitsToDouble(significand | ONE_BITS);
89  }
90
91  static double bigToDouble(BigInteger x) {
92    // This is an extremely fast implementation of BigInteger.doubleValue().  JDK patch pending.
93    BigInteger absX = x.abs();
94    int exponent = absX.bitLength() - 1;
95    // exponent == floor(log2(abs(x)))
96    if (exponent < Long.SIZE - 1) {
97      return x.longValue();
98    } else if (exponent > MAX_EXPONENT) {
99      return x.signum() * POSITIVE_INFINITY;
100    }
101
102    /*
103     * We need the top SIGNIFICAND_BITS + 1 bits, including the "implicit" one bit. To make
104     * rounding easier, we pick out the top SIGNIFICAND_BITS + 2 bits, so we have one to help us
105     * round up or down. twiceSignifFloor will contain the top SIGNIFICAND_BITS + 2 bits, and
106     * signifFloor the top SIGNIFICAND_BITS + 1.
107     *
108     * It helps to consider the real number signif = absX * 2^(SIGNIFICAND_BITS - exponent).
109     */
110    int shift = exponent - SIGNIFICAND_BITS - 1;
111    long twiceSignifFloor = absX.shiftRight(shift).longValue();
112    long signifFloor = twiceSignifFloor >> 1;
113    signifFloor &= SIGNIFICAND_MASK; // remove the implied bit
114
115    /*
116     * We round up if either the fractional part of signif is strictly greater than 0.5 (which is
117     * true if the 0.5 bit is set and any lower bit is set), or if the fractional part of signif is
118     * >= 0.5 and signifFloor is odd (which is true if both the 0.5 bit and the 1 bit are set).
119     */
120    boolean increment = (twiceSignifFloor & 1) != 0
121        && ((signifFloor & 1) != 0 || absX.getLowestSetBit() < shift);
122    long signifRounded = increment ? signifFloor + 1 : signifFloor;
123    long bits = (long) ((exponent + EXPONENT_BIAS)) << SIGNIFICAND_BITS;
124    bits += signifRounded;
125    /*
126     * If signifRounded == 2^53, we'd need to set all of the significand bits to zero and add 1 to
127     * the exponent. This is exactly the behavior we get from just adding signifRounded to bits
128     * directly.  If the exponent is MAX_DOUBLE_EXPONENT, we round up (correctly) to
129     * Double.POSITIVE_INFINITY.
130     */
131    bits |= x.signum() & SIGN_MASK;
132    return longBitsToDouble(bits);
133  }
134
135  /**
136   * Returns its argument if it is non-negative, zero if it is negative.
137   */
138  static double ensureNonNegative(double value) {
139    checkArgument(!isNaN(value));
140    if (value > 0.0) {
141      return value;
142    } else {
143      return 0.0;
144    }
145  }
146
147  private static final long ONE_BITS = doubleToRawLongBits(1.0);
148}
149