Correct SSE4.2 ChangeLog entry.
[binutils.git] / elfcpp / elfcpp_swap.h
blob71b02eb8d3680cbccd2d124f8e8c56b5857f54b9
1 // elfcpp_swap.h -- Handle swapping for elfcpp -*- C++ -*-
3 // This header file defines basic template classes to efficiently swap
4 // numbers between host form and target form. When the host and
5 // target have the same endianness, these turn into no-ops.
7 #ifndef ELFCPP_SWAP_H
8 #define ELFCPP_SWAP_H
10 #include <stdint.h>
11 #include <endian.h>
12 #include <byteswap.h>
14 namespace elfcpp
17 // Endian simply indicates whether the host is big endian or not.
19 struct Endian
21 public:
22 // Used for template specializations.
23 static const bool host_big_endian = __BYTE_ORDER == __BIG_ENDIAN;
26 // Valtype_base is a template based on size (8, 16, 32, 64) which
27 // defines the type Valtype as the unsigned integer of the specified
28 // size.
30 template<int size>
31 struct Valtype_base;
33 template<>
34 struct Valtype_base<8>
36 typedef unsigned char Valtype;
39 template<>
40 struct Valtype_base<16>
42 typedef uint16_t Valtype;
45 template<>
46 struct Valtype_base<32>
48 typedef uint32_t Valtype;
51 template<>
52 struct Valtype_base<64>
54 typedef uint64_t Valtype;
57 // Convert_endian is a template based on size and on whether the host
58 // and target have the same endianness. It defines the type Valtype
59 // as Valtype_base does, and also defines a function convert_host
60 // which takes an argument of type Valtype and returns the same value,
61 // but swapped if the host and target have different endianness.
63 template<int size, bool same_endian>
64 struct Convert_endian;
66 template<int size>
67 struct Convert_endian<size, true>
69 typedef typename Valtype_base<size>::Valtype Valtype;
71 static inline Valtype
72 convert_host(Valtype v)
73 { return v; }
76 template<>
77 struct Convert_endian<8, false>
79 typedef Valtype_base<8>::Valtype Valtype;
81 static inline Valtype
82 convert_host(Valtype v)
83 { return v; }
86 template<>
87 struct Convert_endian<16, false>
89 typedef Valtype_base<16>::Valtype Valtype;
91 static inline Valtype
92 convert_host(Valtype v)
93 { return bswap_16(v); }
96 template<>
97 struct Convert_endian<32, false>
99 typedef Valtype_base<32>::Valtype Valtype;
101 static inline Valtype
102 convert_host(Valtype v)
103 { return bswap_32(v); }
106 template<>
107 struct Convert_endian<64, false>
109 typedef Valtype_base<64>::Valtype Valtype;
111 static inline Valtype
112 convert_host(Valtype v)
113 { return bswap_64(v); }
116 // Convert is a template based on size and on whether the target is
117 // big endian. It defines Valtype and convert_host like
118 // Convert_endian. That is, it is just like Convert_endian except in
119 // the meaning of the second template parameter.
121 template<int size, bool big_endian>
122 struct Convert
124 typedef typename Valtype_base<size>::Valtype Valtype;
126 static inline Valtype
127 convert_host(Valtype v)
129 return Convert_endian<size, big_endian == Endian::host_big_endian>
130 ::convert_host(v);
134 // Swap is a template based on size and on whether the target is big
135 // endian. It defines the type Valtype and the functions readval and
136 // writeval. The functions read and write values of the appropriate
137 // size out of buffers, swapping them if necessary. readval and
138 // writeval are overloaded to take pointers to the appropriate type or
139 // pointers to unsigned char.
141 template<int size, bool big_endian>
142 struct Swap
144 typedef typename Valtype_base<size>::Valtype Valtype;
146 static inline Valtype
147 readval(const Valtype* wv)
148 { return Convert<size, big_endian>::convert_host(*wv); }
150 static inline void
151 writeval(Valtype* wv, Valtype v)
152 { *wv = Convert<size, big_endian>::convert_host(v); }
154 static inline Valtype
155 readval(const unsigned char* wv)
156 { return readval(reinterpret_cast<const Valtype*>(wv)); }
158 static inline void
159 writeval(unsigned char* wv, Valtype v)
160 { writeval(reinterpret_cast<Valtype*>(wv), v); }
163 // We need to specialize the 8-bit version of Swap to avoid
164 // conflicting overloads, since both versions of readval and writeval
165 // will have the same type parameters.
167 template<bool big_endian>
168 struct Swap<8, big_endian>
170 typedef typename Valtype_base<8>::Valtype Valtype;
172 static inline Valtype
173 readval(const Valtype* wv)
174 { return *wv; }
176 static inline void
177 writeval(Valtype* wv, Valtype v)
178 { *wv = v; }
181 // Swap_unaligned is a template based on size and on whether the
182 // target is big endian. It defines the type Valtype and the
183 // functions readval and writeval. The functions read and write
184 // values of the appropriate size out of buffers which may be
185 // misaligned.
187 template<int size, bool big_endian>
188 struct Swap_unaligned;
190 template<bool big_endian>
191 struct Swap_unaligned<8, big_endian>
193 typedef typename Valtype_base<8>::Valtype Valtype;
195 static inline Valtype
196 readval(const unsigned char* wv)
197 { return *wv; }
199 static inline void
200 writeval(unsigned char* wv, Valtype v)
201 { *wv = v; }
204 template<>
205 struct Swap_unaligned<16, false>
207 typedef Valtype_base<16>::Valtype Valtype;
209 static inline Valtype
210 readval(const unsigned char* wv)
212 return (wv[1] << 8) | wv[0];
215 static inline void
216 writeval(unsigned char* wv, Valtype v)
218 wv[1] = v >> 8;
219 wv[0] = v;
223 template<>
224 struct Swap_unaligned<16, true>
226 typedef Valtype_base<16>::Valtype Valtype;
228 static inline Valtype
229 readval(const unsigned char* wv)
231 return (wv[0] << 8) | wv[1];
234 static inline void
235 writeval(unsigned char* wv, Valtype v)
237 wv[0] = v >> 8;
238 wv[1] = v;
242 template<>
243 struct Swap_unaligned<32, false>
245 typedef Valtype_base<32>::Valtype Valtype;
247 static inline Valtype
248 readval(const unsigned char* wv)
250 return (wv[3] << 24) | (wv[2] << 16) | (wv[1] << 8) | wv[0];
253 static inline void
254 writeval(unsigned char* wv, Valtype v)
256 wv[3] = v >> 24;
257 wv[2] = v >> 16;
258 wv[1] = v >> 8;
259 wv[0] = v;
263 template<>
264 struct Swap_unaligned<32, true>
266 typedef Valtype_base<32>::Valtype Valtype;
268 static inline Valtype
269 readval(const unsigned char* wv)
271 return (wv[0] << 24) | (wv[1] << 16) | (wv[2] << 8) | wv[3];
274 static inline void
275 writeval(unsigned char* wv, Valtype v)
277 wv[0] = v >> 24;
278 wv[1] = v >> 16;
279 wv[2] = v >> 8;
280 wv[3] = v;
284 template<>
285 struct Swap_unaligned<64, false>
287 typedef Valtype_base<64>::Valtype Valtype;
289 static inline Valtype
290 readval(const unsigned char* wv)
292 return ((static_cast<Valtype>(wv[7]) << 56)
293 | (static_cast<Valtype>(wv[6]) << 48)
294 | (static_cast<Valtype>(wv[5]) << 40)
295 | (static_cast<Valtype>(wv[4]) << 32)
296 | (static_cast<Valtype>(wv[3]) << 24)
297 | (static_cast<Valtype>(wv[2]) << 16)
298 | (static_cast<Valtype>(wv[1]) << 8)
299 | static_cast<Valtype>(wv[0]));
302 static inline void
303 writeval(unsigned char* wv, Valtype v)
305 wv[7] = v >> 56;
306 wv[6] = v >> 48;
307 wv[5] = v >> 40;
308 wv[4] = v >> 32;
309 wv[3] = v >> 24;
310 wv[2] = v >> 16;
311 wv[1] = v >> 8;
312 wv[0] = v;
316 template<>
317 struct Swap_unaligned<64, true>
319 typedef Valtype_base<64>::Valtype Valtype;
321 static inline Valtype
322 readval(const unsigned char* wv)
324 return ((static_cast<Valtype>(wv[0]) << 56)
325 | (static_cast<Valtype>(wv[1]) << 48)
326 | (static_cast<Valtype>(wv[2]) << 40)
327 | (static_cast<Valtype>(wv[3]) << 32)
328 | (static_cast<Valtype>(wv[4]) << 24)
329 | (static_cast<Valtype>(wv[5]) << 16)
330 | (static_cast<Valtype>(wv[6]) << 8)
331 | static_cast<Valtype>(wv[7]));
334 static inline void
335 writeval(unsigned char* wv, Valtype v)
337 wv[7] = v >> 56;
338 wv[6] = v >> 48;
339 wv[5] = v >> 40;
340 wv[4] = v >> 32;
341 wv[3] = v >> 24;
342 wv[2] = v >> 16;
343 wv[1] = v >> 8;
344 wv[0] = v;
348 } // End namespace elfcpp.
350 #endif // !defined(ELFCPP_SWAP_H)