7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.compiler.core.common.util;
26
27 import static org.graalvm.compiler.core.common.util.UnsafeAccess.UNSAFE;
28 import sun.misc.Unsafe;
29
30 /**
31 * Provides low-level read access from a byte[] array for signed and unsigned values of size 1, 2,
32 * 4, and 8 bytes.
33 *
34 * The class can either be instantiated for sequential access to the byte[] array; or static methods
35 * can be used to read values without the overhead of creating an instance.
36 *
37 * The flag {@code supportsUnalignedMemoryAccess} must be set according to the capabilities of the
38 * hardware architecture: the value {@code true} allows more efficient memory access on
39 * architectures that support unaligned memory accesses; the value {@code false} is the safe
40 * fallback that works on every hardware.
41 */
42 public abstract class UnsafeArrayTypeReader implements TypeReader {
43
44 public static int getS1(byte[] data, long byteIndex) {
45 return UNSAFE.getByte(data, readOffset(data, byteIndex, Byte.BYTES));
46 }
47
48 public static int getU1(byte[] data, long byteIndex) {
49 return UNSAFE.getByte(data, readOffset(data, byteIndex, Byte.BYTES)) & 0xFF;
50 }
51
52 public static int getS2(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
53 if (supportsUnalignedMemoryAccess) {
54 return UnalignedUnsafeArrayTypeReader.getS2(data, byteIndex);
55 } else {
56 return AlignedUnsafeArrayTypeReader.getS2(data, byteIndex);
57 }
58 }
59
60 public static int getU2(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
61 return getS2(data, byteIndex, supportsUnalignedMemoryAccess) & 0xFFFF;
62 }
63
125
126 @Override
127 public final int getU1() {
128 int result = getU1(data, byteIndex);
129 byteIndex += Byte.BYTES;
130 return result;
131 }
132
133 @Override
134 public final int getU2() {
135 return getS2() & 0xFFFF;
136 }
137
138 @Override
139 public final long getU4() {
140 return getS4() & 0xFFFFFFFFL;
141 }
142 }
143
144 final class UnalignedUnsafeArrayTypeReader extends UnsafeArrayTypeReader {
145 protected static int getS2(byte[] data, long byteIndex) {
146 return UNSAFE.getShort(data, readOffset(data, byteIndex, Short.BYTES));
147 }
148
149 protected static int getS4(byte[] data, long byteIndex) {
150 return UNSAFE.getInt(data, readOffset(data, byteIndex, Integer.BYTES));
151 }
152
153 protected static long getS8(byte[] data, long byteIndex) {
154 return UNSAFE.getLong(data, readOffset(data, byteIndex, Long.BYTES));
155 }
156
157 protected UnalignedUnsafeArrayTypeReader(byte[] data, long byteIndex) {
158 super(data, byteIndex);
159 }
160
161 @Override
162 public int getS2() {
163 int result = getS2(data, byteIndex);
164 byteIndex += Short.BYTES;
165 return result;
166 }
167
168 @Override
169 public int getS4() {
170 int result = getS4(data, byteIndex);
171 byteIndex += Integer.BYTES;
172 return result;
173 }
174
175 @Override
176 public long getS8() {
177 long result = getS8(data, byteIndex);
178 byteIndex += Long.BYTES;
179 return result;
180 }
181 }
182
183 class AlignedUnsafeArrayTypeReader extends UnsafeArrayTypeReader {
184 protected static int getS2(byte[] data, long byteIndex) {
185 long offset = readOffset(data, byteIndex, Short.BYTES);
186 return ((UNSAFE.getByte(data, offset + 0) & 0xFF) << 0) | //
187 (UNSAFE.getByte(data, offset + 1) << 8);
188 }
189
190 protected static int getS4(byte[] data, long byteIndex) {
191 long offset = readOffset(data, byteIndex, Integer.BYTES);
192 return ((UNSAFE.getByte(data, offset + 0) & 0xFF) << 0) | //
193 ((UNSAFE.getByte(data, offset + 1) & 0xFF) << 8) | //
194 ((UNSAFE.getByte(data, offset + 2) & 0xFF) << 16) | //
195 (UNSAFE.getByte(data, offset + 3) << 24);
196 }
197
198 protected static long getS8(byte[] data, long byteIndex) {
199 long offset = readOffset(data, byteIndex, Long.BYTES);
200 return ((long) ((UNSAFE.getByte(data, offset + 0) & 0xFF)) << 0) | //
201 ((long) ((UNSAFE.getByte(data, offset + 1) & 0xFF)) << 8) | //
202 ((long) ((UNSAFE.getByte(data, offset + 2) & 0xFF)) << 16) | //
203 ((long) ((UNSAFE.getByte(data, offset + 3) & 0xFF)) << 24) | //
|
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.compiler.core.common.util;
26
27 import static org.graalvm.compiler.serviceprovider.GraalUnsafeAccess.getUnsafe;
28
29 import sun.misc.Unsafe;
30
31 /**
32 * Provides low-level read access from a byte[] array for signed and unsigned values of size 1, 2,
33 * 4, and 8 bytes.
34 *
35 * The class can either be instantiated for sequential access to the byte[] array; or static methods
36 * can be used to read values without the overhead of creating an instance.
37 *
38 * The flag {@code supportsUnalignedMemoryAccess} must be set according to the capabilities of the
39 * hardware architecture: the value {@code true} allows more efficient memory access on
40 * architectures that support unaligned memory accesses; the value {@code false} is the safe
41 * fallback that works on every hardware.
42 */
43 public abstract class UnsafeArrayTypeReader implements TypeReader {
44
45 private static final Unsafe UNSAFE = getUnsafe();
46
47 public static int getS1(byte[] data, long byteIndex) {
48 return UNSAFE.getByte(data, readOffset(data, byteIndex, Byte.BYTES));
49 }
50
51 public static int getU1(byte[] data, long byteIndex) {
52 return UNSAFE.getByte(data, readOffset(data, byteIndex, Byte.BYTES)) & 0xFF;
53 }
54
55 public static int getS2(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
56 if (supportsUnalignedMemoryAccess) {
57 return UnalignedUnsafeArrayTypeReader.getS2(data, byteIndex);
58 } else {
59 return AlignedUnsafeArrayTypeReader.getS2(data, byteIndex);
60 }
61 }
62
63 public static int getU2(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
64 return getS2(data, byteIndex, supportsUnalignedMemoryAccess) & 0xFFFF;
65 }
66
128
129 @Override
130 public final int getU1() {
131 int result = getU1(data, byteIndex);
132 byteIndex += Byte.BYTES;
133 return result;
134 }
135
136 @Override
137 public final int getU2() {
138 return getS2() & 0xFFFF;
139 }
140
141 @Override
142 public final long getU4() {
143 return getS4() & 0xFFFFFFFFL;
144 }
145 }
146
147 final class UnalignedUnsafeArrayTypeReader extends UnsafeArrayTypeReader {
148 private static final Unsafe UNSAFE = getUnsafe();
149
150 protected static int getS2(byte[] data, long byteIndex) {
151 return UNSAFE.getShort(data, readOffset(data, byteIndex, Short.BYTES));
152 }
153
154 protected static int getS4(byte[] data, long byteIndex) {
155 return UNSAFE.getInt(data, readOffset(data, byteIndex, Integer.BYTES));
156 }
157
158 protected static long getS8(byte[] data, long byteIndex) {
159 return UNSAFE.getLong(data, readOffset(data, byteIndex, Long.BYTES));
160 }
161
162 protected UnalignedUnsafeArrayTypeReader(byte[] data, long byteIndex) {
163 super(data, byteIndex);
164 }
165
166 @Override
167 public int getS2() {
168 int result = getS2(data, byteIndex);
169 byteIndex += Short.BYTES;
170 return result;
171 }
172
173 @Override
174 public int getS4() {
175 int result = getS4(data, byteIndex);
176 byteIndex += Integer.BYTES;
177 return result;
178 }
179
180 @Override
181 public long getS8() {
182 long result = getS8(data, byteIndex);
183 byteIndex += Long.BYTES;
184 return result;
185 }
186 }
187
188 class AlignedUnsafeArrayTypeReader extends UnsafeArrayTypeReader {
189 private static final Unsafe UNSAFE = getUnsafe();
190
191 protected static int getS2(byte[] data, long byteIndex) {
192 long offset = readOffset(data, byteIndex, Short.BYTES);
193 return ((UNSAFE.getByte(data, offset + 0) & 0xFF) << 0) | //
194 (UNSAFE.getByte(data, offset + 1) << 8);
195 }
196
197 protected static int getS4(byte[] data, long byteIndex) {
198 long offset = readOffset(data, byteIndex, Integer.BYTES);
199 return ((UNSAFE.getByte(data, offset + 0) & 0xFF) << 0) | //
200 ((UNSAFE.getByte(data, offset + 1) & 0xFF) << 8) | //
201 ((UNSAFE.getByte(data, offset + 2) & 0xFF) << 16) | //
202 (UNSAFE.getByte(data, offset + 3) << 24);
203 }
204
205 protected static long getS8(byte[] data, long byteIndex) {
206 long offset = readOffset(data, byteIndex, Long.BYTES);
207 return ((long) ((UNSAFE.getByte(data, offset + 0) & 0xFF)) << 0) | //
208 ((long) ((UNSAFE.getByte(data, offset + 1) & 0xFF)) << 8) | //
209 ((long) ((UNSAFE.getByte(data, offset + 2) & 0xFF)) << 16) | //
210 ((long) ((UNSAFE.getByte(data, offset + 3) & 0xFF)) << 24) | //
|