Example usage for java.lang Float intBitsToFloat

List of usage examples for java.lang Float intBitsToFloat

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static native float intBitsToFloat(int bits);

Source Link

Document

Returns the float value corresponding to a given bit representation.

Usage

From source file:cross.io.misc.Base64Util.java

/**
 *
 * @param raw//from w ww  . j  a v  a  2s.  c  om
 * @param bigEndian
 * @param length
 * @return
 */
public static float[] byteArrayToFloatArray(final byte[] raw, final boolean bigEndian, final int length) {
    final float[] f = new float[length];
    int i = 0;
    if (bigEndian) {
        for (int iii = 0; iii < raw.length; iii += 4) {
            int ieee754 = 0;
            ieee754 |= ((raw[iii]) & 0xff);
            ieee754 <<= 8;
            ieee754 |= ((raw[iii + 1]) & 0xff);
            ieee754 <<= 8;
            ieee754 |= ((raw[iii + 2]) & 0xff);
            ieee754 <<= 8;
            ieee754 |= ((raw[iii + 3]) & 0xff);
            final float aFloat = Float.intBitsToFloat(ieee754);
            f[i++] = aFloat;
        }
    } else {
        for (int iii = 0; iii < raw.length; iii += 4) {
            int ieee754 = 0;
            ieee754 |= ((raw[iii + 3]) & 0xff);
            ieee754 <<= 8;
            ieee754 |= ((raw[iii + 2]) & 0xff);
            ieee754 <<= 8;
            ieee754 |= ((raw[iii + 1]) & 0xff);
            ieee754 <<= 8;
            ieee754 |= ((raw[iii]) & 0xff);
            final float aFloat = Float.intBitsToFloat(ieee754);
            f[i++] = aFloat;
        }
    }
    return f;
}

From source file:com.tibbo.linkserver.plugin.device.file.item.NumericItem.java

/**
 *
 * @param data//from  w  w  w  .j  a  v a2s. c  o  m
 * @param offset
 * @return
 */
