Java Code Examples for sun.misc.DoubleConsts#MIN_EXPONENT

The following examples show how to use sun.misc.DoubleConsts#MIN_EXPONENT . 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: FpUtils.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code double}; 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 d floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
public static int ilogb(double d) {
    int exponent = getExponent(d);

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

    case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
        if(d == 0.0) {
            return -(1<<28);        // -(2^28)
        }
        else {
            long transducer = Double.doubleToRawLongBits(d);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when d'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 &= DoubleConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0L);

            // 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 <
                   (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < DoubleConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= DoubleConsts.MIN_EXPONENT &&
                exponent <= DoubleConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 2
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 double}; 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 d floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
public static int ilogb(double d) {
    int exponent = getExponent(d);

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

    case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
        if(d == 0.0) {
            return -(1<<28);        // -(2^28)
        }
        else {
            long transducer = Double.doubleToRawLongBits(d);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when d'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 &= DoubleConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0L);

            // 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 <
                   (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < DoubleConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= DoubleConsts.MIN_EXPONENT &&
                exponent <= DoubleConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 3
Source File: ToHexString.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
static String hexLongStringtoHexDoubleString(String transString) {
    transString = transString.toLowerCase();

    String zeros = "";
    StringBuffer result = new StringBuffer(24);

    for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
    transString = zeros + transString;

    // assert transString.length == 16;

        char topChar;
        // Extract sign
        if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
            result.append("-");
            // clear sign bit
            transString =
                Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
                transString.substring(1,16);
        }

        // check for NaN and infinity
        String signifString = transString.substring(3,16);

        if( transString.substring(0,3).equals("7ff") ) {
            if(signifString.equals("0000000000000")) {
                result.append("Infinity");
            }
            else
                result.append("NaN");
        }
        else { // finite value
            // Extract exponent
            int exponent = Integer.parseInt(transString.substring(0,3), 16) -
                DoubleConsts.EXP_BIAS;
            result.append("0x");

            if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
                if(signifString.equals("0000000000000")) {
                    result.append("0.0p0");
                }
                else {
                    result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                                  "p-1022");
                }
            }
            else {  // normal value
                result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                              "p" + exponent);
            }
        }
        return result.toString();
}
 
Example 4
Source File: FpUtils.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code double}; 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 d floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
public static int ilogb(double d) {
    int exponent = getExponent(d);

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

    case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
        if(d == 0.0) {
            return -(1<<28);        // -(2^28)
        }
        else {
            long transducer = Double.doubleToRawLongBits(d);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when d'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 &= DoubleConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0L);

            // 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 <
                   (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < DoubleConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= DoubleConsts.MIN_EXPONENT &&
                exponent <= DoubleConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 5
Source File: ToHexString.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
static String hexLongStringtoHexDoubleString(String transString) {
    transString = transString.toLowerCase();

    String zeros = "";
    StringBuffer result = new StringBuffer(24);

    for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
    transString = zeros + transString;

    // assert transString.length == 16;

        char topChar;
        // Extract sign
        if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
            result.append("-");
            // clear sign bit
            transString =
                Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
                transString.substring(1,16);
        }

        // check for NaN and infinity
        String signifString = transString.substring(3,16);

        if( transString.substring(0,3).equals("7ff") ) {
            if(signifString.equals("0000000000000")) {
                result.append("Infinity");
            }
            else
                result.append("NaN");
        }
        else { // finite value
            // Extract exponent
            int exponent = Integer.parseInt(transString.substring(0,3), 16) -
                DoubleConsts.EXP_BIAS;
            result.append("0x");

            if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
                if(signifString.equals("0000000000000")) {
                    result.append("0.0p0");
                }
                else {
                    result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                                  "p-1022");
                }
            }
            else {  // normal value
                result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                              "p" + exponent);
            }
        }
        return result.toString();
}
 
