/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ #ifndef MPT_ENDIAN_INT24_HPP #define MPT_ENDIAN_INT24_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/integer.hpp" #include "mpt/endian/type_traits.hpp" #include #include #include #include namespace mpt { inline namespace MPT_INLINE_NS { struct uint24 { std::array bytes; uint24() = default; template ::value, bool>::type = true> explicit uint24(T other) noexcept { using Tunsigned = typename std::make_unsigned::type; MPT_MAYBE_CONSTANT_IF (mpt::endian_is_big()) { bytes[0] = mpt::byte_cast(static_cast((static_cast(other) >> 16) & 0xff)); bytes[1] = mpt::byte_cast(static_cast((static_cast(other) >> 8) & 0xff)); bytes[2] = mpt::byte_cast(static_cast((static_cast(other) >> 0) & 0xff)); } else { bytes[0] = mpt::byte_cast(static_cast((static_cast(other) >> 0) & 0xff)); bytes[1] = mpt::byte_cast(static_cast((static_cast(other) >> 8) & 0xff)); bytes[2] = mpt::byte_cast(static_cast((static_cast(other) >> 16) & 0xff)); } } operator unsigned int() const noexcept { MPT_MAYBE_CONSTANT_IF (mpt::endian_is_big()) { return (mpt::byte_cast(bytes[0]) * 65536) + (mpt::byte_cast(bytes[1]) * 256) + mpt::byte_cast(bytes[2]); } else { return (mpt::byte_cast(bytes[2]) * 65536) + (mpt::byte_cast(bytes[1]) * 256) + mpt::byte_cast(bytes[0]); } } friend bool operator==(uint24 a, uint24 b) noexcept { return static_cast(a) == static_cast(b); } friend bool operator!=(uint24 a, uint24 b) noexcept { return static_cast(a) != static_cast(b); } }; static_assert(sizeof(uint24) == 3); struct int24 { std::array bytes; int24() = default; template ::value, bool>::type = true> explicit int24(T other) noexcept { using Tunsigned = typename std::make_unsigned::type; MPT_MAYBE_CONSTANT_IF (mpt::endian_is_big()) { bytes[0] = mpt::byte_cast(static_cast((static_cast(other) >> 16) & 0xff)); bytes[1] = mpt::byte_cast(static_cast((static_cast(other) >> 8) & 0xff)); bytes[2] = mpt::byte_cast(static_cast((static_cast(other) >> 0) & 0xff)); } else { bytes[0] = mpt::byte_cast(static_cast((static_cast(other) >> 0) & 0xff)); bytes[1] = mpt::byte_cast(static_cast((static_cast(other) >> 8) & 0xff)); bytes[2] = mpt::byte_cast(static_cast((static_cast(other) >> 16) & 0xff)); } } operator int() const noexcept { MPT_MAYBE_CONSTANT_IF (mpt::endian_is_big()) { return (static_cast(mpt::byte_cast(bytes[0])) * 65536) + (mpt::byte_cast(bytes[1]) * 256) + mpt::byte_cast(bytes[2]); } else { return (static_cast(mpt::byte_cast(bytes[2])) * 65536) + (mpt::byte_cast(bytes[1]) * 256) + mpt::byte_cast(bytes[0]); } } friend bool operator==(int24 a, int24 b) noexcept { return static_cast(a) == static_cast(b); } friend bool operator!=(int24 a, int24 b) noexcept { return static_cast(a) != static_cast(b); } }; static_assert(sizeof(int24) == 3); template <> struct packed_int_type { using type = int32; }; template <> struct packed_int_type { using type = uint32; }; template MPT_CONSTEXPRINLINE std::array EndianEncode24(base_type val) noexcept { static_assert(std::is_same::value || std::is_same::value); static_assert(endian == mpt::endian::little || endian == mpt::endian::big); static_assert(std::numeric_limits::is_integer); if constexpr (endian == mpt::endian::native) { std::array data{}; for (std::size_t i = 0; i < sizeof(base_type); ++i) { data[i] = val.bytes[i]; } return data; } else { return EndianEncodeImpl(val); } } template MPT_CONSTEXPRINLINE base_type EndianDecode24(std::array data) noexcept { static_assert(std::is_same::value || std::is_same::value); static_assert(endian == mpt::endian::little || endian == mpt::endian::big); static_assert(std::numeric_limits::is_integer); if constexpr (endian == mpt::endian::native) { base_type val = base_type(); for (std::size_t i = 0; i < sizeof(base_type); ++i) { val.bytes[i] = data[i]; } return val; } else { return EndianDecodeImpl(data); } } template <> MPT_CONSTEXPRINLINE std::array EndianEncode(int24 val) noexcept { return mpt::EndianEncode24(val); } template <> MPT_CONSTEXPRINLINE int24 EndianDecode(std::array data) noexcept { return mpt::EndianDecode24(data); } template <> MPT_CONSTEXPRINLINE std::array EndianEncode(int24 val) noexcept { return mpt::EndianEncode24(val); } template <> MPT_CONSTEXPRINLINE int24 EndianDecode(std::array data) noexcept { return mpt::EndianDecode24(data); } template <> MPT_CONSTEXPRINLINE std::array EndianEncode(uint24 val) noexcept { return mpt::EndianEncode24(val); } template <> MPT_CONSTEXPRINLINE uint24 EndianDecode(std::array data) noexcept { return mpt::EndianDecode24(data); } template <> MPT_CONSTEXPRINLINE std::array EndianEncode(uint24 val) noexcept { return mpt::EndianEncode24(val); } template <> MPT_CONSTEXPRINLINE uint24 EndianDecode(std::array data) noexcept { return mpt::EndianDecode24(data); } using int24le = packed; using uint24le = packed; using int24be = packed; using uint24be = packed; constexpr bool declare_binary_safe(const int24le &) { return true; } constexpr bool declare_binary_safe(const uint24le &) { return true; } constexpr bool declare_binary_safe(const int24be &) { return true; } constexpr bool declare_binary_safe(const uint24be &) { return true; } static_assert(mpt::check_binary_size(3)); static_assert(mpt::check_binary_size(3)); static_assert(mpt::check_binary_size(3)); static_assert(mpt::check_binary_size(3)); } // namespace MPT_INLINE_NS } // namespace mpt #if !defined(CPPCHECK) // work-around crash in cppcheck 2.4.1 namespace std { template <> class numeric_limits : public std::numeric_limits { public: static constexpr mpt::uint32 min() noexcept { return 0; } static constexpr mpt::uint32 lowest() noexcept { return 0; } static constexpr mpt::uint32 max() noexcept { return 0x00ffffff; } }; template <> class numeric_limits : public std::numeric_limits { public: static constexpr mpt::int32 min() noexcept { return 0 - 0x00800000; } static constexpr mpt::int32 lowest() noexcept { return 0 - 0x00800000; } static constexpr mpt::int32 max() noexcept { return 0 + 0x007fffff; } }; } // namespace std #endif // !CPPCHECK #endif // MPT_ENDIAN_INT24_HPP