byteswap.hh revision 3927
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 402779Sbinkertn@umich.edu#include "base/misc.hh" 412036SN/A#include "sim/host.hh" 422036SN/A 432036SN/A// This lets us figure out what the byte order of the host system is 442036SN/A#if defined(linux) 452036SN/A#include <endian.h> 462565SN/A// If this is a linux system, lets used the optimized definitions if they exist. 472565SN/A// If one doesn't exist, we pretty much get what is listed below, so it all 482565SN/A// works out 492565SN/A#include <byteswap.h> 503483Ssaidi@eecs.umich.edu#elif defined (__sun__) 513483Ssaidi@eecs.umich.edu#include <sys/isa_defs.h> 522036SN/A#else 532036SN/A#include <machine/endian.h> 542036SN/A#endif 552036SN/A 562778Ssaidi@eecs.umich.edu#if defined(__APPLE__) 572778Ssaidi@eecs.umich.edu#include <libkern/OSByteOrder.h> 582778Ssaidi@eecs.umich.edu#endif 592778Ssaidi@eecs.umich.edu 602036SN/A//These functions actually perform the swapping for parameters 612036SN/A//of various bit lengths 622036SN/Astatic inline uint64_t 632036SN/Aswap_byte64(uint64_t x) 642036SN/A{ 652565SN/A#if defined(linux) 662565SN/A return bswap_64(x); 672778Ssaidi@eecs.umich.edu#elif defined(__APPLE__) 682778Ssaidi@eecs.umich.edu return OSSwapInt64(x); 692565SN/A#else 702036SN/A return (uint64_t)((((uint64_t)(x) & 0xff) << 56) | 712036SN/A ((uint64_t)(x) & 0xff00ULL) << 40 | 722036SN/A ((uint64_t)(x) & 0xff0000ULL) << 24 | 732036SN/A ((uint64_t)(x) & 0xff000000ULL) << 8 | 742036SN/A ((uint64_t)(x) & 0xff00000000ULL) >> 8 | 752036SN/A ((uint64_t)(x) & 0xff0000000000ULL) >> 24 | 762036SN/A ((uint64_t)(x) & 0xff000000000000ULL) >> 40 | 772036SN/A ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ; 782565SN/A#endif 792036SN/A} 802036SN/A 812036SN/Astatic inline uint32_t 822036SN/Aswap_byte32(uint32_t x) 832036SN/A{ 842565SN/A#if defined(linux) 852565SN/A return bswap_32(x); 862778Ssaidi@eecs.umich.edu#elif defined(__APPLE__) 872778Ssaidi@eecs.umich.edu return OSSwapInt32(x); 882565SN/A#else 892036SN/A return (uint32_t)(((uint32_t)(x) & 0xff) << 24 | 902036SN/A ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 | 912036SN/A ((uint32_t)(x) & 0xff000000) >> 24); 922565SN/A#endif 932036SN/A} 942036SN/A 952036SN/Astatic inline uint16_t 962036SN/Aswap_byte16(uint16_t x) 972036SN/A{ 982565SN/A#if defined(linux) 992565SN/A return bswap_16(x); 1002778Ssaidi@eecs.umich.edu#elif defined(__APPLE__) 1012778Ssaidi@eecs.umich.edu return OSSwapInt16(x); 1022565SN/A#else 1032036SN/A return (uint16_t)(((uint16_t)(x) & 0xff) << 8 | 1042036SN/A ((uint16_t)(x) & 0xff00) >> 8); 1052565SN/A#endif 1062036SN/A} 1072036SN/A 1082764Sstever@eecs.umich.edu// This function lets the compiler figure out how to call the 1092764Sstever@eecs.umich.edu// swap_byte functions above for different data types. Since the 1102764Sstever@eecs.umich.edu// sizeof() values are known at compiel time, it should inline to a 1112764Sstever@eecs.umich.edu// direct call to the right swap_byteNN() function. 1122764Sstever@eecs.umich.edutemplate <typename T> 1132764Sstever@eecs.umich.edustatic inline T swap_byte(T x) { 1142764Sstever@eecs.umich.edu if (sizeof(T) == 8) 1152764Sstever@eecs.umich.edu return swap_byte64((uint64_t)x); 1162764Sstever@eecs.umich.edu else if (sizeof(T) == 4) 1172764Sstever@eecs.umich.edu return swap_byte32((uint32_t)x); 1182764Sstever@eecs.umich.edu else if (sizeof(T) == 2) 1192764Sstever@eecs.umich.edu return swap_byte16((uint16_t)x); 1202764Sstever@eecs.umich.edu else if (sizeof(T) == 1) 1212764Sstever@eecs.umich.edu return x; 1222764Sstever@eecs.umich.edu else 1232764Sstever@eecs.umich.edu panic("Can't byte-swap values larger than 64 bits"); 1242764Sstever@eecs.umich.edu} 1252036SN/A 1262036SN/A//The conversion functions with fixed endianness on both ends don't need to 1272036SN/A//be in a namespace 1282036SN/Atemplate <typename T> static inline T betole(T value) {return swap_byte(value);} 1292036SN/Atemplate <typename T> static inline T letobe(T value) {return swap_byte(value);} 1302036SN/A 1312036SN/A//For conversions not involving the guest system, we can define the functions 1322036SN/A//conditionally based on the BYTE_ORDER macro and outside of the namespaces 1333927Ssaidi@eecs.umich.edu#if defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN) && BYTE_ORDER == BIG_ENDIAN 1342036SN/Atemplate <typename T> static inline T htole(T value) {return swap_byte(value);} 1352036SN/Atemplate <typename T> static inline T letoh(T value) {return swap_byte(value);} 1362036SN/Atemplate <typename T> static inline T htobe(T value) {return value;} 1372036SN/Atemplate <typename T> static inline T betoh(T value) {return value;} 1383483Ssaidi@eecs.umich.edu#elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN 1392036SN/Atemplate <typename T> static inline T htole(T value) {return value;} 1402036SN/Atemplate <typename T> static inline T letoh(T value) {return value;} 1412036SN/Atemplate <typename T> static inline T htobe(T value) {return swap_byte(value);} 1422036SN/Atemplate <typename T> static inline T betoh(T value) {return swap_byte(value);} 1432036SN/A#else 1442036SN/A #error Invalid Endianess 1452036SN/A#endif 1462036SN/A 1472036SN/Anamespace BigEndianGuest 1482036SN/A{ 1492036SN/A template <typename T> 1502036SN/A static inline T gtole(T value) {return betole(value);} 1512036SN/A template <typename T> 1522036SN/A static inline T letog(T value) {return letobe(value);} 1532036SN/A template <typename T> 1542036SN/A static inline T gtobe(T value) {return value;} 1552036SN/A template <typename T> 1562036SN/A static inline T betog(T value) {return value;} 1572036SN/A template <typename T> 1582036SN/A static inline T htog(T value) {return htobe(value);} 1592036SN/A template <typename T> 1602036SN/A static inline T gtoh(T value) {return betoh(value);} 1612036SN/A} 1622036SN/A 1632036SN/Anamespace LittleEndianGuest 1642036SN/A{ 1652036SN/A template <typename T> 1662036SN/A static inline T gtole(T value) {return value;} 1672036SN/A template <typename T> 1682036SN/A static inline T letog(T value) {return value;} 1692036SN/A template <typename T> 1702036SN/A static inline T gtobe(T value) {return letobe(value);} 1712036SN/A template <typename T> 1722036SN/A static inline T betog(T value) {return betole(value);} 1732036SN/A template <typename T> 1742036SN/A static inline T htog(T value) {return htole(value);} 1752036SN/A template <typename T> 1762036SN/A static inline T gtoh(T value) {return letoh(value);} 1772036SN/A} 1782036SN/A#endif // __SIM_BYTE_SWAP_HH__ 179