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)),
80 } // end namespace endian
83 template<typename value_type,
85 std::size_t alignment>
86 struct packed_endian_specific_integral {
87 operator value_type() const {
88 return endian::read<value_type, endian, alignment>(
89 (const void*)Value.buffer);
92 void operator=(value_type newValue) {
93 endian::write<value_type, endian, alignment>(
94 (void*)Value.buffer, newValue);
97 packed_endian_specific_integral &operator+=(value_type newValue) {
98 *this = *this + newValue;
102 packed_endian_specific_integral &operator-=(value_type newValue) {
103 *this = *this - newValue;
107 packed_endian_specific_integral &operator|=(value_type newValue) {
108 *this = *this | newValue;
112 packed_endian_specific_integral &operator&=(value_type newValue) {
113 *this = *this & newValue;
118 AlignedCharArray<PickAlignment<value_type, alignment>::value,
119 sizeof(value_type)> Value;
123 explicit ref(void *Ptr) : Ptr(Ptr) {}
125 operator value_type() const {
126 return endian::read<value_type, endian, alignment>(Ptr);
129 void operator=(value_type NewValue) {
130 endian::write<value_type, endian, alignment>(Ptr, NewValue);
138 } // end namespace detail
140 typedef detail::packed_endian_specific_integral
141 <uint16_t, little, unaligned> ulittle16_t;
142 typedef detail::packed_endian_specific_integral
143 <uint32_t, little, unaligned> ulittle32_t;
144 typedef detail::packed_endian_specific_integral
145 <uint64_t, little, unaligned> ulittle64_t;
147 typedef detail::packed_endian_specific_integral
148 <int16_t, little, unaligned> little16_t;
149 typedef detail::packed_endian_specific_integral
150 <int32_t, little, unaligned> little32_t;
151 typedef detail::packed_endian_specific_integral
152 <int64_t, little, unaligned> little64_t;
154 typedef detail::packed_endian_specific_integral
155 <uint16_t, little, aligned> aligned_ulittle16_t;
156 typedef detail::packed_endian_specific_integral
157 <uint32_t, little, aligned> aligned_ulittle32_t;
158 typedef detail::packed_endian_specific_integral
159 <uint64_t, little, aligned> aligned_ulittle64_t;
161 typedef detail::packed_endian_specific_integral
162 <int16_t, little, aligned> aligned_little16_t;
163 typedef detail::packed_endian_specific_integral
164 <int32_t, little, aligned> aligned_little32_t;
165 typedef detail::packed_endian_specific_integral
166 <int64_t, little, aligned> aligned_little64_t;
168 typedef detail::packed_endian_specific_integral
169 <uint16_t, big, unaligned> ubig16_t;
170 typedef detail::packed_endian_specific_integral
171 <uint32_t, big, unaligned> ubig32_t;
172 typedef detail::packed_endian_specific_integral
173 <uint64_t, big, unaligned> ubig64_t;
175 typedef detail::packed_endian_specific_integral
176 <int16_t, big, unaligned> big16_t;
177 typedef detail::packed_endian_specific_integral
178 <int32_t, big, unaligned> big32_t;
179 typedef detail::packed_endian_specific_integral
180 <int64_t, big, unaligned> big64_t;
182 typedef detail::packed_endian_specific_integral
183 <uint16_t, big, aligned> aligned_ubig16_t;
184 typedef detail::packed_endian_specific_integral
185 <uint32_t, big, aligned> aligned_ubig32_t;
186 typedef detail::packed_endian_specific_integral
187 <uint64_t, big, aligned> aligned_ubig64_t;
189 typedef detail::packed_endian_specific_integral
190 <int16_t, big, aligned> aligned_big16_t;
191 typedef detail::packed_endian_specific_integral
192 <int32_t, big, aligned> aligned_big32_t;
193 typedef detail::packed_endian_specific_integral
194 <int64_t, big, aligned> aligned_big64_t;
196 typedef detail::packed_endian_specific_integral
197 <uint16_t, native, unaligned> unaligned_uint16_t;
198 typedef detail::packed_endian_specific_integral
199 <uint32_t, native, unaligned> unaligned_uint32_t;
200 typedef detail::packed_endian_specific_integral
201 <uint64_t, native, unaligned> unaligned_uint64_t;
203 typedef detail::packed_endian_specific_integral
204 <int16_t, native, unaligned> unaligned_int16_t;
205 typedef detail::packed_endian_specific_integral
206 <int32_t, native, unaligned> unaligned_int32_t;
207 typedef detail::packed_endian_specific_integral
208 <int64_t, native, unaligned> unaligned_int64_t;
211 inline uint16_t read16le(const void *p) { return *(const ulittle16_t *)p; }
212 inline uint32_t read32le(const void *p) { return *(const ulittle32_t *)p; }
213 inline uint64_t read64le(const void *p) { return *(const ulittle64_t *)p; }
214 inline uint16_t read16be(const void *p) { return *(const ubig16_t *)p; }
215 inline uint32_t read32be(const void *p) { return *(const ubig32_t *)p; }
216 inline uint64_t read64be(const void *p) { return *(const ubig64_t *)p; }
218 inline void write16le(void *p, uint16_t v) { *(ulittle16_t *)p = v; }
219 inline void write32le(void *p, uint32_t v) { *(ulittle32_t *)p = v; }
220 inline void write64le(void *p, uint64_t v) { *(ulittle64_t *)p = v; }
221 inline void write16be(void *p, uint16_t v) { *(ubig16_t *)p = v; }
222 inline void write32be(void *p, uint32_t v) { *(ubig32_t *)p = v; }
223 inline void write64be(void *p, uint64_t v) { *(ubig64_t *)p = v; }
224 } // end namespace endian
225 } // end namespace support
226 } // end namespace llvm