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 406214Snate@binkert.org#include "base/types.hh" 412036SN/A 422036SN/A// This lets us figure out what the byte order of the host system is 438902Sandreas.hansson@arm.com#if defined(__linux__) 442036SN/A#include <endian.h> 452565SN/A// If this is a linux system, lets used the optimized definitions if they exist. 462565SN/A// If one doesn't exist, we pretty much get what is listed below, so it all 472565SN/A// works out 482565SN/A#include <byteswap.h> 493918Ssaidi@eecs.umich.edu#elif defined (__sun) 503483Ssaidi@eecs.umich.edu#include <sys/isa_defs.h> 512036SN/A#else 522036SN/A#include <machine/endian.h> 532036SN/A#endif 542036SN/A 552778Ssaidi@eecs.umich.edu#if defined(__APPLE__) 562778Ssaidi@eecs.umich.edu#include <libkern/OSByteOrder.h> 572778Ssaidi@eecs.umich.edu#endif 582778Ssaidi@eecs.umich.edu 592036SN/A//These functions actually perform the swapping for parameters 602036SN/A//of various bit lengths 615549Snate@binkert.orginline uint64_t 622036SN/Aswap_byte64(uint64_t x) 632036SN/A{ 648902Sandreas.hansson@arm.com#if defined(__linux__) 652565SN/A return bswap_64(x); 662778Ssaidi@eecs.umich.edu#elif defined(__APPLE__) 672778Ssaidi@eecs.umich.edu return OSSwapInt64(x); 682565SN/A#else 692036SN/A return (uint64_t)((((uint64_t)(x) & 0xff) << 56) | 702036SN/A ((uint64_t)(x) & 0xff00ULL) << 40 | 712036SN/A ((uint64_t)(x) & 0xff0000ULL) << 24 | 722036SN/A ((uint64_t)(x) & 0xff000000ULL) << 8 | 732036SN/A ((uint64_t)(x) & 0xff00000000ULL) >> 8 | 742036SN/A ((uint64_t)(x) & 0xff0000000000ULL) >> 24 | 752036SN/A ((uint64_t)(x) & 0xff000000000000ULL) >> 40 | 762036SN/A ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ; 772565SN/A#endif 782036SN/A} 792036SN/A 805549Snate@binkert.orginline uint32_t 812036SN/Aswap_byte32(uint32_t x) 822036SN/A{ 838902Sandreas.hansson@arm.com#if defined(__linux__) 842565SN/A return bswap_32(x); 852778Ssaidi@eecs.umich.edu#elif defined(__APPLE__) 862778Ssaidi@eecs.umich.edu return OSSwapInt32(x); 872565SN/A#else 882036SN/A return (uint32_t)(((uint32_t)(x) & 0xff) << 24 | 892036SN/A ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 | 902036SN/A ((uint32_t)(x) & 0xff000000) >> 24); 912565SN/A#endif 922036SN/A} 932036SN/A 945549Snate@binkert.orginline uint16_t 952036SN/Aswap_byte16(uint16_t x) 962036SN/A{ 978902Sandreas.hansson@arm.com#if defined(__linux__) 982565SN/A return bswap_16(x); 992778Ssaidi@eecs.umich.edu#elif defined(__APPLE__) 1002778Ssaidi@eecs.umich.edu return OSSwapInt16(x); 1012565SN/A#else 1022036SN/A return (uint16_t)(((uint16_t)(x) & 0xff) << 8 | 1032036SN/A ((uint16_t)(x) & 0xff00) >> 8); 1042565SN/A#endif 1052036SN/A} 1062036SN/A 1072764Sstever@eecs.umich.edu// This function lets the compiler figure out how to call the 1082764Sstever@eecs.umich.edu// swap_byte functions above for different data types. Since the 1094176Sgblack@eecs.umich.edu// sizeof() values are known at compile time, it should inline to a 1102764Sstever@eecs.umich.edu// direct call to the right swap_byteNN() function. 1112764Sstever@eecs.umich.edutemplate <typename T> 1125549Snate@binkert.orginline T swap_byte(T x) { 1132764Sstever@eecs.umich.edu if (sizeof(T) == 8) 1142764Sstever@eecs.umich.edu return swap_byte64((uint64_t)x); 1152764Sstever@eecs.umich.edu else if (sizeof(T) == 4) 1162764Sstever@eecs.umich.edu return swap_byte32((uint32_t)x); 1172764Sstever@eecs.umich.edu else if (sizeof(T) == 2) 1182764Sstever@eecs.umich.edu return swap_byte16((uint16_t)x); 1192764Sstever@eecs.umich.edu else if (sizeof(T) == 1) 1202764Sstever@eecs.umich.edu return x; 1212764Sstever@eecs.umich.edu else 1222764Sstever@eecs.umich.edu panic("Can't byte-swap values larger than 64 bits"); 1232764Sstever@eecs.umich.edu} 1242036SN/A 12512383Sgabeblack@google.comtemplate <typename T, size_t N> 12612383Sgabeblack@google.cominline std::array<T, N> 12712383Sgabeblack@google.comswap_byte(std::array<T, N> a) 12812383Sgabeblack@google.com{ 12912383Sgabeblack@google.com for (T &v: a) 13012383Sgabeblack@google.com v = swap_byte(v); 13112383Sgabeblack@google.com return a; 13212383Sgabeblack@google.com} 13312383Sgabeblack@google.com 1342036SN/A//The conversion functions with fixed endianness on both ends don't need to 1352036SN/A//be in a namespace 1365549Snate@binkert.orgtemplate <typename T> inline T betole(T value) {return swap_byte(value);} 1375549Snate@binkert.orgtemplate <typename T> inline T letobe(T value) {return swap_byte(value);} 1382036SN/A 1392036SN/A//For conversions not involving the guest system, we can define the functions 1402036SN/A//conditionally based on the BYTE_ORDER macro and outside of the namespaces 14110776Sbr@bsdpad.com#if (defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN)) && BYTE_ORDER == BIG_ENDIAN 1423799Sgblack@eecs.umich.educonst ByteOrder HostByteOrder = BigEndianByteOrder; 1435549Snate@binkert.orgtemplate <typename T> inline T htole(T value) {return swap_byte(value);} 1445549Snate@binkert.orgtemplate <typename T> inline T letoh(T value) {return swap_byte(value);} 1455549Snate@binkert.orgtemplate <typename T> inline T htobe(T value) {return value;} 1465549Snate@binkert.orgtemplate <typename T> inline T betoh(T value) {return value;} 1473483Ssaidi@eecs.umich.edu#elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN 1483799Sgblack@eecs.umich.educonst ByteOrder HostByteOrder = LittleEndianByteOrder; 1495549Snate@binkert.orgtemplate <typename T> inline T htole(T value) {return value;} 1505549Snate@binkert.orgtemplate <typename T> inline T letoh(T value) {return value;} 1515549Snate@binkert.orgtemplate <typename T> inline T htobe(T value) {return swap_byte(value);} 1525549Snate@binkert.orgtemplate <typename T> inline T betoh(T value) {return swap_byte(value);} 1532036SN/A#else 1542036SN/A #error Invalid Endianess 1552036SN/A#endif 1562036SN/A 15712521Schuan.zhu@arm.comtemplate <typename T> 15812521Schuan.zhu@arm.cominline T htog(T value, ByteOrder guest_byte_order) 15912521Schuan.zhu@arm.com{ 16012521Schuan.zhu@arm.com return guest_byte_order == BigEndianByteOrder ? 16112521Schuan.zhu@arm.com htobe(value) : htole(value); 16212521Schuan.zhu@arm.com} 16312521Schuan.zhu@arm.com 16412521Schuan.zhu@arm.comtemplate <typename T> 16512521Schuan.zhu@arm.cominline T gtoh(T value, ByteOrder guest_byte_order) 16612521Schuan.zhu@arm.com{ 16712521Schuan.zhu@arm.com return guest_byte_order == BigEndianByteOrder ? 16812521Schuan.zhu@arm.com betoh(value) : letoh(value); 16912521Schuan.zhu@arm.com} 17012521Schuan.zhu@arm.com 1712036SN/Anamespace BigEndianGuest 1722036SN/A{ 1738561Sgblack@eecs.umich.edu const ByteOrder GuestByteOrder = BigEndianByteOrder; 1743799Sgblack@eecs.umich.edu template <typename T> 1755549Snate@binkert.org inline T gtole(T value) {return betole(value);} 1763799Sgblack@eecs.umich.edu template <typename T> 1775549Snate@binkert.org inline T letog(T value) {return letobe(value);} 1783799Sgblack@eecs.umich.edu template <typename T> 1795549Snate@binkert.org inline T gtobe(T value) {return value;} 1803799Sgblack@eecs.umich.edu template <typename T> 1815549Snate@binkert.org inline T betog(T value) {return value;} 1823799Sgblack@eecs.umich.edu template <typename T> 1835549Snate@binkert.org inline T htog(T value) {return htobe(value);} 1843799Sgblack@eecs.umich.edu template <typename T> 1855549Snate@binkert.org inline T gtoh(T value) {return betoh(value);} 1862036SN/A} 1872036SN/A 1882036SN/Anamespace LittleEndianGuest 1892036SN/A{ 1908561Sgblack@eecs.umich.edu const ByteOrder GuestByteOrder = LittleEndianByteOrder; 1913799Sgblack@eecs.umich.edu template <typename T> 1925549Snate@binkert.org inline T gtole(T value) {return value;} 1933799Sgblack@eecs.umich.edu template <typename T> 1945549Snate@binkert.org inline T letog(T value) {return value;} 1953799Sgblack@eecs.umich.edu template <typename T> 1965549Snate@binkert.org inline T gtobe(T value) {return letobe(value);} 1973799Sgblack@eecs.umich.edu template <typename T> 1985549Snate@binkert.org inline T betog(T value) {return betole(value);} 1993799Sgblack@eecs.umich.edu template <typename T> 2005549Snate@binkert.org inline T htog(T value) {return htole(value);} 2013799Sgblack@eecs.umich.edu template <typename T> 2025549Snate@binkert.org inline T gtoh(T value) {return letoh(value);} 2032036SN/A} 2042036SN/A#endif // __SIM_BYTE_SWAP_HH__ 205