comparison include/llvm/Support/Endian.h @ 77:54457678186b LLVM3.6

LLVM 3.6
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Mon, 08 Sep 2014 22:06:00 +0900
parents 95c75e76d11b
children 60c9769439b8
comparison
equal deleted inserted replaced
34:e874dbf0ad9d 77:54457678186b
15 #define LLVM_SUPPORT_ENDIAN_H 15 #define LLVM_SUPPORT_ENDIAN_H
16 16
17 #include "llvm/Support/AlignOf.h" 17 #include "llvm/Support/AlignOf.h"
18 #include "llvm/Support/Host.h" 18 #include "llvm/Support/Host.h"
19 #include "llvm/Support/SwapByteOrder.h" 19 #include "llvm/Support/SwapByteOrder.h"
20 #include "llvm/Support/type_traits.h"
21 20
22 namespace llvm { 21 namespace llvm {
23 namespace support { 22 namespace support {
24 enum endianness {big, little, native}; 23 enum endianness {big, little, native};
25 24
33 enum {value = alignment == 0 ? AlignOf<T>::Alignment : alignment}; 32 enum {value = alignment == 0 ? AlignOf<T>::Alignment : alignment};
34 }; 33 };
35 } // end namespace detail 34 } // end namespace detail
36 35
37 namespace endian { 36 namespace endian {
37 /// Swap the bytes of value to match the given endianness.
38 template<typename value_type, endianness endian> 38 template<typename value_type, endianness endian>
39 inline value_type byte_swap(value_type value) { 39 inline value_type byte_swap(value_type value) {
40 if (endian != native && sys::IsBigEndianHost != (endian == big)) 40 if (endian != native && sys::IsBigEndianHost != (endian == big))
41 return sys::SwapByteOrder(value); 41 sys::swapByteOrder(value);
42 return value; 42 return value;
43 } 43 }
44 44
45 /// Read a value of a particular endianness from memory.
45 template<typename value_type, 46 template<typename value_type,
46 endianness endian, 47 endianness endian,
47 std::size_t alignment> 48 std::size_t alignment>
48 inline value_type read(const void *memory) { 49 inline value_type read(const void *memory) {
49 value_type ret; 50 value_type ret;
53 (detail::PickAlignment<value_type, alignment>::value)), 54 (detail::PickAlignment<value_type, alignment>::value)),
54 sizeof(value_type)); 55 sizeof(value_type));
55 return byte_swap<value_type, endian>(ret); 56 return byte_swap<value_type, endian>(ret);
56 } 57 }
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.
58 template<typename value_type, 69 template<typename value_type,
59 endianness endian, 70 endianness endian,
60 std::size_t alignment> 71 std::size_t alignment>
61 inline void write(void *memory, value_type value) { 72 inline void write(void *memory, value_type value) {
62 value = byte_swap<value_type, endian>(value); 73 value = byte_swap<value_type, endian>(value);
83 } 94 }
84 95
85 private: 96 private:
86 AlignedCharArray<PickAlignment<value_type, alignment>::value, 97 AlignedCharArray<PickAlignment<value_type, alignment>::value,
87 sizeof(value_type)> 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 };
88 }; 115 };
116
89 } // end namespace detail 117 } // end namespace detail
90 118
91 typedef detail::packed_endian_specific_integral 119 typedef detail::packed_endian_specific_integral
92 <uint8_t, little, unaligned> ulittle8_t; 120 <uint8_t, little, unaligned> ulittle8_t;
93 typedef detail::packed_endian_specific_integral 121 typedef detail::packed_endian_specific_integral