25#ifndef JAU_BYTE_UTIL_HPP_
26#define JAU_BYTE_UTIL_HPP_
62 #if defined __has_builtin
63 #if __has_builtin(__builtin_bswap16)
64 #define __has_builtin_bswap16 1
66 #elif defined(__GNUC__) && __GNUC_PREREQ (4, 8)
67 #define __has_builtin_bswap16 1
69 #if defined __has_builtin
70 #if __has_builtin(__builtin_bswap32)
71 #define __has_builtin_bswap32 1
73 #elif defined(__GNUC__) && __GNUC_PREREQ (4, 8)
74 #define __has_builtin_bswap32 1
76 #if defined __has_builtin
77 #if __has_builtin(__builtin_bswap64)
78 #define __has_builtin_bswap64 1
80 #elif defined(__GNUC__) && __GNUC_PREREQ (4, 8)
81 #define __has_builtin_bswap64 1
86 constexpr uint16_t
bswap(uint16_t
const source)
noexcept {
87 #if defined __has_builtin_bswap16
88 return __builtin_bswap16(source);
90 return (uint16_t) ( ( ( (source) >> 8 ) & 0xff ) |
91 ( ( (source) & 0xff) << 8 ) );
95 constexpr int16_t
bswap(int16_t
const source)
noexcept {
96 #if defined __has_builtin_bswap64
99 return (int16_t) ( ( ( (source) >> 8 ) & 0xff ) |
100 ( ( (source) & 0xff) << 8 ) );
104 constexpr uint32_t
bswap(uint32_t
const source)
noexcept {
105 #if defined __has_builtin_bswap32
106 return __builtin_bswap32(source);
108 return ( ( source & 0xff000000U ) >> 24 ) |
109 ( ( source & 0x00ff0000U ) >> 8 ) |
110 ( ( source & 0x0000ff00U ) << 8 ) |
111 ( ( source & 0x000000ffU ) << 24 );
115 constexpr int32_t
bswap(int32_t
const source)
noexcept {
116 #if defined __has_builtin_bswap64
120 ( ( source & 0x00ff0000U ) >> 8 ) |
121 ( ( source & 0x0000ff00U ) << 8 ) |
122 ( ( source & 0x000000ffU ) << 24 ) );
126 constexpr uint64_t
bswap(uint64_t
const & source)
noexcept {
127 #if defined __has_builtin_bswap64
128 return __builtin_bswap64(source);
130 return ( ( source & 0xff00000000000000ULL ) >> 56 ) |
131 ( ( source & 0x00ff000000000000ULL ) >> 40 ) |
132 ( ( source & 0x0000ff0000000000ULL ) >> 24 ) |
133 ( ( source & 0x000000ff00000000ULL ) >> 8 ) |
134 ( ( source & 0x00000000ff000000ULL ) << 8 ) |
135 ( ( source & 0x0000000000ff0000ULL ) << 24 ) |
136 ( ( source & 0x000000000000ff00ULL ) << 40 ) |
137 ( ( source & 0x00000000000000ffULL ) << 56 );
141 constexpr int64_t
bswap(int64_t
const & source)
noexcept {
142 #if defined __has_builtin_bswap64
146 ( ( source & 0x00ff000000000000ULL ) >> 40 ) |
147 ( ( source & 0x0000ff0000000000ULL ) >> 24 ) |
148 ( ( source & 0x000000ff00000000ULL ) >> 8 ) |
149 ( ( source & 0x00000000ff000000ULL ) << 8 ) |
150 ( ( source & 0x0000000000ff0000ULL ) << 24 ) |
151 ( ( source & 0x000000000000ff00ULL ) << 40 ) |
152 ( ( source & 0x00000000000000ffULL ) << 56 ) );
156 constexpr void bswap(uint8_t * sink, uint8_t
const * source,
nsize_t len) {
158 for (; len > 0; len--) {
182 return reinterpret_cast<char*
>(b);
185 return reinterpret_cast<const char*
>(b);
189 return reinterpret_cast<const uint8_t*
>(s);
204 constexpr uint8_t b[4] { 0x44, 0x43, 0x42, 0x41 };
369 template <
typename Dummy_type>
struct has_endian_little : std::integral_constant<bool, endian_t::little == endian_t::native> {};
389 template <
typename Dummy_type>
struct has_endian_big : std::integral_constant<bool, endian_t::big == endian_t::native> {};
411 constexpr uint16_t
be_to_cpu(uint16_t
const n)
noexcept {
420 constexpr uint16_t
cpu_to_be(uint16_t
const h)
noexcept {
429 constexpr uint16_t
le_to_cpu(uint16_t
const l)
noexcept {
438 constexpr uint16_t
cpu_to_le(uint16_t
const h)
noexcept {
485 constexpr uint32_t
be_to_cpu(uint32_t
const n)
noexcept {
494 constexpr uint32_t
cpu_to_be(uint32_t
const h)
noexcept {
503 constexpr uint32_t
le_to_cpu(uint32_t
const l)
noexcept {
512 constexpr uint32_t
cpu_to_le(uint32_t
const h)
noexcept {
559 constexpr uint64_t
be_to_cpu(uint64_t
const & n)
noexcept {
568 constexpr uint64_t
cpu_to_be(uint64_t
const & h)
noexcept {
577 constexpr uint64_t
le_to_cpu(uint64_t
const & l)
noexcept {
586 constexpr uint64_t
cpu_to_le(uint64_t
const & h)
noexcept {
596 constexpr int64_t
be_to_cpu(int64_t
const & n)
noexcept {
605 constexpr int64_t
cpu_to_be(int64_t
const & h)
noexcept {
614 constexpr int64_t
le_to_cpu(int64_t
const & l)
noexcept {
623 constexpr int64_t
cpu_to_le(int64_t
const & h)
noexcept {
751 constexpr void put_uint8(uint8_t * buffer,
const uint8_t v)
noexcept
755 constexpr uint8_t
get_uint8(uint8_t
const * buffer)
noexcept
759 constexpr int8_t
get_int8(uint8_t
const * buffer)
noexcept
782 constexpr void put_uint16(uint8_t * buffer,
const uint16_t v)
noexcept
805 constexpr uint16_t
get_uint16(uint8_t
const * buffer)
noexcept
829 constexpr void put_int16(uint8_t * buffer,
const int16_t v)
noexcept
852 constexpr int16_t
get_int16(uint8_t
const * buffer)
noexcept
874 constexpr void put_uint32(uint8_t * buffer,
const uint32_t v)
noexcept
890 constexpr uint32_t
get_uint32(uint8_t
const * buffer)
noexcept
907 constexpr void put_int32(uint8_t * buffer,
const int32_t v)
noexcept
923 constexpr int32_t
get_int32(uint8_t
const * buffer)
noexcept
940 constexpr void put_uint64(uint8_t * buffer,
const uint64_t & v)
noexcept
956 constexpr uint64_t
get_uint64(uint8_t
const * buffer)
noexcept
973 constexpr void put_int64(uint8_t * buffer,
const int64_t & v)
noexcept
989 constexpr int64_t
get_int64(uint8_t
const * buffer)
noexcept
1116 template<
typename T>
1118 typename std::enable_if_t<
1119 std::is_standard_layout_v<T>,
1140 template<
typename T>
1142 typename std::enable_if_t<
1143 std::is_standard_layout_v<T>,
1159 template<
typename T>
1161 typename std::enable_if_t<
1162 std::is_standard_layout_v<T>,
1182 template<
typename T>
1184 typename std::enable_if_t<
1185 std::is_standard_layout_v<T>,
constexpr uint192dp_t get_uint192(uint8_t const *buffer) noexcept
See get_uint16() for reference.
constexpr bool has_endian_big_v
Value access of big-endian type trait for convenience .
constexpr uint128dp_t get_uint128(uint8_t const *buffer) noexcept
See get_uint16() for reference.
constexpr int64_t get_int64(uint8_t const *buffer) noexcept
See get_int16() for reference.
constexpr T get_packed_value(const packed_t< T > *source, const lb_endian_t byte_order) noexcept
Return packed_t::store after converting it to from either lb_endian::little or lb_endian::big dependi...
constexpr bool is_little_endian() noexcept
Evaluates true if platform is running in little endian mode, i.e.
constexpr bool is_big_endian() noexcept
Evaluates true if platform is running in big endian mode, i.e.
constexpr std::enable_if_t< std::is_standard_layout_v< T >, T > get_value(uint8_t const *buffer) noexcept
Returns a T value from the given byte address using packed_t to resolve a potential memory alignment ...
constexpr void put_uint256(uint8_t *buffer, const uint256dp_t &v) noexcept
See put_uint16() for reference.
constexpr bool is_defined_endian(const endian_t &v) noexcept
Evaluates true if the given endian is defined, i.e.
constexpr uint16_t get_uint16(uint8_t const *buffer) noexcept
Returns a uint16_t value from the given byte address using packed_t to resolve a potential memory ali...
constexpr uint16_t bswap(uint16_t const source) noexcept
constexpr bool is_little_or_big_endian() noexcept
Evaluates true if platform is running in little or big endian mode, i.e.
constexpr uint16_t cpu_to_be(uint16_t const h) noexcept
constexpr endian_t to_endian(const lb_endian_t v) noexcept
constexpr uint32_t get_uint32(uint8_t const *buffer) noexcept
See get_uint16() for reference.
char * cast_uint8_ptr_to_char(uint8_t *b) noexcept
constexpr void put_uint32(uint8_t *buffer, const uint32_t v) noexcept
See put_uint16() for reference.
constexpr int16_t get_int16(uint8_t const *buffer) noexcept
Returns a int16_t value from the given byte address using packed_t to resolve a potential memory alig...
endian_t
Endian identifier, indicating endianess of all scalar types.
constexpr int32_t get_int32(uint8_t const *buffer) noexcept
See get_int16() for reference.
constexpr uint256dp_t get_uint256(uint8_t const *buffer) noexcept
See get_uint16() for reference.
constexpr void put_uint192(uint8_t *buffer, const uint192dp_t &v) noexcept
See put_uint16() for reference.
constexpr bool has_endian_little_v
Value access of little-endian type trait for convenience .
constexpr void put_int16(uint8_t *buffer, const int16_t v) noexcept
Put the given int16_t value into the given byte address using packed_t to resolve a potential memory ...
constexpr uint16_t le_to_cpu(uint16_t const l) noexcept
constexpr void put_uint8(uint8_t *buffer, const uint8_t v) noexcept
lb_endian_t
Simplified reduced endian type only covering little- and big-endian.
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
constexpr void put_uint64(uint8_t *buffer, const uint64_t &v) noexcept
See put_uint16() for reference.
constexpr void put_uint16(uint8_t *buffer, const uint16_t v) noexcept
Put the given uint16_t value into the given byte address using packed_t to resolve a potential memory...
constexpr uint16_t be_to_cpu(uint16_t const n) noexcept
constexpr void put_uint128(uint8_t *buffer, const uint128dp_t &v) noexcept
See put_uint16() for reference.
constexpr std::enable_if_t< std::is_standard_layout_v< T >, void > put_value(uint8_t *buffer, const T &v) noexcept
Put the given T value into the given byte address using packed_t to resolve a potential memory alignm...
constexpr lb_endian_t to_lb_endian(const endian_t v) noexcept
constexpr void put_int64(uint8_t *buffer, const int64_t &v) noexcept
See put_int16() for reference.
constexpr uint16_t cpu_to_le(uint16_t const h) noexcept
constexpr void put_int32(uint8_t *buffer, const int32_t v) noexcept
See put_int16() for reference.
constexpr uint64_t get_uint64(uint8_t const *buffer) noexcept
See get_uint16() for reference.
constexpr int8_t get_int8(uint8_t const *buffer) noexcept
constexpr uint8_t get_uint8(uint8_t const *buffer) noexcept
const uint8_t * cast_char_ptr_to_uint8(const char *s) noexcept
@ pdp
Identifier for DEC PDP-11, aka ENDIAN_LITTLE_WORD.
@ undefined
Undetermined endian.
@ native
Identifier for native platform type, one of the above.
@ little
Identifier for little endian.
@ honeywell
Identifier for Honeywell 316, aka ENDIAN_BIG_WORD.
@ big
Identifier for big endian.
@ little
Identifier for little endian, equivalent to endian::little.
@ big
Identifier for big endian, equivalent to endian::big.
constexpr std::enable_if_t< sizeof(Dest)==sizeof(Source) &&std::is_trivially_copyable_v< Dest > &&std::is_trivially_copyable_v< Source >, Dest > bit_cast(const Source &src) noexcept
C++20 bit_cast<>(arg) implementation for C++17.
#define PRAGMA_DISABLE_WARNING_PUSH
consteval_cxx20 bool has_builtin_bit_cast() noexcept
Query whether __builtin_bit_cast(Dest_type, arg) is available via __has_builtin(__builtin_bit_cast).
constexpr std::enable_if_t< sizeof(Dest)==sizeof(Source) &&std::is_pointer_v< Source > &&std::is_pointer_v< Dest >, Dest > pointer_cast(const Source &src) noexcept
A constexpr pointer cast implementation for C++17, inspired by C++20 bit_cast<>(arg).
#define PRAGMA_DISABLE_WARNING_MULTICHAR
#define PRAGMA_DISABLE_WARNING_POP
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
constexpr uint32_t get_host_order() noexcept
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
A big-endian type trait for convenience .
A little-endian type trait for convenience .
Support aligned memory transfer from and to potentially unaligned memory.
A 128-bit packed uint8_t data array.
A 196-bit packed uint8_t data array.
A 256-bit packed uint8_t data array.