Fix UBSan test error from r248897 about left shift of unsigned value.
[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
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) {
85   assert(startBit < 8);
86   if (startBit == 0)
87     return read<value_type, endian, alignment>(memory);
88   else {
89     // Read two values and compose the result from them.
90     value_type val[2];
91     memcpy(&val[0],
92            LLVM_ASSUME_ALIGNED(
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]);
97
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;
103
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;
108
109     return lowerVal | upperVal;
110   }
111 }
112
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,
117                                 uint64_t startBit) {
118   assert(startBit < 8);
119   if (startBit == 0)
120     write<value_type, endian, alignment>(memory, value);
121   else {
122     // Read two values and shift the result into them.
123     value_type val[2];
124     memcpy(&val[0],
125            LLVM_ASSUME_ALIGNED(
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]);
130
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     unsigned numBitsFirstVal = (sizeof(value_type) * 8) - startBit;
135     unsigned lowerVal = value;
136     if (startBit > 0) {
137       // Mask off the upper bits in the new value that are not going to go into
138       // the lower value. This avoids a left shift of a negative value, which
139       // is undefined behavior.
140       lowerVal &= ((1 << numBitsFirstVal) - 1);
141       // Now shift the new bits into place
142       lowerVal <<= startBit;
143     }
144     val[0] |= lowerVal;
145
146     // Mask off any existing bits in the lower part of the upper value that
147     // we want to replace.
148     val[1] &= ~((1 << startBit) - 1);
149     // Next shift the bits that go into the upper value into position.
150     unsigned upperVal = value >> numBitsFirstVal;
151     // Mask off upper bits after right shift in case of signed type.
152     upperVal &= (1 << startBit) - 1;
153     val[1] |= upperVal;
154
155     // Finally, rewrite values.
156     val[0] = byte_swap<value_type, endian>(val[0]);
157     val[1] = byte_swap<value_type, endian>(val[1]);
158     memcpy(LLVM_ASSUME_ALIGNED(
159                memory, (detail::PickAlignment<value_type, alignment>::value)),
160            &val[0], sizeof(value_type) * 2);
161   }
162 }
163 } // end namespace endian
164
165 namespace detail {
166 template<typename value_type,
167          endianness endian,
168          std::size_t alignment>
169 struct packed_endian_specific_integral {
170   operator value_type() const {
171     return endian::read<value_type, endian, alignment>(
172       (const void*)Value.buffer);
173   }
174
175   void operator=(value_type newValue) {
176     endian::write<value_type, endian, alignment>(
177       (void*)Value.buffer, newValue);
178   }
179
180   packed_endian_specific_integral &operator+=(value_type newValue) {
181     *this = *this + newValue;
182     return *this;
183   }
184
185   packed_endian_specific_integral &operator-=(value_type newValue) {
186     *this = *this - newValue;
187     return *this;
188   }
189
190   packed_endian_specific_integral &operator|=(value_type newValue) {
191     *this = *this | newValue;
192     return *this;
193   }
194
195   packed_endian_specific_integral &operator&=(value_type newValue) {
196     *this = *this & newValue;
197     return *this;
198   }
199
200 private:
201   AlignedCharArray<PickAlignment<value_type, alignment>::value,
202                    sizeof(value_type)> Value;
203
204 public:
205   struct ref {
206     explicit ref(void *Ptr) : Ptr(Ptr) {}
207
208     operator value_type() const {
209       return endian::read<value_type, endian, alignment>(Ptr);
210     }
211
212     void operator=(value_type NewValue) {
213       endian::write<value_type, endian, alignment>(Ptr, NewValue);
214     }
215
216   private:
217     void *Ptr;
218   };
219 };
220
221 } // end namespace detail
222
223 typedef detail::packed_endian_specific_integral
224                   <uint16_t, little, unaligned> ulittle16_t;
225 typedef detail::packed_endian_specific_integral
226                   <uint32_t, little, unaligned> ulittle32_t;
227 typedef detail::packed_endian_specific_integral
228                   <uint64_t, little, unaligned> ulittle64_t;
229
230 typedef detail::packed_endian_specific_integral
231                    <int16_t, little, unaligned> little16_t;
232 typedef detail::packed_endian_specific_integral
233                    <int32_t, little, unaligned> little32_t;
234 typedef detail::packed_endian_specific_integral
235                    <int64_t, little, unaligned> little64_t;
236
237 typedef detail::packed_endian_specific_integral
238                     <uint16_t, little, aligned> aligned_ulittle16_t;
239 typedef detail::packed_endian_specific_integral
240                     <uint32_t, little, aligned> aligned_ulittle32_t;
241 typedef detail::packed_endian_specific_integral
242                     <uint64_t, little, aligned> aligned_ulittle64_t;
243
244 typedef detail::packed_endian_specific_integral
245                      <int16_t, little, aligned> aligned_little16_t;
246 typedef detail::packed_endian_specific_integral
247                      <int32_t, little, aligned> aligned_little32_t;
248 typedef detail::packed_endian_specific_integral
249                      <int64_t, little, aligned> aligned_little64_t;
250
251 typedef detail::packed_endian_specific_integral
252                   <uint16_t, big, unaligned>    ubig16_t;
253 typedef detail::packed_endian_specific_integral
254                   <uint32_t, big, unaligned>    ubig32_t;
255 typedef detail::packed_endian_specific_integral
256                   <uint64_t, big, unaligned>    ubig64_t;
257
258 typedef detail::packed_endian_specific_integral
259                    <int16_t, big, unaligned>    big16_t;
260 typedef detail::packed_endian_specific_integral
261                    <int32_t, big, unaligned>    big32_t;
262 typedef detail::packed_endian_specific_integral
263                    <int64_t, big, unaligned>    big64_t;
264
265 typedef detail::packed_endian_specific_integral
266                     <uint16_t, big, aligned>    aligned_ubig16_t;
267 typedef detail::packed_endian_specific_integral
268                     <uint32_t, big, aligned>    aligned_ubig32_t;
269 typedef detail::packed_endian_specific_integral
270                     <uint64_t, big, aligned>    aligned_ubig64_t;
271
272 typedef detail::packed_endian_specific_integral
273                      <int16_t, big, aligned>    aligned_big16_t;
274 typedef detail::packed_endian_specific_integral
275                      <int32_t, big, aligned>    aligned_big32_t;
276 typedef detail::packed_endian_specific_integral
277                      <int64_t, big, aligned>    aligned_big64_t;
278
279 typedef detail::packed_endian_specific_integral
280                   <uint16_t, native, unaligned> unaligned_uint16_t;
281 typedef detail::packed_endian_specific_integral
282                   <uint32_t, native, unaligned> unaligned_uint32_t;
283 typedef detail::packed_endian_specific_integral
284                   <uint64_t, native, unaligned> unaligned_uint64_t;
285
286 typedef detail::packed_endian_specific_integral
287                    <int16_t, native, unaligned> unaligned_int16_t;
288 typedef detail::packed_endian_specific_integral
289                    <int32_t, native, unaligned> unaligned_int32_t;
290 typedef detail::packed_endian_specific_integral
291                    <int64_t, native, unaligned> unaligned_int64_t;
292
293 namespace endian {
294 inline uint16_t read16le(const void *p) { return *(const ulittle16_t *)p; }
295 inline uint32_t read32le(const void *p) { return *(const ulittle32_t *)p; }
296 inline uint64_t read64le(const void *p) { return *(const ulittle64_t *)p; }
297 inline uint16_t read16be(const void *p) { return *(const ubig16_t *)p; }
298 inline uint32_t read32be(const void *p) { return *(const ubig32_t *)p; }
299 inline uint64_t read64be(const void *p) { return *(const ubig64_t *)p; }
300
301 inline void write16le(void *p, uint16_t v) { *(ulittle16_t *)p = v; }
302 inline void write32le(void *p, uint32_t v) { *(ulittle32_t *)p = v; }
303 inline void write64le(void *p, uint64_t v) { *(ulittle64_t *)p = v; }
304 inline void write16be(void *p, uint16_t v) { *(ubig16_t *)p = v; }
305 inline void write32be(void *p, uint32_t v) { *(ubig32_t *)p = v; }
306 inline void write64be(void *p, uint64_t v) { *(ubig64_t *)p = v; }
307 } // end namespace endian
308 } // end namespace support
309 } // end namespace llvm
310
311 #endif