fd59009e0d3a60f843c4573c255f5294b66b56eb
[oota-llvm.git] / include / llvm / Support / Endian.h
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"
19 #include "llvm/Support/SwapByteOrder.h"
20
21 namespace llvm {
22 namespace support {
23 enum endianness {big, little, native};
24
25 // These are named values for common alignments.
26 enum {aligned = 0, unaligned = 1};
27
28 namespace detail {
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};
33   };
34 } // end namespace detail
35
36 namespace endian {
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);
42   return value;
43 }
44
45 /// Read a value of a particular endianness from memory.
46 template<typename value_type,
47          endianness endian,
48          std::size_t alignment>
49 inline value_type read(const void *memory) {
50   value_type ret;
51
52   memcpy(&ret,
53          LLVM_ASSUME_ALIGNED(memory,
54            (detail::PickAlignment<value_type, alignment>::value)),
55          sizeof(value_type));
56   return byte_swap<value_type, endian>(ret);
57 }
58
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          typename CharT>
63 inline value_type readNext(const CharT *&memory) {
64   value_type ret = read<value_type, endian, alignment>(memory);
65   memory += sizeof(value_type);
66   return ret;
67 }
68
69 /// Write a value to memory with a particular endianness.
70 template<typename value_type,
71          endianness endian,
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)),
77          &value,
78          sizeof(value_type));
79 }
80 } // end namespace endian
81
82 namespace detail {
83 template<typename value_type,
84          endianness endian,
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);
90   }
91
92   void operator=(value_type newValue) {
93     endian::write<value_type, endian, alignment>(
94       (void*)Value.buffer, newValue);
95   }
96
97   packed_endian_specific_integral &operator+=(value_type newValue) {
98     *this = *this + newValue;
99     return *this;
100   }
101
102   packed_endian_specific_integral &operator-=(value_type newValue) {
103     *this = *this - newValue;
104     return *this;
105   }
106
107   packed_endian_specific_integral &operator|=(value_type newValue) {
108     *this = *this | newValue;
109     return *this;
110   }
111
112   packed_endian_specific_integral &operator&=(value_type newValue) {
113     *this = *this & newValue;
114     return *this;
115   }
116
117 private:
118   AlignedCharArray<PickAlignment<value_type, alignment>::value,
119                    sizeof(value_type)> Value;
120
121 public:
122   struct ref {
123     explicit ref(void *Ptr) : Ptr(Ptr) {}
124
125     operator value_type() const {
126       return endian::read<value_type, endian, alignment>(Ptr);
127     }
128
129     void operator=(value_type NewValue) {
130       endian::write<value_type, endian, alignment>(Ptr, NewValue);
131     }
132
133   private:
134     void *Ptr;
135   };
136 };
137
138 } // end namespace detail
139
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;
146
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;
153
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;
160
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;
167
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;
174
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;
181
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;
188
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;
195
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;
202
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;
209
210 namespace endian {
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; }
217
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
227
228 #endif