1 /* |
|
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
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 #include "precompiled.hpp" |
|
26 #include "utilities/endian.hpp" |
|
27 #include "utilities/bytes.hpp" |
|
28 |
|
29 #ifndef bswap_16 |
|
30 extern "C" inline u2 bswap_16(u2 x) { |
|
31 return ((x & 0xFF) << 8) | |
|
32 ((x >> 8) & 0xFF); |
|
33 } |
|
34 #endif |
|
35 |
|
36 #ifndef bswap_32 |
|
37 extern "C" inline u4 bswap_32(u4 x) { |
|
38 return ((x & 0xFF) << 24) | |
|
39 ((x & 0xFF00) << 8) | |
|
40 ((x >> 8) & 0xFF00) | |
|
41 ((x >> 24) & 0xFF); |
|
42 } |
|
43 #endif |
|
44 |
|
45 #ifndef bswap_64 |
|
46 extern "C" inline u8 bswap_64(u8 x) { |
|
47 return (u8)bswap_32((u4)x) << 32 | |
|
48 (u8)bswap_32((u4)(x >> 32)); |
|
49 } |
|
50 #endif |
|
51 |
|
52 u2 NativeEndian::get(u2 x) { return x; } |
|
53 u4 NativeEndian::get(u4 x) { return x; } |
|
54 u8 NativeEndian::get(u8 x) { return x; } |
|
55 s2 NativeEndian::get(s2 x) { return x; } |
|
56 s4 NativeEndian::get(s4 x) { return x; } |
|
57 s8 NativeEndian::get(s8 x) { return x; } |
|
58 |
|
59 void NativeEndian::set(u2& x, u2 y) { x = y; } |
|
60 void NativeEndian::set(u4& x, u4 y) { x = y; } |
|
61 void NativeEndian::set(u8& x, u8 y) { x = y; } |
|
62 void NativeEndian::set(s2& x, s2 y) { x = y; } |
|
63 void NativeEndian::set(s4& x, s4 y) { x = y; } |
|
64 void NativeEndian::set(s8& x, s8 y) { x = y; } |
|
65 |
|
66 NativeEndian NativeEndian::_native; |
|
67 |
|
68 u2 SwappingEndian::get(u2 x) { return bswap_16(x); } |
|
69 u4 SwappingEndian::get(u4 x) { return bswap_32(x); } |
|
70 u8 SwappingEndian::get(u8 x) { return bswap_64(x); } |
|
71 s2 SwappingEndian::get(s2 x) { return bswap_16(x); } |
|
72 s4 SwappingEndian::get(s4 x) { return bswap_32(x); } |
|
73 s8 SwappingEndian::get(s8 x) { return bswap_64(x); } |
|
74 |
|
75 void SwappingEndian::set(u2& x, u2 y) { x = bswap_16(y); } |
|
76 void SwappingEndian::set(u4& x, u4 y) { x = bswap_32(y); } |
|
77 void SwappingEndian::set(u8& x, u8 y) { x = bswap_64(y); } |
|
78 void SwappingEndian::set(s2& x, s2 y) { x = bswap_16(y); } |
|
79 void SwappingEndian::set(s4& x, s4 y) { x = bswap_32(y); } |
|
80 void SwappingEndian::set(s8& x, s8 y) { x = bswap_64(y); } |
|
81 |
|
82 SwappingEndian SwappingEndian::_swapping; |
|
83 |
|
84 Endian* Endian::get_handler(bool big_endian) { |
|
85 // If requesting little endian on a little endian machine or |
|
86 // big endian on a big endian machine use native handler |
|
87 if (big_endian == is_big_endian()) { |
|
88 return NativeEndian::get_native(); |
|
89 } else { |
|
90 // Use swapping handler. |
|
91 return SwappingEndian::get_swapping(); |
|
92 } |
|
93 } |
|
94 |
|
95 Endian* Endian::get_native_handler() { |
|
96 return NativeEndian::get_native(); |
|
97 } |
|