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