Merge zizzer:/bk/multiarch
[gem5.git] / sim / byteswap.hh
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
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 #else
42 #include <machine/endian.h>
43 #endif
44
45 //These functions actually perform the swapping for parameters
46 //of various bit lengths
47 static inline uint64_t
48 swap_byte64(uint64_t x)
49 {
50 return (uint64_t)((((uint64_t)(x) & 0xff) << 56) |
51 ((uint64_t)(x) & 0xff00ULL) << 40 |
52 ((uint64_t)(x) & 0xff0000ULL) << 24 |
53 ((uint64_t)(x) & 0xff000000ULL) << 8 |
54 ((uint64_t)(x) & 0xff00000000ULL) >> 8 |
55 ((uint64_t)(x) & 0xff0000000000ULL) >> 24 |
56 ((uint64_t)(x) & 0xff000000000000ULL) >> 40 |
57 ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ;
58 }
59
60 static inline uint32_t
61 swap_byte32(uint32_t x)
62 {
63 return (uint32_t)(((uint32_t)(x) & 0xff) << 24 |
64 ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 |
65 ((uint32_t)(x) & 0xff000000) >> 24);
66
67 }
68
69 static inline uint16_t
70 swap_byte16(uint16_t x)
71 {
72 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 |
73 ((uint16_t)(x) & 0xff00) >> 8);
74 }
75
76 //This lets the compiler figure out how to call the swap_byte functions above
77 //for different data types.
78 static inline uint64_t swap_byte(uint64_t x) {return swap_byte64(x);}
79 static inline int64_t swap_byte(int64_t x) {return swap_byte64((uint64_t)x);}
80 static inline uint32_t swap_byte(uint32_t x) {return swap_byte32(x);}
81 static inline int32_t swap_byte(int32_t x) {return swap_byte32((uint32_t)x);}
82 #if defined(__APPLE__)
83 static inline long swap_byte(long x) {return swap_byte32((long)x);}
84 static inline unsigned long swap_byte(unsigned long x)
85 { return swap_byte32((unsigned long)x);}
86 #endif
87 static inline uint16_t swap_byte(uint16_t x) {return swap_byte32(x);}
88 static inline int16_t swap_byte(int16_t x) {return swap_byte16((uint16_t)x);}
89 static inline uint8_t swap_byte(uint8_t x) {return x;}
90 static inline int8_t swap_byte(int8_t x) {return x;}
91 static inline double swap_byte(double x) {return swap_byte64((uint64_t)x);}
92 static inline float swap_byte(float x) {return swap_byte32((uint32_t)x);}
93
94 //The conversion functions with fixed endianness on both ends don't need to
95 //be in a namespace
96 template <typename T> static inline T betole(T value) {return swap_byte(value);}
97 template <typename T> static inline T letobe(T value) {return swap_byte(value);}
98
99 //For conversions not involving the guest system, we can define the functions
100 //conditionally based on the BYTE_ORDER macro and outside of the namespaces
101 #if BYTE_ORDER == BIG_ENDIAN
102 template <typename T> static inline T htole(T value) {return swap_byte(value);}
103 template <typename T> static inline T letoh(T value) {return swap_byte(value);}
104 template <typename T> static inline T htobe(T value) {return value;}
105 template <typename T> static inline T betoh(T value) {return value;}
106 #elif BYTE_ORDER == LITTLE_ENDIAN
107 template <typename T> static inline T htole(T value) {return value;}
108 template <typename T> static inline T letoh(T value) {return value;}
109 template <typename T> static inline T htobe(T value) {return swap_byte(value);}
110 template <typename T> static inline T betoh(T value) {return swap_byte(value);}
111 #else
112 #error Invalid Endianess
113 #endif
114
115 namespace BigEndianGuest
116 {
117 template <typename T>
118 static inline T gtole(T value) {return betole(value);}
119 template <typename T>
120 static inline T letog(T value) {return letobe(value);}
121 template <typename T>
122 static inline T gtobe(T value) {return value;}
123 template <typename T>
124 static inline T betog(T value) {return value;}
125 template <typename T>
126 static inline T htog(T value) {return htobe(value);}
127 template <typename T>
128 static inline T gtoh(T value) {return betoh(value);}
129 }
130
131 namespace LittleEndianGuest
132 {
133 template <typename T>
134 static inline T gtole(T value) {return value;}
135 template <typename T>
136 static inline T letog(T value) {return value;}
137 template <typename T>
138 static inline T gtobe(T value) {return letobe(value);}
139 template <typename T>
140 static inline T betog(T value) {return betole(value);}
141 template <typename T>
142 static inline T htog(T value) {return htole(value);}
143 template <typename T>
144 static inline T gtoh(T value) {return letoh(value);}
145 }
146 #endif // __SIM_BYTE_SWAP_HH__