Debian Codegraph @

src/mpt/endian/integer.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_ENDIAN_INTEGER_HPP
4 #define MPT_ENDIAN_INTEGER_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/bit.hpp"
10 #include "mpt/base/integer.hpp"
11 #include "mpt/base/macros.hpp"
12 #include "mpt/base/memory.hpp"
13 #include "mpt/base/namespace.hpp"
14 #include "mpt/endian/type_traits.hpp"
15
16 #include <array>
17 #include <limits>
18 #include <type_traits>
19
20 #include <cstddef>
21 #include <cstdint>
22 #include <cstring>
23
24
25
26 namespace mpt {
27 inline namespace MPT_INLINE_NS {
28
29
30
31 template <typename base_type, mpt::endian endian, typename int_type = base_type>
32 MPT_CONSTEXPRINLINE std::array<std::byte, sizeof(base_type)> EndianEncodeImpl(base_type val) noexcept {
33 static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
34 static_assert(std::numeric_limits<int_type>::is_integer);
35 using unsigned_int_type = typename std::make_unsigned<int_type>::type;
36 unsigned_int_type uval = static_cast<unsigned_int_type>(static_cast<int_type>(val));
37 std::array<std::byte, sizeof(base_type)> data{};
38 if constexpr (endian == mpt::endian::little) {
39 for (std::size_t i = 0; i < sizeof(base_type); ++i) {
40 data[i] = static_cast<std::byte>(static_cast<uint8>((uval >> (i * 8)) & 0xffu));
41 }
42 } else {
43 for (std::size_t i = 0; i < sizeof(base_type); ++i) {
44 data[(sizeof(base_type) - 1) - i] = static_cast<std::byte>(static_cast<uint8>((uval >> (i * 8)) & 0xffu));
45 }
46 }
47 return data;
48 }
49
50 template <typename base_type, mpt::endian endian, typename int_type = base_type>
51 MPT_CONSTEXPRINLINE std::array<std::byte, sizeof(base_type)> EndianEncode(base_type val) noexcept {
52 return EndianEncodeImpl<base_type, endian, int_type>(val);
53 }
54
55 template <typename base_type, mpt::endian endian, typename int_type = base_type>
56 MPT_CONSTEXPRINLINE base_type EndianDecodeImpl(std::array<std::byte, sizeof(base_type)> data) noexcept {
57 static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
58 static_assert(std::numeric_limits<int_type>::is_integer);
59 using unsigned_int_type = typename std::make_unsigned<int_type>::type;
60 base_type val = base_type();
61 unsigned_int_type uval = unsigned_int_type();
62 if constexpr (endian == mpt::endian::little) {
63 for (std::size_t i = 0; i < sizeof(base_type); ++i) {
64 uval |= static_cast<unsigned_int_type>(static_cast<unsigned_int_type>(static_cast<uint8>(data[i])) << (i * 8));
65 }
66 } else {
67 for (std::size_t i = 0; i < sizeof(base_type); ++i) {
68 uval |= static_cast<unsigned_int_type>(static_cast<unsigned_int_type>(static_cast<uint8>(data[(sizeof(base_type) - 1) - i])) << (i * 8));
69 }
70 }
71 val = static_cast<base_type>(static_cast<int_type>(uval));
72 return val;
73 }
74
75 template <typename base_type, mpt::endian endian, typename int_type = base_type>
76 MPT_CONSTEXPRINLINE base_type EndianDecode(std::array<std::byte, sizeof(base_type)> data) noexcept {
77 return EndianDecodeImpl<base_type, endian, int_type>(data);
78 }
79
80
81
82 template <typename T>
83 struct packed_int_type {
84 using type = T;
85 };
86
87 // On-disk integer types with defined endianness and no alignemnt requirements
88 // Note: To easily debug module loaders (and anything else that uses this
89 // wrapper struct), you can use the Debugger Visualizers available in
90 // build/vs/debug/ to conveniently view the wrapped contents.
91
92 template <typename T, mpt::endian endian, typename Tint = typename mpt::packed_int_type<T>::type>
93 struct packed {
94 public:
95 using base_type = T;
96 using int_type = Tint;
97
98 public:
99 std::array<std::byte, sizeof(base_type)> data;
100
101 public:
102 MPT_CONSTEXPR20_FUN void set(base_type val) noexcept {
103 static_assert(std::numeric_limits<int_type>::is_integer);
104 MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
105 if constexpr (endian == mpt::endian::big) {
106 typename std::make_unsigned<int_type>::type uval = val;
107 for (std::size_t i = 0; i < sizeof(base_type); ++i) {
108 data[i] = static_cast<std::byte>((uval >> (8 * (sizeof(base_type) - 1 - i))) & 0xffu);
109 }
110 } else {
111 typename std::make_unsigned<int_type>::type uval = val;
112 for (std::size_t i = 0; i < sizeof(base_type); ++i) {
113 data[i] = static_cast<std::byte>((uval >> (8 * i)) & 0xffu);
114 }
115 }
116 } else {
117 if constexpr (std::is_integral<base_type>::value && (mpt::endian::native == mpt::endian::little || mpt::endian::native == mpt::endian::big)) {
118 if constexpr (mpt::endian::native != endian) {
119 val = mpt::byteswap(val);
120 }
121 std::memcpy(data.data(), &val, sizeof(val));
122 } else {
123 data = EndianEncode<base_type, endian, int_type>(val);
124 }
125 }
126 }
127 MPT_CONSTEXPR20_FUN base_type get() const noexcept {
128 static_assert(std::numeric_limits<int_type>::is_integer);
129 MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
130 if constexpr (endian == mpt::endian::big) {
131 typename std::make_unsigned<int_type>::type uval = 0;
132 for (std::size_t i = 0; i < sizeof(base_type); ++i) {
133 uval |= static_cast<typename std::make_unsigned<int_type>::type>(static_cast<typename std::make_unsigned<int_type>::type>(data[i]) << (8 * (sizeof(base_type) - 1 - i)));
134 }
135 return static_cast<base_type>(uval);
136 } else {
137 typename std::make_unsigned<int_type>::type uval = 0;
138 for (std::size_t i = 0; i < sizeof(base_type); ++i) {
139 uval |= static_cast<typename std::make_unsigned<int_type>::type>(static_cast<typename std::make_unsigned<int_type>::type>(data[i]) << (8 * i));
140 }
141 return static_cast<base_type>(uval);
142 }
143 } else {
144 if constexpr (std::is_integral<base_type>::value && (mpt::endian::native == mpt::endian::little || mpt::endian::native == mpt::endian::big)) {
145 base_type val = base_type();
146 std::memcpy(&val, data.data(), sizeof(val));
147 if constexpr (mpt::endian::native != endian) {
148 val = mpt::byteswap(val);
149 }
150 return val;
151 } else {
152 return EndianDecode<base_type, endian, int_type>(data);
153 }
154 }
155 }
156 MPT_CONSTEXPR20_FUN packed & operator=(const base_type & val) noexcept {
157 set(val);
158 return *this;
159 }
160 MPT_CONSTEXPR20_FUN operator base_type() const noexcept {
161 return get();
162 }
163
164 public:
165 MPT_CONSTEXPR20_FUN packed & operator&=(base_type val) noexcept {
166 set(get() & val);
167 return *this;
168 }
169 MPT_CONSTEXPR20_FUN packed & operator|=(base_type val) noexcept {
170 set(get() | val);
171 return *this;
172 }
173 MPT_CONSTEXPR20_FUN packed & operator^=(base_type val) noexcept {
174 set(get() ^ val);
175 return *this;
176 }
177 MPT_CONSTEXPR20_FUN packed & operator+=(base_type val) noexcept {
178 set(get() + val);
179 return *this;
180 }
181 MPT_CONSTEXPR20_FUN packed & operator-=(base_type val) noexcept {
182 set(get() - val);
183 return *this;
184 }
185 MPT_CONSTEXPR20_FUN packed & operator*=(base_type val) noexcept {
186 set(get() * val);
187 return *this;
188 }
189 MPT_CONSTEXPR20_FUN packed & operator/=(base_type val) noexcept {
190 set(get() / val);
191 return *this;
192 }
193 MPT_CONSTEXPR20_FUN packed & operator%=(base_type val) noexcept {
194 set(get() % val);
195 return *this;
196 }
197 MPT_CONSTEXPR20_FUN packed & operator++() noexcept { // prefix
198 set(get() + 1);
199 return *this;
200 }
201 MPT_CONSTEXPR20_FUN packed & operator--() noexcept { // prefix
202 set(get() - 1);
203 return *this;
204 }
205 MPT_CONSTEXPR20_FUN base_type operator++(int) noexcept { // postfix
206 base_type old = get();
207 set(old + 1);
208 return old;
209 }
210 MPT_CONSTEXPR20_FUN base_type operator--(int) noexcept { // postfix
211 base_type old = get();
212 set(old - 1);
213 return old;
214 }
215 };
216
217 using int64le = packed<int64, mpt::endian::little>;
218 using int32le = packed<int32, mpt::endian::little>;
219 using int16le = packed<int16, mpt::endian::little>;
220 using int8le = packed<int8, mpt::endian::little>;
221 using uint64le = packed<uint64, mpt::endian::little>;
222 using uint32le = packed<uint32, mpt::endian::little>;
223 using uint16le = packed<uint16, mpt::endian::little>;
224 using uint8le = packed<uint8, mpt::endian::little>;
225
226 using int64be = packed<int64, mpt::endian::big>;
227 using int32be = packed<int32, mpt::endian::big>;
228 using int16be = packed<int16, mpt::endian::big>;
229 using int8be = packed<int8, mpt::endian::big>;
230 using uint64be = packed<uint64, mpt::endian::big>;
231 using uint32be = packed<uint32, mpt::endian::big>;
232 using uint16be = packed<uint16, mpt::endian::big>;
233 using uint8be = packed<uint8, mpt::endian::big>;
234
235 constexpr bool declare_binary_safe(const int64le &) {
236 return true;
237 }
238 constexpr bool declare_binary_safe(const int32le &) {
239 return true;
240 }
241 constexpr bool declare_binary_safe(const int16le &) {
242 return true;
243 }
244 constexpr bool declare_binary_safe(const int8le &) {
245 return true;
246 }
247 constexpr bool declare_binary_safe(const uint64le &) {
248 return true;
249 }
250 constexpr bool declare_binary_safe(const uint32le &) {
251 return true;
252 }
253 constexpr bool declare_binary_safe(const uint16le &) {
254 return true;
255 }
256 constexpr bool declare_binary_safe(const uint8le &) {
257 return true;
258 }
259
260 constexpr bool declare_binary_safe(const int64be &) {
261 return true;
262 }
263 constexpr bool declare_binary_safe(const int32be &) {
264 return true;
265 }
266 constexpr bool declare_binary_safe(const int16be &) {
267 return true;
268 }
269 constexpr bool declare_binary_safe(const int8be &) {
270 return true;
271 }
272 constexpr bool declare_binary_safe(const uint64be &) {
273 return true;
274 }
275 constexpr bool declare_binary_safe(const uint32be &) {
276 return true;
277 }
278 constexpr bool declare_binary_safe(const uint16be &) {
279 return true;
280 }
281 constexpr bool declare_binary_safe(const uint8be &) {
282 return true;
283 }
284
285 static_assert(mpt::check_binary_size<int64le>(8));
286 static_assert(mpt::check_binary_size<int32le>(4));
287 static_assert(mpt::check_binary_size<int16le>(2));
288 static_assert(mpt::check_binary_size<int8le>(1));
289 static_assert(mpt::check_binary_size<uint64le>(8));
290 static_assert(mpt::check_binary_size<uint32le>(4));
291 static_assert(mpt::check_binary_size<uint16le>(2));
292 static_assert(mpt::check_binary_size<uint8le>(1));
293
294 static_assert(mpt::check_binary_size<int64be>(8));
295 static_assert(mpt::check_binary_size<int32be>(4));
296 static_assert(mpt::check_binary_size<int16be>(2));
297 static_assert(mpt::check_binary_size<int8be>(1));
298 static_assert(mpt::check_binary_size<uint64be>(8));
299 static_assert(mpt::check_binary_size<uint32be>(4));
300 static_assert(mpt::check_binary_size<uint16be>(2));
301 static_assert(mpt::check_binary_size<uint8be>(1));
302
303
304
305 template <typename T>
306 struct make_endian<mpt::endian::little, T> {
307 using type = packed<typename std::remove_const<T>::type, mpt::endian::little>;
308 };
309
310 template <typename T>
311 struct make_endian<mpt::endian::big, T> {
312 using type = packed<typename std::remove_const<T>::type, mpt::endian::big>;
313 };
314
315
316
317 template <typename T>
318 MPT_CONSTEXPR20_FUN auto as_le(T v) noexcept -> typename mpt::make_le<typename std::remove_const<T>::type>::type {
319 typename mpt::make_le<typename std::remove_const<T>::type>::type res{};
320 res = v;
321 return res;
322 }
323
324 template <typename T>
325 MPT_CONSTEXPR20_FUN auto as_be(T v) noexcept -> typename mpt::make_be<typename std::remove_const<T>::type>::type {
326 typename mpt::make_be<typename std::remove_const<T>::type>::type res{};
327 res = v;
328 return res;
329 }
330
331
332
333 template <typename Tpacked>
334 MPT_CONSTEXPR20_FUN Tpacked as_endian(typename Tpacked::base_type v) noexcept {
335 Tpacked res{};
336 res = v;
337 return res;
338 }
339
340
341
342 } // namespace MPT_INLINE_NS
343 } // namespace mpt
344
345
346
347 namespace std {
348 template <typename T, mpt::endian endian>
349 class numeric_limits<mpt::packed<T, endian>> : public std::numeric_limits<T> { };
350 template <typename T, mpt::endian endian>
351 class numeric_limits<const mpt::packed<T, endian>> : public std::numeric_limits<const T> { };
352 } // namespace std
353
354
355
356 #endif // MPT_ENDIAN_INTEGER_HPP