bytes_ppc.hpp revision 11658:8a5735c11a84
1238104Sdes/*
2238104Sdes * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3238104Sdes * Copyright (c) 2012, 2016 SAP SE. All rights reserved.
4238104Sdes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5238104Sdes *
6238104Sdes * This code is free software; you can redistribute it and/or modify it
7238104Sdes * under the terms of the GNU General Public License version 2 only, as
8238104Sdes * published by the Free Software Foundation.
9238104Sdes *
10238104Sdes * This code is distributed in the hope that it will be useful, but WITHOUT
11238104Sdes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12238104Sdes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13238104Sdes * version 2 for more details (a copy is included in the LICENSE file that
14238104Sdes * accompanied this code).
15238104Sdes *
16238104Sdes * You should have received a copy of the GNU General Public License version
17238104Sdes * 2 along with this work; if not, write to the Free Software Foundation,
18238104Sdes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19238104Sdes *
20238104Sdes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21238104Sdes * or visit www.oracle.com if you need additional information or have any
22238104Sdes * questions.
23238104Sdes *
24238104Sdes */
25238104Sdes
26238104Sdes#ifndef CPU_PPC_VM_BYTES_PPC_HPP
27238104Sdes#define CPU_PPC_VM_BYTES_PPC_HPP
28238104Sdes
29238104Sdes#include "memory/allocation.hpp"
30238104Sdes
31238104Sdesclass Bytes: AllStatic {
32238104Sdes public:
33238104Sdes  // Efficient reading and writing of unaligned unsigned data in platform-specific byte ordering
34238104Sdes  // PowerPC needs to check for alignment.
35238104Sdes
36238104Sdes  // Can I count on address always being a pointer to an unsigned char? Yes.
37238104Sdes
38238104Sdes#if defined(VM_LITTLE_ENDIAN)
39238104Sdes
40238104Sdes  // Returns true, if the byte ordering used by Java is different from the native byte ordering
41238104Sdes  // of the underlying machine. For example, true for Intel x86, False, for Solaris on Sparc.
42238104Sdes  static inline bool is_Java_byte_ordering_different() { return true; }
43238104Sdes
44238104Sdes  // Forward declarations of the compiler-dependent implementation
45238104Sdes  static inline u2 swap_u2(u2 x);
46238104Sdes  static inline u4 swap_u4(u4 x);
47238104Sdes  static inline u8 swap_u8(u8 x);
48238104Sdes
49238104Sdes  static inline u2   get_native_u2(address p) {
50238104Sdes    return (intptr_t(p) & 1) == 0
51238104Sdes             ?   *(u2*)p
52238104Sdes             :   ( u2(p[1]) << 8 )
53238104Sdes               | ( u2(p[0])      );
54238104Sdes  }
55238104Sdes
56238104Sdes  static inline u4   get_native_u4(address p) {
57238104Sdes    switch (intptr_t(p) & 3) {
58238104Sdes     case 0:  return *(u4*)p;
59238104Sdes
60238104Sdes     case 2:  return (  u4( ((u2*)p)[1] ) << 16  )
61238104Sdes                   | (  u4( ((u2*)p)[0] )        );
62238104Sdes
63238104Sdes    default:  return ( u4(p[3]) << 24 )
64238104Sdes                   | ( u4(p[2]) << 16 )
65238104Sdes                   | ( u4(p[1]) <<  8 )
66238104Sdes                   |   u4(p[0]);
67238104Sdes    }
68238104Sdes  }
69238104Sdes
70238104Sdes  static inline u8   get_native_u8(address p) {
71238104Sdes    switch (intptr_t(p) & 7) {
72238104Sdes      case 0:  return *(u8*)p;
73238104Sdes
74238104Sdes      case 4:  return (  u8( ((u4*)p)[1] ) << 32  )
75238104Sdes                    | (  u8( ((u4*)p)[0] )        );
76238104Sdes
77238104Sdes      case 2:  return (  u8( ((u2*)p)[3] ) << 48  )
78238104Sdes                    | (  u8( ((u2*)p)[2] ) << 32  )
79238104Sdes                    | (  u8( ((u2*)p)[1] ) << 16  )
80238104Sdes                    | (  u8( ((u2*)p)[0] )        );
81238104Sdes
82238104Sdes     default:  return ( u8(p[7]) << 56 )
83238104Sdes                    | ( u8(p[6]) << 48 )
84238104Sdes                    | ( u8(p[5]) << 40 )
85238104Sdes                    | ( u8(p[4]) << 32 )
86238104Sdes                    | ( u8(p[3]) << 24 )
87238104Sdes                    | ( u8(p[2]) << 16 )
88238104Sdes                    | ( u8(p[1]) <<  8 )
89238104Sdes                    |   u8(p[0]);
90238104Sdes    }
91238104Sdes  }
92238104Sdes
93238104Sdes
94238104Sdes
95238104Sdes  static inline void put_native_u2(address p, u2 x) {
96238104Sdes    if ( (intptr_t(p) & 1) == 0 )  *(u2*)p = x;
97238104Sdes    else {
98238104Sdes      p[1] = x >> 8;
99238104Sdes      p[0] = x;
100238104Sdes    }
101238104Sdes  }
102238104Sdes
103238104Sdes  static inline void put_native_u4(address p, u4 x) {
104238104Sdes    switch ( intptr_t(p) & 3 ) {
105238104Sdes    case 0:  *(u4*)p = x;
106238104Sdes              break;
107238104Sdes
108238104Sdes    case 2:  ((u2*)p)[1] = x >> 16;
109238104Sdes             ((u2*)p)[0] = x;
110238104Sdes             break;
111238104Sdes
112238104Sdes    default: ((u1*)p)[3] = x >> 24;
113238104Sdes             ((u1*)p)[2] = x >> 16;
114238104Sdes             ((u1*)p)[1] = x >>  8;
115238104Sdes             ((u1*)p)[0] = x;
116             break;
117    }
118  }
119
120  static inline void put_native_u8(address p, u8 x) {
121    switch ( intptr_t(p) & 7 ) {
122    case 0:  *(u8*)p = x;
123             break;
124
125    case 4:  ((u4*)p)[1] = x >> 32;
126             ((u4*)p)[0] = x;
127             break;
128
129    case 2:  ((u2*)p)[3] = x >> 48;
130             ((u2*)p)[2] = x >> 32;
131             ((u2*)p)[1] = x >> 16;
132             ((u2*)p)[0] = x;
133             break;
134
135    default: ((u1*)p)[7] = x >> 56;
136             ((u1*)p)[6] = x >> 48;
137             ((u1*)p)[5] = x >> 40;
138             ((u1*)p)[4] = x >> 32;
139             ((u1*)p)[3] = x >> 24;
140             ((u1*)p)[2] = x >> 16;
141             ((u1*)p)[1] = x >>  8;
142             ((u1*)p)[0] = x;
143    }
144  }
145
146  // Efficient reading and writing of unaligned unsigned data in Java byte ordering (i.e. big-endian ordering)
147  // (no byte-order reversal is needed since Power CPUs are big-endian oriented).
148  static inline u2   get_Java_u2(address p) { return swap_u2(get_native_u2(p)); }
149  static inline u4   get_Java_u4(address p) { return swap_u4(get_native_u4(p)); }
150  static inline u8   get_Java_u8(address p) { return swap_u8(get_native_u8(p)); }
151
152  static inline void put_Java_u2(address p, u2 x)     { put_native_u2(p, swap_u2(x)); }
153  static inline void put_Java_u4(address p, u4 x)     { put_native_u4(p, swap_u4(x)); }
154  static inline void put_Java_u8(address p, u8 x)     { put_native_u8(p, swap_u8(x)); }
155
156#else // !defined(VM_LITTLE_ENDIAN)
157
158  // Returns true, if the byte ordering used by Java is different from the nativ byte ordering
159  // of the underlying machine. For example, true for Intel x86, False, for Solaris on Sparc.
160  static inline bool is_Java_byte_ordering_different() { return false; }
161
162  // Thus, a swap between native and Java ordering is always a no-op:
163  static inline u2   swap_u2(u2 x)  { return x; }
164  static inline u4   swap_u4(u4 x)  { return x; }
165  static inline u8   swap_u8(u8 x)  { return x; }
166
167  static inline u2   get_native_u2(address p) {
168    return (intptr_t(p) & 1) == 0
169             ?   *(u2*)p
170             :   ( u2(p[0]) << 8 )
171               | ( u2(p[1])      );
172  }
173
174  static inline u4   get_native_u4(address p) {
175    switch (intptr_t(p) & 3) {
176     case 0:  return *(u4*)p;
177
178     case 2:  return (  u4( ((u2*)p)[0] ) << 16  )
179                   | (  u4( ((u2*)p)[1] )        );
180
181    default:  return ( u4(p[0]) << 24 )
182                   | ( u4(p[1]) << 16 )
183                   | ( u4(p[2]) <<  8 )
184                   |   u4(p[3]);
185    }
186  }
187
188  static inline u8   get_native_u8(address p) {
189    switch (intptr_t(p) & 7) {
190      case 0:  return *(u8*)p;
191
192      case 4:  return (  u8( ((u4*)p)[0] ) << 32  )
193                    | (  u8( ((u4*)p)[1] )        );
194
195      case 2:  return (  u8( ((u2*)p)[0] ) << 48  )
196                    | (  u8( ((u2*)p)[1] ) << 32  )
197                    | (  u8( ((u2*)p)[2] ) << 16  )
198                    | (  u8( ((u2*)p)[3] )        );
199
200     default:  return ( u8(p[0]) << 56 )
201                    | ( u8(p[1]) << 48 )
202                    | ( u8(p[2]) << 40 )
203                    | ( u8(p[3]) << 32 )
204                    | ( u8(p[4]) << 24 )
205                    | ( u8(p[5]) << 16 )
206                    | ( u8(p[6]) <<  8 )
207                    |   u8(p[7]);
208    }
209  }
210
211
212
213  static inline void put_native_u2(address p, u2 x) {
214    if ( (intptr_t(p) & 1) == 0 ) { *(u2*)p = x; }
215    else {
216      p[0] = x >> 8;
217      p[1] = x;
218    }
219  }
220
221  static inline void put_native_u4(address p, u4 x) {
222    switch ( intptr_t(p) & 3 ) {
223    case 0:  *(u4*)p = x;
224              break;
225
226    case 2:  ((u2*)p)[0] = x >> 16;
227             ((u2*)p)[1] = x;
228             break;
229
230    default: ((u1*)p)[0] = x >> 24;
231             ((u1*)p)[1] = x >> 16;
232             ((u1*)p)[2] = x >>  8;
233             ((u1*)p)[3] = x;
234             break;
235    }
236  }
237
238  static inline void put_native_u8(address p, u8 x) {
239    switch ( intptr_t(p) & 7 ) {
240    case 0:  *(u8*)p = x;
241             break;
242
243    case 4:  ((u4*)p)[0] = x >> 32;
244             ((u4*)p)[1] = x;
245             break;
246
247    case 2:  ((u2*)p)[0] = x >> 48;
248             ((u2*)p)[1] = x >> 32;
249             ((u2*)p)[2] = x >> 16;
250             ((u2*)p)[3] = x;
251             break;
252
253    default: ((u1*)p)[0] = x >> 56;
254             ((u1*)p)[1] = x >> 48;
255             ((u1*)p)[2] = x >> 40;
256             ((u1*)p)[3] = x >> 32;
257             ((u1*)p)[4] = x >> 24;
258             ((u1*)p)[5] = x >> 16;
259             ((u1*)p)[6] = x >>  8;
260             ((u1*)p)[7] = x;
261    }
262  }
263
264  // Efficient reading and writing of unaligned unsigned data in Java byte ordering (i.e. big-endian ordering)
265  // (no byte-order reversal is needed since Power CPUs are big-endian oriented).
266  static inline u2   get_Java_u2(address p) { return get_native_u2(p); }
267  static inline u4   get_Java_u4(address p) { return get_native_u4(p); }
268  static inline u8   get_Java_u8(address p) { return get_native_u8(p); }
269
270  static inline void put_Java_u2(address p, u2 x)     { put_native_u2(p, x); }
271  static inline void put_Java_u4(address p, u4 x)     { put_native_u4(p, x); }
272  static inline void put_Java_u8(address p, u8 x)     { put_native_u8(p, x); }
273
274#endif // VM_LITTLE_ENDIAN
275};
276
277#include OS_CPU_HEADER_INLINE(bytes)
278
279#endif // CPU_PPC_VM_BYTES_PPC_HPP
280