@Override
public Object bytesToValueRealOffset(byte data[], int offset) {
    offset *= 2;
    if (dataType == 2) {
        return Integer.valueOf((data[offset] & 0xff) << 8 | data[offset + 1] & 0xff);
    }
    if (dataType == 3) {
        return Short.valueOf((short) ((data[offset] & 0xff) << 8 | data[offset + 1] & 0xff));
    }
    if (dataType == 16) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 2; i++) {
            sb.append(bcdNibbleToInt(data[offset + i], true));
            sb.append(bcdNibbleToInt(data[offset + i], false));
        }

        return Short.valueOf(Short.parseShort(sb.toString()));
    }
    if (dataType == 4) {
        return Long.valueOf((long) (data[offset] & 0xff) << 24 | (long) (data[offset + 1] & 0xff) << 16
                | (long) (data[offset + 2] & 0xff) << 8 | (long) (data[offset + 3] & 0xff));
    }
    if (dataType == 5) {
        return Integer.valueOf((data[offset] & 0xff) << 24 | (data[offset + 1] & 0xff) << 16
                | (data[offset + 2] & 0xff) << 8 | data[offset + 3] & 0xff);
    }
    if (dataType == 6) {
        return Long.valueOf((long) (data[offset + 2] & 0xff) << 24 | (long) (data[offset + 3] & 0xff) << 16
                | (long) (data[offset] & 0xff) << 8 | (long) (data[offset + 1] & 0xff));
    }
    if (dataType == 7) {
        return Integer.valueOf((data[offset + 2] & 0xff) << 24 | (data[offset + 3] & 0xff) << 16
                | (data[offset] & 0xff) << 8 | data[offset + 1] & 0xff);
    }
    if (dataType == 8) {
        return Float.valueOf(Float.intBitsToFloat((data[offset] & 0xff) << 24 | (data[offset + 1] & 0xff) << 16
                | (data[offset + 2] & 0xff) << 8 | data[offset + 3] & 0xff));
    }
    if (dataType == 9) {
        return Float.valueOf(Float.intBitsToFloat((data[offset + 2] & 0xff) << 24
                | (data[offset + 3] & 0xff) << 16 | (data[offset] & 0xff) << 8 | data[offset + 1] & 0xff));
    }
    if (dataType == 17) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            sb.append(bcdNibbleToInt(data[offset + i], true));
            sb.append(bcdNibbleToInt(data[offset + i], false));
        }

        return Integer.valueOf(Integer.parseInt(sb.toString()));
    }
    if (dataType == 10) {
        byte b9[] = new byte[9];
        System.arraycopy(data, offset, b9, 1, 8);
        return new BigInteger(b9);
    }
    if (dataType == 11) {
        return Long.valueOf((long) (data[offset] & 0xff) << 56 | (long) (data[offset + 1] & 0xff) << 48
                | (long) (data[offset + 2] & 0xff) << 40 | (long) (data[offset + 3] & 0xff) << 32
                | (long) (data[offset + 4] & 0xff) << 24 | (long) (data[offset + 5] & 0xff) << 16
                | (long) (data[offset + 6] & 0xff) << 8 | (long) (data[offset + 7] & 0xff));
    }
    if (dataType == 12) {
        byte b9[] = new byte[9];
        b9[1] = data[offset + 6];
        b9[2] = data[offset + 7];
        b9[3] = data[offset + 4];
        b9[4] = data[offset + 5];
        b9[5] = data[offset + 2];
        b9[6] = data[offset + 3];
        b9[7] = data[offset];
        b9[8] = data[offset + 1];
        return new BigInteger(b9);
    }
    if (dataType == 13) {
        return Long.valueOf((long) (data[offset + 6] & 0xff) << 56 | (long) (data[offset + 7] & 0xff) << 48
                | (long) (data[offset + 4] & 0xff) << 40 | (long) (data[offset + 5] & 0xff) << 32
                | (long) (data[offset + 2] & 0xff) << 24 | (long) (data[offset + 3] & 0xff) << 16
                | (long) (data[offset] & 0xff) << 8 | (long) (data[offset + 1] & 0xff));
    }
    if (dataType == 14) {
        return Double.valueOf(Double
                .longBitsToDouble((long) (data[offset] & 0xff) << 56 | (long) (data[offset + 1] & 0xff) << 48
                        | (long) (data[offset + 2] & 0xff) << 40 | (long) (data[offset + 3] & 0xff) << 32
                        | (long) (data[offset + 4] & 0xff) << 24 | (long) (data[offset + 5] & 0xff) << 16
                        | (long) (data[offset + 6] & 0xff) << 8 | (long) (data[offset + 7] & 0xff)));
    }
    if (dataType == 15) {
        return Double.valueOf(Double.longBitsToDouble(
                (long) (data[offset + 6] & 0xff) << 56 | (long) (data[offset + 7] & 0xff) << 48
                        | (long) (data[offset + 4] & 0xff) << 40 | (long) (data[offset + 5] & 0xff) << 32
                        | (long) (data[offset + 2] & 0xff) << 24 | (long) (data[offset + 3] & 0xff) << 16
                        | (long) (data[offset] & 0xff) << 8 | (long) (data[offset + 1] & 0xff)));
    } else {
        throw new RuntimeException(
                (new StringBuilder()).append("Unsupported data type: ").append(dataType).toString());
    }
}

From source file:Main.java

/**
 * Returns the size of an ulp of the argument.  An ulp of a
 * <code>float</code> value is the positive distance between this
 * floating-point value and the <code>float</code> value next
 * larger in magnitude.  Note that for non-NaN <i>x</i>,
 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
 *
 * <p>Special Cases:/*from w w w. ja  va 2 s  . c o m*/
 * <ul>
 * <li> If the argument is NaN, then the result is NaN.
 * <li> If the argument is positive or negative infinity, then the
 * result is positive infinity.
 * <li> If the argument is positive or negative zero, then the result is
 * <code>Float.MIN_VALUE</code>.
 * <li> If the argument is &plusmn;<code>Float.MAX_VALUE</code>, then
 * the result is equal to 2<sup>104</sup>.
 * </ul>
 *
 * @param f the floating-point value whose ulp is to be returned
 * @return the size of an ulp of the argument
 * @author Joseph D. Darcy
 * @since 1.5
 */
public static float ulp(float f) {
    int exp = getExponent(f);

    switch (exp) {
    case FloatConsts.MAX_EXPONENT + 1: // NaN or infinity
        return Math.abs(f);
    // break;

    case FloatConsts.MIN_EXPONENT - 1: // zero or subnormal
        return FloatConsts.MIN_VALUE;
    // break

    default:
        assert exp <= FloatConsts.MAX_EXPONENT && exp >= FloatConsts.MIN_EXPONENT;

        // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
        exp = exp - (FloatConsts.SIGNIFICAND_WIDTH - 1);
        if (exp >= FloatConsts.MIN_EXPONENT) {
            return powerOfTwoF(exp);
        } else {
            // return a subnormal result; left shift integer
            // representation of FloatConsts.MIN_VALUE appropriate
            // number of positions
            return Float.intBitsToFloat(
                    1 << (exp - (FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH - 1))));
        }
        // break
    }
}

