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