Example usage for java.lang Float floatToIntBits

List of usage examples for java.lang Float floatToIntBits

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static int floatToIntBits(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.

Usage

From source file:HashCodeAssist.java

/**
 * <p>/*  w w  w. ja  v  a  2s . c  o  m*/
 * Append a <code>hashCode</code> for a <code>float</code>.
 * </p>
 * 
 * @param value
 *            the float to add to the <code>hashCode</code>
 * @return this
 */
public HashCodeAssist append(float value) {
    iTotal = iTotal * iConstant + Float.floatToIntBits(value);
    return this;
}

From source file:com.ebay.nest.io.sede.binarysortable.BinarySortableSerDe.java

static void serialize(OutputByteBuffer buffer, Object o, ObjectInspector oi, boolean invert)
        throws SerDeException {
    // Is this field a null?
    if (o == null) {
        buffer.write((byte) 0, invert);
        return;//w w w.jav a  2s.co m
    }
    // This field is not a null.
    buffer.write((byte) 1, invert);

    switch (oi.getCategory()) {
    case PRIMITIVE: {
        PrimitiveObjectInspector poi = (PrimitiveObjectInspector) oi;
        switch (poi.getPrimitiveCategory()) {
        case VOID: {
            return;
        }
        case BOOLEAN: {
            boolean v = ((BooleanObjectInspector) poi).get(o);
            buffer.write((byte) (v ? 2 : 1), invert);
            return;
        }
        case BYTE: {
            ByteObjectInspector boi = (ByteObjectInspector) poi;
            byte v = boi.get(o);
            buffer.write((byte) (v ^ 0x80), invert);
            return;
        }
        case SHORT: {
            ShortObjectInspector spoi = (ShortObjectInspector) poi;
            short v = spoi.get(o);
            buffer.write((byte) ((v >> 8) ^ 0x80), invert);
            buffer.write((byte) v, invert);
            return;
        }
        case INT: {
            IntObjectInspector ioi = (IntObjectInspector) poi;
            int v = ioi.get(o);
            serializeInt(buffer, v, invert);
            return;
        }
        case LONG: {
            LongObjectInspector loi = (LongObjectInspector) poi;
            long v = loi.get(o);
            buffer.write((byte) ((v >> 56) ^ 0x80), invert);
            buffer.write((byte) (v >> 48), invert);
            buffer.write((byte) (v >> 40), invert);
            buffer.write((byte) (v >> 32), invert);
            buffer.write((byte) (v >> 24), invert);
            buffer.write((byte) (v >> 16), invert);
            buffer.write((byte) (v >> 8), invert);
            buffer.write((byte) v, invert);
            return;
        }
        case FLOAT: {
            FloatObjectInspector foi = (FloatObjectInspector) poi;
            int v = Float.floatToIntBits(foi.get(o));
            if ((v & (1 << 31)) != 0) {
                // negative number, flip all bits
                v = ~v;
            } else {
                // positive number, flip the first bit
                v = v ^ (1 << 31);
            }
            buffer.write((byte) (v >> 24), invert);
            buffer.write((byte) (v >> 16), invert);
            buffer.write((byte) (v >> 8), invert);
            buffer.write((byte) v, invert);
            return;
        }
        case DOUBLE: {
            DoubleObjectInspector doi = (DoubleObjectInspector) poi;
            long v = Double.doubleToLongBits(doi.get(o));
            if ((v & (1L << 63)) != 0) {
                // negative number, flip all bits
                v = ~v;
            } else {
                // positive number, flip the first bit
                v = v ^ (1L << 63);
            }
            buffer.write((byte) (v >> 56), invert);
            buffer.write((byte) (v >> 48), invert);
            buffer.write((byte) (v >> 40), invert);
            buffer.write((byte) (v >> 32), invert);
            buffer.write((byte) (v >> 24), invert);
            buffer.write((byte) (v >> 16), invert);
            buffer.write((byte) (v >> 8), invert);
            buffer.write((byte) v, invert);
            return;
        }
        case STRING: {
            StringObjectInspector soi = (StringObjectInspector) poi;
            Text t = soi.getPrimitiveWritableObject(o);
            serializeBytes(buffer, t.getBytes(), t.getLength(), invert);
            return;
        }

        case VARCHAR: {
            HiveVarcharObjectInspector hcoi = (HiveVarcharObjectInspector) poi;
            HiveVarcharWritable hc = hcoi.getPrimitiveWritableObject(o);
            // use varchar's text field directly
            Text t = hc.getTextValue();
            serializeBytes(buffer, t.getBytes(), t.getLength(), invert);
            return;
        }

        case BINARY: {
            BinaryObjectInspector baoi = (BinaryObjectInspector) poi;
            BytesWritable ba = baoi.getPrimitiveWritableObject(o);
            byte[] toSer = new byte[ba.getLength()];
            System.arraycopy(ba.getBytes(), 0, toSer, 0, ba.getLength());
            serializeBytes(buffer, toSer, ba.getLength(), invert);
            return;
        }
        case DATE: {
            DateObjectInspector doi = (DateObjectInspector) poi;
            int v = doi.getPrimitiveWritableObject(o).getDays();
            serializeInt(buffer, v, invert);
            return;
        }
        case TIMESTAMP: {
            TimestampObjectInspector toi = (TimestampObjectInspector) poi;
            TimestampWritable t = toi.getPrimitiveWritableObject(o);
            byte[] data = t.getBinarySortable();
            for (int i = 0; i < data.length; i++) {
                buffer.write(data[i], invert);
            }
            return;
        }
        case DECIMAL: {
            // decimals are encoded in three pieces:
            // sign: 1, 2 or 3 for smaller, equal or larger than 0 respectively
            // factor: Number that indicates the amount of digits you have to move
            // the decimal point left or right until the resulting number is smaller
            // than zero but has something other than 0 as the first digit.
            // digits: which is a string of all the digits in the decimal. If the number
            // is negative the binary string will be inverted to get the correct ordering.
            // Example: 0.00123
            // Sign is 3 (bigger than 0)
            // Factor is -2 (move decimal point 2 positions right)
            // Digits are: 123

            HiveDecimalObjectInspector boi = (HiveDecimalObjectInspector) poi;
            HiveDecimal dec = boi.getPrimitiveJavaObject(o);

            // get the sign of the big decimal
            int sign = dec.compareTo(HiveDecimal.ZERO);

            // we'll encode the absolute value (sign is separate)
            dec = dec.abs();

            // get the scale factor to turn big decimal into a decimal < 1
            int factor = dec.precision() - dec.scale();
            factor = sign == 1 ? factor : -factor;

            // convert the absolute big decimal to string
            dec.scaleByPowerOfTen(Math.abs(dec.scale()));
            String digits = dec.unscaledValue().toString();

            // finally write out the pieces (sign, scale, digits)
            buffer.write((byte) (sign + 1), invert);
            buffer.write((byte) ((factor >> 24) ^ 0x80), invert);
            buffer.write((byte) (factor >> 16), invert);
            buffer.write((byte) (factor >> 8), invert);
            buffer.write((byte) factor, invert);
            serializeBytes(buffer, digits.getBytes(decimalCharSet), digits.length(),
                    sign == -1 ? !invert : invert);
            return;
        }

        default: {
            throw new RuntimeException("Unrecognized type: " + poi.getPrimitiveCategory());
        }
        }
    }
    case LIST: {
        ListObjectInspector loi = (ListObjectInspector) oi;
        ObjectInspector eoi = loi.getListElementObjectInspector();

        // \1 followed by each element
        int size = loi.getListLength(o);
        for (int eid = 0; eid < size; eid++) {
            buffer.write((byte) 1, invert);
            serialize(buffer, loi.getListElement(o, eid), eoi, invert);
        }
        // and \0 to terminate
        buffer.write((byte) 0, invert);
        return;
    }
    case MAP: {
        MapObjectInspector moi = (MapObjectInspector) oi;
        ObjectInspector koi = moi.getMapKeyObjectInspector();
        ObjectInspector voi = moi.getMapValueObjectInspector();

        // \1 followed by each key and then each value
        Map<?, ?> map = moi.getMap(o);
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            buffer.write((byte) 1, invert);
            serialize(buffer, entry.getKey(), koi, invert);
            serialize(buffer, entry.getValue(), voi, invert);
        }
        // and \0 to terminate
        buffer.write((byte) 0, invert);
        return;
    }
    case STRUCT: {
        StructObjectInspector soi = (StructObjectInspector) oi;
        List<? extends StructField> fields = soi.getAllStructFieldRefs();

        for (int i = 0; i < fields.size(); i++) {
            serialize(buffer, soi.getStructFieldData(o, fields.get(i)), fields.get(i).getFieldObjectInspector(),
                    invert);
        }
        return;
    }
    case UNION: {
        UnionObjectInspector uoi = (UnionObjectInspector) oi;
        byte tag = uoi.getTag(o);
        buffer.write(tag, invert);
        serialize(buffer, uoi.getField(o), uoi.getObjectInspectors().get(tag), invert);
        return;
    }
    default: {
        throw new RuntimeException("Unrecognized type: " + oi.getCategory());
    }
    }

}

