8#include "reflection.hpp"
9#include "../macros.hpp"
20#if HI_COMPILER == HI_CC_MSVC
27hi_export_module(hikogui.utility.endian);
33hi_warning_ignore_msvc(26472);
35hi_export
namespace hi {
inline namespace v1 {
39template<std::
integral T>
42 if constexpr (std::endian::native == std::endian::little) {
45 return std::byteswap(x);
51template<std::
integral T>
54 if constexpr (std::endian::native == std::endian::big) {
57 return std::byteswap(x);
63template<std::
integral T>
66 if constexpr (std::endian::native == std::endian::little) {
69 return std::byteswap(x);
75template<std::
integral T>
78 if constexpr (std::endian::native == std::endian::big) {
81 return std::byteswap(x);
92template<std::
integral Out, std::endian Endian = std::endian::native,
typename In>
93[[nodiscard]]
constexpr Out
load(In
const *src)
noexcept
95 if constexpr (Endian != std::endian::native) {
96 return std::byteswap(unaligned_load<Out>(src));
98 return unaligned_load<Out>(src);
109template<std::
integral T, std::endian Endian = std::endian::native>
110[[nodiscard]]
inline T
load(
void const *src)
noexcept
112 auto value = unaligned_load<T>(src);
113 if constexpr (Endian != std::endian::native) {
114 value = std::byteswap(value);
125template<std::
integral T>
126[[nodiscard]]
constexpr T
load_le(T
const *src)
noexcept
137template<std::
integral T,
byte_like B>
138[[nodiscard]]
constexpr T
load_le(B
const *src)
noexcept
149template<std::
integral T>
150[[nodiscard]]
inline T
load_le(
void const *src)
noexcept
161template<std::
integral T>
162[[nodiscard]]
constexpr T
load_be(T
const *src)
noexcept
173template<std::
integral T,
byte_like B>
174[[nodiscard]]
constexpr T
load_be(B
const *src)
noexcept
185template<std::
integral T>
186[[nodiscard]]
inline T
load_be(
void const *src)
noexcept
206template<
unsigned int NumBits,
byte_like B>
207[[nodiscard]]
constexpr auto load_bits_be(B
const *src,
size_t bit_index)
noexcept
209 static_assert(NumBits <=
sizeof(
unsigned long long) * CHAR_BIT);
211 constexpr auto num_bits = NumBits;
212 constexpr auto num_bytes = (num_bits + CHAR_BIT - 1) / CHAR_BIT;
217 std::conditional_t<num_bytes <
sizeof(
unsigned short),
unsigned short,
218 std::conditional_t<num_bytes <
sizeof(
unsigned int),
unsigned int,
219 std::conditional_t<num_bytes <
sizeof(
unsigned long),
unsigned long,
unsigned long long>>>;
222 constexpr auto value_bits =
sizeof(value_type) * CHAR_BIT;
224 hilet byte_offset = bit_index / CHAR_BIT;
225 hilet bit_offset = bit_index % CHAR_BIT;
228 if constexpr (num_bits == CHAR_BIT) {
229 if (bit_offset == 0) {
241 if constexpr (num_bytes ==
sizeof(value_type)) {
245 auto bits_done = value_bits - bit_offset;
246 if (bits_done < num_bits) {
248 rest >>= CHAR_BIT - bit_offset;
254 r >>= value_bits - num_bits;
258template<std::endian Endian = std::endian::native, std::
integral T,
byte_like B>
259constexpr void store(T value, B
const *dst)
noexcept
261 if constexpr (Endian != std::endian::native) {
262 value = std::byteswap(value);
264 unaligned_store<T>(value, dst);
267template<std::endian Endian = std::endian::native, std::
integral T>
268constexpr void store(T value,
void const *dst)
noexcept
270 if constexpr (Endian != std::endian::native) {
271 value = std::byteswap(value);
273 unaligned_store<T>(value, dst);
276template<std::
integral T,
byte_like B>
277constexpr void store_le(T value, B
const *dst)
noexcept
279 store<std::endian::little>(value, dst);
282template<std::
integral T>
283inline void store_le(T value,
void const *dst)
noexcept
285 store<std::endian::little>(value, dst);
288template<std::
integral T,
byte_like B>
289constexpr void store_be(T value, B
const *dst)
noexcept
291 store<std::endian::big>(value, dst);
294template<std::
integral T>
295inline void store_be(T value,
void const *dst)
noexcept
297 store<std::endian::big>(value, dst);
300template<
typename T, std::endian E, std::
size_t A = alignof(T)>
302 using value_type = T;
303 constexpr static std::endian endian = E;
306 alignas(A) std::byte _value[
sizeof(T)];
308 [[nodiscard]]
constexpr value_type operator*()
const noexcept
313 constexpr endian_buf_t& operator=(value_type x)
noexcept
315 store<endian>(x, _value);
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
The HikoGUI API version 1.
Definition lookahead_iterator.hpp:6
constexpr T big_to_native(T x)
Convert an integral from big-to-native endian.
Definition endian.hpp:52
constexpr Out char_cast(In rhs) noexcept
Cast a character.
Definition cast.hpp:550
constexpr T native_to_little(T x)
Convert an integral from native-to-little endian.
Definition endian.hpp:64
constexpr T load_be(T const *src) noexcept
Load of a numeric value encoded in big-endian format.
Definition endian.hpp:162
constexpr T little_to_native(T x)
Convert an integral from little-to-native endian.
Definition endian.hpp:40
constexpr T load_le(T const *src) noexcept
Load of a numeric value encoded in little-endian format.
Definition endian.hpp:126
constexpr Out load(In const *src) noexcept
Unaligned Load of a numeric value from an array.
Definition endian.hpp:93
constexpr T native_to_big(T x)
Convert an integral from native-to-big endian.
Definition endian.hpp:76
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:207
Horizontal/Vertical alignment combination.
Definition alignment.hpp:242
Definition endian.hpp:301