make byte functions more concise

v2
Brett 2024-12-09 16:00:08 -05:00
parent d32b5d398a
commit d1a9aab859
3 changed files with 182 additions and 174 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake)
set(BLT_VERSION 2.1.11)
set(BLT_VERSION 2.1.12)
set(BLT_TARGET BLT)

View File

@ -28,29 +28,29 @@
#if defined(__clang__) || defined(__llvm__) || defined(__GNUC__) || defined(__GNUG__)
#if (defined(__GNUC__) || defined(__GNUG__)) && !defined(WIN32)
#if (defined(__GNUC__) || defined(__GNUG__)) && !defined(WIN32)
#include <byteswap.h>
#include <byteswap.h>
#define SWAP16(val) bswap_16(val)
#define SWAP32(val) bswap_32(val)
#define SWAP64(val) bswap_64(val)
#else
#define SWAP16(val) bswap_16(val)
#define SWAP32(val) bswap_32(val)
#define SWAP64(val) bswap_64(val)
#else
#define SWAP16(val) __builtin_bswap16(val)
#define SWAP32(val) __builtin_bswap32(val)
#define SWAP64(val) __builtin_bswap64(val)
#endif
#endif
#if __cplusplus >= 202002L
#if __cplusplus >= 202002L
#include <bit>
#define ENDIAN_LOOKUP(little_endian) (std::endian::native == std::endian::little && !little_endian) || \
(std::endian::native == std::endian::big && little_endian)
#else
#define ENDIAN_LOOKUP(little_endian) !little_endian
#endif
#else
#define ENDIAN_LOOKUP(little_endian) !little_endian
#endif
#elif defined(_MSC_VER)
#include <intrin.h>
#define SWAP16(val) _byteswap_ushort(val)
@ -61,13 +61,43 @@
namespace blt::mem
{
template <typename R, typename T>
inline static R type_cast(T type)
{
static_assert(std::is_trivially_copyable_v<T>, "Type must be trivially copyable to be type casted!");
static_assert(sizeof(T) == sizeof(R));
R r;
std::memcpy(&r, &type, sizeof(type));
return r;
}
template <typename T>
inline void reverse(T& out)
{
static_assert(std::is_trivially_copyable_v<T>, "Type must be trivially copyable to be reversible!");
// if we need to swap find the best way to do so
if constexpr (std::is_same_v<T, int16_t> || std::is_same_v<T, uint16_t>)
out = type_cast<T>(SWAP16(type_cast<uint16_t>(out)));
else if constexpr (std::is_same_v<T, int32_t> || std::is_same_v<T, uint32_t> || std::is_same_v<T, f32>)
out = type_cast<T>(SWAP32(type_cast<uint32_t>(out)));
else if constexpr (std::is_same_v<T, int64_t> || std::is_same_v<T, uint64_t> || std::is_same_v<T, f64>)
out = type_cast<T>(SWAP64(type_cast<uint64_t>(out)));
else
{
std::array<std::byte, sizeof(T)> data;
std::memcpy(data.data(), &out, sizeof(T));
std::reverse(data.begin(), data.end());
std::memcpy(&out, data.data(), sizeof(T));
}
}
// Used to grab the byte-data of any T element. Defaults to Big Endian, however can be configured to use little endian
template<bool little_endian = false, typename BYTE_TYPE, typename T>
inline static int toBytes(const T& in, BYTE_TYPE* out)
template <bool little_endian = false, typename BYTE_TYPE, typename T>
inline int toBytes(const T& in, BYTE_TYPE* out)
{
if constexpr (!(std::is_same_v<BYTE_TYPE, std::int8_t> || std::is_same_v<BYTE_TYPE, std::uint8_t>))
static_assert("Must provide a signed/unsigned int8 type");
std::memcpy(out, (void*) &in, sizeof(T));
std::memcpy(out, &in, sizeof(T));
if constexpr (ENDIAN_LOOKUP(little_endian))
{
@ -80,35 +110,22 @@ namespace blt::mem
}
// Used to cast the binary data of any T object, into a T object. Assumes data is in big ending (configurable)
template<bool little_endian = false, typename BYTE_TYPE, typename T>
inline static int fromBytes(const BYTE_TYPE* in, T& out)
template <bool little_endian = false, typename BYTE_TYPE, typename T>
inline int fromBytes(const BYTE_TYPE* in, T& out)
{
if constexpr (!(std::is_same_v<BYTE_TYPE, std::int8_t> || std::is_same_v<BYTE_TYPE, std::uint8_t>))
static_assert("Must provide a signed/unsigned int8 type");
std::array<BYTE_TYPE, sizeof(T)> data;
std::memcpy(data.data(), in, sizeof(T));
std::memcpy(&out, in, sizeof(T));
if constexpr (ENDIAN_LOOKUP(little_endian))
{
// if we need to swap find the best way to do so
if constexpr (std::is_same_v<T, int16_t> || std::is_same_v<T, uint16_t>)
out = SWAP16(*reinterpret_cast<T*>(data.data()));
else if constexpr (std::is_same_v<T, int32_t> || std::is_same_v<T, uint32_t>)
out = SWAP32(*reinterpret_cast<T*>(data.data()));
else if constexpr (std::is_same_v<T, int64_t> || std::is_same_v<T, uint64_t>)
out = SWAP64(*reinterpret_cast<T*>(data.data()));
else
{
std::reverse(data.begin(), data.end());
out = *reinterpret_cast<T*>(data.data());
}
reverse(out);
}
return 0;
}
template<bool little_endian = false, typename BYTE_TYPE, typename T>
template <bool little_endian = false, typename BYTE_TYPE, typename T>
inline static int fromBytes(const BYTE_TYPE* in, T* out)
{
return fromBytes(in, *out);
@ -122,21 +139,11 @@ namespace blt::mem
return prev_size * 2;
return prev_size + default_allocation_block;
}
template<typename R, typename T>
inline static R type_cast(T type)
{
static_assert(sizeof(T) == sizeof(R));
R r;
std::memcpy(&r, &type, sizeof(type));
return r;
}
}
namespace blt
{
template<typename V>
template <typename V>
struct ptr_iterator
{
public:
@ -148,7 +155,8 @@ namespace blt
using iter_reference = ptr_iterator&;
explicit ptr_iterator(V* v): _v(v)
{}
{
}
reference operator*() const
{

@ -1 +1 @@
Subproject commit 2d0b499273ea02f7211ae2af705418c9509691fb
Subproject commit 0bad0987f1989294e73cdb1ebf41c75f49cdb0c1