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:CircleOfSquares.java

void ExecuteNextInstruction() {

    int a, b, result, i;
    float fa, fb, fresult;
    Float f;//from www  . j  a v  a  2  s.com
    int operand0, operand1;

    int nextOpCode = methodAreaMemorySection.getAtAddress(pcRegister);

    switch (nextOpCode) {

    case OpCode.BIPUSH:
        operand0 = methodAreaMemorySection.getAtAddress(pcRegister + 1);
        stackMemorySection.setAtAddress(optopRegister, operand0);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, StringTable.operand);
        optopRegister += 4;
        pcRegister += 2;
        break;

    case OpCode.FCONST_0:
        stackMemorySection.setAtAddress(optopRegister, Float.floatToIntBits((float) 0));
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "0");
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.FCONST_2:
        stackMemorySection.setAtAddress(optopRegister, Float.floatToIntBits((float) 2));
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "2");
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.FLOAD_0:
        a = stackMemorySection.getAtAddress(varsRegister);
        stackMemorySection.setAtAddress(optopRegister, a);
        fa = Float.intBitsToFloat(a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Float.toString(fa));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.FMUL:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        fa = Float.intBitsToFloat(a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        optopRegister -= 4;
        b = stackMemorySection.getAtAddress(optopRegister);
        fb = Float.intBitsToFloat(b);
        fresult = fa * fb;
        result = Float.floatToIntBits(fresult);
        stackMemorySection.setAtAddress(optopRegister, result);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Float.toString(fresult));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.FSTORE_0:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        stackMemorySection.setAtAddress(varsRegister, a);
        fa = Float.intBitsToFloat(a);
        stackMemorySection.setLogicalValueAtAddress(varsRegister, Float.toString(fa));
        ++pcRegister;
        break;

    case OpCode.FSUB:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        fa = Float.intBitsToFloat(a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        optopRegister -= 4;
        b = stackMemorySection.getAtAddress(optopRegister);
        fb = Float.intBitsToFloat(b);
        fresult = fb - fa;
        result = Float.floatToIntBits(fresult);
        stackMemorySection.setAtAddress(optopRegister, result);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Float.toString(fresult));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.GOTO:
        operand1 = methodAreaMemorySection.getAtAddress(pcRegister + 1);
        operand0 = methodAreaMemorySection.getAtAddress(pcRegister + 2);

        int offset = (operand1 << 8) | (operand0 & 0xff);

        pcRegister += offset;
        break;

    case OpCode.IADD:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        optopRegister -= 4;
        b = stackMemorySection.getAtAddress(optopRegister);
        result = a + b;
        stackMemorySection.setAtAddress(optopRegister, result);
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ICONST_M1:
        stackMemorySection.setAtAddress(optopRegister, -1);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, StringTable.operand);
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ICONST_0:
        stackMemorySection.setAtAddress(optopRegister, 0);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, StringTable.operand);
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ICONST_1:
        stackMemorySection.setAtAddress(optopRegister, 1);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, StringTable.operand);
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ICONST_2:
        stackMemorySection.setAtAddress(optopRegister, 2);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, StringTable.operand);
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.IINC:
        operand0 = methodAreaMemorySection.getAtAddress(pcRegister + 1);
        operand1 = methodAreaMemorySection.getAtAddress(pcRegister + 2);
        a = stackMemorySection.getAtAddress(varsRegister + (operand0 * 4));
        a += operand1;
        stackMemorySection.setAtAddress(varsRegister + (operand0 * 4), a);
        pcRegister += 3;
        break;

    case OpCode.ILOAD_0:
        a = stackMemorySection.getAtAddress(varsRegister);
        stackMemorySection.setAtAddress(optopRegister, a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, StringTable.operand);
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ILOAD_1:
        a = stackMemorySection.getAtAddress(varsRegister + 4);
        stackMemorySection.setAtAddress(optopRegister, a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, StringTable.operand);
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.IMUL:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        optopRegister -= 4;
        b = stackMemorySection.getAtAddress(optopRegister);
        result = a * b;
        stackMemorySection.setAtAddress(optopRegister, result);
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.INT2BYTE:
        a = stackMemorySection.getAtAddress(optopRegister - 4);
        a = (byte) a;
        stackMemorySection.setAtAddress(optopRegister - 4, a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister - 4, StringTable.operand);
        ++pcRegister;
        break;

    case OpCode.ISTORE_0:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        stackMemorySection.setAtAddress(varsRegister, a);
        ++pcRegister;
        break;

    case OpCode.ISTORE_1:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister + 4, "");
        stackMemorySection.setAtAddress(varsRegister + 4, a);
        ++pcRegister;
        break;
    }
}

From source file:ac.elements.parser.SimpleDBConverter.java

