UnsafeArrayTypeReader.java revision 12651:6ef01bd40ce2
1105756Srwatson/*
2105756Srwatson * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
3107391Sru * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4105756Srwatson *
5105756Srwatson * This code is free software; you can redistribute it and/or modify it
6105756Srwatson * under the terms of the GNU General Public License version 2 only, as
7105756Srwatson * published by the Free Software Foundation.
8105756Srwatson *
9107391Sru * This code is distributed in the hope that it will be useful, but WITHOUT
10105756Srwatson * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11105756Srwatson * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12105756Srwatson * version 2 for more details (a copy is included in the LICENSE file that
13105756Srwatson * accompanied this code).
14105756Srwatson *
15105756Srwatson * You should have received a copy of the GNU General Public License version
16105756Srwatson * 2 along with this work; if not, write to the Free Software Foundation,
17105756Srwatson * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18105756Srwatson *
19105756Srwatson * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20105756Srwatson * or visit www.oracle.com if you need additional information or have any
21107391Sru * questions.
22105756Srwatson */
23105756Srwatsonpackage org.graalvm.compiler.core.common.util;
24105756Srwatson
25105756Srwatsonimport static org.graalvm.compiler.core.common.util.UnsafeAccess.UNSAFE;
26105756Srwatsonimport sun.misc.Unsafe;
27105756Srwatson
28105756Srwatson/**
29105756Srwatson * Provides low-level read access from a byte[] array for signed and unsigned values of size 1, 2,
30105756Srwatson * 4, and 8 bytes.
31105756Srwatson *
32105756Srwatson * The class can either be instantiated for sequential access to the byte[] array; or static methods
33107391Sru * can be used to read values without the overhead of creating an instance.
34105756Srwatson *
35107391Sru * The flag {@code supportsUnalignedMemoryAccess} must be set according to the capabilities of the
36105756Srwatson * hardware architecture: the value {@code true} allows more efficient memory access on
37105756Srwatson * architectures that support unaligned memory accesses; the value {@code false} is the safe
38107744Sru * fallback that works on every hardware.
39105756Srwatson */
40105756Srwatsonpublic abstract class UnsafeArrayTypeReader implements TypeReader {
41105756Srwatson
42105756Srwatson    public static int getS1(byte[] data, long byteIndex) {
43105756Srwatson        return UNSAFE.getByte(data, readOffset(data, byteIndex, Byte.BYTES));
44105756Srwatson    }
45107391Sru
46105756Srwatson    public static int getU1(byte[] data, long byteIndex) {
47105756Srwatson        return UNSAFE.getByte(data, readOffset(data, byteIndex, Byte.BYTES)) & 0xFF;
48105756Srwatson    }
49105756Srwatson
50105756Srwatson    public static int getS2(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
51107391Sru        if (supportsUnalignedMemoryAccess) {
52105756Srwatson            return UnalignedUnsafeArrayTypeReader.getS2(data, byteIndex);
53105756Srwatson        } else {
54109278Schris            return AlignedUnsafeArrayTypeReader.getS2(data, byteIndex);
55109276Schris        }
56105756Srwatson    }
57105756Srwatson
58    public static int getU2(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
59        return getS2(data, byteIndex, supportsUnalignedMemoryAccess) & 0xFFFF;
60    }
61
62    public static int getS4(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
63        if (supportsUnalignedMemoryAccess) {
64            return UnalignedUnsafeArrayTypeReader.getS4(data, byteIndex);
65        } else {
66            return AlignedUnsafeArrayTypeReader.getS4(data, byteIndex);
67        }
68    }
69
70    public static long getU4(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
71        return getS4(data, byteIndex, supportsUnalignedMemoryAccess) & 0xFFFFFFFFL;
72    }
73
74    public static long getS8(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
75        if (supportsUnalignedMemoryAccess) {
76            return UnalignedUnsafeArrayTypeReader.getS8(data, byteIndex);
77        } else {
78            return AlignedUnsafeArrayTypeReader.getS8(data, byteIndex);
79        }
80    }
81
82    protected static long readOffset(byte[] data, long byteIndex, int numBytes) {
83        assert byteIndex >= 0;
84        assert numBytes > 0;
85        assert byteIndex + numBytes <= data.length;
86        assert Unsafe.ARRAY_BYTE_INDEX_SCALE == 1;
87
88        return byteIndex + Unsafe.ARRAY_BYTE_BASE_OFFSET;
89    }
90
91    public static UnsafeArrayTypeReader create(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
92        if (supportsUnalignedMemoryAccess) {
93            return new UnalignedUnsafeArrayTypeReader(data, byteIndex);
94        } else {
95            return new AlignedUnsafeArrayTypeReader(data, byteIndex);
96        }
97    }
98
99    protected final byte[] data;
100    protected long byteIndex;
101
102    protected UnsafeArrayTypeReader(byte[] data, long byteIndex) {
103        this.data = data;
104        this.byteIndex = byteIndex;
105    }
106
107    @Override
108    public long getByteIndex() {
109        return byteIndex;
110    }
111
112    @Override
113    public void setByteIndex(long byteIndex) {
114        this.byteIndex = byteIndex;
115    }
116
117    @Override
118    public final int getS1() {
119        int result = getS1(data, byteIndex);
120        byteIndex += Byte.BYTES;
121        return result;
122    }
123
124    @Override
125    public final int getU1() {
126        int result = getU1(data, byteIndex);
127        byteIndex += Byte.BYTES;
128        return result;
129    }
130
131    @Override
132    public final int getU2() {
133        return getS2() & 0xFFFF;
134    }
135
136    @Override
137    public final long getU4() {
138        return getS4() & 0xFFFFFFFFL;
139    }
140}
141
142final class UnalignedUnsafeArrayTypeReader extends UnsafeArrayTypeReader {
143    protected static int getS2(byte[] data, long byteIndex) {
144        return UNSAFE.getShort(data, readOffset(data, byteIndex, Short.BYTES));
145    }
146
147    protected static int getS4(byte[] data, long byteIndex) {
148        return UNSAFE.getInt(data, readOffset(data, byteIndex, Integer.BYTES));
149    }
150
151    protected static long getS8(byte[] data, long byteIndex) {
152        return UNSAFE.getLong(data, readOffset(data, byteIndex, Long.BYTES));
153    }
154
155    protected UnalignedUnsafeArrayTypeReader(byte[] data, long byteIndex) {
156        super(data, byteIndex);
157    }
158
159    @Override
160    public int getS2() {
161        int result = getS2(data, byteIndex);
162        byteIndex += Short.BYTES;
163        return result;
164    }
165
166    @Override
167    public int getS4() {
168        int result = getS4(data, byteIndex);
169        byteIndex += Integer.BYTES;
170        return result;
171    }
172
173    @Override
174    public long getS8() {
175        long result = getS8(data, byteIndex);
176        byteIndex += Long.BYTES;
177        return result;
178    }
179}
180
181class AlignedUnsafeArrayTypeReader extends UnsafeArrayTypeReader {
182    protected static int getS2(byte[] data, long byteIndex) {
183        long offset = readOffset(data, byteIndex, Short.BYTES);
184        return ((UNSAFE.getByte(data, offset + 0) & 0xFF) << 0) | //
185                        (UNSAFE.getByte(data, offset + 1) << 8);
186    }
187
188    protected static int getS4(byte[] data, long byteIndex) {
189        long offset = readOffset(data, byteIndex, Integer.BYTES);
190        return ((UNSAFE.getByte(data, offset + 0) & 0xFF) << 0) | //
191                        ((UNSAFE.getByte(data, offset + 1) & 0xFF) << 8) | //
192                        ((UNSAFE.getByte(data, offset + 2) & 0xFF) << 16) | //
193                        (UNSAFE.getByte(data, offset + 3) << 24);
194    }
195
196    protected static long getS8(byte[] data, long byteIndex) {
197        long offset = readOffset(data, byteIndex, Long.BYTES);
198        return ((long) ((UNSAFE.getByte(data, offset + 0) & 0xFF)) << 0) | //
199                        ((long) ((UNSAFE.getByte(data, offset + 1) & 0xFF)) << 8) | //
200                        ((long) ((UNSAFE.getByte(data, offset + 2) & 0xFF)) << 16) | //
201                        ((long) ((UNSAFE.getByte(data, offset + 3) & 0xFF)) << 24) | //
202                        ((long) ((UNSAFE.getByte(data, offset + 4) & 0xFF)) << 32) | //
203                        ((long) ((UNSAFE.getByte(data, offset + 5) & 0xFF)) << 40) | //
204                        ((long) ((UNSAFE.getByte(data, offset + 6) & 0xFF)) << 48) | //
205                        ((long) (UNSAFE.getByte(data, offset + 7)) << 56);
206    }
207
208    protected AlignedUnsafeArrayTypeReader(byte[] data, long byteIndex) {
209        super(data, byteIndex);
210    }
211
212    @Override
213    public int getS2() {
214        int result = getS2(data, byteIndex);
215        byteIndex += Short.BYTES;
216        return result;
217    }
218
219    @Override
220    public int getS4() {
221        int result = getS4(data, byteIndex);
222        byteIndex += Integer.BYTES;
223        return result;
224    }
225
226    @Override
227    public long getS8() {
228        long result = getS8(data, byteIndex);
229        byteIndex += Long.BYTES;
230        return result;
231    }
232}
233