Java Code Examples for sun.misc.FloatConsts#SIGNIF_BIT_MASK

The following examples show how to use sun.misc.FloatConsts#SIGNIF_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: Math.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the closest {@code int} to the argument, with ties
 * rounding to positive infinity.
 *
 * <p>
 * Special cases:
 * <ul><li>If the argument is NaN, the result is 0.
 * <li>If the argument is negative infinity or any value less than or
 * equal to the value of {@code Integer.MIN_VALUE}, the result is
 * equal to the value of {@code Integer.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Integer.MAX_VALUE}, the result is
 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to an integer.
 * @return  the value of the argument rounded to the nearest
 *          {@code int} value.
 * @see     java.lang.Integer#MAX_VALUE
 * @see     java.lang.Integer#MIN_VALUE
 */
public static int round(float a) {
    int intBits = Float.floatToRawIntBits(a);
    int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
            >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
    int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
            + FloatConsts.EXP_BIAS) - biasedExp;
    if ((shift & -32) == 0) { // shift >= 0 && shift < 32
        // a is a finite number such that pow(2,-32) <= ulp(a) < 1
        int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                | (FloatConsts.SIGNIF_BIT_MASK + 1));
        if (intBits < 0) {
            r = -r;
        }
        // In the comments below each Java expression evaluates to the value
        // the corresponding mathematical expression:
        // (r) evaluates to a / ulp(a)
        // (r >> shift) evaluates to floor(a * 2)
        // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
        // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
        return ((r >> shift) + 1) >> 1;
    } else {
        // a is either
        // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (int) a;
    }
}
 
Example 2
Source File: Math.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Returns the closest {@code int} to the argument, with ties
 * rounding to positive infinity.
 *
 * <p>
 * Special cases:
 * <ul><li>If the argument is NaN, the result is 0.
 * <li>If the argument is negative infinity or any value less than or
 * equal to the value of {@code Integer.MIN_VALUE}, the result is
 * equal to the value of {@code Integer.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Integer.MAX_VALUE}, the result is
 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to an integer.
 * @return  the value of the argument rounded to the nearest
 *          {@code int} value.
 * @see     java.lang.Integer#MAX_VALUE
 * @see     java.lang.Integer#MIN_VALUE
 */
public static int round(float a) {
    int intBits = Float.floatToRawIntBits(a);
    int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
            >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
    int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
            + FloatConsts.EXP_BIAS) - biasedExp;
    if ((shift & -32) == 0) { // shift >= 0 && shift < 32
        // a is a finite number such that pow(2,-32) <= ulp(a) < 1
        int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                | (FloatConsts.SIGNIF_BIT_MASK + 1));
        if (intBits < 0) {
            r = -r;
        }
        // In the comments below each Java expression evaluates to the value
        // the corresponding mathematical expression:
        // (r) evaluates to a / ulp(a)
        // (r >> shift) evaluates to floor(a * 2)
        // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
        // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
        return ((r >> shift) + 1) >> 1;
    } else {
        // a is either
        // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (int) a;
    }
}
 
Example 3
Source File: Math.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the closest {@code int} to the argument, with ties
 * rounding to positive infinity.
 *
 * <p>
 * Special cases:
 * <ul><li>If the argument is NaN, the result is 0.
 * <li>If the argument is negative infinity or any value less than or
 * equal to the value of {@code Integer.MIN_VALUE}, the result is
 * equal to the value of {@code Integer.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Integer.MAX_VALUE}, the result is
 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to an integer.
 * @return  the value of the argument rounded to the nearest
 *          {@code int} value.
 * @see     java.lang.Integer#MAX_VALUE
 * @see     java.lang.Integer#MIN_VALUE
 */
public static int round(float a) {
    int intBits = Float.floatToRawIntBits(a);
    int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
            >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
    int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
            + FloatConsts.EXP_BIAS) - biasedExp;
    if ((shift & -32) == 0) { // shift >= 0 && shift < 32
        // a is a finite number such that pow(2,-32) <= ulp(a) < 1
        int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                | (FloatConsts.SIGNIF_BIT_MASK + 1));
        if (intBits < 0) {
            r = -r;
        }
        // In the comments below each Java expression evaluates to the value
        // the corresponding mathematical expression:
        // (r) evaluates to a / ulp(a)
        // (r >> shift) evaluates to floor(a * 2)
        // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
        // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
        return ((r >> shift) + 1) >> 1;
    } else {
        // a is either
        // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (int) a;
    }
}
 
