LLVM  8.0.1
Endian.h
Go to the documentation of this file.
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/Compiler.h"
19 #include "llvm/Support/Host.h"
21 #include <cassert>
22 #include <cstddef>
23 #include <cstdint>
24 #include <cstring>
25 #include <type_traits>
26 
27 namespace llvm {
28 namespace support {
29 
31 
32 // These are named values for common alignments.
33 enum {aligned = 0, unaligned = 1};
34 
35 namespace detail {
36 
37 /// ::value is either alignment, or alignof(T) if alignment is 0.
38 template<class T, int alignment>
39 struct PickAlignment {
40  enum { value = alignment == 0 ? alignof(T) : alignment };
41 };
42 
43 } // end namespace detail
44 
45 namespace endian {
46 
48  return sys::IsBigEndianHost ? big : little;
49 }
50 
51 template <typename value_type>
52 inline value_type byte_swap(value_type value, endianness endian) {
53  if ((endian != native) && (endian != system_endianness()))
54  sys::swapByteOrder(value);
55  return value;
56 }
57 
58 /// Swap the bytes of value to match the given endianness.
59 template<typename value_type, endianness endian>
60 inline value_type byte_swap(value_type value) {
61  return byte_swap(value, endian);
62 }
63 
64 /// Read a value of a particular endianness from memory.
65 template <typename value_type, std::size_t alignment>
66 inline value_type read(const void *memory, endianness endian) {
67  value_type ret;
68 
69  memcpy(&ret,
72  sizeof(value_type));
73  return byte_swap<value_type>(ret, endian);
74 }
75 
76 template<typename value_type,
77  endianness endian,
78  std::size_t alignment>
79 inline value_type read(const void *memory) {
80  return read<value_type, alignment>(memory, endian);
81 }
82 
83 /// Read a value of a particular endianness from a buffer, and increment the
84 /// buffer past that value.
85 template <typename value_type, std::size_t alignment, typename CharT>
86 inline value_type readNext(const CharT *&memory, endianness endian) {
87  value_type ret = read<value_type, alignment>(memory, endian);
88  memory += sizeof(value_type);
89  return ret;
90 }
91 
92 template<typename value_type, endianness endian, std::size_t alignment,
93  typename CharT>
94 inline value_type readNext(const CharT *&memory) {
95  return readNext<value_type, alignment, CharT>(memory, endian);
96 }
97 
98 /// Write a value to memory with a particular endianness.
99 template <typename value_type, std::size_t alignment>
100 inline void write(void *memory, value_type value, endianness endian) {
101  value = byte_swap<value_type>(value, endian);
104  &value, sizeof(value_type));
105 }
106 
107 template<typename value_type,
108  endianness endian,
109  std::size_t alignment>
110 inline void write(void *memory, value_type value) {
111  write<value_type, alignment>(memory, value, endian);
112 }
113 
114 template <typename value_type>
115 using make_unsigned_t = typename std::make_unsigned<value_type>::type;
116 
117 /// Read a value of a particular endianness from memory, for a location
118 /// that starts at the given bit offset within the first byte.
119 template <typename value_type, endianness endian, std::size_t alignment>
120 inline value_type readAtBitAlignment(const void *memory, uint64_t startBit) {
121  assert(startBit < 8);
122  if (startBit == 0)
123  return read<value_type, endian, alignment>(memory);
124  else {
125  // Read two values and compose the result from them.
126  value_type val[2];
127  memcpy(&val[0],
130  sizeof(value_type) * 2);
131  val[0] = byte_swap<value_type, endian>(val[0]);
132  val[1] = byte_swap<value_type, endian>(val[1]);
133 
134  // Shift bits from the lower value into place.
135  make_unsigned_t<value_type> lowerVal = val[0] >> startBit;
136  // Mask off upper bits after right shift in case of signed type.
137  make_unsigned_t<value_type> numBitsFirstVal =
138  (sizeof(value_type) * 8) - startBit;
139  lowerVal &= ((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1;
140 
141  // Get the bits from the upper value.
142  make_unsigned_t<value_type> upperVal =
143  val[1] & (((make_unsigned_t<value_type>)1 << startBit) - 1);
144  // Shift them in to place.
145  upperVal <<= numBitsFirstVal;
146 
147  return lowerVal | upperVal;
148  }
149 }
150 
151 /// Write a value to memory with a particular endianness, for a location
152 /// that starts at the given bit offset within the first byte.
153 template <typename value_type, endianness endian, std::size_t alignment>
154 inline void writeAtBitAlignment(void *memory, value_type value,
155  uint64_t startBit) {
156  assert(startBit < 8);
157  if (startBit == 0)
158  write<value_type, endian, alignment>(memory, value);
159  else {
160  // Read two values and shift the result into them.
161  value_type val[2];
162  memcpy(&val[0],
165  sizeof(value_type) * 2);
166  val[0] = byte_swap<value_type, endian>(val[0]);
167  val[1] = byte_swap<value_type, endian>(val[1]);
168 
169  // Mask off any existing bits in the upper part of the lower value that
170  // we want to replace.
171  val[0] &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
172  make_unsigned_t<value_type> numBitsFirstVal =
173  (sizeof(value_type) * 8) - startBit;
175  if (startBit > 0) {
176  // Mask off the upper bits in the new value that are not going to go into
177  // the lower value. This avoids a left shift of a negative value, which
178  // is undefined behavior.
179  lowerVal &= (((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1);
180  // Now shift the new bits into place
181  lowerVal <<= startBit;
182  }
183  val[0] |= lowerVal;
184 
185  // Mask off any existing bits in the lower part of the upper value that
186  // we want to replace.
187  val[1] &= ~(((make_unsigned_t<value_type>)1 << startBit) - 1);
188  // Next shift the bits that go into the upper value into position.
189  make_unsigned_t<value_type> upperVal = value >> numBitsFirstVal;
190  // Mask off upper bits after right shift in case of signed type.
191  upperVal &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
192  val[1] |= upperVal;
193 
194  // Finally, rewrite values.
195  val[0] = byte_swap<value_type, endian>(val[0]);
196  val[1] = byte_swap<value_type, endian>(val[1]);
199  &val[0], sizeof(value_type) * 2);
200  }
201 }
202 
203 } // end namespace endian
204 
205 namespace detail {
206 
207 template<typename value_type,
208  endianness endian,
209  std::size_t alignment>
212 
213  explicit packed_endian_specific_integral(value_type val) { *this = val; }
214 
215  operator value_type() const {
216  return endian::read<value_type, endian, alignment>(
217  (const void*)Value.buffer);
218  }
219 
220  void operator=(value_type newValue) {
221  endian::write<value_type, endian, alignment>(
222  (void*)Value.buffer, newValue);
223  }
224 
226  *this = *this + newValue;
227  return *this;
228  }
229 
231  *this = *this - newValue;
232  return *this;
233  }
234 
236  *this = *this | newValue;
237  return *this;
238  }
239 
241  *this = *this & newValue;
242  return *this;
243  }
244 
245 private:
247  sizeof(value_type)> Value;
248 
249 public:
250  struct ref {
251  explicit ref(void *Ptr) : Ptr(Ptr) {}
252 
253  operator value_type() const {
254  return endian::read<value_type, endian, alignment>(Ptr);
255  }
256 
257  void operator=(value_type NewValue) {
258  endian::write<value_type, endian, alignment>(Ptr, NewValue);
259  }
260 
261  private:
262  void *Ptr;
263  };
264 };
265 
266 } // end namespace detail
267 
268 using ulittle16_t =
270 using ulittle32_t =
272 using ulittle64_t =
274 
275 using little16_t =
277 using little32_t =
279 using little64_t =
281 
282 using aligned_ulittle16_t =
284 using aligned_ulittle32_t =
286 using aligned_ulittle64_t =
288 
289 using aligned_little16_t =
291 using aligned_little32_t =
293 using aligned_little64_t =
295 
296 using ubig16_t =
298 using ubig32_t =
300 using ubig64_t =
302 
303 using big16_t =
305 using big32_t =
307 using big64_t =
309 
310 using aligned_ubig16_t =
312 using aligned_ubig32_t =
314 using aligned_ubig64_t =
316 
317 using aligned_big16_t =
319 using aligned_big32_t =
321 using aligned_big64_t =
323 
324 using unaligned_uint16_t =
326 using unaligned_uint32_t =
328 using unaligned_uint64_t =
330 
331 using unaligned_int16_t =
333 using unaligned_int32_t =
335 using unaligned_int64_t =
337 
338 namespace endian {
339 
340 template <typename T> inline T read(const void *P, endianness E) {
341  return read<T, unaligned>(P, E);
342 }
343 
344 template <typename T, endianness E> inline T read(const void *P) {
346 }
347 
348 inline uint16_t read16(const void *P, endianness E) {
349  return read<uint16_t>(P, E);
350 }
351 inline uint32_t read32(const void *P, endianness E) {
352  return read<uint32_t>(P, E);
353 }
354 inline uint64_t read64(const void *P, endianness E) {
355  return read<uint64_t>(P, E);
356 }
357 
358 template <endianness E> inline uint16_t read16(const void *P) {
359  return read<uint16_t, E>(P);
360 }
361 template <endianness E> inline uint32_t read32(const void *P) {
362  return read<uint32_t, E>(P);
363 }
364 template <endianness E> inline uint64_t read64(const void *P) {
365  return read<uint64_t, E>(P);
366 }
367 
368 inline uint16_t read16le(const void *P) { return read16<little>(P); }
369 inline uint32_t read32le(const void *P) { return read32<little>(P); }
370 inline uint64_t read64le(const void *P) { return read64<little>(P); }
371 inline uint16_t read16be(const void *P) { return read16<big>(P); }
372 inline uint32_t read32be(const void *P) { return read32<big>(P); }
373 inline uint64_t read64be(const void *P) { return read64<big>(P); }
374 
375 template <typename T> inline void write(void *P, T V, endianness E) {
376  write<T, unaligned>(P, V, E);
377 }
378 
379 template <typename T, endianness E> inline void write(void *P, T V) {
381 }
382 
383 inline void write16(void *P, uint16_t V, endianness E) {
384  write<uint16_t>(P, V, E);
385 }
386 inline void write32(void *P, uint32_t V, endianness E) {
387  write<uint32_t>(P, V, E);
388 }
389 inline void write64(void *P, uint64_t V, endianness E) {
390  write<uint64_t>(P, V, E);
391 }
392 
393 template <endianness E> inline void write16(void *P, uint16_t V) {
394  write<uint16_t, E>(P, V);
395 }
396 template <endianness E> inline void write32(void *P, uint32_t V) {
397  write<uint32_t, E>(P, V);
398 }
399 template <endianness E> inline void write64(void *P, uint64_t V) {
400  write<uint64_t, E>(P, V);
401 }
402 
403 inline void write16le(void *P, uint16_t V) { write16<little>(P, V); }
404 inline void write32le(void *P, uint32_t V) { write32<little>(P, V); }
405 inline void write64le(void *P, uint64_t V) { write64<little>(P, V); }
406 inline void write16be(void *P, uint16_t V) { write16<big>(P, V); }
407 inline void write32be(void *P, uint32_t V) { write32<big>(P, V); }
408 inline void write64be(void *P, uint64_t V) { write64<big>(P, V); }
409 
410 } // end namespace endian
411 
412 } // end namespace support
413 } // end namespace llvm
414 
415 #endif // LLVM_SUPPORT_ENDIAN_H
packed_endian_specific_integral & operator+=(value_type newValue)
Definition: Endian.h:225
void write32be(void *P, uint32_t V)
Definition: Endian.h:407
value_type readAtBitAlignment(const void *memory, uint64_t startBit)
Read a value of a particular endianness from memory, for a location that starts at the given bit offs...
Definition: Endian.h:120
void swapByteOrder(T &Value)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void write32le(void *P, uint32_t V)
Definition: Endian.h:404
uint16_t read16le(const void *P)
Definition: Endian.h:368
uint64_t read64be(const void *P)
Definition: Endian.h:373
constexpr bool IsBigEndianHost
Definition: Host.h:47
uint32_t read32be(const void *P)
Definition: Endian.h:372
void write16le(void *P, uint16_t V)
Definition: Endian.h:403
#define LLVM_ASSUME_ALIGNED(p, a)
LLVM_ASSUME_ALIGNED Returns a pointer with an assumed alignment.
Definition: Compiler.h:330
uint64_t read64(const void *P)
Definition: Endian.h:364
void write32(void *P, uint32_t V)
Definition: Endian.h:396
#define T
T read(const void *P)
Definition: Endian.h:344
value_type byte_swap(value_type value)
Swap the bytes of value to match the given endianness.
Definition: Endian.h:60
#define P(N)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void write64(void *P, uint64_t V)
Definition: Endian.h:399
void writeAtBitAlignment(void *memory, value_type value, uint64_t startBit)
Write a value to memory with a particular endianness, for a location that starts at the given bit off...
Definition: Endian.h:154
void write16(void *P, uint16_t V)
Definition: Endian.h:393
static void write(bool isBE, void *P, T V)
packed_endian_specific_integral & operator|=(value_type newValue)
Definition: Endian.h:235
value_type readNext(const CharT *&memory)
Definition: Endian.h:94
packed_endian_specific_integral & operator-=(value_type newValue)
Definition: Endian.h:230
uint32_t read32(const void *P)
Definition: Endian.h:361
constexpr endianness system_endianness()
Definition: Endian.h:47
void write16be(void *P, uint16_t V)
Definition: Endian.h:406
uint64_t read64le(const void *P)
Definition: Endian.h:370
void write64le(void *P, uint64_t V)
Definition: Endian.h:405
uint32_t read32le(const void *P)
Definition: Endian.h:369
typename std::make_unsigned< value_type >::type make_unsigned_t
Definition: Endian.h:115
void write64be(void *P, uint64_t V)
Definition: Endian.h:408
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
E & operator &=(E &LHS, E RHS)
Definition: BitmaskEnum.h:134
::value is either alignment, or alignof(T) if alignment is 0.
Definition: Endian.h:39
uint16_t read16be(const void *P)
Definition: Endian.h:371
Helper for building an aligned character array type.
Definition: AlignOf.h:36
uint16_t read16(const void *P)
Definition: Endian.h:358