Skip to content

Commit a9dba56

Browse files
committed
8300783: Consolidate byteswap implementations
Reviewed-by: kbarrett, kvn
1 parent 769e740 commit a9dba56

File tree

30 files changed

+472
-988
lines changed

30 files changed

+472
-988
lines changed

src/hotspot/cpu/aarch64/bytes_aarch64.hpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define CPU_AARCH64_BYTES_AARCH64_HPP
2828

2929
#include "memory/allStatic.hpp"
30+
#include "utilities/byteswap.hpp"
3031

3132
class Bytes: AllStatic {
3233
public:
@@ -44,23 +45,13 @@ class Bytes: AllStatic {
4445
// Efficient reading and writing of unaligned unsigned data in Java
4546
// byte ordering (i.e. big-endian ordering). Byte-order reversal is
4647
// needed since x86 CPUs use little-endian format.
47-
static inline u2 get_Java_u2(address p) { return swap_u2(get_native_u2(p)); }
48-
static inline u4 get_Java_u4(address p) { return swap_u4(get_native_u4(p)); }
49-
static inline u8 get_Java_u8(address p) { return swap_u8(get_native_u8(p)); }
48+
static inline u2 get_Java_u2(address p) { return byteswap(get_native_u2(p)); }
49+
static inline u4 get_Java_u4(address p) { return byteswap(get_native_u4(p)); }
50+
static inline u8 get_Java_u8(address p) { return byteswap(get_native_u8(p)); }
5051

51-
static inline void put_Java_u2(address p, u2 x) { put_native_u2(p, swap_u2(x)); }
52-
static inline void put_Java_u4(address p, u4 x) { put_native_u4(p, swap_u4(x)); }
53-
static inline void put_Java_u8(address p, u8 x) { put_native_u8(p, swap_u8(x)); }
54-
55-
56-
// Efficient swapping of byte ordering
57-
static inline u2 swap_u2(u2 x); // compiler-dependent implementation
58-
static inline u4 swap_u4(u4 x); // compiler-dependent implementation
59-
static inline u8 swap_u8(u8 x);
52+
static inline void put_Java_u2(address p, u2 x) { put_native_u2(p, byteswap(x)); }
53+
static inline void put_Java_u4(address p, u4 x) { put_native_u4(p, byteswap(x)); }
54+
static inline void put_Java_u8(address p, u8 x) { put_native_u8(p, byteswap(x)); }
6055
};
6156

62-
63-
// The following header contains the implementations of swap_u2, swap_u4, and swap_u8[_base]
64-
#include OS_CPU_HEADER(bytes)
65-
6657
#endif // CPU_AARCH64_BYTES_AARCH64_HPP

src/hotspot/cpu/arm/bytes_arm.hpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,6 @@ class Bytes: AllStatic {
175175
static inline void put_native_u8(address p, u8 x) { put_Java_u8(p, x); }
176176

177177
#endif // VM_LITTLE_ENDIAN
178-
179-
// Efficient swapping of byte ordering
180-
static inline u2 swap_u2(u2 x);
181-
static inline u4 swap_u4(u4 x);
182-
static inline u8 swap_u8(u8 x);
183178
};
184179

185-
186-
// The following header contains the implementations of swap_u2, swap_u4, and swap_u8
187-
#include OS_CPU_HEADER(bytes)
188-
189180
#endif // CPU_ARM_BYTES_ARM_HPP

src/hotspot/cpu/ppc/bytes_ppc.hpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define CPU_PPC_BYTES_PPC_HPP
2828

2929
#include "memory/allStatic.hpp"
30+
#include "utilities/byteswap.hpp"
3031

3132
class Bytes: AllStatic {
3233
public:
@@ -37,11 +38,6 @@ class Bytes: AllStatic {
3738

3839
#if defined(VM_LITTLE_ENDIAN)
3940

40-
// Forward declarations of the compiler-dependent implementation
41-
static inline u2 swap_u2(u2 x);
42-
static inline u4 swap_u4(u4 x);
43-
static inline u8 swap_u8(u8 x);
44-
4541
static inline u2 get_native_u2(address p) {
4642
return (intptr_t(p) & 1) == 0
4743
? *(u2*)p
@@ -141,21 +137,16 @@ class Bytes: AllStatic {
141137

142138
// Efficient reading and writing of unaligned unsigned data in Java byte ordering (i.e. big-endian ordering)
143139
// (no byte-order reversal is needed since Power CPUs are big-endian oriented).
144-
static inline u2 get_Java_u2(address p) { return swap_u2(get_native_u2(p)); }
145-
static inline u4 get_Java_u4(address p) { return swap_u4(get_native_u4(p)); }
146-
static inline u8 get_Java_u8(address p) { return swap_u8(get_native_u8(p)); }
140+
static inline u2 get_Java_u2(address p) { return byteswap(get_native_u2(p)); }
141+
static inline u4 get_Java_u4(address p) { return byteswap(get_native_u4(p)); }
142+
static inline u8 get_Java_u8(address p) { return byteswap(get_native_u8(p)); }
147143

148-
static inline void put_Java_u2(address p, u2 x) { put_native_u2(p, swap_u2(x)); }
149-
static inline void put_Java_u4(address p, u4 x) { put_native_u4(p, swap_u4(x)); }
150-
static inline void put_Java_u8(address p, u8 x) { put_native_u8(p, swap_u8(x)); }
144+
static inline void put_Java_u2(address p, u2 x) { put_native_u2(p, byteswap(x)); }
145+
static inline void put_Java_u4(address p, u4 x) { put_native_u4(p, byteswap(x)); }
146+
static inline void put_Java_u8(address p, u8 x) { put_native_u8(p, byteswap(x)); }
151147

152148
#else // !defined(VM_LITTLE_ENDIAN)
153149

154-
// Thus, a swap between native and Java ordering is always a no-op:
155-
static inline u2 swap_u2(u2 x) { return x; }
156-
static inline u4 swap_u4(u4 x) { return x; }
157-
static inline u8 swap_u8(u8 x) { return x; }
158-
159150
static inline u2 get_native_u2(address p) {
160151
return (intptr_t(p) & 1) == 0
161152
? *(u2*)p
@@ -266,6 +257,4 @@ class Bytes: AllStatic {
266257
#endif // VM_LITTLE_ENDIAN
267258
};
268259

269-
#include OS_CPU_HEADER(bytes)
270-
271260
#endif // CPU_PPC_BYTES_PPC_HPP

src/hotspot/cpu/ppc/stubRoutines_ppc_64.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "runtime/os.hpp"
2929
#include "runtime/stubRoutines.hpp"
3030
#include "runtime/vm_version.hpp"
31+
#include "utilities/byteswap.hpp"
3132

3233
// Implementation of the platform-specific part of StubRoutines - for
3334
// a description of how to extend it, see the stubRoutines.hpp file.
@@ -74,12 +75,6 @@ static julong compute_inverse_poly(julong long_poly) {
7475
return div;
7576
}
7677

77-
#ifndef VM_LITTLE_ENDIAN
78-
static void reverse_bytes(juint &w) {
79-
w = ((w >> 24) & 0xFF) | (((w >> 16) & 0xFF) << 8) | (((w >> 8) & 0xFF) << 16) | ((w & 0xFF) << 24);
80-
}
81-
#endif
82-
8378
// Constants to fold n words as needed by macroAssembler.
8479
address StubRoutines::ppc::generate_crc_constants(juint reverse_poly) {
8580
// Layout of constant table:
@@ -112,10 +107,10 @@ address StubRoutines::ppc::generate_crc_constants(juint reverse_poly) {
112107
c = fold_byte(b, reverse_poly),
113108
d = fold_byte(c, reverse_poly);
114109
#ifndef VM_LITTLE_ENDIAN
115-
reverse_bytes(a);
116-
reverse_bytes(b);
117-
reverse_bytes(c);
118-
reverse_bytes(d);
110+
a = byteswap(a);
111+
b = byteswap(b);
112+
c = byteswap(c);
113+
d = byteswap(d);
119114
#endif
120115
ptr[i ] = a;
121116
ptr[i + 256] = b;

src/hotspot/cpu/riscv/bytes_riscv.hpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,13 @@
2828
#define CPU_RISCV_BYTES_RISCV_HPP
2929

3030
#include "memory/allStatic.hpp"
31+
#include "utilities/byteswap.hpp"
3132

3233
class Bytes: AllStatic {
3334
public:
3435
// Efficient reading and writing of unaligned unsigned data in platform-specific byte ordering
3536
// RISCV needs to check for alignment.
3637

37-
// Forward declarations of the compiler-dependent implementation
38-
static inline u2 swap_u2(u2 x);
39-
static inline u4 swap_u4(u4 x);
40-
static inline u8 swap_u8(u8 x);
41-
4238
static inline u2 get_native_u2(address p) {
4339
if ((intptr_t(p) & 1) == 0) {
4440
return *(u2*)p;
@@ -154,16 +150,18 @@ class Bytes: AllStatic {
154150
}
155151
}
156152

153+
#ifndef VM_LITTLE_ENDIAN
154+
#error RISC-V is little endian, the preprocessor macro VM_LITTLE_ENDIAN should be defined.
155+
#endif
156+
157157
// Efficient reading and writing of unaligned unsigned data in Java byte ordering (i.e. big-endian ordering)
158-
static inline u2 get_Java_u2(address p) { return swap_u2(get_native_u2(p)); }
159-
static inline u4 get_Java_u4(address p) { return swap_u4(get_native_u4(p)); }
160-
static inline u8 get_Java_u8(address p) { return swap_u8(get_native_u8(p)); }
158+
static inline u2 get_Java_u2(address p) { return byteswap(get_native_u2(p)); }
159+
static inline u4 get_Java_u4(address p) { return byteswap(get_native_u4(p)); }
160+
static inline u8 get_Java_u8(address p) { return byteswap(get_native_u8(p)); }
161161

162-
static inline void put_Java_u2(address p, u2 x) { put_native_u2(p, swap_u2(x)); }
163-
static inline void put_Java_u4(address p, u4 x) { put_native_u4(p, swap_u4(x)); }
164-
static inline void put_Java_u8(address p, u8 x) { put_native_u8(p, swap_u8(x)); }
162+
static inline void put_Java_u2(address p, u2 x) { put_native_u2(p, byteswap(x)); }
163+
static inline void put_Java_u4(address p, u4 x) { put_native_u4(p, byteswap(x)); }
164+
static inline void put_Java_u8(address p, u8 x) { put_native_u8(p, byteswap(x)); }
165165
};
166166

167-
#include OS_CPU_HEADER(bytes)
168-
169167
#endif // CPU_RISCV_BYTES_RISCV_HPP

src/hotspot/cpu/s390/bytes_s390.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ class Bytes: AllStatic {
5050
static inline void put_native_u4(address p, u4 x) { *(u4*)p = x; }
5151
static inline void put_native_u8(address p, u8 x) { *(u8*)p = x; }
5252

53-
// The following header contains the implementations of swap_u2, swap_u4, and swap_u8.
54-
#include OS_CPU_HEADER(bytes)
55-
5653
// Efficient reading and writing of unaligned unsigned data in Java byte ordering (i.e. big-endian ordering)
5754
static inline u2 get_Java_u2(address p) { return get_native_u2(p); }
5855
static inline u4 get_Java_u4(address p) { return get_native_u4(p); }

src/hotspot/cpu/x86/bytes_x86.hpp

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,10 @@
2727

2828
#include "memory/allStatic.hpp"
2929
#include "utilities/align.hpp"
30+
#include "utilities/byteswap.hpp"
3031
#include "utilities/macros.hpp"
3132

3233
class Bytes: AllStatic {
33-
private:
34-
#ifndef AMD64
35-
// Helper function for swap_u8
36-
static inline u8 swap_u8_base(u4 x, u4 y); // compiler-dependent implementation
37-
#endif // AMD64
38-
3934
public:
4035
// Efficient reading and writing of unaligned unsigned data in platform-specific byte ordering
4136
template <typename T>
@@ -79,7 +74,7 @@ class Bytes: AllStatic {
7974
T x = get_native<T>(p);
8075

8176
if (Endian::is_Java_byte_ordering_different()) {
82-
x = swap<T>(x);
77+
x = byteswap(x);
8378
}
8479

8580
return x;
@@ -88,7 +83,7 @@ class Bytes: AllStatic {
8883
template <typename T>
8984
static inline void put_Java(address p, T x) {
9085
if (Endian::is_Java_byte_ordering_different()) {
91-
x = swap<T>(x);
86+
x = byteswap(x);
9287
}
9388

9489
put_native<T>(p, x);
@@ -101,27 +96,6 @@ class Bytes: AllStatic {
10196
static inline void put_Java_u2(address p, u2 x) { put_Java<u2>(p, x); }
10297
static inline void put_Java_u4(address p, u4 x) { put_Java<u4>(p, x); }
10398
static inline void put_Java_u8(address p, u8 x) { put_Java<u8>(p, x); }
104-
105-
// Efficient swapping of byte ordering
106-
template <typename T>
107-
static T swap(T x) {
108-
switch (sizeof(T)) {
109-
case sizeof(u1): return x;
110-
case sizeof(u2): return swap_u2(x);
111-
case sizeof(u4): return swap_u4(x);
112-
case sizeof(u8): return swap_u8(x);
113-
default:
114-
guarantee(false, "invalid size: " SIZE_FORMAT "\n", sizeof(T));
115-
return 0;
116-
}
117-
}
118-
119-
static inline u2 swap_u2(u2 x); // compiler-dependent implementation
120-
static inline u4 swap_u4(u4 x); // compiler-dependent implementation
121-
static inline u8 swap_u8(u8 x);
12299
};
123100

124-
// The following header contains the implementations of swap_u2, swap_u4, and swap_u8[_base]
125-
#include OS_CPU_HEADER(bytes)
126-
127101
#endif // CPU_X86_BYTES_X86_HPP

src/hotspot/cpu/zero/bytes_zero.hpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,6 @@ class Bytes: AllStatic {
118118
p[6] = lo >> 8;
119119
p[7] = lo;
120120
}
121-
122-
// Efficient swapping of byte ordering
123-
static inline u2 swap_u2(u2 x);
124-
static inline u4 swap_u4(u4 x);
125-
static inline u8 swap_u8(u8 x);
126121
#else
127122
// No byte-order reversal is needed
128123
static inline u2 get_Java_u2(address p) {
@@ -144,20 +139,7 @@ class Bytes: AllStatic {
144139
static inline void put_Java_u8(address p, u8 x) {
145140
put_native_u8(p, x);
146141
}
147-
148-
// No byte-order reversal is needed
149-
static inline u2 swap_u2(u2 x) { return x; }
150-
static inline u4 swap_u4(u4 x) { return x; }
151-
static inline u8 swap_u8(u8 x) { return x; }
152142
#endif // VM_LITTLE_ENDIAN
153143
};
154144

155-
#ifdef VM_LITTLE_ENDIAN
156-
// The following header contains the implementations of swap_u2,
157-
// swap_u4, and swap_u8
158-
159-
#include OS_CPU_HEADER(bytes)
160-
161-
#endif // VM_LITTLE_ENDIAN
162-
163145
#endif // CPU_ZERO_BYTES_ZERO_HPP

src/hotspot/os_cpu/aix_ppc/bytes_aix_ppc.hpp

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/hotspot/os_cpu/bsd_aarch64/bytes_bsd_aarch64.hpp

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)