Example usage for java.lang Float floatToRawIntBits

List of usage examples for java.lang Float floatToRawIntBits

Introduction

In this page you can find the example usage for java.lang Float floatToRawIntBits.

Prototype

@HotSpotIntrinsicCandidate
public static native int floatToRawIntBits(float value);

Source Link

Document

Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout, preserving Not-a-Number (NaN) values.

Usage

From source file:Main.java

/**
 * Returns the floating-point value adjacent to <code>f</code> in
 * the direction of negative infinity.  This method is
 * semantically equivalent to <code>nextAfter(f,
 * Float.NEGATIVE_INFINITY)</code>; however, a
 * <code>nextDown</code> implementation may run faster than its
 * equivalent <code>nextAfter</code> call.
 *
 * <p>Special Cases:/*from   w w w  . ja va 2s  .c  om*/
 * <ul>
 * <li> If the argument is NaN, the result is NaN.
 *
 * <li> If the argument is negative infinity, the result is
 * negative infinity.
 *
 * <li> If the argument is zero, the result is
 * <code>-Float.MIN_VALUE</code>
 *
 * </ul>
 *
 * @param f  starting floating-point value
 * @return The adjacent floating-point value closer to negative
 * infinity.
 * @author Joseph D. Darcy
 */
public static double nextDown(float f) {
    if (isNaN(f) || f == Float.NEGATIVE_INFINITY)
        return f;
    else {
        if (f == 0.0f)
            return -Float.MIN_VALUE;
        else
            return Float.intBitsToFloat(Float.floatToRawIntBits(f) + ((f > 0.0f) ? -1 : +1));
    }
}

From source file:altermarkive.uploader.Report.java

private static JSONObject reportFloat(float value) throws JSONException {
    JSONObject hifi = new JSONObject();
    long bits = Float.floatToRawIntBits(value);
    hifi.put("sign", bits >> 31);
    bits &= 0x7FFFFFFF;//from  w  ww .j  a  v a 2  s.c om
    hifi.put("exponent", bits >> 23);
    bits &= 0x007FFFFF;
    hifi.put("mantissa", bits);
    return hifi;
}

From source file:Main.java

/**
 * Returns the first floating-point argument with the sign of the
 * second floating-point argument.  Note that unlike the {@link
 * FpUtils#copySign(float, float) copySign} method, this method
 * does not require NaN <code>sign</code> arguments to be treated
 * as positive values; implementations are permitted to treat some
 * NaN arguments as positive and other NaN arguments as negative
 * to allow greater performance.// ww  w.ja  va  2s.co  m
 *
 * @param magnitude  the parameter providing the magnitude of the result
 * @param sign   the parameter providing the sign of the result
 * @return a value with the magnitude of <code>magnitude</code>
 * and the sign of <code>sign</code>.
 * @author Joseph D. Darcy
 */
public static float rawCopySign(float magnitude, float sign) {
    return Float.intBitsToFloat((Float.floatToRawIntBits(sign) & (FloatConsts.SIGN_BIT_MASK))
            | (Float.floatToRawIntBits(magnitude) & (FloatConsts.EXP_BIT_MASK | FloatConsts.SIGNIF_BIT_MASK)));
}

From source file:org.briljantframework.data.Is.java

/**
 * Check if value is NA//w  ww  .  ja va 2  s .  c  o m
 *
 * @param value the value
 * @return true if value is NA
 */
public static boolean NA(float value) {
    return Float.isNaN(value) && (Float.floatToRawIntBits(value) & Na.FLOAT_NA_MASK) == Na.FLOAT_NA_RES;
}

From source file:resources.common.MathUtilities.java

public float fastInverseSqrt(float x) {
    float xHalf = 0.5F * x;
    int temp = Float.floatToRawIntBits(x);
    temp = 0x5F3759DF - (temp >> 1); // magic
    float newX = Float.intBitsToFloat(temp);
    newX = newX * (1.5F - xHalf * newX * newX);
    return newX;//from  w ww.j  a va 2  s  .  co  m
}

From source file:Main.java

