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;
108 AlignedCharArray<PickAlignment<value_type, alignment>::value,
109 sizeof(value_type)> Value;
113 explicit ref(void *Ptr) : Ptr(Ptr) {}
115 operator value_type() const {
116 return endian::read<value_type, endian, alignment>(Ptr);
119 void operator=(value_type NewValue) {
120 endian::write<value_type, endian, alignment>(Ptr, NewValue);
128 } // end namespace detail
130 typedef detail::packed_endian_specific_integral
131 <uint16_t, little, unaligned> ulittle16_t;
132 typedef detail::packed_endian_specific_integral
133 <uint32_t, little, unaligned> ulittle32_t;
134 typedef detail::packed_endian_specific_integral
135 <uint64_t, little, unaligned> ulittle64_t;
137 typedef detail::packed_endian_specific_integral
138 <int16_t, little, unaligned> little16_t;
139 typedef detail::packed_endian_specific_integral
140 <int32_t, little, unaligned> little32_t;
141 typedef detail::packed_endian_specific_integral
142 <int64_t, little, unaligned> little64_t;
144 typedef detail::packed_endian_specific_integral
145 <uint16_t, little, aligned> aligned_ulittle16_t;
146 typedef detail::packed_endian_specific_integral
147 <uint32_t, little, aligned> aligned_ulittle32_t;
148 typedef detail::packed_endian_specific_integral
149 <uint64_t, little, aligned> aligned_ulittle64_t;
151 typedef detail::packed_endian_specific_integral
152 <int16_t, little, aligned> aligned_little16_t;
153 typedef detail::packed_endian_specific_integral
154 <int32_t, little, aligned> aligned_little32_t;
155 typedef detail::packed_endian_specific_integral
156 <int64_t, little, aligned> aligned_little64_t;
158 typedef detail::packed_endian_specific_integral
159 <uint16_t, big, unaligned> ubig16_t;
160 typedef detail::packed_endian_specific_integral
161 <uint32_t, big, unaligned> ubig32_t;
162 typedef detail::packed_endian_specific_integral
163 <uint64_t, big, unaligned> ubig64_t;
165 typedef detail::packed_endian_specific_integral
166 <int16_t, big, unaligned> big16_t;
167 typedef detail::packed_endian_specific_integral
168 <int32_t, big, unaligned> big32_t;
169 typedef detail::packed_endian_specific_integral
170 <int64_t, big, unaligned> big64_t;
172 typedef detail::packed_endian_specific_integral
173 <uint16_t, big, aligned> aligned_ubig16_t;
174 typedef detail::packed_endian_specific_integral
175 <uint32_t, big, aligned> aligned_ubig32_t;
176 typedef detail::packed_endian_specific_integral
177 <uint64_t, big, aligned> aligned_ubig64_t;
179 typedef detail::packed_endian_specific_integral
180 <int16_t, big, aligned> aligned_big16_t;
181 typedef detail::packed_endian_specific_integral
182 <int32_t, big, aligned> aligned_big32_t;
183 typedef detail::packed_endian_specific_integral
184 <int64_t, big, aligned> aligned_big64_t;
186 typedef detail::packed_endian_specific_integral
187 <uint16_t, native, unaligned> unaligned_uint16_t;
188 typedef detail::packed_endian_specific_integral
189 <uint32_t, native, unaligned> unaligned_uint32_t;
190 typedef detail::packed_endian_specific_integral
191 <uint64_t, native, unaligned> unaligned_uint64_t;
193 typedef detail::packed_endian_specific_integral
194 <int16_t, native, unaligned> unaligned_int16_t;
195 typedef detail::packed_endian_specific_integral
196 <int32_t, native, unaligned> unaligned_int32_t;
197 typedef detail::packed_endian_specific_integral
198 <int64_t, native, unaligned> unaligned_int64_t;
201 inline uint16_t read16le(const void *p) { return *(const ulittle16_t *)p; }
202 inline uint32_t read32le(const void *p) { return *(const ulittle32_t *)p; }
203 inline uint64_t read64le(const void *p) { return *(const ulittle64_t *)p; }
204 inline uint16_t read16be(const void *p) { return *(const ubig16_t *)p; }
205 inline uint32_t read32be(const void *p) { return *(const ubig32_t *)p; }
206 inline uint64_t read64be(const void *p) { return *(const ubig64_t *)p; }
208 inline void write16le(void *p, uint16_t v) { *(ulittle16_t *)p = v; }
209 inline void write32le(void *p, uint32_t v) { *(ulittle32_t *)p = v; }
210 inline void write64le(void *p, uint64_t v) { *(ulittle64_t *)p = v; }
211 inline void write16be(void *p, uint16_t v) { *(ubig16_t *)p = v; }
212 inline void write32be(void *p, uint32_t v) { *(ubig32_t *)p = v; }
213 inline void write64be(void *p, uint64_t v) { *(ubig64_t *)p = v; }
214 } // end namespace endian
215 } // end namespace support
216 } // end namespace llvm