1/*
2 * Copyright (c) 2004 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Gabe Black
29 *          Ali Saidi
30 *          Nathan Binkert
31 */
32
33//The purpose of this file is to provide endainness conversion utility
34//functions. Depending on the endianness of the guest system, either
35//the LittleEndianGuest or BigEndianGuest namespace is used.
36
37#ifndef __SIM_BYTE_SWAP_HH__
38#define __SIM_BYTE_SWAP_HH__
39
40#include "base/types.hh"
41
42// This lets us figure out what the byte order of the host system is
43#if defined(__linux__)
44#include <endian.h>
45// If this is a linux system, lets used the optimized definitions if they exist.
46// If one doesn't exist, we pretty much get what is listed below, so it all
47// works out
48#include <byteswap.h>
49#elif defined (__sun)
50#include <sys/isa_defs.h>
51#else
52#include <machine/endian.h>
53#endif
54
55#if defined(__APPLE__)
56#include <libkern/OSByteOrder.h>
57#endif
58
59//These functions actually perform the swapping for parameters
60//of various bit lengths
61inline uint64_t
62swap_byte64(uint64_t x)
63{
64#if defined(__linux__)
65    return bswap_64(x);
66#elif defined(__APPLE__)
67    return OSSwapInt64(x);
68#else
69    return  (uint64_t)((((uint64_t)(x) & 0xff) << 56) |
70            ((uint64_t)(x) & 0xff00ULL) << 40 |
71            ((uint64_t)(x) & 0xff0000ULL) << 24 |
72            ((uint64_t)(x) & 0xff000000ULL) << 8 |
73            ((uint64_t)(x) & 0xff00000000ULL) >> 8 |
74            ((uint64_t)(x) & 0xff0000000000ULL) >> 24 |
75            ((uint64_t)(x) & 0xff000000000000ULL) >> 40 |
76            ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ;
77#endif
78}
79
80inline uint32_t
81swap_byte32(uint32_t x)
82{
83#if defined(__linux__)
84    return bswap_32(x);
85#elif defined(__APPLE__)
86    return OSSwapInt32(x);
87#else
88    return  (uint32_t)(((uint32_t)(x) & 0xff) << 24 |
89            ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 |
90            ((uint32_t)(x) & 0xff000000) >> 24);
91#endif
92}
93
94inline uint16_t
95swap_byte16(uint16_t x)
96{
97#if defined(__linux__)
98    return bswap_16(x);
99#elif defined(__APPLE__)
100    return OSSwapInt16(x);
101#else
102    return (uint16_t)(((uint16_t)(x) & 0xff) << 8 |
103                      ((uint16_t)(x) & 0xff00) >> 8);
104#endif
105}
106
107// This function lets the compiler figure out how to call the
108// swap_byte functions above for different data types.  Since the
109// sizeof() values are known at compile time, it should inline to a
110// direct call to the right swap_byteNN() function.
111template <typename T>
112inline T swap_byte(T x) {
113    if (sizeof(T) == 8)
114        return swap_byte64((uint64_t)x);
115    else if (sizeof(T) == 4)
116        return swap_byte32((uint32_t)x);
117    else if (sizeof(T) == 2)
118        return swap_byte16((uint16_t)x);
119    else if (sizeof(T) == 1)
120        return x;
121    else
122        panic("Can't byte-swap values larger than 64 bits");
123}
124
125template <typename T, size_t N>
126inline std::array<T, N>
127swap_byte(std::array<T, N> a)
128{
129    for (T &v: a)
130        v = swap_byte(v);
131    return a;
132}
133
134//The conversion functions with fixed endianness on both ends don't need to
135//be in a namespace
136template <typename T> inline T betole(T value) {return swap_byte(value);}
137template <typename T> inline T letobe(T value) {return swap_byte(value);}
138
139//For conversions not involving the guest system, we can define the functions
140//conditionally based on the BYTE_ORDER macro and outside of the namespaces
141#if (defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN)) && BYTE_ORDER == BIG_ENDIAN
142const ByteOrder HostByteOrder = BigEndianByteOrder;
143template <typename T> inline T htole(T value) {return swap_byte(value);}
144template <typename T> inline T letoh(T value) {return swap_byte(value);}
145template <typename T> inline T htobe(T value) {return value;}
146template <typename T> inline T betoh(T value) {return value;}
147#elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN
148const ByteOrder HostByteOrder = LittleEndianByteOrder;
149template <typename T> inline T htole(T value) {return value;}
150template <typename T> inline T letoh(T value) {return value;}
151template <typename T> inline T htobe(T value) {return swap_byte(value);}
152template <typename T> inline T betoh(T value) {return swap_byte(value);}
153#else
154        #error Invalid Endianess
155#endif
156
157template <typename T>
158inline T htog(T value, ByteOrder guest_byte_order)
159{
160    return guest_byte_order == BigEndianByteOrder ?
161        htobe(value) : htole(value);
162}
163
164template <typename T>
165inline T gtoh(T value, ByteOrder guest_byte_order)
166{
167    return guest_byte_order == BigEndianByteOrder ?
168        betoh(value) : letoh(value);
169}
170
171namespace BigEndianGuest
172{
173    const ByteOrder GuestByteOrder = BigEndianByteOrder;
174    template <typename T>
175    inline T gtole(T value) {return betole(value);}
176    template <typename T>
177    inline T letog(T value) {return letobe(value);}
178    template <typename T>
179    inline T gtobe(T value) {return value;}
180    template <typename T>
181    inline T betog(T value) {return value;}
182    template <typename T>
183    inline T htog(T value) {return htobe(value);}
184    template <typename T>
185    inline T gtoh(T value) {return betoh(value);}
186}
187
188namespace LittleEndianGuest
189{
190    const ByteOrder GuestByteOrder = LittleEndianByteOrder;
191    template <typename T>
192    inline T gtole(T value) {return value;}
193    template <typename T>
194    inline T letog(T value) {return value;}
195    template <typename T>
196    inline T gtobe(T value) {return letobe(value);}
197    template <typename T>
198    inline T betog(T value) {return betole(value);}
199    template <typename T>
200    inline T htog(T value) {return htole(value);}
201    template <typename T>
202    inline T gtoh(T value) {return letoh(value);}
203}
204#endif // __SIM_BYTE_SWAP_HH__
205