8#include "reflection.hpp"
10#include "../macros.hpp"
21#if HI_COMPILER == HI_CC_MSVC
28hi_export_module(hikogui.utility.endian);
34hi_warning_ignore_msvc(26472);
36hi_export
namespace hi {
inline namespace v1 {
40template<std::
integral T>
43 if constexpr (std::endian::native == std::endian::little) {
46 return std::byteswap(x);
52template<std::
integral T>
55 if constexpr (std::endian::native == std::endian::big) {
58 return std::byteswap(x);
64template<std::
integral T>
67 if constexpr (std::endian::native == std::endian::little) {
70 return std::byteswap(x);
76template<std::
integral T>
79 if constexpr (std::endian::native == std::endian::big) {
82 return std::byteswap(x);
93template<std::
integral Out, std::endian Endian = std::endian::native,
typename In>
96 if constexpr (
Endian != std::endian::native) {
110template<std::
integral T, std::endian Endian = std::endian::native>
114 if constexpr (
Endian != std::endian::native) {
115 value = std::byteswap(value);
126template<std::
integral T>
138template<std::
integral T,
byte_like B>
150template<std::
integral T>
162template<std::
integral T>
174template<std::
integral T,
byte_like B>
186template<std::
integral T>
207template<
unsigned int NumBits,
byte_like B>
212 constexpr auto num_bits =
NumBits;
218 std::conditional_t<num_bytes <
sizeof(
unsigned short),
unsigned short,
219 std::conditional_t<num_bytes <
sizeof(
unsigned int),
unsigned int,
220 std::conditional_t<num_bytes <
sizeof(
unsigned long),
unsigned long,
unsigned long long>>>;
229 if constexpr (num_bits ==
CHAR_BIT) {
242 if constexpr (num_bytes ==
sizeof(value_type)) {
259template<std::endian Endian = std::endian::native, std::
integral T,
byte_like B>
260constexpr void store(T value, B
const *
dst)
noexcept
262 if constexpr (
Endian != std::endian::native) {
263 value = std::byteswap(value);
268template<std::endian Endian = std::endian::native, std::
integral T>
269constexpr void store(T value,
void const *
dst)
noexcept
271 if constexpr (
Endian != std::endian::native) {
272 value = std::byteswap(value);
277template<std::
integral T,
byte_like B>
278constexpr void store_le(T value, B
const *
dst)
noexcept
283template<std::
integral T>
284hi_inline
void store_le(T value,
void const *
dst)
noexcept
289template<std::
integral T,
byte_like B>
290constexpr void store_be(T value, B
const *
dst)
noexcept
295template<std::
integral T>
296hi_inline
void store_be(T value,
void const *
dst)
noexcept
301template<
typename T, std::endian E, std::
size_t A = alignof(T)>
303 using value_type = T;
304 constexpr static std::endian endian = E;
307 alignas(A) std::byte _value[
sizeof(T)];
309 [[
nodiscard]]
constexpr value_type operator*()
const noexcept
314 constexpr endian_buf_t& operator=(value_type x)
noexcept
Functions for casting values between types savely.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
constexpr T big_to_native(T x)
Convert an integral from big-to-native endian.
Definition endian.hpp:53
constexpr T native_to_little(T x)
Convert an integral from native-to-little endian.
Definition endian.hpp:65
constexpr T load_be(T const *src) noexcept
Load of a numeric value encoded in big-endian format.
Definition endian.hpp:163
constexpr T little_to_native(T x)
Convert an integral from little-to-native endian.
Definition endian.hpp:41
constexpr T load_le(T const *src) noexcept
Load of a numeric value encoded in little-endian format.
Definition endian.hpp:127
constexpr Out load(In const *src) noexcept
Unaligned Load of a numeric value from an array.
Definition endian.hpp:94
constexpr T native_to_big(T x)
Convert an integral from native-to-big endian.
Definition endian.hpp:77
constexpr auto load_bits_be(B const *src, size_t bit_index) noexcept
Unaligned load bits from a big-endian buffer at a bit-offset.
Definition endian.hpp:208
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
Horizontal/Vertical alignment combination.
Definition alignment.hpp:244
Definition endian.hpp:302