/**
 * Returns unbiased exponent of a <code>float</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).//from   ww  w  .  ja v a2  s  . co m
 * <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
        // break;

    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;
        }
        // break;

    default:
        assert (exponent >= FloatConsts.MIN_EXPONENT && exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    // break;
    }
}

From source file:com.dal.vv.type.AbstractValue.java

@Override
public Value load(float data) {
    return load(Float.floatToRawIntBits(data));
}

From source file:net.minecraftforge.client.model.pipeline.LightUtil.java

public static void pack(float[] from, int[] to, VertexFormat formatTo, int v, int e) {
    VertexFormatElement element = formatTo.getElement(e);
    int vertexStart = v * formatTo.getNextOffset() + formatTo.getOffset(e);
    int count = element.getElementCount();
    VertexFormatElement.EnumType type = element.getType();
    int size = type.getSize();
    int mask = (256 << (8 * (size - 1))) - 1;
    for (int i = 0; i < 4; i++) {
        if (i < count) {
            int pos = vertexStart + size * i;
            int index = pos >> 2;
            int offset = pos & 3;
            int bits = 0;
            float f = i < from.length ? from[i] : 0;
            if (type == VertexFormatElement.EnumType.FLOAT) {
                bits = Float.floatToRawIntBits(f);
            } else if (type == VertexFormatElement.EnumType.UBYTE || type == VertexFormatElement.EnumType.USHORT
                    || type == VertexFormatElement.EnumType.UINT) {
                bits = (int) (f * mask);
            } else {
                bits = (int) (f * mask / 2);
            }//from w  w w . ja  va2  s.  c  om
            to[index] &= ~(mask << (offset * 8));
            to[index] |= (((bits & mask) << (offset * 8)));
            // TODO handle overflow into to[index + 1]
        }
    }
}

From source file:Main.java

/**
 * Returns the floating-point number adjacent to the first
 * argument in the direction of the second argument.  If both
 * arguments compare as equal, the second argument is returned.
 *
 * <p>/*  w w  w. ja  v  a 2  s  . co  m*/
 * Special cases:
 * <ul>
 * <li> If either argument is a NaN, then NaN is returned.
 *
 * <li> If both arguments are signed zeros, a <code>float</code>
 * zero with the same sign as <code>direction</code> is returned
 * (as implied by the requirement of returning the second argument
 * if the arguments compare as equal).
 *
 * <li> If <code>start</code> is
 * &plusmn;<code>Float.MIN_VALUE</code> and <code>direction</code>
 * has a value such that the result should have a smaller
 * magnitude, then a zero with the same sign as <code>start</code>
 * is returned.
 *
 * <li> If <code>start</code> is infinite and
 * <code>direction</code> has a value such that the result should
 * have a smaller magnitude, <code>Float.MAX_VALUE</code> with the
 * same sign as <code>start</code> is returned.
 *
 * <li> If <code>start</code> is equal to &plusmn;
 * <code>Float.MAX_VALUE</code> and <code>direction</code> has a
 * value such that the result should have a larger magnitude, an
 * infinity with same sign as <code>start</code> is returned.
 * </ul>
 *
 * @param start     starting floating-point value
 * @param direction value indicating which of
 * <code>start</code>'s neighbors or <code>start</code> should
 * be returned
 * @return The floating-point number adjacent to <code>start</code> in the
 * direction of <code>direction</code>.
 * @author Joseph D. Darcy
 */
public static float nextAfter(float start, double direction) {
    /*
     * The cases:
     *
     * nextAfter(+infinity, 0)  == MAX_VALUE
     * nextAfter(+infinity, +infinity)  == +infinity
     * nextAfter(-infinity, 0)  == -MAX_VALUE
     * nextAfter(-infinity, -infinity)  == -infinity
     *
     * are naturally handled without any additional testing
     */

    // First check for NaN values
    if (isNaN(start) || isNaN(direction)) {
        // return a NaN derived from the input NaN(s)
        return start + (float) direction;
    } else if (start == direction) {
        return (float) direction;
    } else { // start > direction or start < direction
        // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
        // then bitwise convert start to integer.
        int transducer = Float.floatToRawIntBits(start + 0.0f);

        /*
         * IEEE 754 floating-point numbers are lexicographically
         * ordered if treated as signed- magnitude integers .
         * Since Java's integers are two's complement,
         * incrementing" the two's complement representation of a
         * logically negative floating-point value *decrements*
         * the signed-magnitude representation. Therefore, when
         * the integer representation of a floating-point values
         * is less than zero, the adjustment to the representation
         * is in the opposite direction than would be expected at
         * first.
         */
        if (direction > start) {// Calculate next greater value
            transducer = transducer + (transducer >= 0 ? 1 : -1);
        } else { // Calculate next lesser value
            assert direction < start;
            if (transducer > 0)
                --transducer;
            else if (transducer < 0)
                ++transducer;
            /*
             * transducer==0, the result is -MIN_VALUE
             *
             * The transition from zero (implicitly
             * positive) to the smallest negative
             * signed magnitude value must be done
             * explicitly.
             */
            else
                transducer = FloatConsts.SIGN_BIT_MASK | 1;
        }

        return Float.intBitsToFloat(transducer);
    }
}

From source file:TypeConversionHelper.java

/**
 * Convert an instance of our value class into a byte[].
 *
 * @param value Object to be converted/*  w w w  .  ja  v  a 2 s  .  c  o m*/
 *
 * @return converted byte array
 */
public static byte[] getByteArrayFromFloatArray(Object value) {
    if (value == null) {
        return null;
    }

    float[] a = (float[]) value;
    int n = a.length;
    byte[] buf = new byte[n * 4];
    int i = 0;
    int j = 0;

    for (; i < n;) {
        int x = Float.floatToRawIntBits(a[i++]);
        buf[j++] = (byte) ((x >>> 24) & 0xFF);
        buf[j++] = (byte) ((x >>> 16) & 0xFF);
        buf[j++] = (byte) ((x >>> 8) & 0xFF);
        buf[j++] = (byte) (x & 0xFF);
    }

    return buf;
}