byteswap.hh revision 2665
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 * 282665Ssaidi@eecs.umich.edu * Authors: Gabe Black 292036SN/A */ 302036SN/A 312036SN/A//The purpose of this file is to provide endainness conversion utility 322036SN/A//functions. Depending on the endianness of the guest system, either 332036SN/A//the LittleEndianGuest or BigEndianGuest namespace is used. 342036SN/A 352036SN/A#ifndef __SIM_BYTE_SWAP_HH__ 362036SN/A#define __SIM_BYTE_SWAP_HH__ 372036SN/A 382036SN/A#include "sim/host.hh" 392036SN/A 402036SN/A// This lets us figure out what the byte order of the host system is 412036SN/A#if defined(linux) 422036SN/A#include <endian.h> 432565SN/A// If this is a linux system, lets used the optimized definitions if they exist. 442565SN/A// If one doesn't exist, we pretty much get what is listed below, so it all 452565SN/A// works out 462565SN/A#include <byteswap.h> 472036SN/A#else 482036SN/A#include <machine/endian.h> 492036SN/A#endif 502036SN/A 512036SN/A//These functions actually perform the swapping for parameters 522036SN/A//of various bit lengths 532036SN/Astatic inline uint64_t 542036SN/Aswap_byte64(uint64_t x) 552036SN/A{ 562565SN/A#if defined(linux) 572565SN/A return bswap_64(x); 582565SN/A#else 592036SN/A return (uint64_t)((((uint64_t)(x) & 0xff) << 56) | 602036SN/A ((uint64_t)(x) & 0xff00ULL) << 40 | 612036SN/A ((uint64_t)(x) & 0xff0000ULL) << 24 | 622036SN/A ((uint64_t)(x) & 0xff000000ULL) << 8 | 632036SN/A ((uint64_t)(x) & 0xff00000000ULL) >> 8 | 642036SN/A ((uint64_t)(x) & 0xff0000000000ULL) >> 24 | 652036SN/A ((uint64_t)(x) & 0xff000000000000ULL) >> 40 | 662036SN/A ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ; 672565SN/A#endif 682036SN/A} 692036SN/A 702036SN/Astatic inline uint32_t 712036SN/Aswap_byte32(uint32_t x) 722036SN/A{ 732565SN/A#if defined(linux) 742565SN/A return bswap_32(x); 752565SN/A#else 762036SN/A return (uint32_t)(((uint32_t)(x) & 0xff) << 24 | 772036SN/A ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 | 782036SN/A ((uint32_t)(x) & 0xff000000) >> 24); 792565SN/A#endif 802036SN/A} 812036SN/A 822036SN/Astatic inline uint16_t 832036SN/Aswap_byte16(uint16_t x) 842036SN/A{ 852565SN/A#if defined(linux) 862565SN/A return bswap_16(x); 872565SN/A#else 882036SN/A return (uint16_t)(((uint16_t)(x) & 0xff) << 8 | 892036SN/A ((uint16_t)(x) & 0xff00) >> 8); 902565SN/A#endif 912036SN/A} 922036SN/A 932036SN/A//This lets the compiler figure out how to call the swap_byte functions above 942036SN/A//for different data types. 952073SN/Astatic inline uint64_t swap_byte(uint64_t x) {return swap_byte64(x);} 962073SN/Astatic inline int64_t swap_byte(int64_t x) {return swap_byte64((uint64_t)x);} 972073SN/Astatic inline uint32_t swap_byte(uint32_t x) {return swap_byte32(x);} 982073SN/Astatic inline int32_t swap_byte(int32_t x) {return swap_byte32((uint32_t)x);} 992483SN/A//This is to prevent the following two functions from compiling on 1002483SN/A//64bit machines. It won't detect everything, so it should be changed. 1012483SN/A#ifndef __x86_64__ 1022091SN/Astatic inline long swap_byte(long x) {return swap_byte32((long)x);} 1032091SN/Astatic inline unsigned long swap_byte(unsigned long x) 1042091SN/A { return swap_byte32((unsigned long)x);} 1052483SN/A#endif 1062073SN/Astatic inline uint16_t swap_byte(uint16_t x) {return swap_byte32(x);} 1072073SN/Astatic inline int16_t swap_byte(int16_t x) {return swap_byte16((uint16_t)x);} 1082073SN/Astatic inline uint8_t swap_byte(uint8_t x) {return x;} 1092073SN/Astatic inline int8_t swap_byte(int8_t x) {return x;} 1102073SN/Astatic inline double swap_byte(double x) {return swap_byte64((uint64_t)x);} 1112036SN/Astatic inline float swap_byte(float x) {return swap_byte32((uint32_t)x);} 1122036SN/A 1132036SN/A//The conversion functions with fixed endianness on both ends don't need to 1142036SN/A//be in a namespace 1152036SN/Atemplate <typename T> static inline T betole(T value) {return swap_byte(value);} 1162036SN/Atemplate <typename T> static inline T letobe(T value) {return swap_byte(value);} 1172036SN/A 1182036SN/A//For conversions not involving the guest system, we can define the functions 1192036SN/A//conditionally based on the BYTE_ORDER macro and outside of the namespaces 1202036SN/A#if BYTE_ORDER == BIG_ENDIAN 1212036SN/Atemplate <typename T> static inline T htole(T value) {return swap_byte(value);} 1222036SN/Atemplate <typename T> static inline T letoh(T value) {return swap_byte(value);} 1232036SN/Atemplate <typename T> static inline T htobe(T value) {return value;} 1242036SN/Atemplate <typename T> static inline T betoh(T value) {return value;} 1252036SN/A#elif BYTE_ORDER == LITTLE_ENDIAN 1262036SN/Atemplate <typename T> static inline T htole(T value) {return value;} 1272036SN/Atemplate <typename T> static inline T letoh(T value) {return value;} 1282036SN/Atemplate <typename T> static inline T htobe(T value) {return swap_byte(value);} 1292036SN/Atemplate <typename T> static inline T betoh(T value) {return swap_byte(value);} 1302036SN/A#else 1312036SN/A #error Invalid Endianess 1322036SN/A#endif 1332036SN/A 1342036SN/Anamespace BigEndianGuest 1352036SN/A{ 1362036SN/A template <typename T> 1372036SN/A static inline T gtole(T value) {return betole(value);} 1382036SN/A template <typename T> 1392036SN/A static inline T letog(T value) {return letobe(value);} 1402036SN/A template <typename T> 1412036SN/A static inline T gtobe(T value) {return value;} 1422036SN/A template <typename T> 1432036SN/A static inline T betog(T value) {return value;} 1442036SN/A template <typename T> 1452036SN/A static inline T htog(T value) {return htobe(value);} 1462036SN/A template <typename T> 1472036SN/A static inline T gtoh(T value) {return betoh(value);} 1482036SN/A} 1492036SN/A 1502036SN/Anamespace LittleEndianGuest 1512036SN/A{ 1522036SN/A template <typename T> 1532036SN/A static inline T gtole(T value) {return value;} 1542036SN/A template <typename T> 1552036SN/A static inline T letog(T value) {return value;} 1562036SN/A template <typename T> 1572036SN/A static inline T gtobe(T value) {return letobe(value);} 1582036SN/A template <typename T> 1592036SN/A static inline T betog(T value) {return betole(value);} 1602036SN/A template <typename T> 1612036SN/A static inline T htog(T value) {return htole(value);} 1622036SN/A template <typename T> 1632036SN/A static inline T gtoh(T value) {return letoh(value);} 1642036SN/A} 1652036SN/A#endif // __SIM_BYTE_SWAP_HH__ 166