BumpPtrAllocator: do the size check without moving any pointers
[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 inline value_type readNext(const unsigned char *&memory) {
63   value_type ret = read<value_type, endian, alignment>(memory);
64   memory += sizeof(value_type);
65   return ret;
66 }
67
68 /// Write a value to memory with a particular endianness.
69 template<typename value_type,
70          endianness endian,
71          std::size_t alignment>
72 inline void write(void *memory, value_type value) {
73   value = byte_swap<value_type, endian>(value);
74   memcpy(LLVM_ASSUME_ALIGNED(memory,
75            (detail::PickAlignment<value_type, alignment>::value)),
76          &value,
77          sizeof(value_type));
78 }
79 } // end namespace endian
80
81 namespace detail {
82 template<typename value_type,
83          endianness endian,
84          std::size_t alignment>
85 struct packed_endian_specific_integral {
86   operator value_type() const {
87     return endian::read<value_type, endian, alignment>(
88       (const void*)Value.buffer);
89   }
90
91   void operator=(value_type newValue) {
92     endian::write<value_type, endian, alignment>(
93       (void*)Value.buffer, newValue);
94   }
95
96 private:
97   AlignedCharArray<PickAlignment<value_type, alignment>::value,
98                    sizeof(value_type)> Value;
99
100 public:
101   struct ref {
102     explicit ref(void *Ptr) : Ptr(Ptr) {}
103
104     operator value_type() const {
105       return endian::read<value_type, endian, alignment>(Ptr);
106     }
107
108     void operator=(value_type NewValue) {
109       endian::write<value_type, endian, alignment>(Ptr, NewValue);
110     }
111
112   private:
113     void *Ptr;
114   };
115 };
116
117 } // end namespace detail
118
119 typedef detail::packed_endian_specific_integral
120                   <uint8_t, little, unaligned>  ulittle8_t;
121 typedef detail::packed_endian_specific_integral
122                   <uint16_t, little, unaligned> ulittle16_t;
123 typedef detail::packed_endian_specific_integral
124                   <uint32_t, little, unaligned> ulittle32_t;
125 typedef detail::packed_endian_specific_integral
126                   <uint64_t, little, unaligned> ulittle64_t;
127
128 typedef detail::packed_endian_specific_integral
129                    <int8_t, little, unaligned>  little8_t;
130 typedef detail::packed_endian_specific_integral
131                    <int16_t, little, unaligned> little16_t;
132 typedef detail::packed_endian_specific_integral
133                    <int32_t, little, unaligned> little32_t;
134 typedef detail::packed_endian_specific_integral
135                    <int64_t, little, unaligned> little64_t;
136
137 typedef detail::packed_endian_specific_integral
138                     <uint8_t, little, aligned>  aligned_ulittle8_t;
139 typedef detail::packed_endian_specific_integral
140                     <uint16_t, little, aligned> aligned_ulittle16_t;
141 typedef detail::packed_endian_specific_integral
142                     <uint32_t, little, aligned> aligned_ulittle32_t;
143 typedef detail::packed_endian_specific_integral
144                     <uint64_t, little, aligned> aligned_ulittle64_t;
145
146 typedef detail::packed_endian_specific_integral
147                      <int8_t, little, aligned>  aligned_little8_t;
148 typedef detail::packed_endian_specific_integral
149                      <int16_t, little, aligned> aligned_little16_t;
150 typedef detail::packed_endian_specific_integral
151                      <int32_t, little, aligned> aligned_little32_t;
152 typedef detail::packed_endian_specific_integral
153                      <int64_t, little, aligned> aligned_little64_t;
154
155 typedef detail::packed_endian_specific_integral
156                   <uint8_t, big, unaligned>     ubig8_t;
157 typedef detail::packed_endian_specific_integral
158                   <uint16_t, big, unaligned>    ubig16_t;
159 typedef detail::packed_endian_specific_integral
160                   <uint32_t, big, unaligned>    ubig32_t;
161 typedef detail::packed_endian_specific_integral
162                   <uint64_t, big, unaligned>    ubig64_t;
163
164 typedef detail::packed_endian_specific_integral
165                    <int8_t, big, unaligned>     big8_t;
166 typedef detail::packed_endian_specific_integral
167                    <int16_t, big, unaligned>    big16_t;
168 typedef detail::packed_endian_specific_integral
169                    <int32_t, big, unaligned>    big32_t;
170 typedef detail::packed_endian_specific_integral
171                    <int64_t, big, unaligned>    big64_t;
172
173 typedef detail::packed_endian_specific_integral
174                     <uint8_t, big, aligned>     aligned_ubig8_t;
175 typedef detail::packed_endian_specific_integral
176                     <uint16_t, big, aligned>    aligned_ubig16_t;
177 typedef detail::packed_endian_specific_integral
178                     <uint32_t, big, aligned>    aligned_ubig32_t;
179 typedef detail::packed_endian_specific_integral
180                     <uint64_t, big, aligned>    aligned_ubig64_t;
181
182 typedef detail::packed_endian_specific_integral
183                      <int8_t, big, aligned>     aligned_big8_t;
184 typedef detail::packed_endian_specific_integral
185                      <int16_t, big, aligned>    aligned_big16_t;
186 typedef detail::packed_endian_specific_integral
187                      <int32_t, big, aligned>    aligned_big32_t;
188 typedef detail::packed_endian_specific_integral
189                      <int64_t, big, aligned>    aligned_big64_t;
190
191 typedef detail::packed_endian_specific_integral
192                   <uint16_t, native, unaligned> unaligned_uint16_t;
193 typedef detail::packed_endian_specific_integral
194                   <uint32_t, native, unaligned> unaligned_uint32_t;
195 typedef detail::packed_endian_specific_integral
196                   <uint64_t, native, unaligned> unaligned_uint64_t;
197
198 typedef detail::packed_endian_specific_integral
199                    <int16_t, native, unaligned> unaligned_int16_t;
200 typedef detail::packed_endian_specific_integral
201                    <int32_t, native, unaligned> unaligned_int32_t;
202 typedef detail::packed_endian_specific_integral
203                    <int64_t, native, unaligned> unaligned_int64_t;
204 } // end namespace llvm
205 } // end namespace support
206
207 #endif