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 #ifndef CPU_X86_VM_BYTES_X86_HPP
26 #define CPU_X86_VM_BYTES_X86_HPP
27
28 #include "memory/allocation.hpp"
29 #include "utilities/macros.hpp"
30
31 class Bytes: AllStatic {
32 private:
33 #ifndef AMD64
34 // Helper function for swap_u8
35 static inline u8 swap_u8_base(u4 x, u4 y); // compiler-dependent implementation
36 #endif // AMD64
37
38 public:
39 // Returns true if the byte ordering used by Java is different from the native byte ordering
40 // of the underlying machine. For example, this is true for Intel x86, but false for Solaris
41 // on Sparc.
42 static inline bool is_Java_byte_ordering_different(){ return true; }
43
44
45 // Efficient reading and writing of unaligned unsigned data in platform-specific byte ordering
46 // (no special code is needed since x86 CPUs can access unaligned data)
47 static inline u2 get_native_u2(address p) { return *(u2*)p; }
48 static inline u4 get_native_u4(address p) { return *(u4*)p; }
49 static inline u8 get_native_u8(address p) { return *(u8*)p; }
50
51 static inline void put_native_u2(address p, u2 x) { *(u2*)p = x; }
52 static inline void put_native_u4(address p, u4 x) { *(u4*)p = x; }
53 static inline void put_native_u8(address p, u8 x) { *(u8*)p = x; }
54
55
56 // Efficient reading and writing of unaligned unsigned data in Java
57 // byte ordering (i.e. big-endian ordering). Byte-order reversal is
58 // needed since x86 CPUs use little-endian format.
59 static inline u2 get_Java_u2(address p) { return swap_u2(get_native_u2(p)); }
60 static inline u4 get_Java_u4(address p) { return swap_u4(get_native_u4(p)); }
61 static inline u8 get_Java_u8(address p) { return swap_u8(get_native_u8(p)); }
62
63 static inline void put_Java_u2(address p, u2 x) { put_native_u2(p, swap_u2(x)); }
64 static inline void put_Java_u4(address p, u4 x) { put_native_u4(p, swap_u4(x)); }
65 static inline void put_Java_u8(address p, u8 x) { put_native_u8(p, swap_u8(x)); }
66
67
68 // Efficient swapping of byte ordering
69 static inline u2 swap_u2(u2 x); // compiler-dependent implementation
70 static inline u4 swap_u4(u4 x); // compiler-dependent implementation
71 static inline u8 swap_u8(u8 x);
72 };
73
74 // The following header contains the implementations of swap_u2, swap_u4, and swap_u8[_base]
75 #include OS_CPU_HEADER_INLINE(bytes)
76
77 #endif // CPU_X86_VM_BYTES_X86_HPP
|
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 #ifndef CPU_X86_VM_BYTES_X86_HPP
26 #define CPU_X86_VM_BYTES_X86_HPP
27
28 #include "memory/allocation.hpp"
29 #include "utilities/macros.hpp"
30
31 class Bytes: AllStatic {
32 private:
33 #ifndef AMD64
34 // Helper function for swap_u8
35 static inline u8 swap_u8_base(u4 x, u4 y); // compiler-dependent implementation
36 #endif // AMD64
37
38 public:
39 // Efficient reading and writing of unaligned unsigned data in platform-specific byte ordering
40 template <typename T>
41 static inline T get_native(const void* p) {
42 assert(p != NULL, "null pointer");
43
44 T x;
45
46 if (is_ptr_aligned(p, sizeof(T))) {
47 x = *(T*)p;
48 } else {
49 memcpy(&x, p, sizeof(T));
50 }
51
52 return x;
53 }
54
55 template <typename T>
56 static inline void put_native(void* p, T x) {
57 assert(p != NULL, "null pointer");
58
59 if (is_ptr_aligned(p, sizeof(T))) {
60 *(T*)p = x;
61 } else {
62 memcpy(p, &x, sizeof(T));
63 }
64 }
65
66 static inline u2 get_native_u2(address p) { return get_native<u2>((void*)p); }
67 static inline u4 get_native_u4(address p) { return get_native<u4>((void*)p); }
68 static inline u8 get_native_u8(address p) { return get_native<u8>((void*)p); }
69 static inline void put_native_u2(address p, u2 x) { put_native<u2>((void*)p, x); }
70 static inline void put_native_u4(address p, u4 x) { put_native<u4>((void*)p, x); }
71 static inline void put_native_u8(address p, u8 x) { put_native<u8>((void*)p, x); }
72
73 // Efficient reading and writing of unaligned unsigned data in Java
74 // byte ordering (i.e. big-endian ordering). Byte-order reversal is
75 // needed since x86 CPUs use little-endian format.
76 template <typename T>
77 static inline T get_Java(const address p) {
78 T x = get_native<T>(p);
79
80 if (Endian::is_Java_byte_ordering_different()) {
81 x = swap<T>(x);
82 }
83
84 return x;
85 }
86
87 template <typename T>
88 static inline void put_Java(address p, T x) {
89 if (Endian::is_Java_byte_ordering_different()) {
90 x = swap<T>(x);
91 }
92
93 put_native<T>(p, x);
94 }
95
96 static inline u2 get_Java_u2(address p) { return get_Java<u2>(p); }
97 static inline u4 get_Java_u4(address p) { return get_Java<u4>(p); }
98 static inline u8 get_Java_u8(address p) { return get_Java<u8>(p); }
99
100 static inline void put_Java_u2(address p, u2 x) { put_Java<u2>(p, x); }
101 static inline void put_Java_u4(address p, u4 x) { put_Java<u4>(p, x); }
102 static inline void put_Java_u8(address p, u8 x) { put_Java<u8>(p, x); }
103
104 // Efficient swapping of byte ordering
105 template <typename T>
106 static T swap(T x) {
107 switch (sizeof(T)) {
108 case sizeof(u1): return x;
109 case sizeof(u2): return swap_u2(x);
110 case sizeof(u4): return swap_u4(x);
111 case sizeof(u8): return swap_u8(x);
112 default:
113 guarantee(false, "invalid size: " SIZE_FORMAT "\n", sizeof(T));
114 return 0;
115 }
116 }
117
118 static inline u2 swap_u2(u2 x); // compiler-dependent implementation
119 static inline u4 swap_u4(u4 x); // compiler-dependent implementation
120 static inline u8 swap_u8(u8 x);
121 };
122
123 // The following header contains the implementations of swap_u2, swap_u4, and swap_u8[_base]
124 #include OS_CPU_HEADER_INLINE(bytes)
125
126 #endif // CPU_X86_VM_BYTES_X86_HPP
|