a31e31ab5a447ae072a1a086e4da67897df24d9f
[babeltrace.git] / src / cpp-common / endian.hpp
1 /*
2 * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 #ifndef BABELTRACE_CPP_COMMON_ENDIAN_HPP
8 #define BABELTRACE_CPP_COMMON_ENDIAN_HPP
9
10 #include <cstdint>
11
12 #include "compat/endian.h" /* IWYU pragma: keep */
13
14 namespace bt2_common {
15
16 static inline std::uint8_t littleEndianToNative(const std::uint8_t val) noexcept
17 {
18 return val;
19 }
20
21 static inline std::uint8_t bigEndianToNative(const std::uint8_t val) noexcept
22 {
23 return val;
24 }
25
26 static inline std::int8_t littleEndianToNative(const std::int8_t val) noexcept
27 {
28 return val;
29 }
30
31 static inline std::int8_t bigEndianToNative(const std::int8_t val) noexcept
32 {
33 return val;
34 }
35
36 static inline std::uint16_t littleEndianToNative(const std::uint16_t val) noexcept
37 {
38 return static_cast<std::uint16_t>(le16toh(val));
39 }
40
41 static inline std::uint16_t bigEndianToNative(const std::uint16_t val) noexcept
42 {
43 return static_cast<std::uint16_t>(be16toh(val));
44 }
45
46 static inline std::int16_t littleEndianToNative(const std::int16_t val) noexcept
47 {
48 return static_cast<std::int16_t>(littleEndianToNative(static_cast<std::uint16_t>(val)));
49 }
50
51 static inline std::int16_t bigEndianToNative(const std::int16_t val) noexcept
52 {
53 return static_cast<std::int16_t>(bigEndianToNative(static_cast<std::uint16_t>(val)));
54 }
55
56 static inline std::uint32_t littleEndianToNative(const std::uint32_t val) noexcept
57 {
58 return static_cast<std::uint32_t>(le32toh(val));
59 }
60
61 static inline std::uint32_t bigEndianToNative(const std::uint32_t val) noexcept
62 {
63 return static_cast<std::uint32_t>(be32toh(val));
64 }
65
66 static inline std::int32_t littleEndianToNative(const std::int32_t val) noexcept
67 {
68 return static_cast<std::int32_t>(littleEndianToNative(static_cast<std::uint32_t>(val)));
69 }
70
71 static inline std::int32_t bigEndianToNative(const std::int32_t val) noexcept
72 {
73 return static_cast<std::int32_t>(bigEndianToNative(static_cast<std::uint32_t>(val)));
74 }
75
76 static inline std::uint64_t littleEndianToNative(const std::uint64_t val) noexcept
77 {
78 return static_cast<std::uint64_t>(le64toh(val));
79 }
80
81 static inline std::uint64_t bigEndianToNative(const std::uint64_t val) noexcept
82 {
83 return static_cast<std::uint64_t>(be64toh(val));
84 }
85
86 static inline std::int64_t littleEndianToNative(const std::int64_t val) noexcept
87 {
88 return static_cast<std::int64_t>(littleEndianToNative(static_cast<std::uint64_t>(val)));
89 }
90
91 static inline std::int64_t bigEndianToNative(const std::int64_t val) noexcept
92 {
93 return static_cast<std::int64_t>(bigEndianToNative(static_cast<std::uint64_t>(val)));
94 }
95
96 } /* namespace bt2_common */
97
98 #endif /* BABELTRACE_CPP_COMMON_ENDIAN_HPP */
This page took 0.03271 seconds and 3 git commands to generate.