byteswap.hh revision 4176
12036SN/A/* 22036SN/A * Copyright (c) 2004 The Regents of The University of Michigan 32036SN/A * All rights reserved. 42036SN/A * 52036SN/A * Redistribution and use in source and binary forms, with or without 62036SN/A * modification, are permitted provided that the following conditions are 72036SN/A * met: redistributions of source code must retain the above copyright 82036SN/A * notice, this list of conditions and the following disclaimer; 92036SN/A * redistributions in binary form must reproduce the above copyright 102036SN/A * notice, this list of conditions and the following disclaimer in the 112036SN/A * documentation and/or other materials provided with the distribution; 122036SN/A * neither the name of the copyright holders nor the names of its 132036SN/A * contributors may be used to endorse or promote products derived from 142036SN/A * this software without specific prior written permission. 152036SN/A * 162036SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172036SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182036SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192036SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202036SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212036SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222036SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232036SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242036SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252036SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262036SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282956Sgblack@eecs.umich.edu * Authors: Gabe Black 292956Sgblack@eecs.umich.edu * Ali Saidi 302772Ssaidi@eecs.umich.edu * Nathan Binkert 312036SN/A */ 322036SN/A 332036SN/A//The purpose of this file is to provide endainness conversion utility 342036SN/A//functions. Depending on the endianness of the guest system, either 352036SN/A//the LittleEndianGuest or BigEndianGuest namespace is used. 362036SN/A 372036SN/A#ifndef __SIM_BYTE_SWAP_HH__ 382036SN/A#define __SIM_BYTE_SWAP_HH__ 392036SN/A 404176Sgblack@eecs.umich.edu#include "base/bigint.hh" 412779Sbinkertn@umich.edu#include "base/misc.hh" 422036SN/A#include "sim/host.hh" 432036SN/A 442036SN/A// This lets us figure out what the byte order of the host system is 452036SN/A#if defined(linux) 462036SN/A#include <endian.h> 472565SN/A// If this is a linux system, lets used the optimized definitions if they exist. 482565SN/A// If one doesn't exist, we pretty much get what is listed below, so it all 492565SN/A// works out 502565SN/A#include <byteswap.h> 513918Ssaidi@eecs.umich.edu#elif defined (__sun) 523483Ssaidi@eecs.umich.edu#include <sys/isa_defs.h> 532036SN/A#else 542036SN/A#include <machine/endian.h> 552036SN/A#endif 562036SN/A 572778Ssaidi@eecs.umich.edu#if defined(__APPLE__) 582778Ssaidi@eecs.umich.edu#include <libkern/OSByteOrder.h> 592778Ssaidi@eecs.umich.edu#endif 602778Ssaidi@eecs.umich.edu 613799Sgblack@eecs.umich.eduenum ByteOrder {BigEndianByteOrder, LittleEndianByteOrder}; 623799Sgblack@eecs.umich.edu 632036SN/A//These functions actually perform the swapping for parameters 642036SN/A//of various bit lengths 652036SN/Astatic inline uint64_t 662036SN/Aswap_byte64(uint64_t x) 672036SN/A{ 682565SN/A#if defined(linux) 692565SN/A return bswap_64(x); 702778Ssaidi@eecs.umich.edu#elif defined(__APPLE__) 712778Ssaidi@eecs.umich.edu return OSSwapInt64(x); 722565SN/A#else 732036SN/A return (uint64_t)((((uint64_t)(x) & 0xff) << 56) | 742036SN/A ((uint64_t)(x) & 0xff00ULL) << 40 | 752036SN/A ((uint64_t)(x) & 0xff0000ULL) << 24 | 762036SN/A ((uint64_t)(x) & 0xff000000ULL) << 8 | 772036SN/A ((uint64_t)(x) & 0xff00000000ULL) >> 8 | 782036SN/A ((uint64_t)(x) & 0xff0000000000ULL) >> 24 | 792036SN/A ((uint64_t)(x) & 0xff000000000000ULL) >> 40 | 802036SN/A ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ; 812565SN/A#endif 822036SN/A} 832036SN/A 842036SN/Astatic inline uint32_t 852036SN/Aswap_byte32(uint32_t x) 862036SN/A{ 872565SN/A#if defined(linux) 882565SN/A return bswap_32(x); 892778Ssaidi@eecs.umich.edu#elif defined(__APPLE__) 902778Ssaidi@eecs.umich.edu return OSSwapInt32(x); 912565SN/A#else 922036SN/A return (uint32_t)(((uint32_t)(x) & 0xff) << 24 | 932036SN/A ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 | 942036SN/A ((uint32_t)(x) & 0xff000000) >> 24); 952565SN/A#endif 962036SN/A} 972036SN/A 982036SN/Astatic inline uint16_t 992036SN/Aswap_byte16(uint16_t x) 1002036SN/A{ 1012565SN/A#if defined(linux) 1022565SN/A return bswap_16(x); 1032778Ssaidi@eecs.umich.edu#elif defined(__APPLE__) 1042778Ssaidi@eecs.umich.edu return OSSwapInt16(x); 1052565SN/A#else 1062036SN/A return (uint16_t)(((uint16_t)(x) & 0xff) << 8 | 1072036SN/A ((uint16_t)(x) & 0xff00) >> 8); 1082565SN/A#endif 1092036SN/A} 1102036SN/A 1112764Sstever@eecs.umich.edu// This function lets the compiler figure out how to call the 1122764Sstever@eecs.umich.edu// swap_byte functions above for different data types. Since the 1134176Sgblack@eecs.umich.edu// sizeof() values are known at compile time, it should inline to a 1142764Sstever@eecs.umich.edu// direct call to the right swap_byteNN() function. 1152764Sstever@eecs.umich.edutemplate <typename T> 1162764Sstever@eecs.umich.edustatic inline T swap_byte(T x) { 1172764Sstever@eecs.umich.edu if (sizeof(T) == 8) 1182764Sstever@eecs.umich.edu return swap_byte64((uint64_t)x); 1192764Sstever@eecs.umich.edu else if (sizeof(T) == 4) 1202764Sstever@eecs.umich.edu return swap_byte32((uint32_t)x); 1212764Sstever@eecs.umich.edu else if (sizeof(T) == 2) 1222764Sstever@eecs.umich.edu return swap_byte16((uint16_t)x); 1232764Sstever@eecs.umich.edu else if (sizeof(T) == 1) 1242764Sstever@eecs.umich.edu return x; 1252764Sstever@eecs.umich.edu else 1262764Sstever@eecs.umich.edu panic("Can't byte-swap values larger than 64 bits"); 1272764Sstever@eecs.umich.edu} 1282036SN/A 1294176Sgblack@eecs.umich.edutemplate<> 1304176Sgblack@eecs.umich.edustatic inline Twin64_t swap_byte<Twin64_t>(Twin64_t x) 1314176Sgblack@eecs.umich.edu{ 1324176Sgblack@eecs.umich.edu x.a = swap_byte(x.a); 1334176Sgblack@eecs.umich.edu x.b = swap_byte(x.b); 1344176Sgblack@eecs.umich.edu return x; 1354176Sgblack@eecs.umich.edu} 1364176Sgblack@eecs.umich.edu 1374176Sgblack@eecs.umich.edutemplate<> 1384176Sgblack@eecs.umich.edustatic inline Twin32_t swap_byte<Twin32_t>(Twin32_t x) 1394176Sgblack@eecs.umich.edu{ 1404176Sgblack@eecs.umich.edu x.a = swap_byte(x.a); 1414176Sgblack@eecs.umich.edu x.b = swap_byte(x.b); 1424176Sgblack@eecs.umich.edu return x; 1434176Sgblack@eecs.umich.edu} 1444176Sgblack@eecs.umich.edu 1452036SN/A//The conversion functions with fixed endianness on both ends don't need to 1462036SN/A//be in a namespace 1472036SN/Atemplate <typename T> static inline T betole(T value) {return swap_byte(value);} 1482036SN/Atemplate <typename T> static inline T letobe(T value) {return swap_byte(value);} 1492036SN/A 1502036SN/A//For conversions not involving the guest system, we can define the functions 1512036SN/A//conditionally based on the BYTE_ORDER macro and outside of the namespaces 1523927Ssaidi@eecs.umich.edu#if defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN) && BYTE_ORDER == BIG_ENDIAN 1533799Sgblack@eecs.umich.educonst ByteOrder HostByteOrder = BigEndianByteOrder; 1542036SN/Atemplate <typename T> static inline T htole(T value) {return swap_byte(value);} 1552036SN/Atemplate <typename T> static inline T letoh(T value) {return swap_byte(value);} 1562036SN/Atemplate <typename T> static inline T htobe(T value) {return value;} 1572036SN/Atemplate <typename T> static inline T betoh(T value) {return value;} 1583483Ssaidi@eecs.umich.edu#elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN 1593799Sgblack@eecs.umich.educonst ByteOrder HostByteOrder = LittleEndianByteOrder; 1602036SN/Atemplate <typename T> static inline T htole(T value) {return value;} 1612036SN/Atemplate <typename T> static inline T letoh(T value) {return value;} 1622036SN/Atemplate <typename T> static inline T htobe(T value) {return swap_byte(value);} 1632036SN/Atemplate <typename T> static inline T betoh(T value) {return swap_byte(value);} 1642036SN/A#else 1652036SN/A #error Invalid Endianess 1662036SN/A#endif 1672036SN/A 1682036SN/Anamespace BigEndianGuest 1692036SN/A{ 1703799Sgblack@eecs.umich.edu const bool ByteOrderDiffers = (HostByteOrder != BigEndianByteOrder); 1713799Sgblack@eecs.umich.edu template <typename T> 1723799Sgblack@eecs.umich.edu static inline T gtole(T value) {return betole(value);} 1733799Sgblack@eecs.umich.edu template <typename T> 1743799Sgblack@eecs.umich.edu static inline T letog(T value) {return letobe(value);} 1753799Sgblack@eecs.umich.edu template <typename T> 1763799Sgblack@eecs.umich.edu static inline T gtobe(T value) {return value;} 1773799Sgblack@eecs.umich.edu template <typename T> 1783799Sgblack@eecs.umich.edu static inline T betog(T value) {return value;} 1793799Sgblack@eecs.umich.edu template <typename T> 1803799Sgblack@eecs.umich.edu static inline T htog(T value) {return htobe(value);} 1813799Sgblack@eecs.umich.edu template <typename T> 1823799Sgblack@eecs.umich.edu static inline T gtoh(T value) {return betoh(value);} 1832036SN/A} 1842036SN/A 1852036SN/Anamespace LittleEndianGuest 1862036SN/A{ 1873799Sgblack@eecs.umich.edu const bool ByteOrderDiffers = (HostByteOrder != LittleEndianByteOrder); 1883799Sgblack@eecs.umich.edu template <typename T> 1893799Sgblack@eecs.umich.edu static inline T gtole(T value) {return value;} 1903799Sgblack@eecs.umich.edu template <typename T> 1913799Sgblack@eecs.umich.edu static inline T letog(T value) {return value;} 1923799Sgblack@eecs.umich.edu template <typename T> 1933799Sgblack@eecs.umich.edu static inline T gtobe(T value) {return letobe(value);} 1943799Sgblack@eecs.umich.edu template <typename T> 1953799Sgblack@eecs.umich.edu static inline T betog(T value) {return betole(value);} 1963799Sgblack@eecs.umich.edu template <typename T> 1973799Sgblack@eecs.umich.edu static inline T htog(T value) {return htole(value);} 1983799Sgblack@eecs.umich.edu template <typename T> 1993799Sgblack@eecs.umich.edu static inline T gtoh(T value) {return letoh(value);} 2002036SN/A} 2012036SN/A#endif // __SIM_BYTE_SWAP_HH__ 202