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: Ali Saidi 29 * Nathan Binkert 30 */ 31 32//The purpose of this file is to provide endainness conversion utility 33//functions. Depending on the endianness of the guest system, either 34//the LittleEndianGuest or BigEndianGuest namespace is used. 35 36#ifndef __SIM_BYTE_SWAP_HH__ 37#define __SIM_BYTE_SWAP_HH__ 38 39#include "sim/host.hh" 40 41// This lets us figure out what the byte order of the host system is 42#if defined(linux) 43#include <endian.h> 44// If this is a linux system, lets used the optimized definitions if they exist. 45// If one doesn't exist, we pretty much get what is listed below, so it all 46// works out 47#include <byteswap.h> 48#else 49#include <machine/endian.h> 50#endif 51
| 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: Ali Saidi 29 * Nathan Binkert 30 */ 31 32//The purpose of this file is to provide endainness conversion utility 33//functions. Depending on the endianness of the guest system, either 34//the LittleEndianGuest or BigEndianGuest namespace is used. 35 36#ifndef __SIM_BYTE_SWAP_HH__ 37#define __SIM_BYTE_SWAP_HH__ 38 39#include "sim/host.hh" 40 41// This lets us figure out what the byte order of the host system is 42#if defined(linux) 43#include <endian.h> 44// If this is a linux system, lets used the optimized definitions if they exist. 45// If one doesn't exist, we pretty much get what is listed below, so it all 46// works out 47#include <byteswap.h> 48#else 49#include <machine/endian.h> 50#endif 51
|
88#else 89 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 | 90 ((uint16_t)(x) & 0xff00) >> 8); 91#endif 92} 93 94// This function lets the compiler figure out how to call the 95// swap_byte functions above for different data types. Since the 96// sizeof() values are known at compiel time, it should inline to a 97// direct call to the right swap_byteNN() function. 98template <typename T> 99static inline T swap_byte(T x) { 100 if (sizeof(T) == 8) 101 return swap_byte64((uint64_t)x); 102 else if (sizeof(T) == 4) 103 return swap_byte32((uint32_t)x); 104 else if (sizeof(T) == 2) 105 return swap_byte16((uint16_t)x); 106 else if (sizeof(T) == 1) 107 return x; 108 else 109 panic("Can't byte-swap values larger than 64 bits"); 110} 111 112//The conversion functions with fixed endianness on both ends don't need to 113//be in a namespace 114template <typename T> static inline T betole(T value) {return swap_byte(value);} 115template <typename T> static inline T letobe(T value) {return swap_byte(value);} 116 117//For conversions not involving the guest system, we can define the functions 118//conditionally based on the BYTE_ORDER macro and outside of the namespaces 119#if BYTE_ORDER == BIG_ENDIAN 120template <typename T> static inline T htole(T value) {return swap_byte(value);} 121template <typename T> static inline T letoh(T value) {return swap_byte(value);} 122template <typename T> static inline T htobe(T value) {return value;} 123template <typename T> static inline T betoh(T value) {return value;} 124#elif BYTE_ORDER == LITTLE_ENDIAN 125template <typename T> static inline T htole(T value) {return value;} 126template <typename T> static inline T letoh(T value) {return value;} 127template <typename T> static inline T htobe(T value) {return swap_byte(value);} 128template <typename T> static inline T betoh(T value) {return swap_byte(value);} 129#else 130 #error Invalid Endianess 131#endif 132 133namespace BigEndianGuest 134{ 135 template <typename T> 136 static inline T gtole(T value) {return betole(value);} 137 template <typename T> 138 static inline T letog(T value) {return letobe(value);} 139 template <typename T> 140 static inline T gtobe(T value) {return value;} 141 template <typename T> 142 static inline T betog(T value) {return value;} 143 template <typename T> 144 static inline T htog(T value) {return htobe(value);} 145 template <typename T> 146 static inline T gtoh(T value) {return betoh(value);} 147} 148 149namespace LittleEndianGuest 150{ 151 template <typename T> 152 static inline T gtole(T value) {return value;} 153 template <typename T> 154 static inline T letog(T value) {return value;} 155 template <typename T> 156 static inline T gtobe(T value) {return letobe(value);} 157 template <typename T> 158 static inline T betog(T value) {return betole(value);} 159 template <typename T> 160 static inline T htog(T value) {return htole(value);} 161 template <typename T> 162 static inline T gtoh(T value) {return letoh(value);} 163} 164#endif // __SIM_BYTE_SWAP_HH__
| 98#else 99 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 | 100 ((uint16_t)(x) & 0xff00) >> 8); 101#endif 102} 103 104// This function lets the compiler figure out how to call the 105// swap_byte functions above for different data types. Since the 106// sizeof() values are known at compiel time, it should inline to a 107// direct call to the right swap_byteNN() function. 108template <typename T> 109static inline T swap_byte(T x) { 110 if (sizeof(T) == 8) 111 return swap_byte64((uint64_t)x); 112 else if (sizeof(T) == 4) 113 return swap_byte32((uint32_t)x); 114 else if (sizeof(T) == 2) 115 return swap_byte16((uint16_t)x); 116 else if (sizeof(T) == 1) 117 return x; 118 else 119 panic("Can't byte-swap values larger than 64 bits"); 120} 121 122//The conversion functions with fixed endianness on both ends don't need to 123//be in a namespace 124template <typename T> static inline T betole(T value) {return swap_byte(value);} 125template <typename T> static inline T letobe(T value) {return swap_byte(value);} 126 127//For conversions not involving the guest system, we can define the functions 128//conditionally based on the BYTE_ORDER macro and outside of the namespaces 129#if BYTE_ORDER == BIG_ENDIAN 130template <typename T> static inline T htole(T value) {return swap_byte(value);} 131template <typename T> static inline T letoh(T value) {return swap_byte(value);} 132template <typename T> static inline T htobe(T value) {return value;} 133template <typename T> static inline T betoh(T value) {return value;} 134#elif BYTE_ORDER == LITTLE_ENDIAN 135template <typename T> static inline T htole(T value) {return value;} 136template <typename T> static inline T letoh(T value) {return value;} 137template <typename T> static inline T htobe(T value) {return swap_byte(value);} 138template <typename T> static inline T betoh(T value) {return swap_byte(value);} 139#else 140 #error Invalid Endianess 141#endif 142 143namespace BigEndianGuest 144{ 145 template <typename T> 146 static inline T gtole(T value) {return betole(value);} 147 template <typename T> 148 static inline T letog(T value) {return letobe(value);} 149 template <typename T> 150 static inline T gtobe(T value) {return value;} 151 template <typename T> 152 static inline T betog(T value) {return value;} 153 template <typename T> 154 static inline T htog(T value) {return htobe(value);} 155 template <typename T> 156 static inline T gtoh(T value) {return betoh(value);} 157} 158 159namespace LittleEndianGuest 160{ 161 template <typename T> 162 static inline T gtole(T value) {return value;} 163 template <typename T> 164 static inline T letog(T value) {return value;} 165 template <typename T> 166 static inline T gtobe(T value) {return letobe(value);} 167 template <typename T> 168 static inline T betog(T value) {return betole(value);} 169 template <typename T> 170 static inline T htog(T value) {return htole(value);} 171 template <typename T> 172 static inline T gtoh(T value) {return letoh(value);} 173} 174#endif // __SIM_BYTE_SWAP_HH__
|