5#ifndef MLAB_BYTE_ORDER_HPP
6#define MLAB_BYTE_ORDER_HPP
20 template <
unsigned Bits>
23 template <
unsigned Bits>
27 template <
unsigned Bits>
30 template <
unsigned Bits>
33 template <
unsigned Bits>
34 using int_least_t [[maybe_unused]] = std::make_signed_t<uint_least_t<Bits>>;
36 template <
byte_order Order,
unsigned Bits,
class Num>
37 std::array<std::uint8_t, Bits / 8>
encode(Num n);
39 template <
byte_order Order,
unsigned Bits,
class Num>
40 Num
decode(std::array<std::uint8_t, Bits / 8>
const &b);
45#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
47#elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
56 template <
class Num, std::
size_t NBytes>
59 template <
class Num, std::size_t NBytes, std::size_t I = NBytes - 1>
62 template <
class Num, std::
size_t NBytes>
65 template <
class Num, std::
size_t NBytes>
76 template <
unsigned Bits>
78 if constexpr (Bits == 8) {
79 return std::uint8_t{};
80 }
else if constexpr (Bits == 16) {
81 return std::uint16_t{};
82 }
else if constexpr (Bits == 32) {
83 return std::uint32_t{};
84 }
else if constexpr (Bits == 64) {
85 return std::uint64_t{};
87 static_assert(Bits % 8 == 0 and Bits > 0 and Bits <= 64);
91 template <
unsigned Bits>
93 if constexpr (Bits <= 8) {
94 return std::uint8_t{};
95 }
else if constexpr (Bits <= 16) {
96 return std::uint16_t{};
97 }
else if constexpr (Bits <= 32) {
98 return std::uint32_t{};
99 }
else if constexpr (Bits <= 64) {
100 return std::uint64_t{};
102 static_assert(Bits > 0 and Bits <= 64);
107 template <
class Num, std::
size_t NBytes>
109 static_assert(std::is_integral_v<Num> and std::is_unsigned_v<Num> and
sizeof(Num) >= NBytes,
"Use a sufficiently large unsigned integer.");
111 for (std::size_t i = 0; i < NBytes; ++i) {
112 n |= Num(b[i]) << (i * 8);
117 template <
class Num, std::
size_t NBytes>
119 static_assert(std::is_integral_v<Num> and std::is_unsigned_v<Num> and
sizeof(Num) >= NBytes,
"Use a sufficiently large unsigned integer.");
121 for (std::size_t i = 0; i < NBytes; ++i) {
128 template <
class Num, std::
size_t NBytes>
130 static_assert(std::is_integral_v<Num> and std::is_unsigned_v<Num> and
sizeof(Num) >= NBytes,
"Use a sufficiently large unsigned integer.");
131 std::array<std::uint8_t, NBytes> a{};
132 for (std::size_t i = 0; i < NBytes; ++i, n >>= 8) {
133 a[i] = std::uint8_t(n & 0xff);
138 template <
class Num, std::
size_t NBytes>
140 static_assert(std::is_integral_v<Num> and std::is_unsigned_v<Num> and
sizeof(Num) >= NBytes,
"Use a sufficiently large unsigned integer.");
141 std::array<std::uint8_t, NBytes> a{};
142 for (std::size_t i = 0; i < NBytes; ++i, n >>= 8) {
143 a[NBytes - i - 1] = std::uint8_t(n & 0xff);
148 template <
byte_order Order,
unsigned Bits,
class Num>
149 std::array<std::uint8_t, Bits / 8>
encode(Num n) {
150 static_assert(
sizeof(Num) >= Bits / 8,
"Use a sufficiently large number.");
151 static_assert(std::is_integral_v<Num> or std::is_floating_point_v<Num>);
152 if constexpr (std::is_floating_point_v<Num>) {
155 }
else if constexpr (std::is_signed_v<Num>) {
156 using UNum = std::make_unsigned_t<Num>;
167 template <
byte_order Order,
unsigned Bits,
class Num>
168 Num
decode(std::array<std::uint8_t, Bits / 8>
const &b) {
169 static_assert(
sizeof(Num) >= Bits / 8,
"Use a sufficiently large number.");
170 static_assert(std::is_integral_v<Num> or std::is_floating_point_v<Num>);
172 if constexpr (std::is_floating_point_v<Num>) {
175 }
else if constexpr (std::is_signed_v<Num>) {
176 using UNum = std::make_unsigned_t<Num>;
constexpr std::array< std::uint8_t, NBytes > msb_unsigned_encode(Num n)
Definition byte_order.hpp:139
constexpr Num msb_unsigned_decode(std::array< std::uint8_t, NBytes > b)
constexpr Num lsb_unsigned_decode(std::array< std::uint8_t, NBytes > b)
Definition byte_order.hpp:108
constexpr std::array< std::uint8_t, NBytes > lsb_unsigned_encode(Num n)
Definition byte_order.hpp:129
auto get_uint_least()
Definition byte_order.hpp:92
auto get_uint_exact()
Definition byte_order.hpp:77
decltype(impl::get_uint_exact< Bits >()) uint_exact_t
Definition byte_order.hpp:31
byte_order
Definition byte_order.hpp:14
std::array< std::uint8_t, Bits/8 > encode(Num n)
Definition byte_order.hpp:149
decltype(impl::get_uint_least< Bits >()) uint_least_t
Definition byte_order.hpp:28
Num decode(std::array< std::uint8_t, Bits/8 > const &b)
Definition byte_order.hpp:168