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

Generated on Wed Sep 30 2020 14:02:14 for gem5 by doxygen 1.8.17