Example 4
Source File: Math.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the closest {@code int} to the argument, with ties
 * rounding to positive infinity.
 *
 * <p>
 * Special cases:
 * <ul><li>If the argument is NaN, the result is 0.
 * <li>If the argument is negative infinity or any value less than or
 * equal to the value of {@code Integer.MIN_VALUE}, the result is
 * equal to the value of {@code Integer.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Integer.MAX_VALUE}, the result is
 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to an integer.
 * @return  the value of the argument rounded to the nearest
 *          {@code int} value.
 * @see     java.lang.Integer#MAX_VALUE
 * @see     java.lang.Integer#MIN_VALUE
 */
public static int round(float a) {
    int intBits = Float.floatToRawIntBits(a);
    int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
            >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
    int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
            + FloatConsts.EXP_BIAS) - biasedExp;
    if ((shift & -32) == 0) { // shift >= 0 && shift < 32
        // a is a finite number such that pow(2,-32) <= ulp(a) < 1
        int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                | (FloatConsts.SIGNIF_BIT_MASK + 1));
        if (intBits < 0) {
            r = -r;
        }
        // In the comments below each Java expression evaluates to the value
        // the corresponding mathematical expression:
        // (r) evaluates to a / ulp(a)
        // (r >> shift) evaluates to floor(a * 2)
        // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
        // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
        return ((r >> shift) + 1) >> 1;
    } else {
        // a is either
        // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (int) a;
    }
}
 
Example 5
Source File: Math.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the closest {@code int} to the argument, with ties
 * rounding to positive infinity.
 *
 * <p>
 * Special cases:
 * <ul><li>If the argument is NaN, the result is 0.
 * <li>If the argument is negative infinity or any value less than or
 * equal to the value of {@code Integer.MIN_VALUE}, the result is
 * equal to the value of {@code Integer.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Integer.MAX_VALUE}, the result is
 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to an integer.
 * @return  the value of the argument rounded to the nearest
 *          {@code int} value.
 * @see     java.lang.Integer#MAX_VALUE
 * @see     java.lang.Integer#MIN_VALUE
 */
public static int round(float a) {
    int intBits = Float.floatToRawIntBits(a);
    int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
            >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
    int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
            + FloatConsts.EXP_BIAS) - biasedExp;
    if ((shift & -32) == 0) { // shift >= 0 && shift < 32
        // a is a finite number such that pow(2,-32) <= ulp(a) < 1
        int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                | (FloatConsts.SIGNIF_BIT_MASK + 1));
        if (intBits < 0) {
            r = -r;
        }
        // In the comments below each Java expression evaluates to the value
        // the corresponding mathematical expression:
        // (r) evaluates to a / ulp(a)
        // (r >> shift) evaluates to floor(a * 2)
        // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
        // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
        return ((r >> shift) + 1) >> 1;
    } else {
        // a is either
        // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (int) a;
    }
}
 
Example 6
Source File: Math.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the closest {@code int} to the argument, with ties
 * rounding to positive infinity.
 *
 * <p>
 * Special cases:
 * <ul><li>If the argument is NaN, the result is 0.
 * <li>If the argument is negative infinity or any value less than or
 * equal to the value of {@code Integer.MIN_VALUE}, the result is
 * equal to the value of {@code Integer.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Integer.MAX_VALUE}, the result is
 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to an integer.
 * @return  the value of the argument rounded to the nearest
 *          {@code int} value.
 * @see     java.lang.Integer#MAX_VALUE
 * @see     java.lang.Integer#MIN_VALUE
 */
public static int round(float a) {
    int intBits = Float.floatToRawIntBits(a);
    int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
            >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
    int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
            + FloatConsts.EXP_BIAS) - biasedExp;
    if ((shift & -32) == 0) { // shift >= 0 && shift < 32
        // a is a finite number such that pow(2,-32) <= ulp(a) < 1
        int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                | (FloatConsts.SIGNIF_BIT_MASK + 1));
        if (intBits < 0) {
            r = -r;
        }
        // In the comments below each Java expression evaluates to the value
        // the corresponding mathematical expression:
        // (r) evaluates to a / ulp(a)
        // (r >> shift) evaluates to floor(a * 2)
        // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
        // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
        return ((r >> shift) + 1) >> 1;
    } else {
        // a is either
        // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (int) a;
    }
}
 
