1 //===- Endian.h - Utilities for IO with endian specific data ----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares generic functions to read and write endian specific data.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_ENDIAN_H
15 #define LLVM_SUPPORT_ENDIAN_H
16
17 #include "llvm/Support/AlignOf.h"
18 #include "llvm/Support/Host.h"
19 #include "llvm/Support/SwapByteOrder.h"
20
21 namespace llvm {
22 namespace support {
23 enum endianness {big, little, native};
24
25 // These are named values for common alignments.
26 enum {aligned = 0, unaligned = 1};
27
28 namespace detail {
29 /// \brief ::value is either alignment, or alignof(T) if alignment is 0.
30 template<class T, int alignment>
31 struct PickAlignment {
32 enum {value = alignment == 0 ? AlignOf<T>::Alignment : alignment};
33 };
34 } // end namespace detail
35
36 namespace endian {
37 /// Swap the bytes of value to match the given endianness.
38 template<typename value_type, endianness endian>
byte_swap(value_type value)39 inline value_type byte_swap(value_type value) {
40 if (endian != native && sys::IsBigEndianHost != (endian == big))
41 sys::swapByteOrder(value);
42 return value;
43 }
44
45 /// Read a value of a particular endianness from memory.
46 template<typename value_type,
47 endianness endian,
48 std::size_t alignment>
read(const void * memory)49 inline value_type read(const void *memory) {
50 value_type ret;
51
52 memcpy(&ret,
53 LLVM_ASSUME_ALIGNED(memory,
54 (detail::PickAlignment<value_type, alignment>::value)),
55 sizeof(value_type));
56 return byte_swap<value_type, endian>(ret);
57 }
58
59 /// Read a value of a particular endianness from a buffer, and increment the
60 /// buffer past that value.
61 template<typename value_type, endianness endian, std::size_t alignment,
62 typename CharT>
readNext(const CharT * & memory)63 inline value_type readNext(const CharT *&memory) {
64 value_type ret = read<value_type, endian, alignment>(memory);
65 memory += sizeof(value_type);
66 return ret;
67 }
68
69 /// Write a value to memory with a particular endianness.
70 template<typename value_type,
71 endianness endian,
72 std::size_t alignment>
write(void * memory,value_type value)73 inline void write(void *memory, value_type value) {
74 value = byte_swap<value_type, endian>(value);
75 memcpy(LLVM_ASSUME_ALIGNED(memory,
76 (detail::PickAlignment<value_type, alignment>::value)),
77 &value,
78 sizeof(value_type));
79 }
80
81 template <typename value_type>
82 using make_unsigned_t = typename std::make_unsigned<value_type>::type;
83
84 /// Read a value of a particular endianness from memory, for a location
85 /// that starts at the given bit offset within the first byte.
86 template <typename value_type, endianness endian, std::size_t alignment>
readAtBitAlignment(const void * memory,uint64_t startBit)87 inline value_type readAtBitAlignment(const void *memory, uint64_t startBit) {
88 assert(startBit < 8);
89 if (startBit == 0)
90 return read<value_type, endian, alignment>(memory);
91 else {
92 // Read two values and compose the result from them.
93 value_type val[2];
94 memcpy(&val[0],
95 LLVM_ASSUME_ALIGNED(
96 memory, (detail::PickAlignment<value_type, alignment>::value)),
97 sizeof(value_type) * 2);
98 val[0] = byte_swap<value_type, endian>(val[0]);
99 val[1] = byte_swap<value_type, endian>(val[1]);
100
101 // Shift bits from the lower value into place.
102 make_unsigned_t<value_type> lowerVal = val[0] >> startBit;
103 // Mask off upper bits after right shift in case of signed type.
104 make_unsigned_t<value_type> numBitsFirstVal =
105 (sizeof(value_type) * 8) - startBit;
106 lowerVal &= ((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1;
107
108 // Get the bits from the upper value.
109 make_unsigned_t<value_type> upperVal =
110 val[1] & (((make_unsigned_t<value_type>)1 << startBit) - 1);
111 // Shift them in to place.
112 upperVal <<= numBitsFirstVal;
113
114 return lowerVal | upperVal;
115 }
116 }
117
118 /// Write a value to memory with a particular endianness, for a location
119 /// that starts at the given bit offset within the first byte.
120 template <typename value_type, endianness endian, std::size_t alignment>
writeAtBitAlignment(void * memory,value_type value,uint64_t startBit)121 inline void writeAtBitAlignment(void *memory, value_type value,
122 uint64_t startBit) {
123 assert(startBit < 8);
124 if (startBit == 0)
125 write<value_type, endian, alignment>(memory, value);
126 else {
127 // Read two values and shift the result into them.
128 value_type val[2];
129 memcpy(&val[0],
130 LLVM_ASSUME_ALIGNED(
131 memory, (detail::PickAlignment<value_type, alignment>::value)),
132 sizeof(value_type) * 2);
133 val[0] = byte_swap<value_type, endian>(val[0]);
134 val[1] = byte_swap<value_type, endian>(val[1]);
135
136 // Mask off any existing bits in the upper part of the lower value that
137 // we want to replace.
138 val[0] &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
139 make_unsigned_t<value_type> numBitsFirstVal =
140 (sizeof(value_type) * 8) - startBit;
141 make_unsigned_t<value_type> lowerVal = value;
142 if (startBit > 0) {
143 // Mask off the upper bits in the new value that are not going to go into
144 // the lower value. This avoids a left shift of a negative value, which
145 // is undefined behavior.
146 lowerVal &= (((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1);
147 // Now shift the new bits into place
148 lowerVal <<= startBit;
149 }
150 val[0] |= lowerVal;
151
152 // Mask off any existing bits in the lower part of the upper value that
153 // we want to replace.
154 val[1] &= ~(((make_unsigned_t<value_type>)1 << startBit) - 1);
155 // Next shift the bits that go into the upper value into position.
156 make_unsigned_t<value_type> upperVal = value >> numBitsFirstVal;
157 // Mask off upper bits after right shift in case of signed type.
158 upperVal &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
159 val[1] |= upperVal;
160
161 // Finally, rewrite values.
162 val[0] = byte_swap<value_type, endian>(val[0]);
163 val[1] = byte_swap<value_type, endian>(val[1]);
164 memcpy(LLVM_ASSUME_ALIGNED(
165 memory, (detail::PickAlignment<value_type, alignment>::value)),
166 &val[0], sizeof(value_type) * 2);
167 }
168 }
169 } // end namespace endian
170
171 namespace detail {
172 template<typename value_type,
173 endianness endian,
174 std::size_t alignment>
175 struct packed_endian_specific_integral {
176 packed_endian_specific_integral() = default;
177
packed_endian_specific_integralpacked_endian_specific_integral178 explicit packed_endian_specific_integral(value_type val) { *this = val; }
179
value_typepacked_endian_specific_integral180 operator value_type() const {
181 return endian::read<value_type, endian, alignment>(
182 (const void*)Value.buffer);
183 }
184
185 void operator=(value_type newValue) {
186 endian::write<value_type, endian, alignment>(
187 (void*)Value.buffer, newValue);
188 }
189
190 packed_endian_specific_integral &operator+=(value_type newValue) {
191 *this = *this + newValue;
192 return *this;
193 }
194
195 packed_endian_specific_integral &operator-=(value_type newValue) {
196 *this = *this - newValue;
197 return *this;
198 }
199
200 packed_endian_specific_integral &operator|=(value_type newValue) {
201 *this = *this | newValue;
202 return *this;
203 }
204
205 packed_endian_specific_integral &operator&=(value_type newValue) {
206 *this = *this & newValue;
207 return *this;
208 }
209
210 private:
211 AlignedCharArray<PickAlignment<value_type, alignment>::value,
212 sizeof(value_type)> Value;
213
214 public:
215 struct ref {
refpacked_endian_specific_integral::ref216 explicit ref(void *Ptr) : Ptr(Ptr) {}
217
value_typepacked_endian_specific_integral::ref218 operator value_type() const {
219 return endian::read<value_type, endian, alignment>(Ptr);
220 }
221
222 void operator=(value_type NewValue) {
223 endian::write<value_type, endian, alignment>(Ptr, NewValue);
224 }
225
226 private:
227 void *Ptr;
228 };
229 };
230
231 } // end namespace detail
232
233 typedef detail::packed_endian_specific_integral
234 <uint16_t, little, unaligned> ulittle16_t;
235 typedef detail::packed_endian_specific_integral
236 <uint32_t, little, unaligned> ulittle32_t;
237 typedef detail::packed_endian_specific_integral
238 <uint64_t, little, unaligned> ulittle64_t;
239
240 typedef detail::packed_endian_specific_integral
241 <int16_t, little, unaligned> little16_t;
242 typedef detail::packed_endian_specific_integral
243 <int32_t, little, unaligned> little32_t;
244 typedef detail::packed_endian_specific_integral
245 <int64_t, little, unaligned> little64_t;
246
247 typedef detail::packed_endian_specific_integral
248 <uint16_t, little, aligned> aligned_ulittle16_t;
249 typedef detail::packed_endian_specific_integral
250 <uint32_t, little, aligned> aligned_ulittle32_t;
251 typedef detail::packed_endian_specific_integral
252 <uint64_t, little, aligned> aligned_ulittle64_t;
253
254 typedef detail::packed_endian_specific_integral
255 <int16_t, little, aligned> aligned_little16_t;
256 typedef detail::packed_endian_specific_integral
257 <int32_t, little, aligned> aligned_little32_t;
258 typedef detail::packed_endian_specific_integral
259 <int64_t, little, aligned> aligned_little64_t;
260
261 typedef detail::packed_endian_specific_integral
262 <uint16_t, big, unaligned> ubig16_t;
263 typedef detail::packed_endian_specific_integral
264 <uint32_t, big, unaligned> ubig32_t;
265 typedef detail::packed_endian_specific_integral
266 <uint64_t, big, unaligned> ubig64_t;
267
268 typedef detail::packed_endian_specific_integral
269 <int16_t, big, unaligned> big16_t;
270 typedef detail::packed_endian_specific_integral
271 <int32_t, big, unaligned> big32_t;
272 typedef detail::packed_endian_specific_integral
273 <int64_t, big, unaligned> big64_t;
274
275 typedef detail::packed_endian_specific_integral
276 <uint16_t, big, aligned> aligned_ubig16_t;
277 typedef detail::packed_endian_specific_integral
278 <uint32_t, big, aligned> aligned_ubig32_t;
279 typedef detail::packed_endian_specific_integral
280 <uint64_t, big, aligned> aligned_ubig64_t;
281
282 typedef detail::packed_endian_specific_integral
283 <int16_t, big, aligned> aligned_big16_t;
284 typedef detail::packed_endian_specific_integral
285 <int32_t, big, aligned> aligned_big32_t;
286 typedef detail::packed_endian_specific_integral
287 <int64_t, big, aligned> aligned_big64_t;
288
289 typedef detail::packed_endian_specific_integral
290 <uint16_t, native, unaligned> unaligned_uint16_t;
291 typedef detail::packed_endian_specific_integral
292 <uint32_t, native, unaligned> unaligned_uint32_t;
293 typedef detail::packed_endian_specific_integral
294 <uint64_t, native, unaligned> unaligned_uint64_t;
295
296 typedef detail::packed_endian_specific_integral
297 <int16_t, native, unaligned> unaligned_int16_t;
298 typedef detail::packed_endian_specific_integral
299 <int32_t, native, unaligned> unaligned_int32_t;
300 typedef detail::packed_endian_specific_integral
301 <int64_t, native, unaligned> unaligned_int64_t;
302
303 namespace endian {
read(const void * P)304 template <typename T, endianness E> inline T read(const void *P) {
305 return *(const detail::packed_endian_specific_integral<T, E, unaligned> *)P;
306 }
307
read16(const void * P)308 template <endianness E> inline uint16_t read16(const void *P) {
309 return read<uint16_t, E>(P);
310 }
read32(const void * P)311 template <endianness E> inline uint32_t read32(const void *P) {
312 return read<uint32_t, E>(P);
313 }
read64(const void * P)314 template <endianness E> inline uint64_t read64(const void *P) {
315 return read<uint64_t, E>(P);
316 }
317
read16le(const void * P)318 inline uint16_t read16le(const void *P) { return read16<little>(P); }
read32le(const void * P)319 inline uint32_t read32le(const void *P) { return read32<little>(P); }
read64le(const void * P)320 inline uint64_t read64le(const void *P) { return read64<little>(P); }
read16be(const void * P)321 inline uint16_t read16be(const void *P) { return read16<big>(P); }
read32be(const void * P)322 inline uint32_t read32be(const void *P) { return read32<big>(P); }
read64be(const void * P)323 inline uint64_t read64be(const void *P) { return read64<big>(P); }
324
write(void * P,T V)325 template <typename T, endianness E> inline void write(void *P, T V) {
326 *(detail::packed_endian_specific_integral<T, E, unaligned> *)P = V;
327 }
328
write16(void * P,uint16_t V)329 template <endianness E> inline void write16(void *P, uint16_t V) {
330 write<uint16_t, E>(P, V);
331 }
write32(void * P,uint32_t V)332 template <endianness E> inline void write32(void *P, uint32_t V) {
333 write<uint32_t, E>(P, V);
334 }
write64(void * P,uint64_t V)335 template <endianness E> inline void write64(void *P, uint64_t V) {
336 write<uint64_t, E>(P, V);
337 }
338
write16le(void * P,uint16_t V)339 inline void write16le(void *P, uint16_t V) { write16<little>(P, V); }
write32le(void * P,uint32_t V)340 inline void write32le(void *P, uint32_t V) { write32<little>(P, V); }
write64le(void * P,uint64_t V)341 inline void write64le(void *P, uint64_t V) { write64<little>(P, V); }
write16be(void * P,uint16_t V)342 inline void write16be(void *P, uint16_t V) { write16<big>(P, V); }
write32be(void * P,uint32_t V)343 inline void write32be(void *P, uint32_t V) { write32<big>(P, V); }
write64be(void * P,uint64_t V)344 inline void write64be(void *P, uint64_t V) { write64<big>(P, V); }
345 } // end namespace endian
346 } // end namespace support
347 } // end namespace llvm
348
349 #endif
350