sim, arch, base: Refactor the base remote GDB class.
[gem5.git] / src / sim / byteswap.hh
1 /*
2 * Copyright (c) 2004 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Gabe Black
29 * Ali Saidi
30 * Nathan Binkert
31 */
32
33 //The purpose of this file is to provide endainness conversion utility
34 //functions. Depending on the endianness of the guest system, either
35 //the LittleEndianGuest or BigEndianGuest namespace is used.
36
37 #ifndef __SIM_BYTE_SWAP_HH__
38 #define __SIM_BYTE_SWAP_HH__
39
40 #include "base/types.hh"
41
42 // This lets us figure out what the byte order of the host system is
43 #if defined(__linux__)
44 #include <endian.h>
45 // If this is a linux system, lets used the optimized definitions if they exist.
46 // If one doesn't exist, we pretty much get what is listed below, so it all
47 // works out
48 #include <byteswap.h>
49 #elif defined (__sun)
50 #include <sys/isa_defs.h>
51 #else
52 #include <machine/endian.h>
53 #endif
54
55 #if defined(__APPLE__)
56 #include <libkern/OSByteOrder.h>
57 #endif
58
59 //These functions actually perform the swapping for parameters
60 //of various bit lengths
61 inline uint64_t
62 swap_byte64(uint64_t x)
63 {
64 #if defined(__linux__)
65 return bswap_64(x);
66 #elif defined(__APPLE__)
67 return OSSwapInt64(x);
68 #else
69 return (uint64_t)((((uint64_t)(x) & 0xff) << 56) |
70 ((uint64_t)(x) & 0xff00ULL) << 40 |
71 ((uint64_t)(x) & 0xff0000ULL) << 24 |
72 ((uint64_t)(x) & 0xff000000ULL) << 8 |
73 ((uint64_t)(x) & 0xff00000000ULL) >> 8 |
74 ((uint64_t)(x) & 0xff0000000000ULL) >> 24 |
75 ((uint64_t)(x) & 0xff000000000000ULL) >> 40 |
76 ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ;
77 #endif
78 }
79
80 inline uint32_t
81 swap_byte32(uint32_t x)
82 {
83 #if defined(__linux__)
84 return bswap_32(x);
85 #elif defined(__APPLE__)
86 return OSSwapInt32(x);
87 #else
88 return (uint32_t)(((uint32_t)(x) & 0xff) << 24 |
89 ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 |
90 ((uint32_t)(x) & 0xff000000) >> 24);
91 #endif
92 }
93
94 inline uint16_t
95 swap_byte16(uint16_t x)
96 {
97 #if defined(__linux__)
98 return bswap_16(x);
99 #elif defined(__APPLE__)
100 return OSSwapInt16(x);
101 #else
102 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 |
103 ((uint16_t)(x) & 0xff00) >> 8);
104 #endif
105 }
106
107 // This function lets the compiler figure out how to call the
108 // swap_byte functions above for different data types. Since the
109 // sizeof() values are known at compile time, it should inline to a
110 // direct call to the right swap_byteNN() function.
111 template <typename T>
112 inline T swap_byte(T x) {
113 if (sizeof(T) == 8)
114 return swap_byte64((uint64_t)x);
115 else if (sizeof(T) == 4)
116 return swap_byte32((uint32_t)x);
117 else if (sizeof(T) == 2)
118 return swap_byte16((uint16_t)x);
119 else if (sizeof(T) == 1)
120 return x;
121 else
122 panic("Can't byte-swap values larger than 64 bits");
123 }
124
125 template <typename T, size_t N>
126 inline std::array<T, N>
127 swap_byte(std::array<T, N> a)
128 {
129 for (T &v: a)
130 v = swap_byte(v);
131 return a;
132 }
133
134 //The conversion functions with fixed endianness on both ends don't need to
135 //be in a namespace
136 template <typename T> inline T betole(T value) {return swap_byte(value);}
137 template <typename T> inline T letobe(T value) {return swap_byte(value);}
138
139 //For conversions not involving the guest system, we can define the functions
140 //conditionally based on the BYTE_ORDER macro and outside of the namespaces
141 #if (defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN)) && BYTE_ORDER == BIG_ENDIAN
142 const ByteOrder HostByteOrder = BigEndianByteOrder;
143 template <typename T> inline T htole(T value) {return swap_byte(value);}
144 template <typename T> inline T letoh(T value) {return swap_byte(value);}
145 template <typename T> inline T htobe(T value) {return value;}
146 template <typename T> inline T betoh(T value) {return value;}
147 #elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN
148 const ByteOrder HostByteOrder = LittleEndianByteOrder;
149 template <typename T> inline T htole(T value) {return value;}
150 template <typename T> inline T letoh(T value) {return value;}
151 template <typename T> inline T htobe(T value) {return swap_byte(value);}
152 template <typename T> inline T betoh(T value) {return swap_byte(value);}
153 #else
154 #error Invalid Endianess
155 #endif
156
157 namespace BigEndianGuest
158 {
159 const ByteOrder GuestByteOrder = BigEndianByteOrder;
160 template <typename T>
161 inline T gtole(T value) {return betole(value);}
162 template <typename T>
163 inline T letog(T value) {return letobe(value);}
164 template <typename T>
165 inline T gtobe(T value) {return value;}
166 template <typename T>
167 inline T betog(T value) {return value;}
168 template <typename T>
169 inline T htog(T value) {return htobe(value);}
170 template <typename T>
171 inline T gtoh(T value) {return betoh(value);}
172 }
173
174 namespace LittleEndianGuest
175 {
176 const ByteOrder GuestByteOrder = LittleEndianByteOrder;
177 template <typename T>
178 inline T gtole(T value) {return value;}
179 template <typename T>
180 inline T letog(T value) {return value;}
181 template <typename T>
182 inline T gtobe(T value) {return letobe(value);}
183 template <typename T>
184 inline T betog(T value) {return betole(value);}
185 template <typename T>
186 inline T htog(T value) {return htole(value);}
187 template <typename T>
188 inline T gtoh(T value) {return letoh(value);}
189 }
190 #endif // __SIM_BYTE_SWAP_HH__