Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
byte_swap.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ENDIAN_BYTE_SWAP_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ENDIAN_BYTE_SWAP_HPP_
7
8// IWYU pragma: private, include "arene/base/endian.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax pmeritted by A7-1-5 Permit #1 v1.0.0"
12
13// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
14
15// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/compiler_support/platform_queries.hpp"
17#include "arene/base/stdlib_choice/cstdint.hpp"
18// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
19
20// parasoft-begin-suppress AUTOSAR-M16_0_7-a-2 "False positive: Not used as macro"
21#if ARENE_HAS_BUILTIN(__builtin_bswap16)
22#include "arene/base/endian/detail/byte_swap16_bswap16.hpp" // IWYU pragma: export
23#else
24#include "arene/base/endian/detail/byte_swap16.hpp" // IWYU pragma: export
25#endif
26#if ARENE_HAS_BUILTIN(__builtin_bswap32)
27#include "arene/base/endian/detail/byte_swap32_bswap32.hpp" // IWYU pragma: export
28#else
29#include "arene/base/endian/detail/byte_swap32.hpp" // IWYU pragma: export
30#endif
31#if ARENE_HAS_BUILTIN(__builtin_bswap64)
32#include "arene/base/endian/detail/byte_swap64_bswap64.hpp" // IWYU pragma: export
33#else
34#include "arene/base/endian/detail/byte_swap64.hpp" // IWYU pragma: export
35#endif
36// parasoft-end-suppress AUTOSAR-M16_0_7-a-2
37
38namespace arene {
39namespace base {
40
41/// @brief Swap the order of bytes in a given value.
42/// @param value The value to byte-swap.
43/// @return A value having the representation that has the same bytes as @c value but with the byte-order reversed.
44constexpr auto byte_swap(std::uint8_t const value) noexcept -> std::uint8_t { return value; }
45/// @brief Swap the order of bytes in a given value.
46/// @param value The value to byte-swap.
47/// @return A value having the representation that has the same bytes as @c value but with the byte-order reversed.
48constexpr auto byte_swap(std::uint16_t const value) noexcept -> std::uint16_t {
49 return byte_swap_detail::byte_swap(value);
50}
51/// @brief Swap the order of bytes in a given value.
52/// @param value The value to byte-swap.
53/// @return A value having the representation that has the same bytes as @c value but with the byte-order reversed.
54constexpr auto byte_swap(std::uint32_t const value) noexcept -> std::uint32_t {
55 return byte_swap_detail::byte_swap(value);
56}
57/// @brief Swap the order of bytes in a given value.
58/// @param value The value to byte-swap.
59/// @return A value having the representation that has the same bytes as @c value but with the byte-order reversed.
60constexpr auto byte_swap(std::uint64_t const value) noexcept -> std::uint64_t {
61 return byte_swap_detail::byte_swap(value);
62}
63/// @brief Swap the order of bytes in a given value.
64/// @param value The value to byte-swap.
65/// @return A value having the representation that has the same bytes as @c value but with the byte-order reversed.
66constexpr auto byte_swap(std::int8_t const value) noexcept -> std::int8_t { return value; }
67/// @brief Swap the order of bytes in a given value.
68/// @param value The value to byte-swap.
69/// @return A value having the representation that has the same bytes as @c value but with the byte-order reversed.
70constexpr auto byte_swap(std::int16_t const value) noexcept -> std::int16_t {
71 return static_cast<std::int16_t>(::arene::base::byte_swap(static_cast<std::uint16_t>(value)));
72}
73
74/// @brief Swap the order of bytes in a given value.
75/// @param value The value to byte-swap.
76/// @return A value having the representation that has the same bytes as @c value but with the byte-order reversed.
77constexpr auto byte_swap(std::int32_t const value) noexcept -> std::int32_t {
78 return static_cast<std::int32_t>(::arene::base::byte_swap(static_cast<std::uint32_t>(value)));
79}
80
81/// @brief Swap the order of bytes in a given value.
82/// @param value The value to byte-swap.
83/// @return A value having the representation that has the same bytes as @c value but with the byte-order reversed.
84constexpr auto byte_swap(std::int64_t const value) noexcept -> std::int64_t {
85 return static_cast<std::int64_t>(::arene::base::byte_swap(static_cast<std::uint64_t>(value)));
86}
87
88} // namespace base
89} // namespace arene
90
91#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ENDIAN_BYTE_SWAP_HPP_
Definition array_exceptions_disabled.cpp:11
constexpr auto byte_swap(std::uint8_t const value) noexcept -> std::uint8_t
Swap the order of bytes in a given value.
Definition byte_swap.hpp:44
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10