annotate include/llvm/Support/Endian.h @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children c2174574ed3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- Endian.h - Utilities for IO with endian specific data ----*- C++ -*-===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 // This file declares generic functions to read and write endian specific data.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #ifndef LLVM_SUPPORT_ENDIAN_H
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 #define LLVM_SUPPORT_ENDIAN_H
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
17 #include "llvm/Support/AlignOf.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
18 #include "llvm/Support/Compiler.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 #include "llvm/Support/Host.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 #include "llvm/Support/SwapByteOrder.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
21 #include <cassert>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
22 #include <cstddef>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
23 #include <cstdint>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
24 #include <cstring>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
25 #include <type_traits>
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 namespace llvm {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 namespace support {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
29
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 enum endianness {big, little, native};
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 // These are named values for common alignments.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 enum {aligned = 0, unaligned = 1};
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 namespace detail {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
36
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
37 /// \brief ::value is either alignment, or alignof(T) if alignment is 0.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
38 template<class T, int alignment>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
39 struct PickAlignment {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
40 enum { value = alignment == 0 ? alignof(T) : alignment };
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
41 };
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
42
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 } // end namespace detail
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 namespace endian {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
46
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
47 constexpr endianness system_endianness() {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
48 return sys::IsBigEndianHost ? big : little;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
49 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
50
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
51 template <typename value_type>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
52 inline value_type byte_swap(value_type value, endianness endian) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
53 if ((endian != native) && (endian != system_endianness()))
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
54 sys::swapByteOrder(value);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 return value;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
58 /// Swap the bytes of value to match the given endianness.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
59 template<typename value_type, endianness endian>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
60 inline value_type byte_swap(value_type value) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
61 return byte_swap(value, endian);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
62 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
63
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
64 /// Read a value of a particular endianness from memory.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
65 template <typename value_type, std::size_t alignment>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
66 inline value_type read(const void *memory, endianness endian) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
67 value_type ret;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
68
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
69 memcpy(&ret,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
70 LLVM_ASSUME_ALIGNED(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
71 memory, (detail::PickAlignment<value_type, alignment>::value)),
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
72 sizeof(value_type));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
73 return byte_swap<value_type>(ret, endian);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
74 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
75
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 template<typename value_type,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 endianness endian,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 std::size_t alignment>
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 inline value_type read(const void *memory) {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
80 return read<value_type, alignment>(memory, endian);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
83 /// Read a value of a particular endianness from a buffer, and increment the
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
84 /// buffer past that value.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
85 template <typename value_type, std::size_t alignment, typename CharT>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
86 inline value_type readNext(const CharT *&memory, endianness endian) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
87 value_type ret = read<value_type, alignment>(memory, endian);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
88 memory += sizeof(value_type);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
89 return ret;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
90 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
91
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
92 template<typename value_type, endianness endian, std::size_t alignment,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
93 typename CharT>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
94 inline value_type readNext(const CharT *&memory) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
95 return readNext<value_type, alignment, CharT>(memory, endian);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
96 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
97
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
98 /// Write a value to memory with a particular endianness.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
99 template <typename value_type, std::size_t alignment>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
100 inline void write(void *memory, value_type value, endianness endian) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
101 value = byte_swap<value_type>(value, endian);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
102 memcpy(LLVM_ASSUME_ALIGNED(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
103 memory, (detail::PickAlignment<value_type, alignment>::value)),
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
104 &value, sizeof(value_type));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
105 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
106
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 template<typename value_type,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 endianness endian,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 std::size_t alignment>
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 inline void write(void *memory, value_type value) {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
111 write<value_type, alignment>(memory, value, endian);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
113
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
114 template <typename value_type>
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
115 using make_unsigned_t = typename std::make_unsigned<value_type>::type;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
116
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
117 /// Read a value of a particular endianness from memory, for a location
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
118 /// that starts at the given bit offset within the first byte.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
119 template <typename value_type, endianness endian, std::size_t alignment>
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
120 inline value_type readAtBitAlignment(const void *memory, uint64_t startBit) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
121 assert(startBit < 8);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
122 if (startBit == 0)
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
123 return read<value_type, endian, alignment>(memory);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
124 else {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
125 // Read two values and compose the result from them.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
126 value_type val[2];
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
127 memcpy(&val[0],
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
128 LLVM_ASSUME_ALIGNED(
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
129 memory, (detail::PickAlignment<value_type, alignment>::value)),
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
130 sizeof(value_type) * 2);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
131 val[0] = byte_swap<value_type, endian>(val[0]);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
132 val[1] = byte_swap<value_type, endian>(val[1]);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
133
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
134 // Shift bits from the lower value into place.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
135 make_unsigned_t<value_type> lowerVal = val[0] >> startBit;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
136 // Mask off upper bits after right shift in case of signed type.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
137 make_unsigned_t<value_type> numBitsFirstVal =
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
138 (sizeof(value_type) * 8) - startBit;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
139 lowerVal &= ((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
140
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
141 // Get the bits from the upper value.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
142 make_unsigned_t<value_type> upperVal =
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
143 val[1] & (((make_unsigned_t<value_type>)1 << startBit) - 1);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
144 // Shift them in to place.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
145 upperVal <<= numBitsFirstVal;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
146
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
147 return lowerVal | upperVal;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
148 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
149 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
150
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
151 /// Write a value to memory with a particular endianness, for a location
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
152 /// that starts at the given bit offset within the first byte.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
153 template <typename value_type, endianness endian, std::size_t alignment>
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
154 inline void writeAtBitAlignment(void *memory, value_type value,
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
155 uint64_t startBit) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
156 assert(startBit < 8);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
157 if (startBit == 0)
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
158 write<value_type, endian, alignment>(memory, value);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
159 else {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
160 // Read two values and shift the result into them.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
161 value_type val[2];
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
162 memcpy(&val[0],
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
163 LLVM_ASSUME_ALIGNED(
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
164 memory, (detail::PickAlignment<value_type, alignment>::value)),
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
165 sizeof(value_type) * 2);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
166 val[0] = byte_swap<value_type, endian>(val[0]);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
167 val[1] = byte_swap<value_type, endian>(val[1]);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
168
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
169 // Mask off any existing bits in the upper part of the lower value that
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
170 // we want to replace.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
171 val[0] &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
172 make_unsigned_t<value_type> numBitsFirstVal =
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
173 (sizeof(value_type) * 8) - startBit;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
174 make_unsigned_t<value_type> lowerVal = value;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
175 if (startBit > 0) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
176 // Mask off the upper bits in the new value that are not going to go into
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
177 // the lower value. This avoids a left shift of a negative value, which
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
178 // is undefined behavior.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
179 lowerVal &= (((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
180 // Now shift the new bits into place
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
181 lowerVal <<= startBit;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
182 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
183 val[0] |= lowerVal;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
184
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
185 // Mask off any existing bits in the lower part of the upper value that
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
186 // we want to replace.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
187 val[1] &= ~(((make_unsigned_t<value_type>)1 << startBit) - 1);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
188 // Next shift the bits that go into the upper value into position.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
189 make_unsigned_t<value_type> upperVal = value >> numBitsFirstVal;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
190 // Mask off upper bits after right shift in case of signed type.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
191 upperVal &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
192 val[1] |= upperVal;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
193
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
194 // Finally, rewrite values.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
195 val[0] = byte_swap<value_type, endian>(val[0]);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
196 val[1] = byte_swap<value_type, endian>(val[1]);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
197 memcpy(LLVM_ASSUME_ALIGNED(
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
198 memory, (detail::PickAlignment<value_type, alignment>::value)),
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
199 &val[0], sizeof(value_type) * 2);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
200 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
201 }
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
202
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 } // end namespace endian
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
204
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 namespace detail {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
206
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 template<typename value_type,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 endianness endian,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 std::size_t alignment>
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 struct packed_endian_specific_integral {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
211 packed_endian_specific_integral() = default;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
212
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
213 explicit packed_endian_specific_integral(value_type val) { *this = val; }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
214
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 operator value_type() const {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 return endian::read<value_type, endian, alignment>(
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 (const void*)Value.buffer);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
219
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 void operator=(value_type newValue) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 endian::write<value_type, endian, alignment>(
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 (void*)Value.buffer, newValue);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
225 packed_endian_specific_integral &operator+=(value_type newValue) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
226 *this = *this + newValue;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
227 return *this;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
228 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
229
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
230 packed_endian_specific_integral &operator-=(value_type newValue) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
231 *this = *this - newValue;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
232 return *this;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
233 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
234
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
235 packed_endian_specific_integral &operator|=(value_type newValue) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
236 *this = *this | newValue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
237 return *this;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
238 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
239
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
240 packed_endian_specific_integral &operator&=(value_type newValue) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
241 *this = *this & newValue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
242 return *this;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
243 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
244
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 private:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 AlignedCharArray<PickAlignment<value_type, alignment>::value,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 sizeof(value_type)> Value;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
248
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
249 public:
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
250 struct ref {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
251 explicit ref(void *Ptr) : Ptr(Ptr) {}
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
252
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
253 operator value_type() const {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
254 return endian::read<value_type, endian, alignment>(Ptr);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
255 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
256
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
257 void operator=(value_type NewValue) {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
258 endian::write<value_type, endian, alignment>(Ptr, NewValue);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
259 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
260
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
261 private:
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
262 void *Ptr;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
263 };
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 };
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
265
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 } // end namespace detail
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
267
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
268 using ulittle16_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
269 detail::packed_endian_specific_integral<uint16_t, little, unaligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
270 using ulittle32_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
271 detail::packed_endian_specific_integral<uint32_t, little, unaligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
272 using ulittle64_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
273 detail::packed_endian_specific_integral<uint64_t, little, unaligned>;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
274
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
275 using little16_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
276 detail::packed_endian_specific_integral<int16_t, little, unaligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
277 using little32_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
278 detail::packed_endian_specific_integral<int32_t, little, unaligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
279 using little64_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
280 detail::packed_endian_specific_integral<int64_t, little, unaligned>;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
281
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
282 using aligned_ulittle16_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
283 detail::packed_endian_specific_integral<uint16_t, little, aligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
284 using aligned_ulittle32_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
285 detail::packed_endian_specific_integral<uint32_t, little, aligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
286 using aligned_ulittle64_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
287 detail::packed_endian_specific_integral<uint64_t, little, aligned>;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
288
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
289 using aligned_little16_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
290 detail::packed_endian_specific_integral<int16_t, little, aligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
291 using aligned_little32_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
292 detail::packed_endian_specific_integral<int32_t, little, aligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
293 using aligned_little64_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
294 detail::packed_endian_specific_integral<int64_t, little, aligned>;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
295
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
296 using ubig16_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
297 detail::packed_endian_specific_integral<uint16_t, big, unaligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
298 using ubig32_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
299 detail::packed_endian_specific_integral<uint32_t, big, unaligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
300 using ubig64_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
301 detail::packed_endian_specific_integral<uint64_t, big, unaligned>;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
302
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
303 using big16_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
304 detail::packed_endian_specific_integral<int16_t, big, unaligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
305 using big32_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
306 detail::packed_endian_specific_integral<int32_t, big, unaligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
307 using big64_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
308 detail::packed_endian_specific_integral<int64_t, big, unaligned>;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
309
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
310 using aligned_ubig16_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
311 detail::packed_endian_specific_integral<uint16_t, big, aligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
312 using aligned_ubig32_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
313 detail::packed_endian_specific_integral<uint32_t, big, aligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
314 using aligned_ubig64_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
315 detail::packed_endian_specific_integral<uint64_t, big, aligned>;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
316
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
317 using aligned_big16_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
318 detail::packed_endian_specific_integral<int16_t, big, aligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
319 using aligned_big32_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
320 detail::packed_endian_specific_integral<int32_t, big, aligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
321 using aligned_big64_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
322 detail::packed_endian_specific_integral<int64_t, big, aligned>;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
323
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
324 using unaligned_uint16_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
325 detail::packed_endian_specific_integral<uint16_t, native, unaligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
326 using unaligned_uint32_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
327 detail::packed_endian_specific_integral<uint32_t, native, unaligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
328 using unaligned_uint64_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
329 detail::packed_endian_specific_integral<uint64_t, native, unaligned>;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
330
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
331 using unaligned_int16_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
332 detail::packed_endian_specific_integral<int16_t, native, unaligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
333 using unaligned_int32_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
334 detail::packed_endian_specific_integral<int32_t, native, unaligned>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
335 using unaligned_int64_t =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
336 detail::packed_endian_specific_integral<int64_t, native, unaligned>;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
337
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
338 namespace endian {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
339
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
340 template <typename T> inline T read(const void *P, endianness E) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
341 return read<T, unaligned>(P, E);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
342 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
343
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
344 template <typename T, endianness E> inline T read(const void *P) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
345 return *(const detail::packed_endian_specific_integral<T, E, unaligned> *)P;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
346 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
347
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
348 inline uint16_t read16(const void *P, endianness E) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
349 return read<uint16_t>(P, E);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
350 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
351 inline uint32_t read32(const void *P, endianness E) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
352 return read<uint32_t>(P, E);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
353 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
354 inline uint64_t read64(const void *P, endianness E) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
355 return read<uint64_t>(P, E);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
356 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
357
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
358 template <endianness E> inline uint16_t read16(const void *P) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
359 return read<uint16_t, E>(P);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
360 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
361 template <endianness E> inline uint32_t read32(const void *P) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
362 return read<uint32_t, E>(P);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
363 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
364 template <endianness E> inline uint64_t read64(const void *P) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
365 return read<uint64_t, E>(P);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
366 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
367
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
368 inline uint16_t read16le(const void *P) { return read16<little>(P); }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
369 inline uint32_t read32le(const void *P) { return read32<little>(P); }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
370 inline uint64_t read64le(const void *P) { return read64<little>(P); }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
371 inline uint16_t read16be(const void *P) { return read16<big>(P); }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
372 inline uint32_t read32be(const void *P) { return read32<big>(P); }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
373 inline uint64_t read64be(const void *P) { return read64<big>(P); }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
374
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
375 template <typename T> inline void write(void *P, T V, endianness E) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
376 write<T, unaligned>(P, V, E);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
377 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
378
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
379 template <typename T, endianness E> inline void write(void *P, T V) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
380 *(detail::packed_endian_specific_integral<T, E, unaligned> *)P = V;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
381 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
382
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
383 inline void write16(void *P, uint16_t V, endianness E) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
384 write<uint16_t>(P, V, E);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
385 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
386 inline void write32(void *P, uint32_t V, endianness E) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
387 write<uint32_t>(P, V, E);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
388 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
389 inline void write64(void *P, uint64_t V, endianness E) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
390 write<uint64_t>(P, V, E);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
391 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
392
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
393 template <endianness E> inline void write16(void *P, uint16_t V) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
394 write<uint16_t, E>(P, V);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
395 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
396 template <endianness E> inline void write32(void *P, uint32_t V) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
397 write<uint32_t, E>(P, V);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
398 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
399 template <endianness E> inline void write64(void *P, uint64_t V) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
400 write<uint64_t, E>(P, V);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
401 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
402
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
403 inline void write16le(void *P, uint16_t V) { write16<little>(P, V); }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
404 inline void write32le(void *P, uint32_t V) { write32<little>(P, V); }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
405 inline void write64le(void *P, uint64_t V) { write64<little>(P, V); }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
406 inline void write16be(void *P, uint16_t V) { write16<big>(P, V); }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
407 inline void write32be(void *P, uint32_t V) { write32<big>(P, V); }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
408 inline void write64be(void *P, uint64_t V) { write64<big>(P, V); }
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
409
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
410 } // end namespace endian
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
411
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
412 } // end namespace support
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
413 } // end namespace llvm
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
414
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
415 #endif // LLVM_SUPPORT_ENDIAN_H