From source file:kx.c.java

void w(float e) {
    w(Float.floatToIntBits(e));
}

From source file:dk.statsbiblioteket.util.LineReader.java

@Override
public void writeFloat(float v) throws IOException {
    writeInt(Float.floatToIntBits(v));
}

From source file:ml.shifu.shifu.udf.NormalizeUDF.java

public static int fromFloat(float fval) {
    int fbits = Float.floatToIntBits(fval);
    int sign = fbits >>> 16 & 0x8000; // sign only
    int val = (fbits & 0x7fffffff) + 0x1000; // rounded value

    if (val >= 0x47800000) // might be or become NaN/Inf
    { // avoid Inf due to rounding
        if ((fbits & 0x7fffffff) >= 0x47800000) { // is or must become NaN/Inf
            if (val < 0x7f800000) // was value but too large
                return sign | 0x7c00; // make it +/-Inf
            return sign | 0x7c00 | // remains +/-Inf or NaN
                    (fbits & 0x007fffff) >>> 13; // keep NaN (and Inf) bits
        }//w ww  .j  a v  a 2 s . c  o  m
        return sign | 0x7bff; // unrounded not quite Inf
    }
    if (val >= 0x38800000) // remains normalized value
        return sign | val - 0x38000000 >>> 13; // exp - 127 + 15
    if (val < 0x33000000) // too small for subnormal
        return sign; // becomes +/-0
    val = (fbits & 0x7fffffff) >>> 23; // tmp exp for subnormal calc
    return sign | ((fbits & 0x7fffff | 0x800000) // add subnormal bit
            + (0x800000 >>> val - 102) // round depending on cut off
    >>> 126 - val); // div by 2^(1-(exp-127+15)) and >> 13 | exp=0
}

