1 //===- Endian.h - Utilities for IO with endian specific data ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares generic functions to read and write endian specific data.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_ENDIAN_H
15 #define LLVM_SUPPORT_ENDIAN_H
17 #include "llvm/Support/AlignOf.h"
18 #include "llvm/Support/Host.h"
19 #include "llvm/Support/SwapByteOrder.h"
23 enum endianness {big, little, native};
25 // These are named values for common alignments.
26 enum {aligned = 0, unaligned = 1};
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};
34 } // end namespace detail
37 /// Swap the bytes of value to match the given endianness.
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 sys::swapByteOrder(value);
45 /// Read a value of a particular endianness from memory.
46 template<typename value_type,
48 std::size_t alignment>
49 inline value_type read(const void *memory) {
53 LLVM_ASSUME_ALIGNED(memory,
54 (detail::PickAlignment<value_type, alignment>::value)),
56 return byte_swap<value_type, endian>(ret);
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 inline value_type readNext(const unsigned char *&memory) {
63 value_type ret = read<value_type, endian, alignment>(memory);
64 memory += sizeof(value_type);
68 /// Write a value to memory with a particular endianness.
69 template<typename value_type,
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,
75 (detail::PickAlignment<value_type, alignment>::value)),
79 } // end namespace endian
82 template<typename value_type,
84 std::size_t alignment>
85 struct packed_endian_specific_integral {
86 operator value_type() const {
87 return endian::read<value_type, endian, alignment>(
88 (const void*)Value.buffer);
91 void operator=(value_type newValue) {
92 endian::write<value_type, endian, alignment>(
93 (void*)Value.buffer, newValue);
97 AlignedCharArray<PickAlignment<value_type, alignment>::value,
98 sizeof(value_type)> Value;
102 explicit ref(void *Ptr) : Ptr(Ptr) {}
104 operator value_type() const {
105 return endian::read<value_type, endian, alignment>(Ptr);
108 void operator=(value_type NewValue) {
109 endian::write<value_type, endian, alignment>(Ptr, NewValue);
117 } // end namespace detail
119 typedef detail::packed_endian_specific_integral
120 <uint8_t, little, unaligned> ulittle8_t;
121 typedef detail::packed_endian_specific_integral
122 <uint16_t, little, unaligned> ulittle16_t;
123 typedef detail::packed_endian_specific_integral
124 <uint32_t, little, unaligned> ulittle32_t;
125 typedef detail::packed_endian_specific_integral
126 <uint64_t, little, unaligned> ulittle64_t;
128 typedef detail::packed_endian_specific_integral
129 <int8_t, little, unaligned> little8_t;
130 typedef detail::packed_endian_specific_integral
131 <int16_t, little, unaligned> little16_t;
132 typedef detail::packed_endian_specific_integral
133 <int32_t, little, unaligned> little32_t;
134 typedef detail::packed_endian_specific_integral
135 <int64_t, little, unaligned> little64_t;
137 typedef detail::packed_endian_specific_integral
138 <uint8_t, little, aligned> aligned_ulittle8_t;
139 typedef detail::packed_endian_specific_integral
140 <uint16_t, little, aligned> aligned_ulittle16_t;
141 typedef detail::packed_endian_specific_integral
142 <uint32_t, little, aligned> aligned_ulittle32_t;
143 typedef detail::packed_endian_specific_integral
144 <uint64_t, little, aligned> aligned_ulittle64_t;
146 typedef detail::packed_endian_specific_integral
147 <int8_t, little, aligned> aligned_little8_t;
148 typedef detail::packed_endian_specific_integral
149 <int16_t, little, aligned> aligned_little16_t;
150 typedef detail::packed_endian_specific_integral
151 <int32_t, little, aligned> aligned_little32_t;
152 typedef detail::packed_endian_specific_integral
153 <int64_t, little, aligned> aligned_little64_t;
155 typedef detail::packed_endian_specific_integral
156 <uint8_t, big, unaligned> ubig8_t;
157 typedef detail::packed_endian_specific_integral
158 <uint16_t, big, unaligned> ubig16_t;
159 typedef detail::packed_endian_specific_integral
160 <uint32_t, big, unaligned> ubig32_t;
161 typedef detail::packed_endian_specific_integral
162 <uint64_t, big, unaligned> ubig64_t;
164 typedef detail::packed_endian_specific_integral
165 <int8_t, big, unaligned> big8_t;
166 typedef detail::packed_endian_specific_integral
167 <int16_t, big, unaligned> big16_t;
168 typedef detail::packed_endian_specific_integral
169 <int32_t, big, unaligned> big32_t;
170 typedef detail::packed_endian_specific_integral
171 <int64_t, big, unaligned> big64_t;
173 typedef detail::packed_endian_specific_integral
174 <uint8_t, big, aligned> aligned_ubig8_t;
175 typedef detail::packed_endian_specific_integral
176 <uint16_t, big, aligned> aligned_ubig16_t;
177 typedef detail::packed_endian_specific_integral
178 <uint32_t, big, aligned> aligned_ubig32_t;
179 typedef detail::packed_endian_specific_integral
180 <uint64_t, big, aligned> aligned_ubig64_t;
182 typedef detail::packed_endian_specific_integral
183 <int8_t, big, aligned> aligned_big8_t;
184 typedef detail::packed_endian_specific_integral
185 <int16_t, big, aligned> aligned_big16_t;
186 typedef detail::packed_endian_specific_integral
187 <int32_t, big, aligned> aligned_big32_t;
188 typedef detail::packed_endian_specific_integral
189 <int64_t, big, aligned> aligned_big64_t;
191 typedef detail::packed_endian_specific_integral
192 <uint16_t, native, unaligned> unaligned_uint16_t;
193 typedef detail::packed_endian_specific_integral
194 <uint32_t, native, unaligned> unaligned_uint32_t;
195 typedef detail::packed_endian_specific_integral
196 <uint64_t, native, unaligned> unaligned_uint64_t;
198 typedef detail::packed_endian_specific_integral
199 <int16_t, native, unaligned> unaligned_int16_t;
200 typedef detail::packed_endian_specific_integral
201 <int32_t, native, unaligned> unaligned_int32_t;
202 typedef detail::packed_endian_specific_integral
203 <int64_t, native, unaligned> unaligned_int64_t;
204 } // end namespace llvm
205 } // end namespace support