byteswap.hh (2779:b20a0595a0a3) byteswap.hh (2956:790b4f39c21c)
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 *
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
28 * Authors: Gabe Black
29 * 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 "base/misc.hh"
40#include "sim/host.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#else
50#include <machine/endian.h>
51#endif
52
53#if defined(__APPLE__)
54#include <libkern/OSByteOrder.h>
55#endif
56
57//These functions actually perform the swapping for parameters
58//of various bit lengths
59static inline uint64_t
60swap_byte64(uint64_t x)
61{
62#if defined(linux)
63 return bswap_64(x);
64#elif defined(__APPLE__)
65 return OSSwapInt64(x);
66#else
67 return (uint64_t)((((uint64_t)(x) & 0xff) << 56) |
68 ((uint64_t)(x) & 0xff00ULL) << 40 |
69 ((uint64_t)(x) & 0xff0000ULL) << 24 |
70 ((uint64_t)(x) & 0xff000000ULL) << 8 |
71 ((uint64_t)(x) & 0xff00000000ULL) >> 8 |
72 ((uint64_t)(x) & 0xff0000000000ULL) >> 24 |
73 ((uint64_t)(x) & 0xff000000000000ULL) >> 40 |
74 ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ;
75#endif
76}
77
78static inline uint32_t
79swap_byte32(uint32_t x)
80{
81#if defined(linux)
82 return bswap_32(x);
83#elif defined(__APPLE__)
84 return OSSwapInt32(x);
85#else
86 return (uint32_t)(((uint32_t)(x) & 0xff) << 24 |
87 ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 |
88 ((uint32_t)(x) & 0xff000000) >> 24);
89#endif
90}
91
92static inline uint16_t
93swap_byte16(uint16_t x)
94{
95#if defined(linux)
96 return bswap_16(x);
97#elif defined(__APPLE__)
98 return OSSwapInt16(x);
99#else
100 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 |
101 ((uint16_t)(x) & 0xff00) >> 8);
102#endif
103}
104
105// This function lets the compiler figure out how to call the
106// swap_byte functions above for different data types. Since the
107// sizeof() values are known at compiel time, it should inline to a
108// direct call to the right swap_byteNN() function.
109template <typename T>
110static inline T swap_byte(T x) {
111 if (sizeof(T) == 8)
112 return swap_byte64((uint64_t)x);
113 else if (sizeof(T) == 4)
114 return swap_byte32((uint32_t)x);
115 else if (sizeof(T) == 2)
116 return swap_byte16((uint16_t)x);
117 else if (sizeof(T) == 1)
118 return x;
119 else
120 panic("Can't byte-swap values larger than 64 bits");
121}
122
123//The conversion functions with fixed endianness on both ends don't need to
124//be in a namespace
125template <typename T> static inline T betole(T value) {return swap_byte(value);}
126template <typename T> static inline T letobe(T value) {return swap_byte(value);}
127
128//For conversions not involving the guest system, we can define the functions
129//conditionally based on the BYTE_ORDER macro and outside of the namespaces
130#if BYTE_ORDER == BIG_ENDIAN
131template <typename T> static inline T htole(T value) {return swap_byte(value);}
132template <typename T> static inline T letoh(T value) {return swap_byte(value);}
133template <typename T> static inline T htobe(T value) {return value;}
134template <typename T> static inline T betoh(T value) {return value;}
135#elif BYTE_ORDER == LITTLE_ENDIAN
136template <typename T> static inline T htole(T value) {return value;}
137template <typename T> static inline T letoh(T value) {return value;}
138template <typename T> static inline T htobe(T value) {return swap_byte(value);}
139template <typename T> static inline T betoh(T value) {return swap_byte(value);}
140#else
141 #error Invalid Endianess
142#endif
143
144namespace BigEndianGuest
145{
146 template <typename T>
147 static inline T gtole(T value) {return betole(value);}
148 template <typename T>
149 static inline T letog(T value) {return letobe(value);}
150 template <typename T>
151 static inline T gtobe(T value) {return value;}
152 template <typename T>
153 static inline T betog(T value) {return value;}
154 template <typename T>
155 static inline T htog(T value) {return htobe(value);}
156 template <typename T>
157 static inline T gtoh(T value) {return betoh(value);}
158}
159
160namespace LittleEndianGuest
161{
162 template <typename T>
163 static inline T gtole(T value) {return value;}
164 template <typename T>
165 static inline T letog(T value) {return value;}
166 template <typename T>
167 static inline T gtobe(T value) {return letobe(value);}
168 template <typename T>
169 static inline T betog(T value) {return betole(value);}
170 template <typename T>
171 static inline T htog(T value) {return htole(value);}
172 template <typename T>
173 static inline T gtoh(T value) {return letoh(value);}
174}
175#endif // __SIM_BYTE_SWAP_HH__
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/misc.hh"
41#include "sim/host.hh"
42
43// This lets us figure out what the byte order of the host system is
44#if defined(linux)
45#include <endian.h>
46// If this is a linux system, lets used the optimized definitions if they exist.
47// If one doesn't exist, we pretty much get what is listed below, so it all
48// works out
49#include <byteswap.h>
50#else
51#include <machine/endian.h>
52#endif
53
54#if defined(__APPLE__)
55#include <libkern/OSByteOrder.h>
56#endif
57
58//These functions actually perform the swapping for parameters
59//of various bit lengths
60static inline uint64_t
61swap_byte64(uint64_t x)
62{
63#if defined(linux)
64 return bswap_64(x);
65#elif defined(__APPLE__)
66 return OSSwapInt64(x);
67#else
68 return (uint64_t)((((uint64_t)(x) & 0xff) << 56) |
69 ((uint64_t)(x) & 0xff00ULL) << 40 |
70 ((uint64_t)(x) & 0xff0000ULL) << 24 |
71 ((uint64_t)(x) & 0xff000000ULL) << 8 |
72 ((uint64_t)(x) & 0xff00000000ULL) >> 8 |
73 ((uint64_t)(x) & 0xff0000000000ULL) >> 24 |
74 ((uint64_t)(x) & 0xff000000000000ULL) >> 40 |
75 ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ;
76#endif
77}
78
79static inline uint32_t
80swap_byte32(uint32_t x)
81{
82#if defined(linux)
83 return bswap_32(x);
84#elif defined(__APPLE__)
85 return OSSwapInt32(x);
86#else
87 return (uint32_t)(((uint32_t)(x) & 0xff) << 24 |
88 ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 |
89 ((uint32_t)(x) & 0xff000000) >> 24);
90#endif
91}
92
93static inline uint16_t
94swap_byte16(uint16_t x)
95{
96#if defined(linux)
97 return bswap_16(x);
98#elif defined(__APPLE__)
99 return OSSwapInt16(x);
100#else
101 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 |
102 ((uint16_t)(x) & 0xff00) >> 8);
103#endif
104}
105
106// This function lets the compiler figure out how to call the
107// swap_byte functions above for different data types. Since the
108// sizeof() values are known at compiel time, it should inline to a
109// direct call to the right swap_byteNN() function.
110template <typename T>
111static inline T swap_byte(T x) {
112 if (sizeof(T) == 8)
113 return swap_byte64((uint64_t)x);
114 else if (sizeof(T) == 4)
115 return swap_byte32((uint32_t)x);
116 else if (sizeof(T) == 2)
117 return swap_byte16((uint16_t)x);
118 else if (sizeof(T) == 1)
119 return x;
120 else
121 panic("Can't byte-swap values larger than 64 bits");
122}
123
124//The conversion functions with fixed endianness on both ends don't need to
125//be in a namespace
126template <typename T> static inline T betole(T value) {return swap_byte(value);}
127template <typename T> static inline T letobe(T value) {return swap_byte(value);}
128
129//For conversions not involving the guest system, we can define the functions
130//conditionally based on the BYTE_ORDER macro and outside of the namespaces
131#if BYTE_ORDER == BIG_ENDIAN
132template <typename T> static inline T htole(T value) {return swap_byte(value);}
133template <typename T> static inline T letoh(T value) {return swap_byte(value);}
134template <typename T> static inline T htobe(T value) {return value;}
135template <typename T> static inline T betoh(T value) {return value;}
136#elif BYTE_ORDER == LITTLE_ENDIAN
137template <typename T> static inline T htole(T value) {return value;}
138template <typename T> static inline T letoh(T value) {return value;}
139template <typename T> static inline T htobe(T value) {return swap_byte(value);}
140template <typename T> static inline T betoh(T value) {return swap_byte(value);}
141#else
142 #error Invalid Endianess
143#endif
144
145namespace BigEndianGuest
146{
147 template <typename T>
148 static inline T gtole(T value) {return betole(value);}
149 template <typename T>
150 static inline T letog(T value) {return letobe(value);}
151 template <typename T>
152 static inline T gtobe(T value) {return value;}
153 template <typename T>
154 static inline T betog(T value) {return value;}
155 template <typename T>
156 static inline T htog(T value) {return htobe(value);}
157 template <typename T>
158 static inline T gtoh(T value) {return betoh(value);}
159}
160
161namespace LittleEndianGuest
162{
163 template <typename T>
164 static inline T gtole(T value) {return value;}
165 template <typename T>
166 static inline T letog(T value) {return value;}
167 template <typename T>
168 static inline T gtobe(T value) {return letobe(value);}
169 template <typename T>
170 static inline T betog(T value) {return betole(value);}
171 template <typename T>
172 static inline T htog(T value) {return htole(value);}
173 template <typename T>
174 static inline T gtoh(T value) {return letoh(value);}
175}
176#endif // __SIM_BYTE_SWAP_HH__