Merge zizzer.eecs.umich.edu:/bk/newmem
[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/misc.hh"
41 #include "sim/host.hh"
42
43 // This lets us figure out what the byte order of the host system is
44 #if defined(linux)
45 #include <endian.h>
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
48 // works out
49 #include <byteswap.h>
50 #elif defined (__sun)
51 #include <sys/isa_defs.h>
52 #else
53 #include <machine/endian.h>
54 #endif
55
56 #if defined(__APPLE__)
57 #include <libkern/OSByteOrder.h>
58 #endif
59
60 enum ByteOrder {BigEndianByteOrder, LittleEndianByteOrder};
61
62 //These functions actually perform the swapping for parameters
63 //of various bit lengths
64 static inline uint64_t
65 swap_byte64(uint64_t x)
66 {
67 #if defined(linux)
68 return bswap_64(x);
69 #elif defined(__APPLE__)
70 return OSSwapInt64(x);
71 #else
72 return (uint64_t)((((uint64_t)(x) & 0xff) << 56) |
73 ((uint64_t)(x) & 0xff00ULL) << 40 |
74 ((uint64_t)(x) & 0xff0000ULL) << 24 |
75 ((uint64_t)(x) & 0xff000000ULL) << 8 |
76 ((uint64_t)(x) & 0xff00000000ULL) >> 8 |
77 ((uint64_t)(x) & 0xff0000000000ULL) >> 24 |
78 ((uint64_t)(x) & 0xff000000000000ULL) >> 40 |
79 ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ;
80 #endif
81 }
82
83 static inline uint32_t
84 swap_byte32(uint32_t x)
85 {
86 #if defined(linux)
87 return bswap_32(x);
88 #elif defined(__APPLE__)
89 return OSSwapInt32(x);
90 #else
91 return (uint32_t)(((uint32_t)(x) & 0xff) << 24 |
92 ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 |
93 ((uint32_t)(x) & 0xff000000) >> 24);
94 #endif
95 }
96
97 static inline uint16_t
98 swap_byte16(uint16_t x)
99 {
100 #if defined(linux)
101 return bswap_16(x);
102 #elif defined(__APPLE__)
103 return OSSwapInt16(x);
104 #else
105 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 |
106 ((uint16_t)(x) & 0xff00) >> 8);
107 #endif
108 }
109
110 // This function lets the compiler figure out how to call the
111 // swap_byte functions above for different data types. Since the
112 // sizeof() values are known at compiel time, it should inline to a
113 // direct call to the right swap_byteNN() function.
114 template <typename T>
115 static inline T swap_byte(T x) {
116 if (sizeof(T) == 8)
117 return swap_byte64((uint64_t)x);
118 else if (sizeof(T) == 4)
119 return swap_byte32((uint32_t)x);
120 else if (sizeof(T) == 2)
121 return swap_byte16((uint16_t)x);
122 else if (sizeof(T) == 1)
123 return x;
124 else
125 panic("Can't byte-swap values larger than 64 bits");
126 }
127
128 //The conversion functions with fixed endianness on both ends don't need to
129 //be in a namespace
130 template <typename T> static inline T betole(T value) {return swap_byte(value);}
131 template <typename T> static inline T letobe(T value) {return swap_byte(value);}
132
133 //For conversions not involving the guest system, we can define the functions
134 //conditionally based on the BYTE_ORDER macro and outside of the namespaces
135 #if defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
136 const ByteOrder HostByteOrder = BigEndianByteOrder;
137 template <typename T> static inline T htole(T value) {return swap_byte(value);}
138 template <typename T> static inline T letoh(T value) {return swap_byte(value);}
139 template <typename T> static inline T htobe(T value) {return value;}
140 template <typename T> static inline T betoh(T value) {return value;}
141 #elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN
142 const ByteOrder HostByteOrder = LittleEndianByteOrder;
143 template <typename T> static inline T htole(T value) {return value;}
144 template <typename T> static inline T letoh(T value) {return value;}
145 template <typename T> static inline T htobe(T value) {return swap_byte(value);}
146 template <typename T> static inline T betoh(T value) {return swap_byte(value);}
147 #else
148 #error Invalid Endianess
149 #endif
150
151 namespace BigEndianGuest
152 {
153 const bool ByteOrderDiffers = (HostByteOrder != BigEndianByteOrder);
154 template <typename T>
155 static inline T gtole(T value) {return betole(value);}
156 template <typename T>
157 static inline T letog(T value) {return letobe(value);}
158 template <typename T>
159 static inline T gtobe(T value) {return value;}
160 template <typename T>
161 static inline T betog(T value) {return value;}
162 template <typename T>
163 static inline T htog(T value) {return htobe(value);}
164 template <typename T>
165 static inline T gtoh(T value) {return betoh(value);}
166 }
167
168 namespace LittleEndianGuest
169 {
170 const bool ByteOrderDiffers = (HostByteOrder != LittleEndianByteOrder);
171 template <typename T>
172 static inline T gtole(T value) {return value;}
173 template <typename T>
174 static inline T letog(T value) {return value;}
175 template <typename T>
176 static inline T gtobe(T value) {return letobe(value);}
177 template <typename T>
178 static inline T betog(T value) {return betole(value);}
179 template <typename T>
180 static inline T htog(T value) {return htole(value);}
181 template <typename T>
182 static inline T gtoh(T value) {return letoh(value);}
183 }
184 #endif // __SIM_BYTE_SWAP_HH__