/**
 * Decodes zero-padded positive float value from the string representation
 * //  w  ww.j a  v a2 s . c  om
 * com.sleepycat.bind.tuple.TupleInput
 * 
 * @param value
 *            zero-padded string representation of the float
 * @return original float value
 */
private static Float decodeSortedFloat(String value) {

    int val = decodeInt(value);
    val ^= (val < 0) ? 0x80000000 : 0xffffffff;
    val = (int) (val ^ 0xffffffff);
    return Float.intBitsToFloat(val);
}

From source file:org.apache.solr.schema.TestUseDocValuesAsStored.java

private String[] nextValues(int arity, String valueType) throws Exception {
    String[] values = new String[arity];
    for (int i = 0; i < arity; ++i) {
        switch (valueType) {
        case "int":
            values[i] = String.valueOf(random().nextInt());
            break;
        case "double":
            values[i] = String.valueOf(Double.longBitsToDouble(random().nextLong()));
            break;
        case "long":
            values[i] = String.valueOf(random().nextLong());
            break;
        case "float":
            values[i] = String.valueOf(Float.intBitsToFloat(random().nextInt()));
            break;
        case "enum":
            values[i] = SEVERITY[TestUtil.nextInt(random(), 0, SEVERITY.length - 1)];
            break;
        case "str": {
            String str = TestUtil.randomRealisticUnicodeString(random());
            values[i] = BAD_CHAR_PATTERN.matcher(str).replaceAll("\uFFFD");
            break;
        }//  w  w  w  .j  a va2  s  .com
        case "date": {
            long epochMillis = TestUtil.nextLong(random(), START_RANDOM_EPOCH_MILLIS, END_RANDOM_EPOCH_MILLIS);
            values[i] = Instant.ofEpochMilli(epochMillis).toString();
            break;
        }
        default:
            throw new Exception("unknown type '" + valueType + "'");
        }
    }
    return values;
}

From source file:savant.file.SavantROFile.java

@Override
public synchronized float readFloat() throws IOException {
    byte[] bytes = new byte[4];
    int result = read(bytes);
    if (result != 4) {
        LOG.warn("Could not read 4 bytes for float");
        throw new IOException("At EOF");
    }//www . ja  v a 2 s.c  o  m
    int intBits = (bytes[0] & 0xFF) << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | bytes[3] & 0xFF;
    return Float.intBitsToFloat(intBits);
}

From source file:dk.dma.enav.model.geometry.Position.java

public static Position fromPackedLong(long l) {
    return new Position(Float.intBitsToFloat((int) (l >> 32)), Float.intBitsToFloat((int) l));
}

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

