comparison include/llvm/Support/Endian.h @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
1 //===- Endian.h - Utilities for IO with endian specific data ----*- C++ -*-===// 1 //===- Endian.h - Utilities for IO with endian specific data ----*- C++ -*-===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 // 8 //
10 // This file declares generic functions to read and write endian specific data. 9 // This file declares generic functions to read and write endian specific data.
11 // 10 //
32 // These are named values for common alignments. 31 // These are named values for common alignments.
33 enum {aligned = 0, unaligned = 1}; 32 enum {aligned = 0, unaligned = 1};
34 33
35 namespace detail { 34 namespace detail {
36 35
37 /// \brief ::value is either alignment, or alignof(T) if alignment is 0. 36 /// ::value is either alignment, or alignof(T) if alignment is 0.
38 template<class T, int alignment> 37 template<class T, int alignment>
39 struct PickAlignment { 38 struct PickAlignment {
40 enum { value = alignment == 0 ? alignof(T) : alignment }; 39 enum { value = alignment == 0 ? alignof(T) : alignment };
41 }; 40 };
42 41
202 201
203 } // end namespace endian 202 } // end namespace endian
204 203
205 namespace detail { 204 namespace detail {
206 205
207 template<typename value_type, 206 template <typename ValueType, endianness Endian, std::size_t Alignment,
208 endianness endian, 207 std::size_t ALIGN = PickAlignment<ValueType, Alignment>::value>
209 std::size_t alignment>
210 struct packed_endian_specific_integral { 208 struct packed_endian_specific_integral {
209 using value_type = ValueType;
210 static constexpr endianness endian = Endian;
211 static constexpr std::size_t alignment = Alignment;
212
211 packed_endian_specific_integral() = default; 213 packed_endian_specific_integral() = default;
212 214
213 explicit packed_endian_specific_integral(value_type val) { *this = val; } 215 explicit packed_endian_specific_integral(value_type val) { *this = val; }
214 216
215 operator value_type() const { 217 operator value_type() const {
241 *this = *this & newValue; 243 *this = *this & newValue;
242 return *this; 244 return *this;
243 } 245 }
244 246
245 private: 247 private:
246 AlignedCharArray<PickAlignment<value_type, alignment>::value, 248 struct {
247 sizeof(value_type)> Value; 249 alignas(ALIGN) char buffer[sizeof(value_type)];
250 } Value;
248 251
249 public: 252 public:
250 struct ref { 253 struct ref {
251 explicit ref(void *Ptr) : Ptr(Ptr) {} 254 explicit ref(void *Ptr) : Ptr(Ptr) {}
252 255
333 using unaligned_int32_t = 336 using unaligned_int32_t =
334 detail::packed_endian_specific_integral<int32_t, native, unaligned>; 337 detail::packed_endian_specific_integral<int32_t, native, unaligned>;
335 using unaligned_int64_t = 338 using unaligned_int64_t =
336 detail::packed_endian_specific_integral<int64_t, native, unaligned>; 339 detail::packed_endian_specific_integral<int64_t, native, unaligned>;
337 340
341 template <typename T>
342 using little_t = detail::packed_endian_specific_integral<T, little, unaligned>;
343 template <typename T>
344 using big_t = detail::packed_endian_specific_integral<T, big, unaligned>;
345
346 template <typename T>
347 using aligned_little_t =
348 detail::packed_endian_specific_integral<T, little, aligned>;
349 template <typename T>
350 using aligned_big_t = detail::packed_endian_specific_integral<T, big, aligned>;
351
338 namespace endian { 352 namespace endian {
339 353
340 template <typename T> inline T read(const void *P, endianness E) { 354 template <typename T> inline T read(const void *P, endianness E) {
341 return read<T, unaligned>(P, E); 355 return read<T, unaligned>(P, E);
342 } 356 }