Java Code Examples for sun.misc.FloatConsts#EXP_BIT_MASK

The following examples show how to use sun.misc.FloatConsts#EXP_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 openjdk-jdk8u 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 2
Source File: Math.java    From AndroidComponentPlugin 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 3
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 4
Source File: FpUtils.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns unbiased exponent of a <code>float</code>.
 */
public static int getExponent(float f){
    /*
     * Bitwise convert f to integer, mask out exponent bits, shift
     * to the right and then subtract out float's bias adjust to
     * get true exponent value
     */
    return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
            (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}
 
Example 5
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 6
Source File: Math.java    From jdk8u60 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 TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the unbiased exponent used in the representation of a
 * {@code float}.  Special cases:
 *
 * <ul>
 * <li>If the argument is NaN or infinite, then the result is
 * {@link Float#MAX_EXPONENT} + 1.
 * <li>If the argument is zero or subnormal, then the result is
 * {@link Float#MIN_EXPONENT} -1.
 * </ul>
 * @param f a {@code float} value
 * @return the unbiased exponent of the argument
 * @since 1.6
 */
public static int getExponent(float f) {
    /*
     * Bitwise convert f to integer, mask out exponent bits, shift
     * to the right and then subtract out float's bias adjust to
     * get true exponent value
     */
    return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
            (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}
 
Example 8
Source File: Float.java    From Java8CN 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 9
Source File: Float.java    From JDKSourceCode1.8 with MIT License 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 10
Source File: Math.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the unbiased exponent used in the representation of a
 * {@code float}.  Special cases:
 *
 * <ul>
 * <li>If the argument is NaN or infinite, then the result is
 * {@link Float#MAX_EXPONENT} + 1.
 * <li>If the argument is zero or subnormal, then the result is
 * {@link Float#MIN_EXPONENT} -1.
 * </ul>
 * @param f a {@code float} value
 * @return the unbiased exponent of the argument
 * @since 1.6
 */
public static int getExponent(float f) {
    /*
     * Bitwise convert f to integer, mask out exponent bits, shift
     * to the right and then subtract out float's bias adjust to
     * get true exponent value
     */
    return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
            (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}
 
Example 11
Source File: Float.java    From openjdk-jdk8u-backup 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 12
Source File: Math.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the unbiased exponent used in the representation of a
 * {@code float}.  Special cases:
 *
 * <ul>
 * <li>If the argument is NaN or infinite, then the result is
 * {@link Float#MAX_EXPONENT} + 1.
 * <li>If the argument is zero or subnormal, then the result is
 * {@link Float#MIN_EXPONENT} -1.
 * </ul>
 * @param f a {@code float} value
 * @return the unbiased exponent of the argument
 * @since 1.6
 */
public static int getExponent(float f) {
    /*
     * Bitwise convert f to integer, mask out exponent bits, shift
     * to the right and then subtract out float's bias adjust to
     * get true exponent value
     */
    return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
            (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}
 
Example 13
Source File: Float.java    From TencentKona-8 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 14
Source File: Math.java    From jdk8u_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the unbiased exponent used in the representation of a
 * {@code float}.  Special cases:
 *
 * <ul>
 * <li>If the argument is NaN or infinite, then the result is
 * {@link Float#MAX_EXPONENT} + 1.
 * <li>If the argument is zero or subnormal, then the result is
 * {@link Float#MIN_EXPONENT} -1.
 * </ul>
 * @param f a {@code float} value
 * @return the unbiased exponent of the argument
 * @since 1.6
 */
public static int getExponent(float f) {
    /*
     * Bitwise convert f to integer, mask out exponent bits, shift
     * to the right and then subtract out float's bias adjust to
     * get true exponent value
     */
    return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
            (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}
 
Example 15
Source File: Math.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the unbiased exponent used in the representation of a
 * {@code float}.  Special cases:
 *
 * <ul>
 * <li>If the argument is NaN or infinite, then the result is
 * {@link Float#MAX_EXPONENT} + 1.
 * <li>If the argument is zero or subnormal, then the result is
 * {@link Float#MIN_EXPONENT} -1.
 * </ul>
 * @param f a {@code float} value
 * @return the unbiased exponent of the argument
 * @since 1.6
 */
public static int getExponent(float f) {
    /*
     * Bitwise convert f to integer, mask out exponent bits, shift
     * to the right and then subtract out float's bias adjust to
     * get true exponent value
     */
    return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
            (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}
 
Example 16
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 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: Math.java    From Java8CN with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the unbiased exponent used in the representation of a
 * {@code float}.  Special cases:
 *
 * <ul>
 * <li>If the argument is NaN or infinite, then the result is
 * {@link Float#MAX_EXPONENT} + 1.
 * <li>If the argument is zero or subnormal, then the result is
 * {@link Float#MIN_EXPONENT} -1.
 * </ul>
 * @param f a {@code float} value
 * @return the unbiased exponent of the argument
 * @since 1.6
 */
public static int getExponent(float f) {
    /*
     * Bitwise convert f to integer, mask out exponent bits, shift
     * to the right and then subtract out float's bias adjust to
     * get true exponent value
     */
    return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
            (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}
 
Example 19
Source File: Float.java    From jdk1.8-source-analysis 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 20
Source File: Math.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the unbiased exponent used in the representation of a
 * {@code float}.  Special cases:
 *
 * <ul>
 * <li>If the argument is NaN or infinite, then the result is
 * {@link Float#MAX_EXPONENT} + 1.
 * <li>If the argument is zero or subnormal, then the result is
 * {@link Float#MIN_EXPONENT} -1.
 * </ul>
 * @param f a {@code float} value
 * @return the unbiased exponent of the argument
 * @since 1.6
 */
public static int getExponent(float f) {
    /*
     * Bitwise convert f to integer, mask out exponent bits, shift
     * to the right and then subtract out float's bias adjust to
     * get true exponent value
     */
    return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
            (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}