byteswap.hh revision 12383:57de7e3b7ba0
15081Sgblack@eecs.umich.edu/*
25081Sgblack@eecs.umich.edu * Copyright (c) 2004 The Regents of The University of Michigan
35081Sgblack@eecs.umich.edu * All rights reserved.
47087Snate@binkert.org *
57087Snate@binkert.org * Redistribution and use in source and binary forms, with or without
67087Snate@binkert.org * modification, are permitted provided that the following conditions are
77087Snate@binkert.org * met: redistributions of source code must retain the above copyright
87087Snate@binkert.org * notice, this list of conditions and the following disclaimer;
97087Snate@binkert.org * redistributions in binary form must reproduce the above copyright
107087Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
117087Snate@binkert.org * documentation and/or other materials provided with the distribution;
125081Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
137087Snate@binkert.org * contributors may be used to endorse or promote products derived from
147087Snate@binkert.org * this software without specific prior written permission.
157087Snate@binkert.org *
167087Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177087Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187087Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
197087Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
207087Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215081Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
227087Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235081Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245081Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255081Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265081Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275081Sgblack@eecs.umich.edu *
285081Sgblack@eecs.umich.edu * Authors: Gabe Black
295081Sgblack@eecs.umich.edu *          Ali Saidi
305081Sgblack@eecs.umich.edu *          Nathan Binkert
315081Sgblack@eecs.umich.edu */
325081Sgblack@eecs.umich.edu
335081Sgblack@eecs.umich.edu//The purpose of this file is to provide endainness conversion utility
345081Sgblack@eecs.umich.edu//functions. Depending on the endianness of the guest system, either
355081Sgblack@eecs.umich.edu//the LittleEndianGuest or BigEndianGuest namespace is used.
365081Sgblack@eecs.umich.edu
375081Sgblack@eecs.umich.edu#ifndef __SIM_BYTE_SWAP_HH__
385081Sgblack@eecs.umich.edu#define __SIM_BYTE_SWAP_HH__
396597Sgblack@eecs.umich.edu
406597Sgblack@eecs.umich.edu#include "base/bigint.hh"
416597Sgblack@eecs.umich.edu#include "base/types.hh"
426597Sgblack@eecs.umich.edu
436597Sgblack@eecs.umich.edu// This lets us figure out what the byte order of the host system is
446597Sgblack@eecs.umich.edu#if defined(__linux__)
456597Sgblack@eecs.umich.edu#include <endian.h>
466597Sgblack@eecs.umich.edu// If this is a linux system, lets used the optimized definitions if they exist.
476597Sgblack@eecs.umich.edu// If one doesn't exist, we pretty much get what is listed below, so it all
486597Sgblack@eecs.umich.edu// works out
496597Sgblack@eecs.umich.edu#include <byteswap.h>
506597Sgblack@eecs.umich.edu#elif defined (__sun)
516597Sgblack@eecs.umich.edu#include <sys/isa_defs.h>
526597Sgblack@eecs.umich.edu#else
536597Sgblack@eecs.umich.edu#include <machine/endian.h>
546597Sgblack@eecs.umich.edu#endif
556597Sgblack@eecs.umich.edu
566597Sgblack@eecs.umich.edu#if defined(__APPLE__)
576597Sgblack@eecs.umich.edu#include <libkern/OSByteOrder.h>
586597Sgblack@eecs.umich.edu#endif
596597Sgblack@eecs.umich.edu
606597Sgblack@eecs.umich.edu//These functions actually perform the swapping for parameters
616597Sgblack@eecs.umich.edu//of various bit lengths
626597Sgblack@eecs.umich.eduinline uint64_t
636597Sgblack@eecs.umich.eduswap_byte64(uint64_t x)
646597Sgblack@eecs.umich.edu{
656597Sgblack@eecs.umich.edu#if defined(__linux__)
666597Sgblack@eecs.umich.edu    return bswap_64(x);
676597Sgblack@eecs.umich.edu#elif defined(__APPLE__)
686597Sgblack@eecs.umich.edu    return OSSwapInt64(x);
696597Sgblack@eecs.umich.edu#else
706597Sgblack@eecs.umich.edu    return  (uint64_t)((((uint64_t)(x) & 0xff) << 56) |
716597Sgblack@eecs.umich.edu            ((uint64_t)(x) & 0xff00ULL) << 40 |
726597Sgblack@eecs.umich.edu            ((uint64_t)(x) & 0xff0000ULL) << 24 |
736597Sgblack@eecs.umich.edu            ((uint64_t)(x) & 0xff000000ULL) << 8 |
746597Sgblack@eecs.umich.edu            ((uint64_t)(x) & 0xff00000000ULL) >> 8 |
756597Sgblack@eecs.umich.edu            ((uint64_t)(x) & 0xff0000000000ULL) >> 24 |
766597Sgblack@eecs.umich.edu            ((uint64_t)(x) & 0xff000000000000ULL) >> 40 |
776597Sgblack@eecs.umich.edu            ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ;
786597Sgblack@eecs.umich.edu#endif
796597Sgblack@eecs.umich.edu}
805081Sgblack@eecs.umich.edu
81inline uint32_t
82swap_byte32(uint32_t x)
83{
84#if defined(__linux__)
85    return bswap_32(x);
86#elif defined(__APPLE__)
87    return OSSwapInt32(x);
88#else
89    return  (uint32_t)(((uint32_t)(x) & 0xff) << 24 |
90            ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 |
91            ((uint32_t)(x) & 0xff000000) >> 24);
92#endif
93}
94
95inline uint16_t
96swap_byte16(uint16_t x)
97{
98#if defined(__linux__)
99    return bswap_16(x);
100#elif defined(__APPLE__)
101    return OSSwapInt16(x);
102#else
103    return (uint16_t)(((uint16_t)(x) & 0xff) << 8 |
104                      ((uint16_t)(x) & 0xff00) >> 8);
105#endif
106}
107
108// This function lets the compiler figure out how to call the
109// swap_byte functions above for different data types.  Since the
110// sizeof() values are known at compile time, it should inline to a
111// direct call to the right swap_byteNN() function.
112template <typename T>
113inline T swap_byte(T x) {
114    if (sizeof(T) == 8)
115        return swap_byte64((uint64_t)x);
116    else if (sizeof(T) == 4)
117        return swap_byte32((uint32_t)x);
118    else if (sizeof(T) == 2)
119        return swap_byte16((uint16_t)x);
120    else if (sizeof(T) == 1)
121        return x;
122    else
123        panic("Can't byte-swap values larger than 64 bits");
124}
125
126template<>
127inline Twin64_t swap_byte<Twin64_t>(Twin64_t x)
128{
129    x.a = swap_byte(x.a);
130    x.b = swap_byte(x.b);
131    return x;
132}
133
134template<>
135inline Twin32_t swap_byte<Twin32_t>(Twin32_t x)
136{
137    x.a = swap_byte(x.a);
138    x.b = swap_byte(x.b);
139    return x;
140}
141
142template <typename T, size_t N>
143inline std::array<T, N>
144swap_byte(std::array<T, N> a)
145{
146    for (T &v: a)
147        v = swap_byte(v);
148    return a;
149}
150
151//The conversion functions with fixed endianness on both ends don't need to
152//be in a namespace
153template <typename T> inline T betole(T value) {return swap_byte(value);}
154template <typename T> inline T letobe(T value) {return swap_byte(value);}
155
156//For conversions not involving the guest system, we can define the functions
157//conditionally based on the BYTE_ORDER macro and outside of the namespaces
158#if (defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN)) && BYTE_ORDER == BIG_ENDIAN
159const ByteOrder HostByteOrder = BigEndianByteOrder;
160template <typename T> inline T htole(T value) {return swap_byte(value);}
161template <typename T> inline T letoh(T value) {return swap_byte(value);}
162template <typename T> inline T htobe(T value) {return value;}
163template <typename T> inline T betoh(T value) {return value;}
164#elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN
165const ByteOrder HostByteOrder = LittleEndianByteOrder;
166template <typename T> inline T htole(T value) {return value;}
167template <typename T> inline T letoh(T value) {return value;}
168template <typename T> inline T htobe(T value) {return swap_byte(value);}
169template <typename T> inline T betoh(T value) {return swap_byte(value);}
170#else
171        #error Invalid Endianess
172#endif
173
174namespace BigEndianGuest
175{
176    const ByteOrder GuestByteOrder = BigEndianByteOrder;
177    template <typename T>
178    inline T gtole(T value) {return betole(value);}
179    template <typename T>
180    inline T letog(T value) {return letobe(value);}
181    template <typename T>
182    inline T gtobe(T value) {return value;}
183    template <typename T>
184    inline T betog(T value) {return value;}
185    template <typename T>
186    inline T htog(T value) {return htobe(value);}
187    template <typename T>
188    inline T gtoh(T value) {return betoh(value);}
189}
190
191namespace LittleEndianGuest
192{
193    const ByteOrder GuestByteOrder = LittleEndianByteOrder;
194    template <typename T>
195    inline T gtole(T value) {return value;}
196    template <typename T>
197    inline T letog(T value) {return value;}
198    template <typename T>
199    inline T gtobe(T value) {return letobe(value);}
200    template <typename T>
201    inline T betog(T value) {return betole(value);}
202    template <typename T>
203    inline T htog(T value) {return htole(value);}
204    template <typename T>
205    inline T gtoh(T value) {return letoh(value);}
206}
207#endif // __SIM_BYTE_SWAP_HH__
208