Example 7
Source File: Math.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the closest {@code int} to the argument, with ties
 * rounding to positive infinity.
 *
 * <p>
 * Special cases:
 * <ul><li>If the argument is NaN, the result is 0.
 * <li>If the argument is negative infinity or any value less than or
 * equal to the value of {@code Integer.MIN_VALUE}, the result is
 * equal to the value of {@code Integer.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Integer.MAX_VALUE}, the result is
 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to an integer.
 * @return  the value of the argument rounded to the nearest
 *          {@code int} value.
 * @see     java.lang.Integer#MAX_VALUE
 * @see     java.lang.Integer#MIN_VALUE
 */
public static int round(float a) {
    int intBits = Float.floatToRawIntBits(a);
    int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
            >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
    int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
            + FloatConsts.EXP_BIAS) - biasedExp;
    if ((shift & -32) == 0) { // shift >= 0 && shift < 32
        // a is a finite number such that pow(2,-32) <= ulp(a) < 1
        int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                | (FloatConsts.SIGNIF_BIT_MASK + 1));
        if (intBits < 0) {
            r = -r;
        }
        // In the comments below each Java expression evaluates to the value
        // the corresponding mathematical expression:
        // (r) evaluates to a / ulp(a)
        // (r >> shift) evaluates to floor(a * 2)
        // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
        // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
        return ((r >> shift) + 1) >> 1;
    } else {
        // a is either
        // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (int) a;
    }
}
 
