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,
63 inline value_type readNext(const CharT *&memory) {
64 value_type ret = read<value_type, endian, alignment>(memory);
65 memory += sizeof(value_type);
69 /// Write a value to memory with a particular endianness.
70 template<typename value_type,
72 std::size_t alignment>
73 inline void write(void *memory, value_type value) {
74 value = byte_swap<value_type, endian>(value);
75 memcpy(LLVM_ASSUME_ALIGNED(memory,
76 (detail::PickAlignment<value_type, alignment>::value)),
81 /// Read a value of a particular endianness from memory, for a location
82 /// that starts at the given bit offset within the first byte.
83 template <typename value_type, endianness endian, std::size_t alignment>
84 inline value_type readAtBitAlignment(const void *memory, uint64_t startBit) {
87 return read<value_type, endian, alignment>(memory);
89 // Read two values and compose the result from them.
93 memory, (detail::PickAlignment<value_type, alignment>::value)),
94 sizeof(value_type) * 2);
95 val[0] = byte_swap<value_type, endian>(val[0]);
96 val[1] = byte_swap<value_type, endian>(val[1]);
98 // Shift bits from the lower value into place.
99 unsigned lowerVal = val[0] >> startBit;
100 // Mask off upper bits after right shift in case of signed type.
101 unsigned numBitsFirstVal = (sizeof(value_type) * 8) - startBit;
102 lowerVal &= (1 << numBitsFirstVal) - 1;
104 // Get the bits from the upper value.
105 unsigned upperVal = val[1] & ((1 << startBit) - 1);
106 // Shift them in to place.
107 upperVal <<= numBitsFirstVal;
109 return lowerVal | upperVal;
113 /// Write a value to memory with a particular endianness, for a location
114 /// that starts at the given bit offset within the first byte.
115 template <typename value_type, endianness endian, std::size_t alignment>
116 inline void writeAtBitAlignment(void *memory, value_type value,
118 assert(startBit < 8);
120 write<value_type, endian, alignment>(memory, value);
122 // Read two values and shift the result into them.
126 memory, (detail::PickAlignment<value_type, alignment>::value)),
127 sizeof(value_type) * 2);
128 val[0] = byte_swap<value_type, endian>(val[0]);
129 val[1] = byte_swap<value_type, endian>(val[1]);
131 // Mask off any existing bits in the upper part of the lower value that
132 // we want to replace.
133 val[0] &= (1 << startBit) - 1;
134 // Now shift in the new bits
135 val[0] |= value << startBit;
137 // Mask off any existing bits in the lower part of the upper value that
138 // we want to replace.
139 val[1] &= ~((1 << startBit) - 1);
140 // Next shift the bits that go into the upper value into position.
141 unsigned numBitsFirstVal = (sizeof(value_type) * 8) - startBit;
142 unsigned upperVal = value >> numBitsFirstVal;
143 // Mask off upper bits after right shift in case of signed type.
144 upperVal &= (1 << startBit) - 1;
147 // Finally, rewrite values.
148 val[0] = byte_swap<value_type, endian>(val[0]);
149 val[1] = byte_swap<value_type, endian>(val[1]);
150 memcpy(LLVM_ASSUME_ALIGNED(
151 memory, (detail::PickAlignment<value_type, alignment>::value)),
152 &val[0], sizeof(value_type) * 2);
155 } // end namespace endian
158 template<typename value_type,
160 std::size_t alignment>
161 struct packed_endian_specific_integral {
162 operator value_type() const {
163 return endian::read<value_type, endian, alignment>(
164 (const void*)Value.buffer);
167 void operator=(value_type newValue) {
168 endian::write<value_type, endian, alignment>(
169 (void*)Value.buffer, newValue);
172 packed_endian_specific_integral &operator+=(value_type newValue) {
173 *this = *this + newValue;
177 packed_endian_specific_integral &operator-=(value_type newValue) {
178 *this = *this - newValue;
182 packed_endian_specific_integral &operator|=(value_type newValue) {
183 *this = *this | newValue;
187 packed_endian_specific_integral &operator&=(value_type newValue) {
188 *this = *this & newValue;
193 AlignedCharArray<PickAlignment<value_type, alignment>::value,
194 sizeof(value_type)> Value;
198 explicit ref(void *Ptr) : Ptr(Ptr) {}
200 operator value_type() const {
201 return endian::read<value_type, endian, alignment>(Ptr);
204 void operator=(value_type NewValue) {
205 endian::write<value_type, endian, alignment>(Ptr, NewValue);
213 } // end namespace detail
215 typedef detail::packed_endian_specific_integral
216 <uint16_t, little, unaligned> ulittle16_t;
217 typedef detail::packed_endian_specific_integral
218 <uint32_t, little, unaligned> ulittle32_t;
219 typedef detail::packed_endian_specific_integral
220 <uint64_t, little, unaligned> ulittle64_t;
222 typedef detail::packed_endian_specific_integral
223 <int16_t, little, unaligned> little16_t;
224 typedef detail::packed_endian_specific_integral
225 <int32_t, little, unaligned> little32_t;
226 typedef detail::packed_endian_specific_integral
227 <int64_t, little, unaligned> little64_t;
229 typedef detail::packed_endian_specific_integral
230 <uint16_t, little, aligned> aligned_ulittle16_t;
231 typedef detail::packed_endian_specific_integral
232 <uint32_t, little, aligned> aligned_ulittle32_t;
233 typedef detail::packed_endian_specific_integral
234 <uint64_t, little, aligned> aligned_ulittle64_t;
236 typedef detail::packed_endian_specific_integral
237 <int16_t, little, aligned> aligned_little16_t;
238 typedef detail::packed_endian_specific_integral
239 <int32_t, little, aligned> aligned_little32_t;
240 typedef detail::packed_endian_specific_integral
241 <int64_t, little, aligned> aligned_little64_t;
243 typedef detail::packed_endian_specific_integral
244 <uint16_t, big, unaligned> ubig16_t;
245 typedef detail::packed_endian_specific_integral
246 <uint32_t, big, unaligned> ubig32_t;
247 typedef detail::packed_endian_specific_integral
248 <uint64_t, big, unaligned> ubig64_t;
250 typedef detail::packed_endian_specific_integral
251 <int16_t, big, unaligned> big16_t;
252 typedef detail::packed_endian_specific_integral
253 <int32_t, big, unaligned> big32_t;
254 typedef detail::packed_endian_specific_integral
255 <int64_t, big, unaligned> big64_t;
257 typedef detail::packed_endian_specific_integral
258 <uint16_t, big, aligned> aligned_ubig16_t;
259 typedef detail::packed_endian_specific_integral
260 <uint32_t, big, aligned> aligned_ubig32_t;
261 typedef detail::packed_endian_specific_integral
262 <uint64_t, big, aligned> aligned_ubig64_t;
264 typedef detail::packed_endian_specific_integral
265 <int16_t, big, aligned> aligned_big16_t;
266 typedef detail::packed_endian_specific_integral
267 <int32_t, big, aligned> aligned_big32_t;
268 typedef detail::packed_endian_specific_integral
269 <int64_t, big, aligned> aligned_big64_t;
271 typedef detail::packed_endian_specific_integral
272 <uint16_t, native, unaligned> unaligned_uint16_t;
273 typedef detail::packed_endian_specific_integral
274 <uint32_t, native, unaligned> unaligned_uint32_t;
275 typedef detail::packed_endian_specific_integral
276 <uint64_t, native, unaligned> unaligned_uint64_t;
278 typedef detail::packed_endian_specific_integral
279 <int16_t, native, unaligned> unaligned_int16_t;
280 typedef detail::packed_endian_specific_integral
281 <int32_t, native, unaligned> unaligned_int32_t;
282 typedef detail::packed_endian_specific_integral
283 <int64_t, native, unaligned> unaligned_int64_t;
286 inline uint16_t read16le(const void *p) { return *(const ulittle16_t *)p; }
287 inline uint32_t read32le(const void *p) { return *(const ulittle32_t *)p; }
288 inline uint64_t read64le(const void *p) { return *(const ulittle64_t *)p; }
289 inline uint16_t read16be(const void *p) { return *(const ubig16_t *)p; }
290 inline uint32_t read32be(const void *p) { return *(const ubig32_t *)p; }
291 inline uint64_t read64be(const void *p) { return *(const ubig64_t *)p; }
293 inline void write16le(void *p, uint16_t v) { *(ulittle16_t *)p = v; }
294 inline void write32le(void *p, uint32_t v) { *(ulittle32_t *)p = v; }
295 inline void write64le(void *p, uint64_t v) { *(ulittle64_t *)p = v; }
296 inline void write16be(void *p, uint16_t v) { *(ubig16_t *)p = v; }
297 inline void write32be(void *p, uint32_t v) { *(ubig32_t *)p = v; }
298 inline void write64be(void *p, uint64_t v) { *(ubig64_t *)p = v; }
299 } // end namespace endian
300 } // end namespace support
301 } // end namespace llvm