Example 6
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 double}; 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 d floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
public static int ilogb(double d) {
    int exponent = getExponent(d);

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

    case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
        if(d == 0.0) {
            return -(1<<28);        // -(2^28)
        }
        else {
            long transducer = Double.doubleToRawLongBits(d);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when d'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 &= DoubleConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0L);

            // 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 <
                   (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < DoubleConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= DoubleConsts.MIN_EXPONENT &&
                exponent <= DoubleConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 7
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 double}; 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 d floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
public static int ilogb(double d) {
    int exponent = getExponent(d);

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

    case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
        if(d == 0.0) {
            return -(1<<28);        // -(2^28)
        }
        else {
            long transducer = Double.doubleToRawLongBits(d);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when d'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 &= DoubleConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0L);

            // 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 <
                   (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < DoubleConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= DoubleConsts.MIN_EXPONENT &&
                exponent <= DoubleConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 8
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 double}; 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 d floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
public static int ilogb(double d) {
    int exponent = getExponent(d);

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

    case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
        if(d == 0.0) {
            return -(1<<28);        // -(2^28)
        }
        else {
            long transducer = Double.doubleToRawLongBits(d);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when d'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 &= DoubleConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0L);

            // 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 <
                   (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < DoubleConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= DoubleConsts.MIN_EXPONENT &&
                exponent <= DoubleConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 9
Source File: FpUtils.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a <code>double</code>; 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 d floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
public static int ilogb(double d) {
    int exponent = getExponent(d);

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

    case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
        if(d == 0.0) {
            return -(1<<28);        // -(2^28)
        }
        else {
            long transducer = Double.doubleToRawLongBits(d);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when d'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 &= DoubleConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0L);

            // 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 <
                   (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < DoubleConsts.MIN_EXPONENT);
            return exponent;
        }
    // break;

    default:
        assert( exponent >= DoubleConsts.MIN_EXPONENT &&
                exponent <= DoubleConsts.MAX_EXPONENT);
        return exponent;
    // break;
    }
}
 
Example 10
Source File: ToHexString.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
static String hexLongStringtoHexDoubleString(String transString) {
    transString = transString.toLowerCase();

    String zeros = "";
    StringBuffer result = new StringBuffer(24);

    for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
    transString = zeros + transString;

    // assert transString.length == 16;

        char topChar;
        // Extract sign
        if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
            result.append("-");
            // clear sign bit
            transString =
                Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
                transString.substring(1,16);
        }

        // check for NaN and infinity
        String signifString = transString.substring(3,16);

        if( transString.substring(0,3).equals("7ff") ) {
            if(signifString.equals("0000000000000")) {
                result.append("Infinity");
            }
            else
                result.append("NaN");
        }
        else { // finite value
            // Extract exponent
            int exponent = Integer.parseInt(transString.substring(0,3), 16) -
                DoubleConsts.EXP_BIAS;
            result.append("0x");

            if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
                if(signifString.equals("0000000000000")) {
                    result.append("0.0p0");
                }
                else {
                    result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                                  "p-1022");
                }
            }
            else {  // normal value
                result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                              "p" + exponent);
            }
        }
        return result.toString();
}
 
Example 11
Source File: FpUtils.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code double}; 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 d floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
public static int ilogb(double d) {
    int exponent = getExponent(d);

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

    case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
        if(d == 0.0) {
            return -(1<<28);        // -(2^28)
        }
        else {
            long transducer = Double.doubleToRawLongBits(d);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when d'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 &= DoubleConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0L);

            // 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 <
                   (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < DoubleConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= DoubleConsts.MIN_EXPONENT &&
                exponent <= DoubleConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 12
Source File: ToHexString.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
static String hexLongStringtoHexDoubleString(String transString) {
    transString = transString.toLowerCase();

    String zeros = "";
    StringBuffer result = new StringBuffer(24);

    for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
    transString = zeros + transString;

    // assert transString.length == 16;

        char topChar;
        // Extract sign
        if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
            result.append("-");
            // clear sign bit
            transString =
                Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
                transString.substring(1,16);
        }

        // check for NaN and infinity
        String signifString = transString.substring(3,16);

        if( transString.substring(0,3).equals("7ff") ) {
            if(signifString.equals("0000000000000")) {
                result.append("Infinity");
            }
            else
                result.append("NaN");
        }
        else { // finite value
            // Extract exponent
            int exponent = Integer.parseInt(transString.substring(0,3), 16) -
                DoubleConsts.EXP_BIAS;
            result.append("0x");

            if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
                if(signifString.equals("0000000000000")) {
                    result.append("0.0p0");
                }
                else {
                    result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                                  "p-1022");
                }
            }
            else {  // normal value
                result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                              "p" + exponent);
            }
        }
        return result.toString();
}
 