Example 8
Source File: FpUtils.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code float}; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 9
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 10
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 11
Source File: FpUtils.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code float}; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 12
Source File: FpUtils.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code float}; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
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: FpUtils.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code float}; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 15
Source File: FpUtils.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code float}; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 16
Source File: FpUtils.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code float}; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 17
Source File: Float.java    From AndroidComponentPlugin with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a representation of the specified floating-point value
 * according to the IEEE 754 floating-point "single format" bit
 * layout.
 *
 * <p>Bit 31 (the bit that is selected by the mask
 * {@code 0x80000000}) represents the sign of the floating-point
 * number.
 * Bits 30-23 (the bits that are selected by the mask
 * {@code 0x7f800000}) represent the exponent.
 * Bits 22-0 (the bits that are selected by the mask
 * {@code 0x007fffff}) represent the significand (sometimes called
 * the mantissa) of the floating-point number.
 *
 * <p>If the argument is positive infinity, the result is
 * {@code 0x7f800000}.
 *
 * <p>If the argument is negative infinity, the result is
 * {@code 0xff800000}.
 *
 * <p>If the argument is NaN, the result is {@code 0x7fc00000}.
 *
 * <p>In all cases, the result is an integer that, when given to the
 * {@link #intBitsToFloat(int)} method, will produce a floating-point
 * value the same as the argument to {@code floatToIntBits}
 * (except all NaN values are collapsed to a single
 * "canonical" NaN value).
 *
 * @param   value   a floating-point number.
 * @return the bits that represent the floating-point number.
 */
public static int floatToIntBits(float value) {
    int result = floatToRawIntBits(value);
    // Check for NaN based on values of bit fields, maximum
    // exponent and nonzero significand.
    if ( ((result & FloatConsts.EXP_BIT_MASK) ==
          FloatConsts.EXP_BIT_MASK) &&
         (result & FloatConsts.SIGNIF_BIT_MASK) != 0)
        result = 0x7fc00000;
    return result;
}
 
Example 18
Source File: Float.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns a representation of the specified floating-point value
 * according to the IEEE 754 floating-point "single format" bit
 * layout.
 *
 * <p>Bit 31 (the bit that is selected by the mask
 * {@code 0x80000000}) represents the sign of the floating-point
 * number.
 * Bits 30-23 (the bits that are selected by the mask
 * {@code 0x7f800000}) represent the exponent.
 * Bits 22-0 (the bits that are selected by the mask
 * {@code 0x007fffff}) represent the significand (sometimes called
 * the mantissa) of the floating-point number.
 *
 * <p>If the argument is positive infinity, the result is
 * {@code 0x7f800000}.
 *
 * <p>If the argument is negative infinity, the result is
 * {@code 0xff800000}.
 *
 * <p>If the argument is NaN, the result is {@code 0x7fc00000}.
 *
 * <p>In all cases, the result is an integer that, when given to the
 * {@link #intBitsToFloat(int)} method, will produce a floating-point
 * value the same as the argument to {@code floatToIntBits}
 * (except all NaN values are collapsed to a single
 * "canonical" NaN value).
 *
 * @param   value   a floating-point number.
 * @return the bits that represent the floating-point number.
 */
public static int floatToIntBits(float value) {
    int result = floatToRawIntBits(value);
    // Check for NaN based on values of bit fields, maximum
    // exponent and nonzero significand.
    if ( ((result & FloatConsts.EXP_BIT_MASK) ==
          FloatConsts.EXP_BIT_MASK) &&
         (result & FloatConsts.SIGNIF_BIT_MASK) != 0)
        result = 0x7fc00000;
    return result;
}
 
Example 19
Source File: Float.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns a representation of the specified floating-point value
 * according to the IEEE 754 floating-point "single format" bit
 * layout.
 *
 * <p>Bit 31 (the bit that is selected by the mask
 * {@code 0x80000000}) represents the sign of the floating-point
 * number.
 * Bits 30-23 (the bits that are selected by the mask
 * {@code 0x7f800000}) represent the exponent.
 * Bits 22-0 (the bits that are selected by the mask
 * {@code 0x007fffff}) represent the significand (sometimes called
 * the mantissa) of the floating-point number.
 *
 * <p>If the argument is positive infinity, the result is
 * {@code 0x7f800000}.
 *
 * <p>If the argument is negative infinity, the result is
 * {@code 0xff800000}.
 *
 * <p>If the argument is NaN, the result is {@code 0x7fc00000}.
 *
 * <p>In all cases, the result is an integer that, when given to the
 * {@link #intBitsToFloat(int)} method, will produce a floating-point
 * value the same as the argument to {@code floatToIntBits}
 * (except all NaN values are collapsed to a single
 * "canonical" NaN value).
 *
 * @param   value   a floating-point number.
 * @return the bits that represent the floating-point number.
 */
public static int floatToIntBits(float value) {
    int result = floatToRawIntBits(value);
    // Check for NaN based on values of bit fields, maximum
    // exponent and nonzero significand.
    if ( ((result & FloatConsts.EXP_BIT_MASK) ==
          FloatConsts.EXP_BIT_MASK) &&
         (result & FloatConsts.SIGNIF_BIT_MASK) != 0)
        result = 0x7fc00000;
    return result;
}
 
Example 20
Source File: Float.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a representation of the specified floating-point value
 * according to the IEEE 754 floating-point "single format" bit
 * layout.
 *
 * <p>Bit 31 (the bit that is selected by the mask
 * {@code 0x80000000}) represents the sign of the floating-point
 * number.
 * Bits 30-23 (the bits that are selected by the mask
 * {@code 0x7f800000}) represent the exponent.
 * Bits 22-0 (the bits that are selected by the mask
 * {@code 0x007fffff}) represent the significand (sometimes called
 * the mantissa) of the floating-point number.
 *
 * <p>If the argument is positive infinity, the result is
 * {@code 0x7f800000}.
 *
 * <p>If the argument is negative infinity, the result is
 * {@code 0xff800000}.
 *
 * <p>If the argument is NaN, the result is {@code 0x7fc00000}.
 *
 * <p>In all cases, the result is an integer that, when given to the
 * {@link #intBitsToFloat(int)} method, will produce a floating-point
 * value the same as the argument to {@code floatToIntBits}
 * (except all NaN values are collapsed to a single
 * "canonical" NaN value).
 *
 * @param   value   a floating-point number.
 * @return the bits that represent the floating-point number.
 */
public static int floatToIntBits(float value) {
    int result = floatToRawIntBits(value);
    // Check for NaN based on values of bit fields, maximum
    // exponent and nonzero significand.
    if ( ((result & FloatConsts.EXP_BIT_MASK) ==
          FloatConsts.EXP_BIT_MASK) &&
         (result & FloatConsts.SIGNIF_BIT_MASK) != 0)
        result = 0x7fc00000;
    return result;
}