/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ #ifndef MPT_ENDIAN_INTEGER_HPP #define MPT_ENDIAN_INTEGER_HPP #include "mpt/base/detect.hpp" #include "mpt/base/bit.hpp" #include "mpt/base/integer.hpp" #include "mpt/base/macros.hpp" #include "mpt/base/memory.hpp" #include "mpt/base/namespace.hpp" #include "mpt/endian/type_traits.hpp" #include #include #include #include #include #include namespace mpt { inline namespace MPT_INLINE_NS { template MPT_CONSTEXPRINLINE std::array EndianEncodeImpl(base_type val) noexcept { static_assert(endian == mpt::endian::little || endian == mpt::endian::big); static_assert(std::numeric_limits::is_integer); using unsigned_int_type = typename std::make_unsigned::type; unsigned_int_type uval = static_cast(static_cast(val)); std::array data{}; if constexpr (endian == mpt::endian::little) { for (std::size_t i = 0; i < sizeof(base_type); ++i) { data[i] = static_cast(static_cast((uval >> (i * 8)) & 0xffu)); } } else { for (std::size_t i = 0; i < sizeof(base_type); ++i) { data[(sizeof(base_type) - 1) - i] = static_cast(static_cast((uval >> (i * 8)) & 0xffu)); } } return data; } template MPT_CONSTEXPRINLINE std::array EndianEncode(base_type val) noexcept { return EndianEncodeImpl(val); } template MPT_CONSTEXPRINLINE base_type EndianDecodeImpl(std::array data) noexcept { static_assert(endian == mpt::endian::little || endian == mpt::endian::big); static_assert(std::numeric_limits::is_integer); using unsigned_int_type = typename std::make_unsigned::type; base_type val = base_type(); unsigned_int_type uval = unsigned_int_type(); if constexpr (endian == mpt::endian::little) { for (std::size_t i = 0; i < sizeof(base_type); ++i) { uval |= static_cast(static_cast(static_cast(data[i])) << (i * 8)); } } else { for (std::size_t i = 0; i < sizeof(base_type); ++i) { uval |= static_cast(static_cast(static_cast(data[(sizeof(base_type) - 1) - i])) << (i * 8)); } } val = static_cast(static_cast(uval)); return val; } template MPT_CONSTEXPRINLINE base_type EndianDecode(std::array data) noexcept { return EndianDecodeImpl(data); } template struct packed_int_type { using type = T; }; // On-disk integer types with defined endianness and no alignemnt requirements // Note: To easily debug module loaders (and anything else that uses this // wrapper struct), you can use the Debugger Visualizers available in // build/vs/debug/ to conveniently view the wrapped contents. template ::type> struct packed { public: using base_type = T; using int_type = Tint; public: std::array data; public: MPT_CONSTEXPR20_FUN void set(base_type val) noexcept { static_assert(std::numeric_limits::is_integer); MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) { if constexpr (endian == mpt::endian::big) { typename std::make_unsigned::type uval = val; for (std::size_t i = 0; i < sizeof(base_type); ++i) { data[i] = static_cast((uval >> (8 * (sizeof(base_type) - 1 - i))) & 0xffu); } } else { typename std::make_unsigned::type uval = val; for (std::size_t i = 0; i < sizeof(base_type); ++i) { data[i] = static_cast((uval >> (8 * i)) & 0xffu); } } } else { if constexpr (std::is_integral::value && (mpt::endian::native == mpt::endian::little || mpt::endian::native == mpt::endian::big)) { if constexpr (mpt::endian::native != endian) { val = mpt::byteswap(val); } std::memcpy(data.data(), &val, sizeof(val)); } else { data = EndianEncode(val); } } } MPT_CONSTEXPR20_FUN base_type get() const noexcept { static_assert(std::numeric_limits::is_integer); MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) { if constexpr (endian == mpt::endian::big) { typename std::make_unsigned::type uval = 0; for (std::size_t i = 0; i < sizeof(base_type); ++i) { uval |= static_cast::type>(static_cast::type>(data[i]) << (8 * (sizeof(base_type) - 1 - i))); } return static_cast(uval); } else { typename std::make_unsigned::type uval = 0; for (std::size_t i = 0; i < sizeof(base_type); ++i) { uval |= static_cast::type>(static_cast::type>(data[i]) << (8 * i)); } return static_cast(uval); } } else { if constexpr (std::is_integral::value && (mpt::endian::native == mpt::endian::little || mpt::endian::native == mpt::endian::big)) { base_type val = base_type(); std::memcpy(&val, data.data(), sizeof(val)); if constexpr (mpt::endian::native != endian) { val = mpt::byteswap(val); } return val; } else { return EndianDecode(data); } } } MPT_CONSTEXPR20_FUN packed & operator=(const base_type & val) noexcept { set(val); return *this; } MPT_CONSTEXPR20_FUN operator base_type() const noexcept { return get(); } public: MPT_CONSTEXPR20_FUN packed & operator&=(base_type val) noexcept { set(get() & val); return *this; } MPT_CONSTEXPR20_FUN packed & operator|=(base_type val) noexcept { set(get() | val); return *this; } MPT_CONSTEXPR20_FUN packed & operator^=(base_type val) noexcept { set(get() ^ val); return *this; } MPT_CONSTEXPR20_FUN packed & operator+=(base_type val) noexcept { set(get() + val); return *this; } MPT_CONSTEXPR20_FUN packed & operator-=(base_type val) noexcept { set(get() - val); return *this; } MPT_CONSTEXPR20_FUN packed & operator*=(base_type val) noexcept { set(get() * val); return *this; } MPT_CONSTEXPR20_FUN packed & operator/=(base_type val) noexcept { set(get() / val); return *this; } MPT_CONSTEXPR20_FUN packed & operator%=(base_type val) noexcept { set(get() % val); return *this; } MPT_CONSTEXPR20_FUN packed & operator++() noexcept { // prefix set(get() + 1); return *this; } MPT_CONSTEXPR20_FUN packed & operator--() noexcept { // prefix set(get() - 1); return *this; } MPT_CONSTEXPR20_FUN base_type operator++(int) noexcept { // postfix base_type old = get(); set(old + 1); return old; } MPT_CONSTEXPR20_FUN base_type operator--(int) noexcept { // postfix base_type old = get(); set(old - 1); return old; } }; using int64le = packed; using int32le = packed; using int16le = packed; using int8le = packed; using uint64le = packed; using uint32le = packed; using uint16le = packed; using uint8le = packed; using int64be = packed; using int32be = packed; using int16be = packed; using int8be = packed; using uint64be = packed; using uint32be = packed; using uint16be = packed; using uint8be = packed; constexpr bool declare_binary_safe(const int64le &) { return true; } constexpr bool declare_binary_safe(const int32le &) { return true; } constexpr bool declare_binary_safe(const int16le &) { return true; } constexpr bool declare_binary_safe(const int8le &) { return true; } constexpr bool declare_binary_safe(const uint64le &) { return true; } constexpr bool declare_binary_safe(const uint32le &) { return true; } constexpr bool declare_binary_safe(const uint16le &) { return true; } constexpr bool declare_binary_safe(const uint8le &) { return true; } constexpr bool declare_binary_safe(const int64be &) { return true; } constexpr bool declare_binary_safe(const int32be &) { return true; } constexpr bool declare_binary_safe(const int16be &) { return true; } constexpr bool declare_binary_safe(const int8be &) { return true; } constexpr bool declare_binary_safe(const uint64be &) { return true; } constexpr bool declare_binary_safe(const uint32be &) { return true; } constexpr bool declare_binary_safe(const uint16be &) { return true; } constexpr bool declare_binary_safe(const uint8be &) { return true; } static_assert(mpt::check_binary_size(8)); static_assert(mpt::check_binary_size(4)); static_assert(mpt::check_binary_size(2)); static_assert(mpt::check_binary_size(1)); static_assert(mpt::check_binary_size(8)); static_assert(mpt::check_binary_size(4)); static_assert(mpt::check_binary_size(2)); static_assert(mpt::check_binary_size(1)); static_assert(mpt::check_binary_size(8)); static_assert(mpt::check_binary_size(4)); static_assert(mpt::check_binary_size(2)); static_assert(mpt::check_binary_size(1)); static_assert(mpt::check_binary_size(8)); static_assert(mpt::check_binary_size(4)); static_assert(mpt::check_binary_size(2)); static_assert(mpt::check_binary_size(1)); template struct make_endian { using type = packed::type, mpt::endian::little>; }; template struct make_endian { using type = packed::type, mpt::endian::big>; }; template MPT_CONSTEXPR20_FUN auto as_le(T v) noexcept -> typename mpt::make_le::type>::type { typename mpt::make_le::type>::type res{}; res = v; return res; } template MPT_CONSTEXPR20_FUN auto as_be(T v) noexcept -> typename mpt::make_be::type>::type { typename mpt::make_be::type>::type res{}; res = v; return res; } template MPT_CONSTEXPR20_FUN Tpacked as_endian(typename Tpacked::base_type v) noexcept { Tpacked res{}; res = v; return res; } } // namespace MPT_INLINE_NS } // namespace mpt namespace std { template class numeric_limits> : public std::numeric_limits { }; template class numeric_limits> : public std::numeric_limits { }; } // namespace std #endif // MPT_ENDIAN_INTEGER_HPP