Java Code Examples for jdk.internal.misc.Unsafe#ARRAY_BYTE_BASE_OFFSET

The following examples show how to use jdk.internal.misc.Unsafe#ARRAY_BYTE_BASE_OFFSET . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HotSpotJVMCIRuntimeProvider.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The offset from the origin of an array to the first element.
 *
 * @return the offset in bytes
 */
static int getArrayBaseOffset(JavaKind kind) {
    switch (kind) {
        case Boolean:
            return Unsafe.ARRAY_BOOLEAN_BASE_OFFSET;
        case Byte:
            return Unsafe.ARRAY_BYTE_BASE_OFFSET;
        case Char:
            return Unsafe.ARRAY_CHAR_BASE_OFFSET;
        case Short:
            return Unsafe.ARRAY_SHORT_BASE_OFFSET;
        case Int:
            return Unsafe.ARRAY_INT_BASE_OFFSET;
        case Long:
            return Unsafe.ARRAY_LONG_BASE_OFFSET;
        case Float:
            return Unsafe.ARRAY_FLOAT_BASE_OFFSET;
        case Double:
            return Unsafe.ARRAY_DOUBLE_BASE_OFFSET;
        case Object:
            return Unsafe.ARRAY_OBJECT_BASE_OFFSET;
        default:
            throw new JVMCIError("%s", kind);
    }
}
 
Example 2
Source File: ArraysSupport.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Find the relative index of a mismatch between two arrays starting from
 * given indexes.
 *
 * <p>This method does not perform bounds checks. It is the responsibility
 * of the caller to perform such bounds checks before calling this method.
 *
 * @param a the first array to be tested for a mismatch
 * @param aFromIndex the index of the first element (inclusive) in the first
 * array to be compared
 * @param b the second array to be tested for a mismatch
 * @param bFromIndex the index of the first element (inclusive) in the
 * second array to be compared
 * @param length the number of bytes from each array to check
 * @return the relative index of a mismatch between the two arrays,
 * otherwise -1 if no mismatch.  The index will be within the range of
 * (inclusive) 0 to (exclusive) the smaller of the two array bounds.
 */
public static int mismatch(byte[] a, int aFromIndex,
                           byte[] b, int bFromIndex,
                           int length) {
    // assert 0 <= aFromIndex < a.length
    // assert 0 <= aFromIndex + length <= a.length
    // assert 0 <= bFromIndex < b.length
    // assert 0 <= bFromIndex + length <= b.length
    // assert length >= 0

    int i = 0;
    if (length > 7) {
        if (a[aFromIndex] != b[bFromIndex])
            return 0;
        int aOffset = Unsafe.ARRAY_BYTE_BASE_OFFSET + aFromIndex;
        int bOffset = Unsafe.ARRAY_BYTE_BASE_OFFSET + bFromIndex;
        i = vectorizedMismatch(
                a, aOffset,
                b, bOffset,
                length, LOG2_ARRAY_BYTE_INDEX_SCALE);
        if (i >= 0)
            return i;
        i = length - ~i;
    }
    for (; i < length; i++) {
        if (a[aFromIndex + i] != b[bFromIndex + i])
            return i;
    }
    return -1;
}
 
Example 3
Source File: RenderBuffer.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public RenderBuffer put(byte[] x, int offset, int length) {
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_BYTE + Unsafe.ARRAY_BYTE_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_BYTE;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putByte(x[i]);
        }
    }
    return this;
}
 
Example 4
Source File: ArraysSupport.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find the relative index of a mismatch between two arrays starting from
 * given indexes.
 *
 * <p>This method does not perform bounds checks. It is the responsibility
 * of the caller to perform such bounds checks before calling this method.
 *
 * @param a the first array to be tested for a mismatch
 * @param aFromIndex the index of the first element (inclusive) in the first
 * array to be compared
 * @param b the second array to be tested for a mismatch
 * @param bFromIndex the index of the first element (inclusive) in the
 * second array to be compared
 * @param length the number of bytes from each array to check
 * @return the relative index of a mismatch between the two arrays,
 * otherwise -1 if no mismatch.  The index will be within the range of
 * (inclusive) 0 to (exclusive) the smaller of the two array bounds.
 */
static int mismatch(byte[] a, int aFromIndex,
                    byte[] b, int bFromIndex,
                    int length) {
    // assert 0 <= aFromIndex < a.length
    // assert 0 <= aFromIndex + length <= a.length
    // assert 0 <= bFromIndex < b.length
    // assert 0 <= bFromIndex + length <= b.length
    // assert length >= 0

    int i = 0;
    if (length > 7) {
        int aOffset = Unsafe.ARRAY_BYTE_BASE_OFFSET + aFromIndex;
        int bOffset = Unsafe.ARRAY_BYTE_BASE_OFFSET + bFromIndex;
        i = vectorizedMismatch(
                a, aOffset,
                b, bOffset,
                length, LOG2_ARRAY_BYTE_INDEX_SCALE);
        if (i >= 0)
            return i;
        i = length - ~i;
    }
    for (; i < length; i++) {
        if (a[aFromIndex + i] != b[bFromIndex + i])
            return i;
    }
    return -1;
}
 
Example 5
Source File: RenderBuffer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(byte[] x, int offset, int length) {
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_BYTE + Unsafe.ARRAY_BYTE_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_BYTE;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putByte(x[i]);
        }
    }
    return this;
}
 
Example 6
Source File: CRC32C.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Updates the CRC-32C checksum with the specified array of bytes.
 */
