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