From source file:WeakIdentityMap.java

static int hashCode(float[] a) {
    int hash = 0;
    for (int i = a.length; --i >= 0;) {
        hash = hash * 31 + Float.floatToIntBits(a[i]);
    }//from   w ww.j  a v a2 s  .  c om
    return hash == 0 ? -1 : hash;
}

From source file:IntRange.java

/**
 * <p>Compares two floats for order.</p>
 *
 * <p>This method is more comprehensive than the standard Java greater than,
 * less than and equals operators.</p>
 * <ul>//from  w ww.  j a  va  2s.co m
 *  <li>It returns <code>-1</code> if the first value is less than the second.
 *  <li>It returns <code>+1</code> if the first value is greater than the second.
 *  <li>It returns <code>0</code> if the values are equal.
 * </ul>
 *
 * <p> The ordering is as follows, largest to smallest:
 * <ul>
 * <li>NaN
 * <li>Positive infinity
 * <li>Maximum float
 * <li>Normal positive numbers
 * <li>+0.0
 * <li>-0.0
 * <li>Normal negative numbers
 * <li>Minimum float (<code>-Float.MAX_VALUE</code>)
 * <li>Negative infinity
 * </ul>
 *
 * <p>Comparing <code>NaN</code> with <code>NaN</code> will return
 * <code>0</code>.</p>
 * 
 * @param lhs  the first <code>float</code>
 * @param rhs  the second <code>float</code>
 * @return <code>-1</code> if lhs is less, <code>+1</code> if greater,
 *  <code>0</code> if equal to rhs
 */
public static int compare(float lhs, float rhs) {
    if (lhs < rhs) {
        return -1;
    }
    if (lhs > rhs) {
        return +1;
    }
    //Need to compare bits to handle 0.0 == -0.0 being true
    // compare should put -0.0 < +0.0
    // Two NaNs are also == for compare purposes
    // where NaN == NaN is false
    int lhsBits = Float.floatToIntBits(lhs);
    int rhsBits = Float.floatToIntBits(rhs);
    if (lhsBits == rhsBits) {
        return 0;
    }
    //Something exotic! A comparison to NaN or 0.0 vs -0.0
    //Fortunately NaN's int is > than everything else
    //Also negzeros bits < poszero
    //NAN: 2143289344
    //MAX: 2139095039
    //NEGZERO: -2147483648
    if (lhsBits < rhsBits) {
        return -1;
    } else {
        return +1;
    }
}

From source file:org.apache.hadoop.hive.serde2.binarysortable.BinarySortableSerDe.java

public static void serializeFloat(ByteStream.Output buffer, float vf, boolean invert) {
    int v = Float.floatToIntBits(vf);
    if ((v & (1 << 31)) != 0) {
        // negative number, flip all bits
        v = ~v;//from   w  ww  .  j  av a 2 s  .  c o  m
    } else {
        // positive number, flip the first bit
        v = v ^ (1 << 31);
    }
    writeByte(buffer, (byte) (v >> 24), invert);
    writeByte(buffer, (byte) (v >> 16), invert);
    writeByte(buffer, (byte) (v >> 8), invert);
    writeByte(buffer, (byte) v, invert);
}

From source file:com.continuent.tungsten.common.mysql.MySQLPacket.java

/**
 * Puts a float in the buffer
 */
public void putFloat(float f) {
    putInt32(Float.floatToIntBits(f));
}

From source file:com.datatorrent.lib.appdata.gpo.GPOUtils.java

/**
 * This method serializes the given float to the given byte buffer to the given offset,
 * the method also increments the offset appropriately.
 * @param valf The value to serialize./*w  w  w  .j ava2s. c o m*/
 * @param buffer The byte buffer to serialize to.
 * @param offset The offset in the buffer to serialize to and also to increment appropriately.
 */
public static void serializeFloat(float valf, byte[] buffer, MutableInt offset) {
    int offsetInt = offset.intValue();
    int val = Float.floatToIntBits(valf);

    buffer[0 + offsetInt] = (byte) ((val >> 24) & 0xFF);
    buffer[1 + offsetInt] = (byte) ((val >> 16) & 0xFF);
    buffer[2 + offsetInt] = (byte) ((val >> 8) & 0xFF);
    buffer[3 + offsetInt] = (byte) (val & 0xFF);

    offset.add(Type.FLOAT.getByteSize());
}