From source file:ArrayByte.java

public float readFloat() throws IndexOutOfBoundsException {
    return Float.intBitsToFloat(readInt());
}

From source file:uk.co.modularaudio.mads.base.stereo_compressor.ui.StereoCompressorMadUiInstance.java

@Override
public void consumeQueueEntry(final StereoCompressorMadInstance instance,
        final IOQueueEvent nextOutgoingEntry) {
    switch (nextOutgoingEntry.command) {
    case StereoCompressorIOQueueBridge.COMMAND_OUT_SIGNAL_IN_METER: {
        final long value = nextOutgoingEntry.value;
        final int chanNum = (int) ((value) & 0xFFFFFFFF);
        final int upper32Bits = (int) ((value >> 32) & 0xFFFFFFFF);
        final float ampValue = Float.intBitsToFloat(upper32Bits);
        if (sourceSignalValueReceiver != null) {
            sourceSignalValueReceiver.receiveMeterReadingLevel(nextOutgoingEntry.frameTime, chanNum, ampValue);
        }//w  w  w .  jav  a  2 s . c  o m
        break;
    }
    case StereoCompressorIOQueueBridge.COMMAND_OUT_SIGNAL_OUT_METER: {
        final long value = nextOutgoingEntry.value;
        final int chanNum = (int) ((value) & 0xFFFFFFFF);
        final int upper32Bits = (int) ((value >> 32) & 0xFFFFFFFF);
        final float ampValue = Float.intBitsToFloat(upper32Bits);
        if (outSignalValueReceiver != null) {
            outSignalValueReceiver.receiveMeterReadingLevel(nextOutgoingEntry.frameTime, chanNum, ampValue);
        }
        break;
    }
    case StereoCompressorIOQueueBridge.COMMAND_OUT_ENV_VALUE: {
        final long value = nextOutgoingEntry.value;
        final int upper32Bits = (int) ((value >> 32) & 0xFFFFFFFF);
        final float ampValue = Float.intBitsToFloat(upper32Bits);
        if (envSignalValueReceiver != null) {
            envSignalValueReceiver.receiveMeterReadingLevel(nextOutgoingEntry.frameTime, 0, ampValue);
        }
        break;
    }
    case StereoCompressorIOQueueBridge.COMMAND_OUT_ATTENUATION: {
        final long value = nextOutgoingEntry.value;
        final int upper32Bits = (int) (value);
        final float ampValue = Float.intBitsToFloat(upper32Bits);
        if (attenuationSignalValueReceiver != null) {
            attenuationSignalValueReceiver.receiveMeterReadingLevel(nextOutgoingEntry.frameTime, 0, ampValue);
        }
        break;
    }
    default: {
        final String msg = "Unknown command receive for UI: " + nextOutgoingEntry.command;
        log.error(msg);
        break;
    }
    }

}

From source file:com.oprisnik.semdroid.app.parser.manifest.AXMLConverter.java

private static String getAttributeValue(AXmlResourceParser parser, int index) {
    int type = parser.getAttributeValueType(index);
    int data = parser.getAttributeValueData(index);
    if (type == TypedValue.TYPE_STRING) {
        return parser.getAttributeValue(index);
    }/*from   www  . j  a  v  a 2s .c  om*/
    if (type == TypedValue.TYPE_ATTRIBUTE) {
        return String.format("?%s%08X", getPackage(data), data);
    }
    if (type == TypedValue.TYPE_REFERENCE) {
        return String.format("@%s%08X", getPackage(data), data);
    }
    if (type == TypedValue.TYPE_FLOAT) {
        return String.valueOf(Float.intBitsToFloat(data));
    }
    if (type == TypedValue.TYPE_INT_HEX) {
        return String.format("0x%08X", data);
    }
    if (type == TypedValue.TYPE_INT_BOOLEAN) {
        return data != 0 ? "true" : "false";
    }
    if (type == TypedValue.TYPE_DIMENSION) {
        return Float.toString(complexToFloat(data)) + DIMENSION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
    }
    if (type == TypedValue.TYPE_FRACTION) {
        return Float.toString(complexToFloat(data)) + FRACTION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
    }
    if (type >= TypedValue.TYPE_FIRST_COLOR_INT && type <= TypedValue.TYPE_LAST_COLOR_INT) {
        return String.format("#%08X", data);
    }
    if (type >= TypedValue.TYPE_FIRST_INT && type <= TypedValue.TYPE_LAST_INT) {
        return String.valueOf(data);
    }
    return String.format("<0x%X, type 0x%02X>", data, type);
}