static Object deserialize(InputByteBuffer buffer, TypeInfo type, boolean invert, Object reuse)
        throws IOException {

    // Is this field a null?
    byte isNull = buffer.read(invert);
    if (isNull == 0) {
        return null;
    }//from   w w  w  .j av a  2 s  . c om
    assert (isNull == 1);

    switch (type.getCategory()) {
    case PRIMITIVE: {
        PrimitiveTypeInfo ptype = (PrimitiveTypeInfo) type;
        switch (ptype.getPrimitiveCategory()) {
        case VOID: {
            return null;
        }
        case BOOLEAN: {
            BooleanWritable r = reuse == null ? new BooleanWritable() : (BooleanWritable) reuse;
            byte b = buffer.read(invert);
            assert (b == 1 || b == 2);
            r.set(b == 2);
            return r;
        }
        case BYTE: {
            ByteWritable r = reuse == null ? new ByteWritable() : (ByteWritable) reuse;
            r.set((byte) (buffer.read(invert) ^ 0x80));
            return r;
        }
        case SHORT: {
            ShortWritable r = reuse == null ? new ShortWritable() : (ShortWritable) reuse;
            int v = buffer.read(invert) ^ 0x80;
            v = (v << 8) + (buffer.read(invert) & 0xff);
            r.set((short) v);
            return r;
        }
        case INT: {
            IntWritable r = reuse == null ? new IntWritable() : (IntWritable) reuse;
            r.set(deserializeInt(buffer, invert));
            return r;
        }
        case LONG: {
            LongWritable r = reuse == null ? new LongWritable() : (LongWritable) reuse;
            long v = buffer.read(invert) ^ 0x80;
            for (int i = 0; i < 7; i++) {
                v = (v << 8) + (buffer.read(invert) & 0xff);
            }
            r.set(v);
            return r;
        }
        case FLOAT: {
            FloatWritable r = reuse == null ? new FloatWritable() : (FloatWritable) reuse;
            int v = 0;
            for (int i = 0; i < 4; i++) {
                v = (v << 8) + (buffer.read(invert) & 0xff);
            }
            if ((v & (1 << 31)) == 0) {
                // negative number, flip all bits
                v = ~v;
            } else {
                // positive number, flip the first bit
                v = v ^ (1 << 31);
            }
            r.set(Float.intBitsToFloat(v));
            return r;
        }
        case DOUBLE: {
            DoubleWritable r = reuse == null ? new DoubleWritable() : (DoubleWritable) reuse;
            long v = 0;
            for (int i = 0; i < 8; i++) {
                v = (v << 8) + (buffer.read(invert) & 0xff);
            }
            if ((v & (1L << 63)) == 0) {
                // negative number, flip all bits
                v = ~v;
            } else {
                // positive number, flip the first bit
                v = v ^ (1L << 63);
            }
            r.set(Double.longBitsToDouble(v));
            return r;
        }
        case STRING: {
            Text r = reuse == null ? new Text() : (Text) reuse;
            return deserializeText(buffer, invert, r);
        }

        case VARCHAR: {
            HiveVarcharWritable r = reuse == null ? new HiveVarcharWritable() : (HiveVarcharWritable) reuse;
            // Use HiveVarchar's internal Text member to read the value.
            deserializeText(buffer, invert, r.getTextValue());
            // If we cache helper data for deserialization we could avoid having
            // to call getVarcharMaxLength() on every deserialize call.
            r.enforceMaxLength(getVarcharMaxLength(type));
            return r;
        }

        case BINARY: {
            BytesWritable bw = new BytesWritable();
            // Get the actual length first
            int start = buffer.tell();
            int length = 0;
            do {
                byte b = buffer.read(invert);
                if (b == 0) {
                    // end of string
                    break;
                }
                if (b == 1) {
                    // the last char is an escape char. read the actual char
                    buffer.read(invert);
                }
                length++;
            } while (true);

            if (length == buffer.tell() - start) {
                // No escaping happened, so we are already done.
                bw.set(buffer.getData(), start, length);
            } else {
                // Escaping happened, we need to copy byte-by-byte.
                // 1. Set the length first.
                bw.set(buffer.getData(), start, length);
                // 2. Reset the pointer.
                buffer.seek(start);
                // 3. Copy the data.
                byte[] rdata = bw.getBytes();
                for (int i = 0; i < length; i++) {
                    byte b = buffer.read(invert);
                    if (b == 1) {
                        // The last char is an escape char, read the actual char.
                        // The serialization format escape \0 to \1, and \1 to \2,
                        // to make sure the string is null-terminated.
                        b = (byte) (buffer.read(invert) - 1);
                    }
                    rdata[i] = b;
                }
                // 4. Read the null terminator.
                byte b = buffer.read(invert);
                assert (b == 0);
            }
            return bw;
        }

        case DATE: {
            DateWritable d = reuse == null ? new DateWritable() : (DateWritable) reuse;
            d.set(deserializeInt(buffer, invert));
            return d;
        }

        case TIMESTAMP:
            TimestampWritable t = (reuse == null ? new TimestampWritable() : (TimestampWritable) reuse);
            byte[] bytes = new byte[TimestampWritable.BINARY_SORTABLE_LENGTH];

            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = buffer.read(invert);
            }
            t.setBinarySortable(bytes, 0);
            return t;

        case DECIMAL: {
            // See serialization of decimal for explanation (below)

            HiveDecimalWritable bdw = (reuse == null ? new HiveDecimalWritable() : (HiveDecimalWritable) reuse);

            int b = buffer.read(invert) - 1;
            assert (b == 1 || b == -1 || b == 0);
            boolean positive = b != -1;

            int factor = buffer.read(invert) ^ 0x80;
            for (int i = 0; i < 3; i++) {
                factor = (factor << 8) + (buffer.read(invert) & 0xff);
            }

            if (!positive) {
                factor = -factor;
            }

            int start = buffer.tell();
            int length = 0;

            do {
                b = buffer.read(positive ? invert : !invert);
                assert (b != 1);

                if (b == 0) {
                    // end of digits
                    break;
                }

                length++;
            } while (true);

            if (decimalBuffer == null || decimalBuffer.length < length) {
                decimalBuffer = new byte[length];
            }

            buffer.seek(start);
            for (int i = 0; i < length; ++i) {
                decimalBuffer[i] = buffer.read(positive ? invert : !invert);
            }

            // read the null byte again
            buffer.read(positive ? invert : !invert);

            String digits = new String(decimalBuffer, 0, length, decimalCharSet);
            BigInteger bi = new BigInteger(digits);
            HiveDecimal bd = new HiveDecimal(bi).scaleByPowerOfTen(factor - length);

            if (!positive) {
                bd = bd.negate();
            }

            bdw.set(bd);
            return bdw;
        }

        default: {
            throw new RuntimeException("Unrecognized type: " + ptype.getPrimitiveCategory());
        }
        }
    }

    case LIST: {
        ListTypeInfo ltype = (ListTypeInfo) type;
        TypeInfo etype = ltype.getListElementTypeInfo();

        // Create the list if needed
        ArrayList<Object> r = reuse == null ? new ArrayList<Object>() : (ArrayList<Object>) reuse;

        // Read the list
        int size = 0;
        while (true) {
            int more = buffer.read(invert);
            if (more == 0) {
                // \0 to terminate
                break;
            }
            // \1 followed by each element
            assert (more == 1);
            if (size == r.size()) {
                r.add(null);
            }
            r.set(size, deserialize(buffer, etype, invert, r.get(size)));
            size++;
        }
        // Remove additional elements if the list is reused
        while (r.size() > size) {
            r.remove(r.size() - 1);
        }
        return r;
    }
    case MAP: {
        MapTypeInfo mtype = (MapTypeInfo) type;
        TypeInfo ktype = mtype.getMapKeyTypeInfo();
        TypeInfo vtype = mtype.getMapValueTypeInfo();

        // Create the map if needed
        Map<Object, Object> r;
        if (reuse == null) {
            r = new HashMap<Object, Object>();
        } else {
            r = (HashMap<Object, Object>) reuse;
            r.clear();
        }

        while (true) {
            int more = buffer.read(invert);
            if (more == 0) {
                // \0 to terminate
                break;
            }
            // \1 followed by each key and then each value
            assert (more == 1);
            Object k = deserialize(buffer, ktype, invert, null);
            Object v = deserialize(buffer, vtype, invert, null);
            r.put(k, v);
        }
        return r;
    }
    case STRUCT: {
        StructTypeInfo stype = (StructTypeInfo) type;
        List<TypeInfo> fieldTypes = stype.getAllStructFieldTypeInfos();
        int size = fieldTypes.size();
        // Create the struct if needed
        ArrayList<Object> r = reuse == null ? new ArrayList<Object>(size) : (ArrayList<Object>) reuse;
        assert (r.size() <= size);
        // Set the size of the struct
        while (r.size() < size) {
            r.add(null);
        }
        // Read one field by one field
        for (int eid = 0; eid < size; eid++) {
            r.set(eid, deserialize(buffer, fieldTypes.get(eid), invert, r.get(eid)));
        }
        return r;
    }
    case UNION: {
        UnionTypeInfo utype = (UnionTypeInfo) type;
        StandardUnion r = reuse == null ? new StandardUnion() : (StandardUnion) reuse;
        // Read the tag
        byte tag = buffer.read(invert);
        r.setTag(tag);
        r.setObject(deserialize(buffer, utype.getAllUnionObjectTypeInfos().get(tag), invert, null));
        return r;
    }
    default: {
        throw new RuntimeException("Unrecognized type: " + type.getCategory());
    }
    }
}

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

