2 * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_ENDIAN_HPP
8 #define BABELTRACE_CPP_COMMON_ENDIAN_HPP
12 #include "compat/endian.h" /* IWYU pragma: keep */
14 namespace bt2_common {
16 static inline std::uint8_t littleEndianToNative(const std::uint8_t val) noexcept
21 static inline std::uint8_t bigEndianToNative(const std::uint8_t val) noexcept
26 static inline std::int8_t littleEndianToNative(const std::int8_t val) noexcept
31 static inline std::int8_t bigEndianToNative(const std::int8_t val) noexcept
36 static inline std::uint16_t littleEndianToNative(const std::uint16_t val) noexcept
38 return static_cast<std::uint16_t>(le16toh(val));
41 static inline std::uint16_t bigEndianToNative(const std::uint16_t val) noexcept
43 return static_cast<std::uint16_t>(be16toh(val));
46 static inline std::int16_t littleEndianToNative(const std::int16_t val) noexcept
48 return static_cast<std::int16_t>(littleEndianToNative(static_cast<std::uint16_t>(val)));
51 static inline std::int16_t bigEndianToNative(const std::int16_t val) noexcept
53 return static_cast<std::int16_t>(bigEndianToNative(static_cast<std::uint16_t>(val)));
56 static inline std::uint32_t littleEndianToNative(const std::uint32_t val) noexcept
58 return static_cast<std::uint32_t>(le32toh(val));
61 static inline std::uint32_t bigEndianToNative(const std::uint32_t val) noexcept
63 return static_cast<std::uint32_t>(be32toh(val));
66 static inline std::int32_t littleEndianToNative(const std::int32_t val) noexcept
68 return static_cast<std::int32_t>(littleEndianToNative(static_cast<std::uint32_t>(val)));
71 static inline std::int32_t bigEndianToNative(const std::int32_t val) noexcept
73 return static_cast<std::int32_t>(bigEndianToNative(static_cast<std::uint32_t>(val)));
76 static inline std::uint64_t littleEndianToNative(const std::uint64_t val) noexcept
78 return static_cast<std::uint64_t>(le64toh(val));
81 static inline std::uint64_t bigEndianToNative(const std::uint64_t val) noexcept
83 return static_cast<std::uint64_t>(be64toh(val));
86 static inline std::int64_t littleEndianToNative(const std::int64_t val) noexcept
88 return static_cast<std::int64_t>(littleEndianToNative(static_cast<std::uint64_t>(val)));
91 static inline std::int64_t bigEndianToNative(const std::int64_t val) noexcept
93 return static_cast<std::int64_t>(bigEndianToNative(static_cast<std::uint64_t>(val)));
96 } /* namespace bt2_common */
98 #endif /* BABELTRACE_CPP_COMMON_ENDIAN_HPP */