Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
endian.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::endian"
2
3// Copyright 2024, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ENDIAN_ENDIAN_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ENDIAN_ENDIAN_HPP_
8
9// IWYU pragma: private, include "arene/base/endian.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
12// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax pmeritted by A7-1-5 Permit #1 v1.0.0"
13
14// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
15
16// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
21#include "arene/base/stdlib_choice/cstddef.hpp"
22#include "arene/base/stdlib_choice/cstdint.hpp"
23#include "arene/base/stdlib_choice/enable_if.hpp"
24#include "arene/base/stdlib_choice/ignore.hpp"
25#include "arene/base/stdlib_choice/is_arithmetic.hpp"
26#include "arene/base/stdlib_choice/is_enum.hpp"
27#include "arene/base/stdlib_choice/is_floating_point.hpp"
28#include "arene/base/stdlib_choice/is_integral.hpp"
29#include "arene/base/stdlib_choice/is_same.hpp"
30#include "arene/base/stdlib_choice/underlying_type.hpp"
32// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
33
34#if defined(_WIN32)
35#include "arene/base/endian/detail/endian_windows_impl.hpp" // IWYU pragma: export
36#elif defined(__BYTE_ORDER__)
37#include "arene/base/endian/detail/endian_specified_byte_order_impl.hpp" // IWYU pragma: export
38#else
39#include "arene/base/endian/detail/endian_generic_impl.hpp" // IWYU pragma: export
40#endif
41
42namespace arene {
43namespace base {
44
45// parasoft-begin-suppress AUTOSAR-A0_4_2-a-2 "long double named as explicitly not supported"
46
47///////////
48//
49// Utilities for reading/writing binary values in LITTLE-ENDIAN format.
50
51/// @brief Read a value from a serialized binary representation stored in little-endian byte order.
52///
53/// @tparam T The type of the value to read.
54/// The bytes to read are passed as @c bytes.
55/// @cond CPPTEST_PARAMETER_DOCUMENTATION
56/// @param bytes A view of the bytes to read the data from.
57/// @endcond
58/// @return The read value
59template <
60 typename T,
61 constraints<std::enable_if_t<!std::is_enum<T>::value>, std::enable_if_t<!std::is_arithmetic<T>::value>> = nullptr>
62auto read_little_endian(span<byte const, sizeof(T)> bytes) noexcept -> T = delete;
63
64/// @brief Read an integral value from a serialized binary representation stored in little-endian byte order.
65///
66/// @tparam T The type of the value to read.
67/// The bytes to read are passed as @c bytes.
68/// @cond CPPTEST_PARAMETER_DOCUMENTATION
69/// @param bytes A view of the bytes to read the data from.
70/// @endcond
71/// @return The read value
72template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>> = nullptr>
73constexpr auto read_little_endian(span<byte const, sizeof(T)> bytes) noexcept -> T;
74
75/// @brief Read an enum value from a serialized binary representation stored in little-endian byte order.
76///
77/// @tparam T The type of the value to read.
78/// The bytes to read are passed as @c bytes.
79/// @cond CPPTEST_PARAMETER_DOCUMENTATION
80/// @param bytes A view of the bytes to read the data from.
81/// @endcond
82/// @return The read value
83template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>> = nullptr>
84constexpr auto read_little_endian(span<byte const, sizeof(T)> bytes) noexcept -> T;
85
86/// @brief Read a floating point value from a serialized binary representation stored in little-endian byte order.
87///
88/// @tparam T The type of the value to read.
89/// The bytes to read are passed as @c bytes.
90/// @cond CPPTEST_PARAMETER_DOCUMENTATION
91/// @param bytes A view of the bytes to read the data from.
92/// @endcond
93/// @return The read value
94template <
95 typename T,
97 std::enable_if_t<std::is_floating_point<T>::value>,
98 std::enable_if_t<!std::is_same<T, long double>::value>> = nullptr>
99auto read_little_endian(span<byte const, sizeof(T)> bytes) noexcept -> T;
100
101/// @brief Write a value to a buffer in little-endian binary representation.
102///
103/// @tparam T The type of the value to write. This template parameter must be explicitly specified.
104/// The value and buffer to write are passed as @c value and @c bytes.
105/// @cond CPPTEST_PARAMETER_DOCUMENTATION
106/// @param value The value to write.
107/// @param bytes The buffer to write the little-endian binary representation to.
108/// @endcond
109template <
110 typename T,
111 constraints<std::enable_if_t<!std::is_enum<T>::value>, std::enable_if_t<!std::is_arithmetic<T>::value>> = nullptr>
112void write_little_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> bytes) noexcept = delete;
113
114/// @brief Write an integral value to a buffer in little-endian binary representation.
115///
116/// @tparam T The type of the value to write. This template parameter must be explicitly specified.
117/// The value and buffer to write are passed as @c value and @c bytes.
118/// @cond CPPTEST_PARAMETER_DOCUMENTATION
119/// @param value The value to write.
120/// @param bytes The buffer to write the little-endian binary representation to.
121/// @endcond
122template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>> = nullptr>
123constexpr void write_little_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> bytes) noexcept;
124
125/// @brief Write an enum value to a buffer in little-endian binary representation.
126///
127/// @tparam T The type of the value to write. This template parameter must be explicitly specified.
128/// The value and buffer to write are passed as @c value and @c bytes.
129/// @cond CPPTEST_PARAMETER_DOCUMENTATION
130/// @param value The value to write.
131/// @param bytes The buffer to write the little-endian binary representation to.
132/// @endcond
133template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>> = nullptr>
134constexpr void write_little_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> bytes) noexcept;
135
136/// @brief Write a floating point value to a buffer in little-endian binary representation.
137///
138/// @tparam T The type of the value to write. This template parameter must be explicitly specified.
139/// The value and buffer to write are passed as @c value and @c bytes.
140/// @cond CPPTEST_PARAMETER_DOCUMENTATION
141/// @param value The value to write.
142/// @param bytes The buffer to write the little-endian binary representation to.
143/// @endcond
144template <
145 typename T,
147 std::enable_if_t<std::is_floating_point<T>::value>,
148 std::enable_if_t<!std::is_same<T, long double>::value>> = nullptr>
149void write_little_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> bytes) noexcept;
150
151///////////
152//
153// Utilities for reading/writing binary values in BIG-ENDIAN format.
154
155/// @brief Read a value from a serialized binary representation stored in big-endian byte order.
156///
157/// @tparam T The type of the value to read.
158/// @param bytes A view of the bytes to read the data from.
159/// @return The read value
160template <
161 typename T,
162 constraints<std::enable_if_t<!std::is_enum<T>::value>, std::enable_if_t<!std::is_arithmetic<T>::value>> = nullptr>
163auto read_big_endian(span<byte const, sizeof(T)> bytes) noexcept -> T = delete;
164
165/// @brief Read a value from a serialized binary representation stored in big-endian byte order.
166///
167/// @tparam T The type of the value to read.
168/// @param bytes A view of the bytes to read the data from.
169/// @return The read value
170template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>> = nullptr>
171constexpr auto read_big_endian(span<byte const, sizeof(T)> bytes) noexcept -> T;
172
173/// @brief Read a value from a serialized binary representation stored in big-endian byte order.
174///
175/// @tparam T The type of the value to read.
176/// @param bytes A view of the bytes to read the data from.
177/// @return The read value
178template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>> = nullptr>
179constexpr auto read_big_endian(span<byte const, sizeof(T)> bytes) noexcept -> T;
180
181/// @brief Read a value from a serialized binary representation stored in big-endian byte order.
182///
183/// @tparam T The type of the value to read.
184/// @param bytes A view of the bytes to read the data from.
185/// @return The read value
186template <
187 typename T,
189 std::enable_if_t<std::is_floating_point<T>::value>,
190 std::enable_if_t<!std::is_same<T, long double>::value>> = nullptr>
191auto read_big_endian(span<byte const, sizeof(T)> bytes) noexcept -> T;
192
193/// @brief Write a value to a buffer in big-endian binary representation.
194///
195/// @tparam T The type of the value to write. This template parameter must be explicitly specified.
196/// @param value The value to write.
197/// @param bytes The buffer to write the big-endian binary representation to.
198template <
199 typename T,
200 constraints<std::enable_if_t<!std::is_enum<T>::value>, std::enable_if_t<!std::is_arithmetic<T>::value>> = nullptr>
201void write_big_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> bytes) noexcept = delete;
202
203/// @brief Write a value to a buffer in big-endian binary representation.
204///
205/// @tparam T The type of the value to write. This template parameter must be explicitly specified.
206/// @param value The value to write.
207/// @param bytes The buffer to write the big-endian binary representation to.
208template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>> = nullptr>
209constexpr void write_big_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> bytes) noexcept;
210
211/// @brief Write a value to a buffer in big-endian binary representation.
212///
213/// @tparam T The type of the value to write. This template parameter must be explicitly specified.
214/// @param value The value to write.
215/// @param bytes The buffer to write the big-endian binary representation to.
216template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>> = nullptr>
217constexpr void write_big_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> bytes) noexcept;
218
219/// @brief Write a value to a buffer in big-endian binary representation.
220///
221/// @tparam T The type of the value to write. This template parameter must be explicitly specified.
222/// @param value The value to write.
223/// @param bytes The buffer to write the big-endian binary representation to.
224template <
225 typename T,
227 std::enable_if_t<std::is_floating_point<T>::value>,
228 std::enable_if_t<!std::is_same<T, long double>::value>> = nullptr>
229void write_big_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> bytes) noexcept;
230
231} // namespace base
232} // namespace arene
233
234////////////////////////////////////////////
235// Implementations
236
237namespace arene {
238namespace base {
239
240namespace endian_detail {
241/// @brief constants for bit offsets
242/// @{
243constexpr std::size_t bits_8{8U};
244constexpr std::size_t bits_16{16U};
245constexpr std::size_t bits_24{24U};
246constexpr std::size_t bits_32{32U};
247constexpr std::size_t bits_40{40U};
248constexpr std::size_t bits_48{48U};
249constexpr std::size_t bits_56{56U};
250/// @}
251
252/// @brief indexes for byte words
253/// @{
254constexpr std::size_t byte_0{0U};
255constexpr std::size_t byte_1{1U};
256constexpr std::size_t byte_2{2U};
257constexpr std::size_t byte_3{3U};
258constexpr std::size_t byte_4{4U};
259constexpr std::size_t byte_5{5U};
260constexpr std::size_t byte_6{6U};
261constexpr std::size_t byte_7{7U};
262/// @}
263
264/// @brief Implementation template specialized for each size of type we want to handle
265/// @tparam Size The number of bytes in the representation
266template <std::size_t Size>
267class endian_impl;
268
269/// @brief Implementation template for an 8-bit type
270template <>
271class endian_impl<sizeof(std::uint8_t)> {
272 public:
273 /// @brief The value type
274 using type = std::uint8_t;
275
276 /// @brief Read the value
277 /// @param bytes The source
278 /// @return The read value
279 static constexpr auto read_little_endian(span<byte const, sizeof(type)> const bytes) noexcept -> type {
280 return ::arene::base::to_integer<type>(bytes[byte_0]);
281 }
282
283 /// @brief Write the value
284 /// @param value The value to write
285 /// @param bytes The target storage
286 static constexpr void write_little_endian(type const value, span<byte, sizeof(type)> const bytes) noexcept {
287 bytes[byte_0] = to_byte(value);
288 }
289
290 /// @brief Read the value
291 /// @param bytes The source
292 /// @return The read value
293 static constexpr auto read_big_endian(span<byte const, sizeof(type)> const bytes) noexcept -> type {
294 return ::arene::base::to_integer<type>(bytes[byte_0]);
295 }
296
297 /// @brief Write the value
298 /// @param value The value to write
299 /// @param bytes The target storage
300 static constexpr void write_big_endian(type const value, span<byte, sizeof(type)> const bytes) noexcept {
301 bytes[byte_0] = to_byte(value);
302 }
303};
304
305/// @brief Implementation template for a 16-bit type
306template <>
307class endian_impl<sizeof(std::uint16_t)> {
308 public:
309 /// @brief The value type
310 using type = std::uint16_t;
311
312 /// @brief Read the value
313 /// @param bytes The source
314 /// @return The read value
315 static constexpr auto read_little_endian(span<byte const, sizeof(type)> const bytes) noexcept -> type {
316 // Note: GCC/Clang can usually recognise this pattern and compile this down
317 // to a single load, optionally with the target architecture's byte-swap
318 // instruction if byte-swapping is required.
319 // However, when we know for sure that we're not being called as part of
320 // a constant expression we dispatch to a memcpy/byte_swap-based
321 // implementation above which the compiler is generally more reliable at
322 // optimising.
323 return ::arene::base::to_integer<std::uint16_t>(bytes[byte_0]) |
324 static_cast<std::uint16_t>(::arene::base::to_integer<std::uint16_t>(bytes[byte_1]) << bits_8);
325 }
326
327 /// @brief Write the value
328 /// @param value The value to write
329 /// @param bytes The target storage
330 static constexpr void write_little_endian(type const value, span<byte, sizeof(type)> const bytes) noexcept {
331 // Note: GCC/Clang can usually recognise this pattern and compile this down
332 // to a single store, optionally with the target architecture's byte-swap
333 // instruction if byte-swapping is required.
334 // However, when we know for sure that we're not being called as part of
335 // a constant expression we dispatch to a memcpy/byte_swap-based
336 // implementation above which the compiler is generally more reliable at
337 // optimising.
338
339 bytes[byte_0] = to_byte(value);
340 bytes[byte_1] = to_byte(static_cast<std::uint8_t>(value >> bits_8));
341 }
342
343 /// @brief Read the value
344 /// @param bytes The source
345 /// @return The read value
346 static constexpr auto read_big_endian(span<byte const, sizeof(type)> const bytes) noexcept -> type {
347 // Note: GCC/Clang can usually recognise this pattern and compile this down
348 // to a single load, optionally with the target architecture's byte-swap
349 // instruction if byte-swapping is required.
350 // However, when we know for sure that we're not being called as part of
351 // a constant expression we dispatch to a memcpy/byte_swap-based
352 // implementation above which the compiler is generally more reliable at
353 // optimising.
354
355 return ::arene::base::to_integer<std::uint16_t>(bytes[byte_1]) |
356 static_cast<std::uint16_t>(::arene::base::to_integer<std::uint16_t>(bytes[byte_0]) << bits_8);
357 }
358
359 /// @brief Write the value
360 /// @param value The value to write
361 /// @param bytes The target storage
362 static constexpr void write_big_endian(type const value, span<byte, sizeof(type)> const bytes) noexcept {
363 // Note: GCC/Clang can usually recognise this pattern and compile this down
364 // to a single store, optionally with the target architecture's byte-swap
365 // instruction if byte-swapping is required.
366 // However, when we know for sure that we're not being called as part of
367 // a constant expression we dispatch to a memcpy/byte_swap-based
368 // implementation above which the compiler is generally more reliable at
369 // optimising.
370
371 bytes[byte_1] = to_byte(value);
372 bytes[byte_0] = to_byte(static_cast<std::uint8_t>(value >> bits_8));
373 }
374};
375
376/// @brief Implementation template for a 32-bit type
377template <>
378class endian_impl<sizeof(std::uint32_t)> {
379 public:
380 /// @brief The value type
381 using type = std::uint32_t;
382 /// @brief Read the value
383 /// @param bytes The source
384 /// @return The read value
385 static constexpr auto read_little_endian(span<byte const, sizeof(type)> const bytes) noexcept -> type {
386 // Note: GCC/Clang can usually recognise this pattern and compile this down
387 // to a single load, optionally with the target architecture's byte-swap
388 // instruction if byte-swapping is required.
389 // However, when we know for sure that we're not being called as part of
390 // a constant expression we dispatch to a memcpy/byte_swap-based
391 // implementation above which the compiler is generally more reliable at
392 // optimising.
393
394 return ::arene::base::to_integer<std::uint32_t>(bytes[byte_0]) |
395
396 (::arene::base::to_integer<std::uint32_t>(bytes[byte_1]) << bits_8) |
397
398 (::arene::base::to_integer<std::uint32_t>(bytes[byte_2]) << bits_16) |
399
400 (::arene::base::to_integer<std::uint32_t>(bytes[byte_3]) << bits_24);
401 }
402
403 /// @brief Write the value
404 /// @param value The value to write
405 /// @param bytes The target storage
406 static constexpr void write_little_endian(type const value, span<byte, sizeof(type)> const bytes) noexcept {
407 // Note: GCC/Clang can usually recognise this pattern and compile this down
408 // to a single store, optionally with the target architecture's byte-swap
409 // instruction if byte-swapping is required.
410 // However, when we know for sure that we're not being called as part of
411 // a constant expression we dispatch to a memcpy/byte_swap-based
412 // implementation above which the compiler is generally more reliable at
413 // optimising.
414 bytes[byte_0] = to_byte(value);
415 bytes[byte_1] = to_byte(value >> bits_8);
416 bytes[byte_2] = to_byte(value >> bits_16);
417 bytes[byte_3] = to_byte(value >> bits_24);
418 }
419
420 /// @brief Read the value
421 /// @param bytes The source
422 /// @return The read value
423 static constexpr auto read_big_endian(span<byte const, sizeof(type)> const bytes) noexcept -> type {
424 // Note: GCC/Clang can usually recognise this pattern and compile this down
425 // to a single load, optionally with the target architecture's byte-swap
426 // instruction if byte-swapping is required.
427 // However, when we know for sure that we're not being called as part of
428 // a constant expression we dispatch to a memcpy/byte_swap-based
429 // implementation above which the compiler is generally more reliable at
430 // optimising.
431
432 return ::arene::base::to_integer<std::uint32_t>(bytes[byte_3]) |
433 (::arene::base::to_integer<std::uint32_t>(bytes[byte_2]) << bits_8) |
434 (::arene::base::to_integer<std::uint32_t>(bytes[byte_1]) << bits_16) |
435 (::arene::base::to_integer<std::uint32_t>(bytes[byte_0]) << bits_24);
436 }
437
438 /// @brief Write the value
439 /// @param value The value to write
440 /// @param bytes The target storage
441 static constexpr void write_big_endian(type const value, span<byte, sizeof(type)> const bytes) noexcept {
442 // Note: GCC/Clang can usually recognise this pattern and compile this down
443 // to a single store, optionally with the target architecture's byte-swap
444 // instruction if byte-swapping is required.
445 // However, when we know for sure that we're not being called as part of
446 // a constant expression we dispatch to a memcpy/byte_swap-based
447 // implementation above which the compiler is generally more reliable at
448 // optimising.
449
450 bytes[byte_3] = to_byte(value);
451 bytes[byte_2] = to_byte(value >> bits_8);
452 bytes[byte_1] = to_byte(value >> bits_16);
453 bytes[byte_0] = to_byte(value >> bits_24);
454 }
455};
456
457/// @brief Implementation template for a 64-bit type
458template <>
459class endian_impl<sizeof(std::uint64_t)> {
460 public:
461 /// @brief The value type
462 using type = std::uint64_t;
463 /// @brief Read the value
464 /// @param bytes The source
465 /// @return The read value
466 static constexpr auto read_little_endian(span<byte const, sizeof(type)> const bytes) noexcept -> type {
467 // Note: GCC/Clang can usually recognise this pattern and compile this down
468 // to a single load, optionally with the target architecture's byte-swap
469 // instruction if byte-swapping is required.
470 // However, when we know for sure that we're not being called as part of
471 // a constant expression we dispatch to a memcpy/byte_swap-based
472 // implementation above which the compiler is generally more reliable at
473 // optimising.
474
475 return ::arene::base::to_integer<std::uint64_t>(bytes[byte_0]) |
476 (::arene::base::to_integer<std::uint64_t>(bytes[byte_1]) << bits_8) |
477 (::arene::base::to_integer<std::uint64_t>(bytes[byte_2]) << bits_16) |
478 (::arene::base::to_integer<std::uint64_t>(bytes[byte_3]) << bits_24) |
479 (::arene::base::to_integer<std::uint64_t>(bytes[byte_4]) << bits_32) |
480 (::arene::base::to_integer<std::uint64_t>(bytes[byte_5]) << bits_40) |
481 (::arene::base::to_integer<std::uint64_t>(bytes[byte_6]) << bits_48) |
482 (::arene::base::to_integer<std::uint64_t>(bytes[byte_7]) << bits_56);
483 }
484
485 /// @brief Write the value
486 /// @param value The value to write
487 /// @param bytes The target storage
488 static constexpr void write_little_endian(type const value, span<byte, sizeof(type)> const bytes) noexcept {
489 // Note: GCC/Clang can usually recognise this pattern and compile this down
490 // to a single store, optionally with the target architecture's byte-swap
491 // instruction if byte-swapping is required.
492 // However, when we know for sure that we're not being called as part of
493 // a constant expression we dispatch to a memcpy/byte_swap-based
494 // implementation above which the compiler is generally more reliable at
495 // optimising.
496
497 bytes[byte_0] = to_byte(value);
498 bytes[byte_1] = to_byte(value >> bits_8);
499 bytes[byte_2] = to_byte(value >> bits_16);
500 bytes[byte_3] = to_byte(value >> bits_24);
501 bytes[byte_4] = to_byte(value >> bits_32);
502 bytes[byte_5] = to_byte(value >> bits_40);
503 bytes[byte_6] = to_byte(value >> bits_48);
504 bytes[byte_7] = to_byte(value >> bits_56);
505 }
506
507 /// @brief Read the value
508 /// @param bytes The source
509 /// @return The read value
510 static constexpr auto read_big_endian(span<byte const, sizeof(type)> const bytes) noexcept -> type {
511 // Note: GCC/Clang can usually recognise this pattern and compile this down
512 // to a single load, optionally with the target architecture's byte-swap
513 // instruction if byte-swapping is required.
514 // However, when we know for sure that we're not being called as part of
515 // a constant expression we dispatch to a memcpy/byte_swap-based
516 // implementation above which the compiler is generally more reliable at
517 // optimising.
518
519 return ::arene::base::to_integer<std::uint64_t>(bytes[byte_7]) |
520 (::arene::base::to_integer<std::uint64_t>(bytes[byte_6]) << bits_8) |
521 (::arene::base::to_integer<std::uint64_t>(bytes[byte_5]) << bits_16) |
522 (::arene::base::to_integer<std::uint64_t>(bytes[byte_4]) << bits_24) |
523 (::arene::base::to_integer<std::uint64_t>(bytes[byte_3]) << bits_32) |
524 (::arene::base::to_integer<std::uint64_t>(bytes[byte_2]) << bits_40) |
525 (::arene::base::to_integer<std::uint64_t>(bytes[byte_1]) << bits_48) |
526 (::arene::base::to_integer<std::uint64_t>(bytes[byte_0]) << bits_56);
527 }
528
529 /// @brief Write the value
530 /// @param value The value to write
531 /// @param bytes The target storage
532 static constexpr void write_big_endian(type const value, span<byte, sizeof(type)> const bytes) noexcept {
533 // Note: GCC/Clang can usually recognise this pattern and compile this down
534 // to a single store, optionally with the target architecture's byte-swap
535 // instruction if byte-swapping is required.
536 // However, when we know for sure that we're not being called as part of
537 // a constant expression we dispatch to a memcpy/byte_swap-based
538 // implementation above which the compiler is generally more reliable at
539 // optimising.
540
541 bytes[byte_7] = to_byte(value);
542 bytes[byte_6] = to_byte(value >> bits_8);
543 bytes[byte_5] = to_byte(value >> bits_16);
544 bytes[byte_4] = to_byte(value >> bits_24);
545 bytes[byte_3] = to_byte(value >> bits_32);
546 bytes[byte_2] = to_byte(value >> bits_40);
547 bytes[byte_1] = to_byte(value >> bits_48);
548 bytes[byte_0] = to_byte(value >> bits_56);
549 }
550};
551
552} // namespace endian_detail
553
554// parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: Documented on predeclaration"
555// parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: Documented on predeclaration"
556
557template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>>>
558constexpr auto read_little_endian(span<byte const, sizeof(T)> const bytes) noexcept -> T {
559 return static_cast<T>(endian_detail::endian_impl<sizeof(T)>::read_little_endian(bytes));
560}
561
562// parasoft-begin-suppress AUTOSAR-A7_2_1-a-2 "Value must be checked after reading, this is generic code"
563// parasoft-begin-suppress CERT_CPP-INT50-a-3 "Value must be checked after reading, this is generic code"
564template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>>>
565constexpr auto read_little_endian(span<byte const, sizeof(T)> const bytes) noexcept -> T {
567}
568// parasoft-end-suppress AUTOSAR-A7_2_1-a-2
569// parasoft-end-suppress CERT_CPP-INT50-a-3
570
571template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>>>
572constexpr void write_little_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> const bytes) noexcept {
573 endian_detail::endian_impl<sizeof(T)>::write_little_endian(
574 static_cast<typename endian_detail::endian_impl<sizeof(T)>::type>(value),
575 bytes
576 );
577}
578
579template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>>>
580constexpr void write_little_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> const bytes) noexcept {
582 static_cast<typename std::underlying_type<T>::type>(value),
583 bytes
584 );
585}
586
587template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>>>
588constexpr auto read_big_endian(span<byte const, sizeof(T)> const bytes) noexcept -> T {
589 return static_cast<T>(endian_detail::endian_impl<sizeof(T)>::read_big_endian(bytes));
590}
591
592// parasoft-begin-suppress AUTOSAR-A7_2_1-a-2 "Value must be checked after reading, this is generic code"
593// parasoft-begin-suppress CERT_CPP-INT50-a-3 "Value must be checked after reading, this is generic code"
594template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>>>
595constexpr auto read_big_endian(span<byte const, sizeof(T)> const bytes) noexcept -> T {
597}
598// parasoft-end-suppress AUTOSAR-A7_2_1-a-2
599// parasoft-end-suppress CERT_CPP-INT50-a-3
600
601template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>>>
602constexpr void write_big_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> const bytes) noexcept {
603 endian_detail::endian_impl<sizeof(T)>::write_big_endian(
604 static_cast<typename endian_detail::endian_impl<sizeof(T)>::type>(value),
605 bytes
606 );
607}
608
609template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>>>
610constexpr void write_big_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> const bytes) noexcept {
612 static_cast<typename std::underlying_type<T>::type>(value),
613 bytes
614 );
615}
616
617template <
618 typename T,
622void write_big_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> const bytes) noexcept {
623 static_assert((endian::native == endian::big) || (endian::native == endian::little), "Must have fixed endian");
624 auto const raw_bytes = as_bytes(span<T, 1U>(&value, 1U));
625 switch (endian::native) {
626 case endian::little:
627 std::ignore = ::arene::base::copy(raw_bytes.rbegin(), raw_bytes.rend(), bytes.begin());
628 break;
629 case endian::big:
630 std::ignore = ::arene::base::copy(raw_bytes.begin(), raw_bytes.end(), bytes.begin());
631 break;
632 default:
633 break;
634 }
635}
636
637template <
638 typename T,
642void write_little_endian(base::type_identity_t<T> value, span<byte, sizeof(T)> const bytes) noexcept {
643 static_assert((endian::native == endian::big) || (endian::native == endian::little), "Must have fixed endian");
644 auto const raw_bytes = ::arene::base::as_bytes(span<T, 1U>(&value, 1U));
645 switch (endian::native) {
646 case endian::little:
647 std::ignore = ::arene::base::copy(raw_bytes.begin(), raw_bytes.end(), bytes.begin());
648 break;
649 case endian::big:
650 std::ignore = ::arene::base::copy(raw_bytes.rbegin(), raw_bytes.rend(), bytes.begin());
651 break;
652 default:
653 break;
654 }
655}
656
657template <
658 typename T,
662auto read_big_endian(span<byte const, sizeof(T)> const bytes) noexcept -> T {
663 static_assert((endian::native == endian::big) || (endian::native == endian::little), "Must have fixed endian");
664 T res{};
665 auto raw_bytes = ::arene::base::as_writable_bytes(span<T, 1U>(&res, 1U));
666 switch (endian::native) {
667 case endian::little:
668 std::ignore = ::arene::base::copy(bytes.rbegin(), bytes.rend(), raw_bytes.begin());
669 break;
670 case endian::big:
671 std::ignore = ::arene::base::copy(bytes.begin(), bytes.end(), raw_bytes.begin());
672 break;
673 default:
674 break;
675 }
676 return res;
677}
678
679template <
680 typename T,
684auto read_little_endian(span<byte const, sizeof(T)> const bytes) noexcept -> T {
685 static_assert((endian::native == endian::big) || (endian::native == endian::little), "Must have fixed endian");
686 T res{};
687 auto raw_bytes = ::arene::base::as_writable_bytes(span<T, 1U>(&res, 1U));
688 switch (endian::native) {
689 case endian::little:
690 std::ignore = ::arene::base::copy(bytes.begin(), bytes.end(), raw_bytes.begin());
691 break;
692 case endian::big:
693 std::ignore = ::arene::base::copy(bytes.rbegin(), bytes.rend(), raw_bytes.begin());
694 break;
695 default:
696 break;
697 }
698 return res;
699}
700
701// parasoft-end-suppress AUTOSAR-A0_4_2-a-2
702
703} // namespace base
704} // namespace arene
705
706#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ENDIAN_ENDIAN_HPP_
A backport of std::span for C++14. Provides a view over a contiguous range of elements of type T.
Definition span.hpp:315
Definition accumulate.hpp:37
void write_big_endian(base::type_identity_t< T > value, span< byte, sizeof(T)> bytes) noexcept=delete
Write a value to a buffer in big-endian binary representation.
Definition endian.hpp:622
void write_little_endian(base::type_identity_t< T > value, span< byte, sizeof(T)> bytes) noexcept=delete
Write a value to a buffer in little-endian binary representation.
Definition endian.hpp:642
::std::uint32_t uint32_t
A 32-bit unsigned integer type.
Definition integer_types.hpp:36
typename type_identity< T >::type type_identity_t
Polyfill of std::type_identity_t added in C++20.
Definition type_identity.hpp:40
constexpr auto copy(InputIterator first, InputIterator last, OutputIterator output) noexcept(noexcept(*(++output)) &&noexcept(*(++first)) &&noexcept(first !=last) &&std::is_nothrow_assignable<, typename std::iterator_traits< InputIterator >::reference >::value) -> OutputIterator
Copies the elements in the range, defined by [first, last), to another range beginning at output.
Definition copy.hpp:40
constexpr auto to_byte(ValueType const val) noexcept -> byte
Convert an integer to an arene::base::byte.
Definition byte.hpp:46
auto as_bytes(span< T, N > spn) noexcept -> span< byte const, span_detail::is_dynamic_extent(N) ? dynamic_extent :(N *sizeof(T))>
Obtain a const view to the object representation of the elements of the span spn.
Definition span.hpp:967
auto read_little_endian(span< byte const, sizeof(T)> bytes) noexcept -> T=delete
Read a value from a serialized binary representation stored in little-endian byte order.
Definition endian.hpp:684
::std::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition integer_types.hpp:46
::std::uint16_t uint16_t
A 16-bit unsigned integer type.
Definition integer_types.hpp:27
auto as_writable_bytes(span< T, N > spn) noexcept -> span< byte, span_detail::is_dynamic_extent(N) ? dynamic_extent :(N *sizeof(T))>
Obtain a non-const view to the object representation of the elements of the span spn.
Definition span.hpp:977
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
auto read_big_endian(span< byte const, sizeof(T)> bytes) noexcept -> T=delete
Read a value from a serialized binary representation stored in big-endian byte order.
Definition endian.hpp:662
::std::uint8_t uint8_t
An 8-bit unsigned integer type.
Definition integer_types.hpp:17
constexpr auto to_integer(byte const val) noexcept -> IntegerType
convert the byte value to an integral representation.
Definition byte.hpp:59
Definition accumulate.hpp:36
underlying_type_detail::underlying_type< T > underlying_type
Obtains the underlying integer type for a given enumeration type.
Definition underlying_type.hpp:52
::uint8_t uint8_t
An 8-bit unsigned integer type.
Definition cstdint.hpp:30
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
::uint16_t uint16_t
A 16-bit unsigned integer type.
Definition cstdint.hpp:36
::uint32_t uint32_t
A 32-bit unsigned integer type.
Definition cstdint.hpp:41
::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition cstdint.hpp:47
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72