Example 13
Source File: FpUtils.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code double}; 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 d floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
public static int ilogb(double d) {
    int exponent = getExponent(d);

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

    case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
        if(d == 0.0) {
            return -(1<<28);        // -(2^28)
        }
        else {
            long transducer = Double.doubleToRawLongBits(d);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when d'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 &= DoubleConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0L);

            // 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 <
                   (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < DoubleConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= DoubleConsts.MIN_EXPONENT &&
                exponent <= DoubleConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 14
Source File: ToHexString.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
static String hexLongStringtoHexDoubleString(String transString) {
    transString = transString.toLowerCase();

    String zeros = "";
    StringBuffer result = new StringBuffer(24);

    for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
    transString = zeros + transString;

    // assert transString.length == 16;

        char topChar;
        // Extract sign
        if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
            result.append("-");
            // clear sign bit
            transString =
                Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
                transString.substring(1,16);
        }

        // check for NaN and infinity
        String signifString = transString.substring(3,16);

        if( transString.substring(0,3).equals("7ff") ) {
            if(signifString.equals("0000000000000")) {
                result.append("Infinity");
            }
            else
                result.append("NaN");
        }
        else { // finite value
            // Extract exponent
            int exponent = Integer.parseInt(transString.substring(0,3), 16) -
                DoubleConsts.EXP_BIAS;
            result.append("0x");

            if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
                if(signifString.equals("0000000000000")) {
                    result.append("0.0p0");
                }
                else {
                    result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                                  "p-1022");
                }
            }
            else {  // normal value
                result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                              "p" + exponent);
            }
        }
        return result.toString();
}
 
Example 15
Source File: ToHexString.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
static String hexLongStringtoHexDoubleString(String transString) {
    transString = transString.toLowerCase();

    String zeros = "";
    StringBuffer result = new StringBuffer(24);

    for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
    transString = zeros + transString;

    // assert transString.length == 16;

        char topChar;
        // Extract sign
        if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
            result.append("-");
            // clear sign bit
            transString =
                Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
                transString.substring(1,16);
        }

        // check for NaN and infinity
        String signifString = transString.substring(3,16);

        if( transString.substring(0,3).equals("7ff") ) {
            if(signifString.equals("0000000000000")) {
                result.append("Infinity");
            }
            else
                result.append("NaN");
        }
        else { // finite value
            // Extract exponent
            int exponent = Integer.parseInt(transString.substring(0,3), 16) -
                DoubleConsts.EXP_BIAS;
            result.append("0x");

            if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
                if(signifString.equals("0000000000000")) {
                    result.append("0.0p0");
                }
                else {
                    result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                                  "p-1022");
                }
            }
            else {  // normal value
                result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                              "p" + exponent);
            }
        }
        return result.toString();
}
 