@HotSpotIntrinsicCandidate
private static int updateBytes(int crc, byte[] b, int off, int end) {

    // Do only byte reads for arrays so short they can't be aligned
    // or if bytes are stored with a larger witdh than one byte.,%
    if (end - off >= 8 && Unsafe.ARRAY_BYTE_INDEX_SCALE == 1) {

        // align on 8 bytes
        int alignLength
                = (8 - ((Unsafe.ARRAY_BYTE_BASE_OFFSET + off) & 0x7)) & 0x7;
        for (int alignEnd = off + alignLength; off < alignEnd; off++) {
            crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
        }

        if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
            crc = Integer.reverseBytes(crc);
        }

        // slicing-by-8
        for (; off < (end - Long.BYTES); off += Long.BYTES) {
            int firstHalf;
            int secondHalf;
            if (Unsafe.ADDRESS_SIZE == 4) {
                // On 32 bit platforms read two ints instead of a single 64bit long
                firstHalf = UNSAFE.getInt(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off);
                secondHalf = UNSAFE.getInt(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off
                                           + Integer.BYTES);
            } else {
                long value = UNSAFE.getLong(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off);
                if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                    firstHalf = (int) value;
                    secondHalf = (int) (value >>> 32);
                } else { // ByteOrder.BIG_ENDIAN
                    firstHalf = (int) (value >>> 32);
                    secondHalf = (int) value;
                }
            }
            crc ^= firstHalf;
            if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                crc = byteTable7[crc & 0xFF]
                        ^ byteTable6[(crc >>> 8) & 0xFF]
                        ^ byteTable5[(crc >>> 16) & 0xFF]
                        ^ byteTable4[crc >>> 24]
                        ^ byteTable3[secondHalf & 0xFF]
                        ^ byteTable2[(secondHalf >>> 8) & 0xFF]
                        ^ byteTable1[(secondHalf >>> 16) & 0xFF]
                        ^ byteTable0[secondHalf >>> 24];
            } else { // ByteOrder.BIG_ENDIAN
                crc = byteTable0[secondHalf & 0xFF]
                        ^ byteTable1[(secondHalf >>> 8) & 0xFF]
                        ^ byteTable2[(secondHalf >>> 16) & 0xFF]
                        ^ byteTable3[secondHalf >>> 24]
                        ^ byteTable4[crc & 0xFF]
                        ^ byteTable5[(crc >>> 8) & 0xFF]
                        ^ byteTable6[(crc >>> 16) & 0xFF]
                        ^ byteTable7[crc >>> 24];
            }
        }

        if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
            crc = Integer.reverseBytes(crc);
        }
    }

    // Tail
    for (; off < end; off++) {
        crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
    }

    return crc;
}
 
Example 7
Source File: CRC32C.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Updates the CRC-32C checksum with the specified array of bytes.
 */
@HotSpotIntrinsicCandidate
private static int updateBytes(int crc, byte[] b, int off, int end) {

    // Do only byte reads for arrays so short they can't be aligned
    // or if bytes are stored with a larger witdh than one byte.,%
    if (end - off >= 8 && Unsafe.ARRAY_BYTE_INDEX_SCALE == 1) {

        // align on 8 bytes
        int alignLength
                = (8 - ((Unsafe.ARRAY_BYTE_BASE_OFFSET + off) & 0x7)) & 0x7;
        for (int alignEnd = off + alignLength; off < alignEnd; off++) {
            crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
        }

        if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
            crc = Integer.reverseBytes(crc);
        }

        // slicing-by-8
        for (; off < (end - Long.BYTES); off += Long.BYTES) {
            int firstHalf;
            int secondHalf;
            if (Unsafe.ADDRESS_SIZE == 4) {
                // On 32 bit platforms read two ints instead of a single 64bit long
                firstHalf = UNSAFE.getInt(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off);
                secondHalf = UNSAFE.getInt(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off
                                           + Integer.BYTES);
            } else {
                long value = UNSAFE.getLong(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off);
                if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                    firstHalf = (int) value;
                    secondHalf = (int) (value >>> 32);
                } else { // ByteOrder.BIG_ENDIAN
                    firstHalf = (int) (value >>> 32);
                    secondHalf = (int) value;
                }
            }
            crc ^= firstHalf;
            if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                crc = byteTable7[crc & 0xFF]
                        ^ byteTable6[(crc >>> 8) & 0xFF]
                        ^ byteTable5[(crc >>> 16) & 0xFF]
                        ^ byteTable4[crc >>> 24]
                        ^ byteTable3[secondHalf & 0xFF]
                        ^ byteTable2[(secondHalf >>> 8) & 0xFF]
                        ^ byteTable1[(secondHalf >>> 16) & 0xFF]
                        ^ byteTable0[secondHalf >>> 24];
            } else { // ByteOrder.BIG_ENDIAN
                crc = byteTable0[secondHalf & 0xFF]
                        ^ byteTable1[(secondHalf >>> 8) & 0xFF]
                        ^ byteTable2[(secondHalf >>> 16) & 0xFF]
                        ^ byteTable3[secondHalf >>> 24]
                        ^ byteTable4[crc & 0xFF]
                        ^ byteTable5[(crc >>> 8) & 0xFF]
                        ^ byteTable6[(crc >>> 16) & 0xFF]
                        ^ byteTable7[crc >>> 24];
            }
        }

        if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
            crc = Integer.reverseBytes(crc);
        }
    }

    // Tail
    for (; off < end; off++) {
        crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
    }

    return crc;
}