From source file:org.efaps.webdav4vfs.test.ramvfs.RamFileRandomAccessContent.java

/**
 * {@inheritDoc}/*from w  ww.  jav a2s . c  o m*/
 */
public float readFloat() throws IOException {
    return Float.intBitsToFloat(this.readInt());
}

From source file:cross.io.misc.Base64Util.java

/**
 *
 * @param raw/*from   w w w  . j a  va  2s.  c  om*/
 * @param bigEndian
 * @return
 */
public static List<Float> byteArrayToFloatList(final byte[] raw, final boolean bigEndian) {
    final List<Float> f = new ArrayList<>();
    if (bigEndian) {
        for (int iii = 0; iii < raw.length; iii += 4) {
            int ieee754 = 0;
            ieee754 |= ((raw[iii]) & 0xff);
            ieee754 <<= 8;
            ieee754 |= ((raw[iii + 1]) & 0xff);
            ieee754 <<= 8;
            ieee754 |= ((raw[iii + 2]) & 0xff);
            ieee754 <<= 8;
            ieee754 |= ((raw[iii + 3]) & 0xff);
            final float aFloat = Float.intBitsToFloat(ieee754);
            f.add(aFloat);
        }
    } else {
        for (int iii = 0; iii < raw.length; iii += 4) {
            int ieee754 = 0;
            ieee754 |= ((raw[iii + 3]) & 0xff);
            ieee754 <<= 8;
            ieee754 |= ((raw[iii + 2]) & 0xff);
            ieee754 <<= 8;
            ieee754 |= ((raw[iii + 1]) & 0xff);
            ieee754 <<= 8;
            ieee754 |= ((raw[iii]) & 0xff);
            final float aFloat = Float.intBitsToFloat(ieee754);
            f.add(aFloat);
        }
    }
    return f;
}

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

public static void unpack(int[] from, float[] to, VertexFormat formatFrom, int v, int e) {
    int length = 4 < to.length ? 4 : to.length;
    VertexFormatElement element = formatFrom.getElement(e);
    int vertexStart = v * formatFrom.getNextOffset() + formatFrom.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 < length; i++) {
        if (i < count) {
            int pos = vertexStart + size * i;
            int index = pos >> 2;
            int offset = pos & 3;
            int bits = from[index];
            bits = bits >>> (offset * 8);
            if ((pos + size - 1) / 4 != index) {
                bits |= from[index + 1] << ((4 - offset) * 8);
            }//from   ww w . j av  a2  s . co  m
            bits &= mask;
            if (type == VertexFormatElement.EnumType.FLOAT) {
                to[i] = Float.intBitsToFloat(bits);
            } else if (type == VertexFormatElement.EnumType.UBYTE
                    || type == VertexFormatElement.EnumType.USHORT) {
                to[i] = (float) bits / mask;
            } else if (type == VertexFormatElement.EnumType.UINT) {
                to[i] = (float) ((double) (bits & 0xFFFFFFFFL) / 0xFFFFFFFFL);
            } else if (type == VertexFormatElement.EnumType.BYTE) {
                to[i] = ((float) (byte) bits) / mask * 2;
            } else if (type == VertexFormatElement.EnumType.SHORT) {
                to[i] = ((float) (short) bits) / mask * 2;
            } else if (type == VertexFormatElement.EnumType.INT) {
                to[i] = ((float) (bits & 0xFFFFFFFFL)) / 0xFFFFFFFFL * 2;
            }
        } else {
            to[i] = 0;
        }
    }
}

From source file:com.zju.ccnt.or.binlog.impl.parser.AbstractRowEventParser.java

/**
 * /*from   w  w w  .j  ava2  s. c  o  m*/
 */