@Override
public float readFloat() throws IOException {
    final boolean invert = columnSortOrderIsDesc[fieldIndex];
    int v = 0;/*w  w  w.  j a  v a 2 s. co  m*/
    for (int i = 0; i < 4; i++) {
        v = (v << 8) + (inputByteBuffer.read(invert) & 0xff);
    }
    if ((v & (1 << 31)) == 0) {
        // negative number, flip all bits
        v = ~v;
    } else {
        // positive number, flip the first bit
        v = v ^ (1 << 31);
    }
    return Float.intBitsToFloat(v);
}

From source file:JVMSimulator.java

void ExecuteNextInstruction() {

    int a, b, result, i, operand0, operand1, operand2, offset;
    float fa, fb, fresult;
    Float f;//from  www.ja  v  a 2s.  co m

    int nextOpCode = methodAreaMemorySection.getAtAddress(pcRegister);

    switch (nextOpCode) {

    case OpCode.AALOAD:
        executeAaload();
        break;

    case OpCode.ALOAD_0:
        executeAload_n(0);
        break;

    case OpCode.ASTORE_0:
        executeAstore_n(0);
        break;

    case OpCode.BIPUSH:
        operand0 = methodAreaMemorySection.getAtAddress(pcRegister + 1);
        stackMemorySection.setAtAddress(optopRegister, operand0);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Integer.toString(operand0));
        optopRegister += 4;
        pcRegister += 2;
        break;

    // The BREAKPOINT opcode will serve as a stop sign for a running simulation.
    case OpCode.BREAKPOINT:
        stopButton.disable();
        runButton.disable();
        stepButton.disable();
        resetButton.enable();
        if (runner != null) {
            // If runner is not null, then this is probably the thread that
            // we want to stop. Therefore, as soon as stop has been executed,
            // nothing else will happen. So we must set runner to null before
            // we call runner.stop(). Therefore I copy runner to temp, assign
            // null to runner, and call stop() on temp.
            Thread temp = runner;
            runner = null;
            temp.stop();
        }
        break;

    case OpCode.FCONST_0:
        stackMemorySection.setAtAddress(optopRegister, Float.floatToIntBits((float) 0));
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "0");
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.FCONST_2:
        stackMemorySection.setAtAddress(optopRegister, Float.floatToIntBits((float) 2));
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "2");
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.FLOAD_0:
        a = stackMemorySection.getAtAddress(varsRegister);
        stackMemorySection.setAtAddress(optopRegister, a);
        fa = Float.intBitsToFloat(a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Float.toString(fa));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.FMUL:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        fa = Float.intBitsToFloat(a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        optopRegister -= 4;
        b = stackMemorySection.getAtAddress(optopRegister);
        fb = Float.intBitsToFloat(b);
        fresult = fa * fb;
        result = Float.floatToIntBits(fresult);
        stackMemorySection.setAtAddress(optopRegister, result);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Float.toString(fresult));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.FSTORE_0:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        stackMemorySection.setAtAddress(varsRegister, a);
        fa = Float.intBitsToFloat(a);
        stackMemorySection.setLogicalValueAtAddress(varsRegister, Float.toString(fa));
        ++pcRegister;
        break;

    case OpCode.FSUB:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        fa = Float.intBitsToFloat(a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        optopRegister -= 4;
        b = stackMemorySection.getAtAddress(optopRegister);
        fb = Float.intBitsToFloat(b);
        fresult = fb - fa;
        result = Float.floatToIntBits(fresult);
        stackMemorySection.setAtAddress(optopRegister, result);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Float.toString(fresult));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.GOTO:
        operand1 = methodAreaMemorySection.getAtAddress(pcRegister + 1);
        operand0 = methodAreaMemorySection.getAtAddress(pcRegister + 2);

        offset = (operand1 << 8) | (operand0 & 0xff);

        pcRegister += offset;
        break;

    case OpCode.IADD:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        optopRegister -= 4;
        b = stackMemorySection.getAtAddress(optopRegister);
        result = a + b;
        stackMemorySection.setAtAddress(optopRegister, result);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Integer.toString(result));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.IAND:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        optopRegister -= 4;
        b = stackMemorySection.getAtAddress(optopRegister);
        result = a & b;
        stackMemorySection.setAtAddress(optopRegister, result);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Integer.toString(result));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.IASTORE:
        executeIastore();
        break;

    case OpCode.ICONST_M1:
        stackMemorySection.setAtAddress(optopRegister, -1);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "-1");
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ICONST_0:
        stackMemorySection.setAtAddress(optopRegister, 0);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "0");
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ICONST_1:
        stackMemorySection.setAtAddress(optopRegister, 1);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "1");
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ICONST_2:
        stackMemorySection.setAtAddress(optopRegister, 2);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "2");
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ICONST_3:
        stackMemorySection.setAtAddress(optopRegister, 3);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "3");
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ICONST_4:
        stackMemorySection.setAtAddress(optopRegister, 4);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "4");
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ICONST_5:
        stackMemorySection.setAtAddress(optopRegister, 5);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "5");
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.IFNE:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        // If a != 0 jump, else go on
        if (a != 0) {
            operand1 = methodAreaMemorySection.getAtAddress(pcRegister + 1);
            operand0 = methodAreaMemorySection.getAtAddress(pcRegister + 2);
            offset = (operand1 << 8) | (operand0 & 0xff);
            pcRegister += offset;
        } else {
            pcRegister += 3;
        }
        break;

    case OpCode.IF_ICMPLT:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        optopRegister -= 4;
        b = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        // If b < a jump, else go on
        if (b < a) {
            operand1 = methodAreaMemorySection.getAtAddress(pcRegister + 1);
            operand0 = methodAreaMemorySection.getAtAddress(pcRegister + 2);
            offset = (operand1 << 8) | (operand0 & 0xff);
            pcRegister += offset;
        } else {
            pcRegister += 3;
        }
        break;

    case OpCode.IINC:
        operand0 = methodAreaMemorySection.getAtAddress(pcRegister + 1);
        operand1 = methodAreaMemorySection.getAtAddress(pcRegister + 2);
        a = stackMemorySection.getAtAddress(varsRegister + (operand0 * 4));
        a += operand1;
        stackMemorySection.setAtAddress(varsRegister + (operand0 * 4), a);
        stackMemorySection.setLogicalValueAtAddress(varsRegister + (operand0 * 4), Integer.toString(a));
        pcRegister += 3;
        break;

    case OpCode.ILOAD_0:
        a = stackMemorySection.getAtAddress(varsRegister);
        stackMemorySection.setAtAddress(optopRegister, a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Integer.toString(a));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ILOAD_1:
        a = stackMemorySection.getAtAddress(varsRegister + 4);
        stackMemorySection.setAtAddress(optopRegister, a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Integer.toString(a));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ILOAD_2:
        a = stackMemorySection.getAtAddress(varsRegister + 8);
        stackMemorySection.setAtAddress(optopRegister, a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Integer.toString(a));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ILOAD_3:
        a = stackMemorySection.getAtAddress(varsRegister + 12);
        stackMemorySection.setAtAddress(optopRegister, a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Integer.toString(a));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.IMUL:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        optopRegister -= 4;
        b = stackMemorySection.getAtAddress(optopRegister);
        result = a * b;
        stackMemorySection.setAtAddress(optopRegister, result);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Integer.toString(result));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.INT2BYTE:
        a = stackMemorySection.getAtAddress(optopRegister - 4);
        a = (byte) a;
        stackMemorySection.setAtAddress(optopRegister - 4, a);
        stackMemorySection.setLogicalValueAtAddress(optopRegister - 4, Integer.toString(a));
        ++pcRegister;
        break;

    case OpCode.IOR:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        optopRegister -= 4;
        b = stackMemorySection.getAtAddress(optopRegister);
        result = a | b;
        stackMemorySection.setAtAddress(optopRegister, result);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Integer.toString(result));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ISHL:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        optopRegister -= 4;
        b = stackMemorySection.getAtAddress(optopRegister);
        result = b << (a & 0x1f);
        stackMemorySection.setAtAddress(optopRegister, result);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Integer.toString(result));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.ISTORE_0:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        stackMemorySection.setAtAddress(varsRegister, a);
        stackMemorySection.setLogicalValueAtAddress(varsRegister, Integer.toString(a));
        ++pcRegister;
        break;

    case OpCode.ISTORE_1:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        stackMemorySection.setAtAddress(varsRegister + 4, a);
        stackMemorySection.setLogicalValueAtAddress(varsRegister + 4, Integer.toString(a));
        ++pcRegister;
        break;

    case OpCode.ISTORE_2:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        stackMemorySection.setAtAddress(varsRegister + 8, a);
        stackMemorySection.setLogicalValueAtAddress(varsRegister + 8, Integer.toString(a));
        ++pcRegister;
        break;

    case OpCode.ISTORE_3:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        stackMemorySection.setAtAddress(varsRegister + 12, a);
        stackMemorySection.setLogicalValueAtAddress(varsRegister + 12, Integer.toString(a));
        ++pcRegister;
        break;

    case OpCode.IXOR:
        optopRegister -= 4;
        a = stackMemorySection.getAtAddress(optopRegister);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, "");
        optopRegister -= 4;
        b = stackMemorySection.getAtAddress(optopRegister);
        result = a ^ b;
        stackMemorySection.setAtAddress(optopRegister, result);
        stackMemorySection.setLogicalValueAtAddress(optopRegister, Integer.toString(result));
        optopRegister += 4;
        ++pcRegister;
        break;

    case OpCode.MULTIANEWARRAY:
        executeMultianewarray();
        pcRegister += 4;
        break;
    }
}