Example 16
Source File: FpUtils.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code double}; 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 d floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
public static int ilogb(double d) {
    int exponent = getExponent(d);

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

    case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
        if(d == 0.0) {
            return -(1<<28);        // -(2^28)
        }
        else {
            long transducer = Double.doubleToRawLongBits(d);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when d'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 &= DoubleConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0L);

            // 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 <
                   (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < DoubleConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= DoubleConsts.MIN_EXPONENT &&
                exponent <= DoubleConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 17
Source File: FpUtils.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code double}; 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 d floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
public static int ilogb(double d) {
    int exponent = getExponent(d);

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

    case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
        if(d == 0.0) {
            return -(1<<28);        // -(2^28)
        }
        else {
            long transducer = Double.doubleToRawLongBits(d);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when d'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 &= DoubleConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0L);

            // 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 <
                   (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < DoubleConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= DoubleConsts.MIN_EXPONENT &&
                exponent <= DoubleConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 18
Source File: ToHexString.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
static String hexLongStringtoHexDoubleString(String transString) {
    transString = transString.toLowerCase();

    String zeros = "";
    StringBuffer result = new StringBuffer(24);

    for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
    transString = zeros + transString;

    // assert transString.length == 16;

        char topChar;
        // Extract sign
        if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
            result.append("-");
            // clear sign bit
            transString =
                Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
                transString.substring(1,16);
        }

        // check for NaN and infinity
        String signifString = transString.substring(3,16);

        if( transString.substring(0,3).equals("7ff") ) {
            if(signifString.equals("0000000000000")) {
                result.append("Infinity");
            }
            else
                result.append("NaN");
        }
        else { // finite value
            // Extract exponent
            int exponent = Integer.parseInt(transString.substring(0,3), 16) -
                DoubleConsts.EXP_BIAS;
            result.append("0x");

            if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
                if(signifString.equals("0000000000000")) {
                    result.append("0.0p0");
                }
                else {
                    result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                                  "p-1022");
                }
            }
            else {  // normal value
                result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                              "p" + exponent);
            }
        }
        return result.toString();
}
 
Example 19
Source File: ToHexString.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
static String hexLongStringtoHexDoubleString(String transString) {
    transString = transString.toLowerCase();

    String zeros = "";
    StringBuffer result = new StringBuffer(24);

    for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
    transString = zeros + transString;

    // assert transString.length == 16;

        char topChar;
        // Extract sign
        if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
            result.append("-");
            // clear sign bit
            transString =
                Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
                transString.substring(1,16);
        }

        // check for NaN and infinity
        String signifString = transString.substring(3,16);

        if( transString.substring(0,3).equals("7ff") ) {
            if(signifString.equals("0000000000000")) {
                result.append("Infinity");
            }
            else
                result.append("NaN");
        }
        else { // finite value
            // Extract exponent
            int exponent = Integer.parseInt(transString.substring(0,3), 16) -
                DoubleConsts.EXP_BIAS;
            result.append("0x");

            if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
                if(signifString.equals("0000000000000")) {
                    result.append("0.0p0");
                }
                else {
                    result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                                  "p-1022");
                }
            }
            else {  // normal value
                result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                              "p" + exponent);
            }
        }
        return result.toString();
}
 
Example 20
Source File: ToHexString.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
static String hexLongStringtoHexDoubleString(String transString) {
    transString = transString.toLowerCase();

    String zeros = "";
    StringBuffer result = new StringBuffer(24);

    for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
    transString = zeros + transString;

    // assert transString.length == 16;

        char topChar;
        // Extract sign
        if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
            result.append("-");
            // clear sign bit
            transString =
                Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
                transString.substring(1,16);
        }

        // check for NaN and infinity
        String signifString = transString.substring(3,16);

        if( transString.substring(0,3).equals("7ff") ) {
            if(signifString.equals("0000000000000")) {
                result.append("Infinity");
            }
            else
                result.append("NaN");
        }
        else { // finite value
            // Extract exponent
            int exponent = Integer.parseInt(transString.substring(0,3), 16) -
                DoubleConsts.EXP_BIAS;
            result.append("0x");

            if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
                if(signifString.equals("0000000000000")) {
                    result.append("0.0p0");
                }
                else {
                    result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                                  "p-1022");
                }
            }
            else {  // normal value
                result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                              "p" + exponent);
            }
        }
        return result.toString();
}