/** * Toggles the endianness of the specified 64-bit long integer. */ public static long swapLong(long value) { return Long.reverseBytes(value); }
public static long adjustL(long i) { if(IS_LITTLE_ENDIAN) { return Long.reverseBytes(i); } else { return i; } } }
public static long adjustL(long i) { if(IS_LITTLE_ENDIAN) { return Long.reverseBytes(i); } else { return i; } } }
/** * Turns a long representing a sequence of 8 bytes read in little-endian order * into a number that when compared produces the same effect as comparing the * original sequence of bytes lexicographically */ private static long longBytesToLong(long bytes) { return Long.reverseBytes(bytes) ^ Long.MIN_VALUE; } }
@Override public void putLongLittleEndian(byte[] array, int offset, long value) { // Reverse the order of the bytes before storing, since we're on big-endian hardware. long littleEndianValue = Long.reverseBytes(value); theUnsafe.putLong(array, (long) offset + BYTE_ARRAY_BASE_OFFSET, littleEndianValue); } };
@Override public long getLongLittleEndian(byte[] array, int offset) { long bigEndian = theUnsafe.getLong(array, (long) offset + BYTE_ARRAY_BASE_OFFSET); // The hardware is big-endian, so we need to reverse the order of the bytes. return Long.reverseBytes(bigEndian); }
/** * Writes a {@code long} as specified by {@link DataOutputStream#writeLong(long)}, except using * little-endian byte order. * * @throws IOException if an I/O error occurs */ @Override public void writeLong(long v) throws IOException { byte[] bytes = Longs.toByteArray(Long.reverseBytes(v)); write(bytes, 0, bytes.length); }
@Override public final ByteBuf writeLong(long value) { wrapped.ensureWritable0(8); _setLong(wrapped, wrapped.writerIndex, nativeByteOrder ? value : Long.reverseBytes(value)); wrapped.writerIndex += 8; return this; }
@Override public final ByteBuf setLong(int index, long value) { wrapped.checkIndex(index, 8); _setLong(wrapped, index, nativeByteOrder ? value : Long.reverseBytes(value)); return this; }
@Override public final long getLong(int index) { wrapped.checkIndex(index, 8); long v = _getLong(wrapped, index); return nativeByteOrder ? v : Long.reverseBytes(v); }
@Description("encode value as a 64-bit 2's complement big endian varbinary") @ScalarFunction("to_big_endian_64") @SqlType(StandardTypes.VARBINARY) public static Slice toBigEndian64(@SqlType(StandardTypes.BIGINT) long value) { Slice slice = Slices.allocate(Long.BYTES); slice.setLong(0, Long.reverseBytes(value)); return slice; }
@Description("decode bigint value from a 64-bit 2's complement big endian varbinary") @ScalarFunction("from_big_endian_64") @SqlType(StandardTypes.BIGINT) public static long fromBigEndian64(@SqlType(StandardTypes.VARBINARY) Slice slice) { if (slice.length() != Long.BYTES) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "expected 8-byte input, but got instead: " + slice.length()); } return Long.reverseBytes(slice.getLong(0)); }
static long getLongLE(byte[] array, int index) { if (UNALIGNED) { long v = PlatformDependent.getLong(array, index); return BIG_ENDIAN_NATIVE_ORDER ? Long.reverseBytes(v) : v; } return PlatformDependent.getByte(array, index) & 0xffL | (PlatformDependent.getByte(array, index + 1) & 0xffL) << 8 | (PlatformDependent.getByte(array, index + 2) & 0xffL) << 16 | (PlatformDependent.getByte(array, index + 3) & 0xffL) << 24 | (PlatformDependent.getByte(array, index + 4) & 0xffL) << 32 | (PlatformDependent.getByte(array, index + 5) & 0xffL) << 40 | (PlatformDependent.getByte(array, index + 6) & 0xffL) << 48 | ((long) PlatformDependent.getByte(array, index + 7)) << 56; }
static void setLongLE(byte[] array, int index, long value) { if (UNALIGNED) { PlatformDependent.putLong(array, index, BIG_ENDIAN_NATIVE_ORDER ? Long.reverseBytes(value) : value); } else { PlatformDependent.putByte(array, index, (byte) value); PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8)); PlatformDependent.putByte(array, index + 2, (byte) (value >>> 16)); PlatformDependent.putByte(array, index + 3, (byte) (value >>> 24)); PlatformDependent.putByte(array, index + 4, (byte) (value >>> 32)); PlatformDependent.putByte(array, index + 5, (byte) (value >>> 40)); PlatformDependent.putByte(array, index + 6, (byte) (value >>> 48)); PlatformDependent.putByte(array, index + 7, (byte) (value >>> 56)); } }
static long getLong(byte[] array, int index) { if (UNALIGNED) { long v = PlatformDependent.getLong(array, index); return BIG_ENDIAN_NATIVE_ORDER ? v : Long.reverseBytes(v); } return ((long) PlatformDependent.getByte(array, index)) << 56 | (PlatformDependent.getByte(array, index + 1) & 0xffL) << 48 | (PlatformDependent.getByte(array, index + 2) & 0xffL) << 40 | (PlatformDependent.getByte(array, index + 3) & 0xffL) << 32 | (PlatformDependent.getByte(array, index + 4) & 0xffL) << 24 | (PlatformDependent.getByte(array, index + 5) & 0xffL) << 16 | (PlatformDependent.getByte(array, index + 6) & 0xffL) << 8 | (PlatformDependent.getByte(array, index + 7)) & 0xffL; }
static void setLongLE(long address, long value) { if (UNALIGNED) { PlatformDependent.putLong(address, BIG_ENDIAN_NATIVE_ORDER ? Long.reverseBytes(value) : value); } else { PlatformDependent.putByte(address, (byte) value); PlatformDependent.putByte(address + 1, (byte) (value >>> 8)); PlatformDependent.putByte(address + 2, (byte) (value >>> 16)); PlatformDependent.putByte(address + 3, (byte) (value >>> 24)); PlatformDependent.putByte(address + 4, (byte) (value >>> 32)); PlatformDependent.putByte(address + 5, (byte) (value >>> 40)); PlatformDependent.putByte(address + 6, (byte) (value >>> 48)); PlatformDependent.putByte(address + 7, (byte) (value >>> 56)); } }
static void setLong(byte[] array, int index, long value) { if (UNALIGNED) { PlatformDependent.putLong(array, index, BIG_ENDIAN_NATIVE_ORDER ? value : Long.reverseBytes(value)); } else { PlatformDependent.putByte(array, index, (byte) (value >>> 56)); PlatformDependent.putByte(array, index + 1, (byte) (value >>> 48)); PlatformDependent.putByte(array, index + 2, (byte) (value >>> 40)); PlatformDependent.putByte(array, index + 3, (byte) (value >>> 32)); PlatformDependent.putByte(array, index + 4, (byte) (value >>> 24)); PlatformDependent.putByte(array, index + 5, (byte) (value >>> 16)); PlatformDependent.putByte(array, index + 6, (byte) (value >>> 8)); PlatformDependent.putByte(array, index + 7, (byte) value); } }
static long getLongLE(long address) { if (UNALIGNED) { long v = PlatformDependent.getLong(address); return BIG_ENDIAN_NATIVE_ORDER ? Long.reverseBytes(v) : v; } return (PlatformDependent.getByte(address)) & 0xffL | (PlatformDependent.getByte(address + 1) & 0xffL) << 8 | (PlatformDependent.getByte(address + 2) & 0xffL) << 16 | (PlatformDependent.getByte(address + 3) & 0xffL) << 24 | (PlatformDependent.getByte(address + 4) & 0xffL) << 32 | (PlatformDependent.getByte(address + 5) & 0xffL) << 40 | (PlatformDependent.getByte(address + 6) & 0xffL) << 48 | ((long) PlatformDependent.getByte(address + 7)) << 56; }
static void setLong(long address, long value) { if (UNALIGNED) { PlatformDependent.putLong(address, BIG_ENDIAN_NATIVE_ORDER ? value : Long.reverseBytes(value)); } else { PlatformDependent.putByte(address, (byte) (value >>> 56)); PlatformDependent.putByte(address + 1, (byte) (value >>> 48)); PlatformDependent.putByte(address + 2, (byte) (value >>> 40)); PlatformDependent.putByte(address + 3, (byte) (value >>> 32)); PlatformDependent.putByte(address + 4, (byte) (value >>> 24)); PlatformDependent.putByte(address + 5, (byte) (value >>> 16)); PlatformDependent.putByte(address + 6, (byte) (value >>> 8)); PlatformDependent.putByte(address + 7, (byte) value); } }
static long getLong(long address) { if (UNALIGNED) { long v = PlatformDependent.getLong(address); return BIG_ENDIAN_NATIVE_ORDER ? v : Long.reverseBytes(v); } return ((long) PlatformDependent.getByte(address)) << 56 | (PlatformDependent.getByte(address + 1) & 0xffL) << 48 | (PlatformDependent.getByte(address + 2) & 0xffL) << 40 | (PlatformDependent.getByte(address + 3) & 0xffL) << 32 | (PlatformDependent.getByte(address + 4) & 0xffL) << 24 | (PlatformDependent.getByte(address + 5) & 0xffL) << 16 | (PlatformDependent.getByte(address + 6) & 0xffL) << 8 | (PlatformDependent.getByte(address + 7)) & 0xffL; }