Java Code Examples for sun.misc.FloatConsts#SIGN_BIT_MASK

The following examples show how to use sun.misc.FloatConsts#SIGN_BIT_MASK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BigInteger.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 2
Source File: BigInteger.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 3
Source File: BigInteger.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 4
Source File: BigInteger.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 5
Source File: BigInteger.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 6
Source File: BigInteger.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 7
Source File: BigInteger.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 8
Source File: BigInteger.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 9
Source File: BigInteger.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 10
Source File: BigInteger.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 11
Source File: BigInteger.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 12
Source File: BigInteger.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 13
Source File: BigInteger.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 14
Source File: BigInteger.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 15
Source File: BigInteger.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}
 
Example 16
Source File: BigInteger.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    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);
}