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