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