LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/Host.h"
21 
22 namespace llvm {
23 namespace support {
25 
26 // These are named values for common alignments.
27 enum {aligned = 0, unaligned = 1};
28 
29 namespace detail {
30  /// \brief ::value is either alignment, or alignof(T) if alignment is 0.
31  template<class T, int alignment>
32  struct PickAlignment {
33  enum {value = alignment == 0 ? AlignOf<T>::Alignment : alignment};
34  };
35 } // end namespace detail
36 
37 namespace endian {
38 template<typename value_type, endianness endian>
39 inline value_type byte_swap(value_type value) {
40  if (endian != native && sys::IsBigEndianHost != (endian == big))
41  return sys::SwapByteOrder(value);
42  return value;
43 }
44 
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 template<typename value_type,
59  endianness endian,
60  std::size_t alignment>
61 inline void write(void *memory, value_type value) {
62  value = byte_swap<value_type, endian>(value);
65  &value,
66  sizeof(value_type));
67 }
68 } // end namespace endian
69 
70 namespace detail {
71 template<typename value_type,
72  endianness endian,
73  std::size_t alignment>
75  operator value_type() const {
76  return endian::read<value_type, endian, alignment>(
77  (const void*)Value.buffer);
78  }
79 
80  void operator=(value_type newValue) {
81  endian::write<value_type, endian, alignment>(
82  (void*)Value.buffer, newValue);
83  }
84 
85 private:
87  sizeof(value_type)> Value;
88 };
89 } // end namespace detail
90 
91 typedef detail::packed_endian_specific_integral
99 
108 
117 
126 
128  <uint8_t, big, unaligned> ubig8_t;
130  <uint16_t, big, unaligned> ubig16_t;
132  <uint32_t, big, unaligned> ubig32_t;
134  <uint64_t, big, unaligned> ubig64_t;
135 
137  <int8_t, big, unaligned> big8_t;
139  <int16_t, big, unaligned> big16_t;
141  <int32_t, big, unaligned> big32_t;
143  <int64_t, big, unaligned> big64_t;
144 
153 
162 
169 
176 } // end namespace llvm
177 } // end namespace support
178 
179 #endif
detail::packed_endian_specific_integral< int8_t, big, unaligned > big8_t
Definition: Endian.h:137
detail::packed_endian_specific_integral< uint16_t, native, unaligned > unaligned_uint16_t
Definition: Endian.h:164
detail::packed_endian_specific_integral< int64_t, native, unaligned > unaligned_int64_t
Definition: Endian.h:175
#define LLVM_ASSUME_ALIGNED(p, a)
Returns a pointer with an assumed alignment.
Definition: Compiler.h:326
value_type read(const void *memory)
Definition: Endian.h:48
detail::packed_endian_specific_integral< uint64_t, big, aligned > aligned_ubig64_t
Definition: Endian.h:152
detail::packed_endian_specific_integral< int8_t, big, aligned > aligned_big8_t
Definition: Endian.h:155
Helper for building an aligned character array type.
detail::packed_endian_specific_integral< int16_t, little, aligned > aligned_little16_t
Definition: Endian.h:121
detail::packed_endian_specific_integral< uint64_t, big, unaligned > ubig64_t
Definition: Endian.h:134
detail::packed_endian_specific_integral< uint32_t, big, unaligned > ubig32_t
Definition: Endian.h:132
detail::packed_endian_specific_integral< uint16_t, big, aligned > aligned_ubig16_t
Definition: Endian.h:148
detail::packed_endian_specific_integral< int64_t, little, unaligned > little64_t
Definition: Endian.h:107
detail::packed_endian_specific_integral< uint8_t, little, aligned > aligned_ulittle8_t
Definition: Endian.h:110
detail::packed_endian_specific_integral< int32_t, big, aligned > aligned_big32_t
Definition: Endian.h:159
value_type byte_swap(value_type value)
Definition: Endian.h:39
detail::packed_endian_specific_integral< int16_t, big, aligned > aligned_big16_t
Definition: Endian.h:157
detail::packed_endian_specific_integral< uint8_t, big, unaligned > ubig8_t
Definition: Endian.h:128
detail::packed_endian_specific_integral< int8_t, little, unaligned > little8_t
Definition: Endian.h:101
detail::packed_endian_specific_integral< uint8_t, little, unaligned > ulittle8_t
Definition: Endian.h:92
detail::packed_endian_specific_integral< int64_t, little, aligned > aligned_little64_t
Definition: Endian.h:125
detail::packed_endian_specific_integral< int16_t, native, unaligned > unaligned_int16_t
Definition: Endian.h:171
detail::packed_endian_specific_integral< uint8_t, big, aligned > aligned_ubig8_t
Definition: Endian.h:146
detail::packed_endian_specific_integral< uint32_t, big, aligned > aligned_ubig32_t
Definition: Endian.h:150
detail::packed_endian_specific_integral< int32_t, little, aligned > aligned_little32_t
Definition: Endian.h:123
detail::packed_endian_specific_integral< uint32_t, little, aligned > aligned_ulittle32_t
Definition: Endian.h:114
detail::packed_endian_specific_integral< uint64_t, little, aligned > aligned_ulittle64_t
Definition: Endian.h:116
detail::packed_endian_specific_integral< int8_t, little, aligned > aligned_little8_t
Definition: Endian.h:119
detail::packed_endian_specific_integral< int32_t, little, unaligned > little32_t
Definition: Endian.h:105
detail::packed_endian_specific_integral< int32_t, big, unaligned > big32_t
Definition: Endian.h:141
unsigned char SwapByteOrder(unsigned char C)
Definition: SwapByteOrder.h:71
detail::packed_endian_specific_integral< int16_t, little, unaligned > little16_t
Definition: Endian.h:103
detail::packed_endian_specific_integral< uint64_t, native, unaligned > unaligned_uint64_t
Definition: Endian.h:168
detail::packed_endian_specific_integral< uint16_t, little, unaligned > ulittle16_t
Definition: Endian.h:94
void write(void *memory, value_type value)
Definition: Endian.h:61
static const bool IsBigEndianHost
Definition: Host.h:35
detail::packed_endian_specific_integral< uint64_t, little, unaligned > ulittle64_t
Definition: Endian.h:98
detail::packed_endian_specific_integral< uint16_t, big, unaligned > ubig16_t
Definition: Endian.h:130
detail::packed_endian_specific_integral< int16_t, big, unaligned > big16_t
Definition: Endian.h:139
detail::packed_endian_specific_integral< int64_t, big, aligned > aligned_big64_t
Definition: Endian.h:161
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Definition: Endian.h:96
LLVM Value Representation.
Definition: Value.h:66
detail::packed_endian_specific_integral< int64_t, big, unaligned > big64_t
Definition: Endian.h:143
detail::packed_endian_specific_integral< int32_t, native, unaligned > unaligned_int32_t
Definition: Endian.h:173
detail::packed_endian_specific_integral< uint32_t, native, unaligned > unaligned_uint32_t
Definition: Endian.h:166
::value is either alignment, or alignof(T) if alignment is 0.
Definition: Endian.h:32
detail::packed_endian_specific_integral< uint16_t, little, aligned > aligned_ulittle16_t
Definition: Endian.h:112