byteswap.hh revision 2772:f0f52cbe744d
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: Ali Saidi
29 *          Nathan Binkert
30 */
31
32//The purpose of this file is to provide endainness conversion utility
33//functions. Depending on the endianness of the guest system, either
34//the LittleEndianGuest or BigEndianGuest namespace is used.
35
36#ifndef __SIM_BYTE_SWAP_HH__
37#define __SIM_BYTE_SWAP_HH__
38
39#include "sim/host.hh"
40
41// This lets us figure out what the byte order of the host system is
42#if defined(linux)
43#include <endian.h>
44// If this is a linux system, lets used the optimized definitions if they exist.
45// If one doesn't exist, we pretty much get what is listed below, so it all
46// works out
47#include <byteswap.h>
48#else
49#include <machine/endian.h>
50#endif
51
52//These functions actually perform the swapping for parameters
53//of various bit lengths
54static inline uint64_t
55swap_byte64(uint64_t x)
56{
57#if defined(linux)
58    return bswap_64(x);
59#else
60    return  (uint64_t)((((uint64_t)(x) & 0xff) << 56) |
61            ((uint64_t)(x) & 0xff00ULL) << 40 |
62            ((uint64_t)(x) & 0xff0000ULL) << 24 |
63            ((uint64_t)(x) & 0xff000000ULL) << 8 |
64            ((uint64_t)(x) & 0xff00000000ULL) >> 8 |
65            ((uint64_t)(x) & 0xff0000000000ULL) >> 24 |
66            ((uint64_t)(x) & 0xff000000000000ULL) >> 40 |
67            ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ;
68#endif
69}
70
71static inline uint32_t
72swap_byte32(uint32_t x)
73{
74#if defined(linux)
75    return bswap_32(x);
76#else
77    return  (uint32_t)(((uint32_t)(x) & 0xff) << 24 |
78            ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 |
79            ((uint32_t)(x) & 0xff000000) >> 24);
80#endif
81}
82
83static inline uint16_t
84swap_byte16(uint16_t x)
85{
86#if defined(linux)
87    return bswap_16(x);
88#else
89    return (uint16_t)(((uint16_t)(x) & 0xff) << 8 |
90                      ((uint16_t)(x) & 0xff00) >> 8);
91#endif
92}
93
94//This lets the compiler figure out how to call the swap_byte functions above
95//for different data types.
96static inline uint64_t swap_byte(uint64_t x) {return swap_byte64(x);}
97static inline int64_t swap_byte(int64_t x) {return swap_byte64((uint64_t)x);}
98static inline uint32_t swap_byte(uint32_t x) {return swap_byte32(x);}
99static inline int32_t swap_byte(int32_t x) {return swap_byte32((uint32_t)x);}
100//This is to prevent the following two functions from compiling on
101//64bit machines. It won't detect everything, so it should be changed.
102#ifndef __x86_64__
103static inline long swap_byte(long x) {return swap_byte32((long)x);}
104static inline unsigned long swap_byte(unsigned long x)
105                                { return swap_byte32((unsigned long)x);}
106#endif
107static inline uint16_t swap_byte(uint16_t x) {return swap_byte32(x);}
108static inline int16_t swap_byte(int16_t x) {return swap_byte16((uint16_t)x);}
109static inline uint8_t swap_byte(uint8_t x) {return x;}
110static inline int8_t swap_byte(int8_t x) {return x;}
111static inline double swap_byte(double x) {return swap_byte64((uint64_t)x);}
112static inline float swap_byte(float x) {return swap_byte32((uint32_t)x);}
113
114//The conversion functions with fixed endianness on both ends don't need to
115//be in a namespace
116template <typename T> static inline T betole(T value) {return swap_byte(value);}
117template <typename T> static inline T letobe(T value) {return swap_byte(value);}
118
119//For conversions not involving the guest system, we can define the functions
120//conditionally based on the BYTE_ORDER macro and outside of the namespaces
121#if BYTE_ORDER == BIG_ENDIAN
122template <typename T> static inline T htole(T value) {return swap_byte(value);}
123template <typename T> static inline T letoh(T value) {return swap_byte(value);}
124template <typename T> static inline T htobe(T value) {return value;}
125template <typename T> static inline T betoh(T value) {return value;}
126#elif BYTE_ORDER == LITTLE_ENDIAN
127template <typename T> static inline T htole(T value) {return value;}
128template <typename T> static inline T letoh(T value) {return value;}
129template <typename T> static inline T htobe(T value) {return swap_byte(value);}
130template <typename T> static inline T betoh(T value) {return swap_byte(value);}
131#else
132        #error Invalid Endianess
133#endif
134
135namespace BigEndianGuest
136{
137        template <typename T>
138        static inline T gtole(T value) {return betole(value);}
139        template <typename T>
140        static inline T letog(T value) {return letobe(value);}
141        template <typename T>
142        static inline T gtobe(T value) {return value;}
143        template <typename T>
144        static inline T betog(T value) {return value;}
145        template <typename T>
146        static inline T htog(T value) {return htobe(value);}
147        template <typename T>
148        static inline T gtoh(T value) {return betoh(value);}
149}
150
151namespace LittleEndianGuest
152{
153        template <typename T>
154        static inline T gtole(T value) {return value;}
155        template <typename T>
156        static inline T letog(T value) {return value;}
157        template <typename T>
158        static inline T gtobe(T value) {return letobe(value);}
159        template <typename T>
160        static inline T betog(T value) {return betole(value);}
161        template <typename T>
162        static inline T htog(T value) {return htole(value);}
163        template <typename T>
164        static inline T gtoh(T value) {return letoh(value);}
165}
166#endif // __SIM_BYTE_SWAP_HH__
167