From source file:com.meetup.memcached.NativeHandler.java

protected static Float decodeFloat(byte[] b) throws Exception {
    Integer l = decodeInteger(b);
    return Float.valueOf(Float.intBitsToFloat(l.intValue()));
}

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

static Object deserialize(InputByteBuffer buffer, TypeInfo type, boolean invert, Object reuse)
        throws IOException {

    // Is this field a null?
    byte isNull = buffer.read(invert);
    if (isNull == 0) {
        return null;
    }//from w w  w.ja va 2 s. co m
    assert (isNull == 1);

    switch (type.getCategory()) {
    case PRIMITIVE: {
        PrimitiveTypeInfo ptype = (PrimitiveTypeInfo) type;
        switch (ptype.getPrimitiveCategory()) {
        case VOID: {
            return null;
        }
        case BOOLEAN: {
            BooleanWritable r = reuse == null ? new BooleanWritable() : (BooleanWritable) reuse;
            byte b = buffer.read(invert);
            assert (b == 1 || b == 2);
            r.set(b == 2);
            return r;
        }
        case BYTE: {
            ByteWritable r = reuse == null ? new ByteWritable() : (ByteWritable) reuse;
            r.set((byte) (buffer.read(invert) ^ 0x80));
            return r;
        }
        case SHORT: {
            ShortWritable r = reuse == null ? new ShortWritable() : (ShortWritable) reuse;
            int v = buffer.read(invert) ^ 0x80;
            v = (v << 8) + (buffer.read(invert) & 0xff);
            r.set((short) v);
            return r;
        }
        case INT: {
            IntWritable r = reuse == null ? new IntWritable() : (IntWritable) reuse;
            r.set(deserializeInt(buffer, invert));
            return r;
        }
        case LONG: {
            LongWritable r = reuse == null ? new LongWritable() : (LongWritable) reuse;
            r.set(deserializeLong(buffer, invert));
            return r;
        }
        case FLOAT: {
            FloatWritable r = reuse == null ? new FloatWritable() : (FloatWritable) reuse;
            int v = 0;
            for (int i = 0; i < 4; i++) {
                v = (v << 8) + (buffer.read(invert) & 0xff);
            }
            if ((v & (1 << 31)) == 0) {
                // negative number, flip all bits
                v = ~v;
            } else {
                // positive number, flip the first bit
                v = v ^ (1 << 31);
            }
            r.set(Float.intBitsToFloat(v));
            return r;
        }
        case DOUBLE: {
            DoubleWritable r = reuse == null ? new DoubleWritable() : (DoubleWritable) reuse;
            long v = 0;
            for (int i = 0; i < 8; i++) {
                v = (v << 8) + (buffer.read(invert) & 0xff);
            }
            if ((v & (1L << 63)) == 0) {
                // negative number, flip all bits
                v = ~v;
            } else {
                // positive number, flip the first bit
                v = v ^ (1L << 63);
            }
            r.set(Double.longBitsToDouble(v));
            return r;
        }
        case STRING: {
            Text r = reuse == null ? new Text() : (Text) reuse;
            return deserializeText(buffer, invert, r);
        }

        case CHAR: {
            HiveCharWritable r = reuse == null ? new HiveCharWritable() : (HiveCharWritable) reuse;
            // Use internal text member to read value
            deserializeText(buffer, invert, r.getTextValue());
            r.enforceMaxLength(getCharacterMaxLength(type));
            return r;
        }

        case VARCHAR: {
            HiveVarcharWritable r = reuse == null ? new HiveVarcharWritable() : (HiveVarcharWritable) reuse;
            // Use HiveVarchar's internal Text member to read the value.
            deserializeText(buffer, invert, r.getTextValue());
            // If we cache helper data for deserialization we could avoid having
            // to call getVarcharMaxLength() on every deserialize call.
            r.enforceMaxLength(getCharacterMaxLength(type));
            return r;
        }

        case BINARY: {
            BytesWritable bw = new BytesWritable();
            // Get the actual length first
            int start = buffer.tell();
            int length = 0;
            do {
                byte b = buffer.read(invert);
                if (b == 0) {
                    // end of string
                    break;
                }
                if (b == 1) {
                    // the last char is an escape char. read the actual char
                    buffer.read(invert);
                }
                length++;
            } while (true);

            if (length == buffer.tell() - start) {
                // No escaping happened, so we are already done.
                bw.set(buffer.getData(), start, length);
            } else {
                // Escaping happened, we need to copy byte-by-byte.
                // 1. Set the length first.
                bw.set(buffer.getData(), start, length);
                // 2. Reset the pointer.
                buffer.seek(start);
                // 3. Copy the data.
                byte[] rdata = bw.getBytes();
                for (int i = 0; i < length; i++) {
                    byte b = buffer.read(invert);
                    if (b == 1) {
                        // The last char is an escape char, read the actual char.
                        // The serialization format escape \0 to \1, and \1 to \2,
                        // to make sure the string is null-terminated.
                        b = (byte) (buffer.read(invert) - 1);
                    }
                    rdata[i] = b;
                }
                // 4. Read the null terminator.
                byte b = buffer.read(invert);
                assert (b == 0);
            }
            return bw;
        }

        case DATE: {
            DateWritable d = reuse == null ? new DateWritable() : (DateWritable) reuse;
            d.set(deserializeInt(buffer, invert));
            return d;
        }

        case TIMESTAMP:
            TimestampWritable t = (reuse == null ? new TimestampWritable() : (TimestampWritable) reuse);
            byte[] bytes = new byte[TimestampWritable.BINARY_SORTABLE_LENGTH];

            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = buffer.read(invert);
            }
            t.setBinarySortable(bytes, 0);
            return t;

        case INTERVAL_YEAR_MONTH: {
            HiveIntervalYearMonthWritable i = reuse == null ? new HiveIntervalYearMonthWritable()
                    : (HiveIntervalYearMonthWritable) reuse;
            i.set(deserializeInt(buffer, invert));
            return i;
        }

        case INTERVAL_DAY_TIME: {
            HiveIntervalDayTimeWritable i = reuse == null ? new HiveIntervalDayTimeWritable()
                    : (HiveIntervalDayTimeWritable) reuse;
            long totalSecs = deserializeLong(buffer, invert);
            int nanos = deserializeInt(buffer, invert);
            i.set(totalSecs, nanos);
            return i;
        }

        case DECIMAL: {
            // See serialization of decimal for explanation (below)

            HiveDecimalWritable bdw = (reuse == null ? new HiveDecimalWritable() : (HiveDecimalWritable) reuse);

            int b = buffer.read(invert) - 1;
            assert (b == 1 || b == -1 || b == 0);
            boolean positive = b != -1;

            int factor = buffer.read(invert) ^ 0x80;
            for (int i = 0; i < 3; i++) {
                factor = (factor << 8) + (buffer.read(invert) & 0xff);
            }

            if (!positive) {
                factor = -factor;
            }

            int start = buffer.tell();
            int length = 0;

            do {
                b = buffer.read(positive ? invert : !invert);
                assert (b != 1);

                if (b == 0) {
                    // end of digits
                    break;
                }

                length++;
            } while (true);

            if (decimalBuffer == null || decimalBuffer.length < length) {
                decimalBuffer = new byte[length];
            }

            buffer.seek(start);
            for (int i = 0; i < length; ++i) {
                decimalBuffer[i] = buffer.read(positive ? invert : !invert);
            }

            // read the null byte again
            buffer.read(positive ? invert : !invert);

            String digits = new String(decimalBuffer, 0, length, decimalCharSet);
            BigInteger bi = new BigInteger(digits);
            HiveDecimal bd = HiveDecimal.create(bi).scaleByPowerOfTen(factor - length);

            if (!positive) {
                bd = bd.negate();
            }

            bdw.set(bd);
            return bdw;
        }

        default: {
            throw new RuntimeException("Unrecognized type: " + ptype.getPrimitiveCategory());
        }
        }
    }

    case LIST: {
        ListTypeInfo ltype = (ListTypeInfo) type;
        TypeInfo etype = ltype.getListElementTypeInfo();

        // Create the list if needed
        ArrayList<Object> r = reuse == null ? new ArrayList<Object>() : (ArrayList<Object>) reuse;

        // Read the list
        int size = 0;
        while (true) {
            int more = buffer.read(invert);
            if (more == 0) {
                // \0 to terminate
                break;
            }
            // \1 followed by each element
            assert (more == 1);
            if (size == r.size()) {
                r.add(null);
            }
            r.set(size, deserialize(buffer, etype, invert, r.get(size)));
            size++;
        }
        // Remove additional elements if the list is reused
        while (r.size() > size) {
            r.remove(r.size() - 1);
        }
        return r;
    }
    case MAP: {
        MapTypeInfo mtype = (MapTypeInfo) type;
        TypeInfo ktype = mtype.getMapKeyTypeInfo();
        TypeInfo vtype = mtype.getMapValueTypeInfo();

        // Create the map if needed
        Map<Object, Object> r;
        if (reuse == null) {
            r = new HashMap<Object, Object>();
        } else {
            r = (HashMap<Object, Object>) reuse;
            r.clear();
        }

        while (true) {
            int more = buffer.read(invert);
            if (more == 0) {
                // \0 to terminate
                break;
            }
            // \1 followed by each key and then each value
            assert (more == 1);
            Object k = deserialize(buffer, ktype, invert, null);
            Object v = deserialize(buffer, vtype, invert, null);
            r.put(k, v);
        }
        return r;
    }
    case STRUCT: {
        StructTypeInfo stype = (StructTypeInfo) type;
        List<TypeInfo> fieldTypes = stype.getAllStructFieldTypeInfos();
        int size = fieldTypes.size();
        // Create the struct if needed
        ArrayList<Object> r = reuse == null ? new ArrayList<Object>(size) : (ArrayList<Object>) reuse;
        assert (r.size() <= size);
        // Set the size of the struct
        while (r.size() < size) {
            r.add(null);
        }
        // Read one field by one field
        for (int eid = 0; eid < size; eid++) {
            r.set(eid, deserialize(buffer, fieldTypes.get(eid), invert, r.get(eid)));
        }
        return r;
    }
    case UNION: {
        UnionTypeInfo utype = (UnionTypeInfo) type;
        StandardUnion r = reuse == null ? new StandardUnion() : (StandardUnion) reuse;
        // Read the tag
        byte tag = buffer.read(invert);
        r.setTag(tag);
        r.setObject(deserialize(buffer, utype.getAllUnionObjectTypeInfos().get(tag), invert, null));
        return r;
    }
    default: {
        throw new RuntimeException("Unrecognized type: " + type.getCategory());
    }
    }
}