protected Row parseRow(XInputStream is, TableMapEvent tme, BitColumn usedColumns) throws IOException {
    //
    int unusedColumnCount = 0;
    final byte[] types = tme.getColumnTypes();
    final Metadata metadata = tme.getColumnMetadata();
    final BitColumn nullColumns = is.readBit(types.length, true);
    final List<Column> columns = new ArrayList<Column>(types.length);
    for (int i = 0; i < types.length; ++i) {
        //
        int length = 0;
        final int meta = metadata.getMetadata(i);
        int type = CodecUtils.toUnsigned(types[i]);
        if (type == MySQLConstants.TYPE_STRING && meta > 256) {
            final int meta0 = meta >> 8;
            final int meta1 = meta & 0xFF;
            if ((meta0 & 0x30) != 0x30) { // a long CHAR() field: see #37426
                type = meta0 | 0x30;
                length = meta1 | (((meta0 & 0x30) ^ 0x30) << 4);
            } else {
                switch (meta0) {
                case MySQLConstants.TYPE_SET:
                case MySQLConstants.TYPE_ENUM:
                case MySQLConstants.TYPE_STRING:
                    type = meta0;
                    length = meta1;
                    break;
                default:
                    throw new NestableRuntimeException("assertion failed, unknown column type: " + type);
                }
            }
        }

        //
        if (!usedColumns.get(i)) {
            unusedColumnCount++;
            continue;
        } else if (nullColumns.get(i - unusedColumnCount)) {
            columns.add(NullColumn.valueOf(type));
            continue;
        }

        //
        switch (type) {
        case MySQLConstants.TYPE_TINY:
            columns.add(TinyColumn.valueOf(is.readInt(1)));
            break;
        case MySQLConstants.TYPE_SHORT:
            columns.add(ShortColumn.valueOf(CodecUtils.toSigned(is.readInt(2), 2)));
            break;
        case MySQLConstants.TYPE_INT24:
            columns.add(Int24Column.valueOf(CodecUtils.toSigned(is.readInt(3), 3)));
            break;
        case MySQLConstants.TYPE_LONG:
            columns.add(LongColumn.valueOf(is.readInt(4)));
            break;
        case MySQLConstants.TYPE_LONGLONG:
            columns.add(LongLongColumn.valueOf(is.readLong(8)));
            break;
        case MySQLConstants.TYPE_FLOAT:
            columns.add(FloatColumn.valueOf(Float.intBitsToFloat(is.readInt(4))));
            break;
        case MySQLConstants.TYPE_DOUBLE:
            columns.add(DoubleColumn.valueOf(Double.longBitsToDouble(is.readLong(8))));
            break;
        case MySQLConstants.TYPE_YEAR:
            columns.add(YearColumn.valueOf(is.readInt(1)));
            break;
        case MySQLConstants.TYPE_DATE:
            columns.add(DateColumn.valueOf(MySQLUtils.toDate(is.readInt(3))));
            break;
        case MySQLConstants.TYPE_TIME:
            columns.add(TimeColumn.valueOf(MySQLUtils.toTime(is.readInt(3))));
            break;
        case MySQLConstants.TYPE_TIMESTAMP:
            columns.add(TimestampColumn.valueOf(MySQLUtils.toTimestamp(is.readInt(4))));
            break;
        case MySQLConstants.TYPE_DATETIME:
            columns.add(DatetimeColumn.valueOf(MySQLUtils.toDatetime(is.readLong(8))));
            break;
        case MySQLConstants.TYPE_ENUM:
            columns.add(EnumColumn.valueOf(is.readInt(length)));
            break;
        case MySQLConstants.TYPE_SET:
            columns.add(SetColumn.valueOf(is.readLong(length)));
            break;
        case MySQLConstants.TYPE_STRING:
            final int stringLength = length < 256 ? is.readInt(1) : is.readInt(2);
            columns.add(StringColumn.valueOf(is.readBytes(stringLength)));
            break;
        case MySQLConstants.TYPE_BIT:
            final int bitLength = (meta >> 8) * 8 + (meta & 0xFF);
            columns.add(BitColumn.valueOf(bitLength, is.readBytes((bitLength + 7) >> 3)));
            break;
        case MySQLConstants.TYPE_NEWDECIMAL:
            final int precision = meta & 0xFF;
            final int scale = meta >> 8;
            final int decimalLength = MySQLUtils.getDecimalBinarySize(precision, scale);
            columns.add(DecimalColumn.valueOf(
                    MySQLUtils.toDecimal(precision, scale, is.readBytes(decimalLength)), precision, scale));
            break;
        case MySQLConstants.TYPE_BLOB:
            final int blobLength = is.readInt(meta);
            columns.add(BlobColumn.valueOf(is.readBytes(blobLength)));
            break;
        case MySQLConstants.TYPE_VARCHAR:
        case MySQLConstants.TYPE_VAR_STRING:
            final int varcharLength = meta < 256 ? is.readInt(1) : is.readInt(2);
            columns.add(StringColumn.valueOf(is.readBytes(varcharLength)));
            break;
        default:
            throw new NestableRuntimeException("assertion failed, unknown column type: " + type);
        }
    }
    return new Row(columns, tme.getDatabaseName().toString(), tme.getTableName().toString());
}