2 * Copyright (c) 2004 The Regents of The University of Michigan
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.
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.
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.
37 #ifndef __SIM_BYTE_SWAP_HH__
38 #define __SIM_BYTE_SWAP_HH__
40 #include "base/bigint.hh"
41 #include "base/types.hh"
43 // This lets us figure out what the byte order of the host system is
44 #if defined(__linux__)
46 // If this is a linux system, lets used the optimized definitions if they exist.
47 // If one doesn't exist, we pretty much get what is listed below, so it all
51 #include <sys/isa_defs.h>
53 #include <machine/endian.h>
56 #if defined(__APPLE__)
57 #include <libkern/OSByteOrder.h>
60 //These functions actually perform the swapping for parameters
61 //of various bit lengths
63 swap_byte64(uint64_t x)
65 #if defined(__linux__)
67 #elif defined(__APPLE__)
68 return OSSwapInt64(x);
70 return (uint64_t)((((uint64_t)(x) & 0xff) << 56) |
71 ((uint64_t)(x) & 0xff00ULL) << 40 |
72 ((uint64_t)(x) & 0xff0000ULL) << 24 |
73 ((uint64_t)(x) & 0xff000000ULL) << 8 |
74 ((uint64_t)(x) & 0xff00000000ULL) >> 8 |
75 ((uint64_t)(x) & 0xff0000000000ULL) >> 24 |
76 ((uint64_t)(x) & 0xff000000000000ULL) >> 40 |
77 ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ;
82 swap_byte32(uint32_t x)
84 #if defined(__linux__)
86 #elif defined(__APPLE__)
87 return OSSwapInt32(x);
89 return (uint32_t)(((uint32_t)(x) & 0xff) << 24 |
90 ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 |
91 ((uint32_t)(x) & 0xff000000) >> 24);
96 swap_byte16(uint16_t x)
98 #if defined(__linux__)
100 #elif defined(__APPLE__)
101 return OSSwapInt16(x);
103 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 |
104 ((uint16_t)(x) & 0xff00) >> 8);
108 // This function lets the compiler figure out how to call the
109 // swap_byte functions above for different data types. Since the
110 // sizeof() values are known at compile time, it should inline to a
111 // direct call to the right swap_byteNN() function.
112 template <typename T>
113 inline T swap_byte(T x) {
115 return swap_byte64((uint64_t)x);
116 else if (sizeof(T) == 4)
117 return swap_byte32((uint32_t)x);
118 else if (sizeof(T) == 2)
119 return swap_byte16((uint16_t)x);
120 else if (sizeof(T) == 1)
123 panic("Can't byte-swap values larger than 64 bits");
127 inline Twin64_t swap_byte<Twin64_t>(Twin64_t x)
129 x.a = swap_byte(x.a);
130 x.b = swap_byte(x.b);
135 inline Twin32_t swap_byte<Twin32_t>(Twin32_t x)
137 x.a = swap_byte(x.a);
138 x.b = swap_byte(x.b);
142 template <typename T, size_t N>
143 inline std::array<T, N>
144 swap_byte(std::array<T, N> a)
151 //The conversion functions with fixed endianness on both ends don't need to
153 template <typename T> inline T betole(T value) {return swap_byte(value);}
154 template <typename T> inline T letobe(T value) {return swap_byte(value);}
156 //For conversions not involving the guest system, we can define the functions
157 //conditionally based on the BYTE_ORDER macro and outside of the namespaces
158 #if (defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN)) && BYTE_ORDER == BIG_ENDIAN
159 const ByteOrder HostByteOrder = BigEndianByteOrder;
160 template <typename T> inline T htole(T value) {return swap_byte(value);}
161 template <typename T> inline T letoh(T value) {return swap_byte(value);}
162 template <typename T> inline T htobe(T value) {return value;}
163 template <typename T> inline T betoh(T value) {return value;}
164 #elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN
165 const ByteOrder HostByteOrder = LittleEndianByteOrder;
166 template <typename T> inline T htole(T value) {return value;}
167 template <typename T> inline T letoh(T value) {return value;}
168 template <typename T> inline T htobe(T value) {return swap_byte(value);}
169 template <typename T> inline T betoh(T value) {return swap_byte(value);}
171 #error Invalid Endianess
174 namespace BigEndianGuest
176 const ByteOrder GuestByteOrder = BigEndianByteOrder;
177 template <typename T>
178 inline T gtole(T value) {return betole(value);}
179 template <typename T>
180 inline T letog(T value) {return letobe(value);}
181 template <typename T>
182 inline T gtobe(T value) {return value;}
183 template <typename T>
184 inline T betog(T value) {return value;}
185 template <typename T>
186 inline T htog(T value) {return htobe(value);}
187 template <typename T>
188 inline T gtoh(T value) {return betoh(value);}
191 namespace LittleEndianGuest
193 const ByteOrder GuestByteOrder = LittleEndianByteOrder;
194 template <typename T>
195 inline T gtole(T value) {return value;}
196 template <typename T>
197 inline T letog(T value) {return value;}
198 template <typename T>
199 inline T gtobe(T value) {return letobe(value);}
200 template <typename T>
201 inline T betog(T value) {return betole(value);}
202 template <typename T>
203 inline T htog(T value) {return htole(value);}
204 template <typename T>
205 inline T gtoh(T value) {return letoh(value);}
207 #endif // __SIM_BYTE_SWAP_HH__