| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_ENDIAN_INT24_HPP |
| 4 |
#define MPT_ENDIAN_INT24_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/bit.hpp" |
| 9 |
#include "mpt/base/integer.hpp" |
| 10 |
#include "mpt/base/macros.hpp" |
| 11 |
#include "mpt/base/memory.hpp" |
| 12 |
#include "mpt/base/namespace.hpp" |
| 13 |
#include "mpt/endian/integer.hpp" |
| 14 |
#include "mpt/endian/type_traits.hpp" |
| 15 |
|
| 16 |
#include <array> |
| 17 |
#include <limits> |
| 18 |
#include <type_traits> |
| 19 |
|
| 20 |
#include <cstddef> |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace mpt { |
| 25 |
inline namespace MPT_INLINE_NS { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
struct uint24 { |
| 30 |
std::array<std::byte, 3> bytes; |
| 31 |
uint24() = default; |
| 32 |
template <typename T, typename std::enable_if<std::is_integral<T>::value, bool>::type = true> |
| 33 |
explicit uint24(T other) noexcept { |
| 34 |
using Tunsigned = typename std::make_unsigned<T>::type; |
| 35 |
MPT_MAYBE_CONSTANT_IF (mpt::endian_is_big()) { |
| 36 |
bytes[0] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 16) & 0xff)); |
| 37 |
bytes[1] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 8) & 0xff)); |
| 38 |
bytes[2] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 0) & 0xff)); |
| 39 |
} else { |
| 40 |
bytes[0] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 0) & 0xff)); |
| 41 |
bytes[1] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 8) & 0xff)); |
| 42 |
bytes[2] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 16) & 0xff)); |
| 43 |
} |
| 44 |
} |
| 45 |
operator unsigned int() const noexcept { |
| 46 |
MPT_MAYBE_CONSTANT_IF (mpt::endian_is_big()) { |
| 47 |
return (mpt::byte_cast<uint8>(bytes[0]) * 65536) + (mpt::byte_cast<uint8>(bytes[1]) * 256) + mpt::byte_cast<uint8>(bytes[2]); |
| 48 |
} else { |
| 49 |
return (mpt::byte_cast<uint8>(bytes[2]) * 65536) + (mpt::byte_cast<uint8>(bytes[1]) * 256) + mpt::byte_cast<uint8>(bytes[0]); |
| 50 |
} |
| 51 |
} |
| 52 |
friend bool operator==(uint24 a, uint24 b) noexcept { |
| 53 |
return static_cast<unsigned int>(a) == static_cast<unsigned int>(b); |
| 54 |
} |
| 55 |
friend bool operator!=(uint24 a, uint24 b) noexcept { |
| 56 |
return static_cast<unsigned int>(a) != static_cast<unsigned int>(b); |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
static_assert(sizeof(uint24) == 3); |
| 61 |
|
| 62 |
|
| 63 |
struct int24 { |
| 64 |
std::array<std::byte, 3> bytes; |
| 65 |
int24() = default; |
| 66 |
template <typename T, typename std::enable_if<std::is_integral<T>::value, bool>::type = true> |
| 67 |
explicit int24(T other) noexcept { |
| 68 |
using Tunsigned = typename std::make_unsigned<T>::type; |
| 69 |
MPT_MAYBE_CONSTANT_IF (mpt::endian_is_big()) { |
| 70 |
bytes[0] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 16) & 0xff)); |
| 71 |
bytes[1] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 8) & 0xff)); |
| 72 |
bytes[2] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 0) & 0xff)); |
| 73 |
} else { |
| 74 |
bytes[0] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 0) & 0xff)); |
| 75 |
bytes[1] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 8) & 0xff)); |
| 76 |
bytes[2] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 16) & 0xff)); |
| 77 |
} |
| 78 |
} |
| 79 |
operator int() const noexcept { |
| 80 |
MPT_MAYBE_CONSTANT_IF (mpt::endian_is_big()) { |
| 81 |
return (static_cast<int8>(mpt::byte_cast<uint8>(bytes[0])) * 65536) + (mpt::byte_cast<uint8>(bytes[1]) * 256) + mpt::byte_cast<uint8>(bytes[2]); |
| 82 |
} else { |
| 83 |
return (static_cast<int8>(mpt::byte_cast<uint8>(bytes[2])) * 65536) + (mpt::byte_cast<uint8>(bytes[1]) * 256) + mpt::byte_cast<uint8>(bytes[0]); |
| 84 |
} |
| 85 |
} |
| 86 |
friend bool operator==(int24 a, int24 b) noexcept { |
| 87 |
return static_cast<int>(a) == static_cast<int>(b); |
| 88 |
} |
| 89 |
friend bool operator!=(int24 a, int24 b) noexcept { |
| 90 |
return static_cast<int>(a) != static_cast<int>(b); |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
static_assert(sizeof(int24) == 3); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
template <> |
| 99 |
struct packed_int_type<int24> { |
| 100 |
using type = int32; |
| 101 |
}; |
| 102 |
template <> |
| 103 |
struct packed_int_type<uint24> { |
| 104 |
using type = uint32; |
| 105 |
}; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
template <typename base_type, mpt::endian endian, typename int_type = base_type> |
| 110 |
MPT_CONSTEXPRINLINE std::array<std::byte, sizeof(base_type)> EndianEncode24(base_type val) noexcept { |
| 111 |
static_assert(std::is_same<base_type, int24>::value || std::is_same<base_type, uint24>::value); |
| 112 |
static_assert(endian == mpt::endian::little || endian == mpt::endian::big); |
| 113 |
static_assert(std::numeric_limits<int_type>::is_integer); |
| 114 |
if constexpr (endian == mpt::endian::native) { |
| 115 |
std::array<std::byte, sizeof(base_type)> data{}; |
| 116 |
for (std::size_t i = 0; i < sizeof(base_type); ++i) { |
| 117 |
data[i] = val.bytes[i]; |
| 118 |
} |
| 119 |
return data; |
| 120 |
} else { |
| 121 |
return EndianEncodeImpl<base_type, endian, int_type>(val); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
template <typename base_type, mpt::endian endian, typename int_type = base_type> |
| 126 |
MPT_CONSTEXPRINLINE base_type EndianDecode24(std::array<std::byte, sizeof(base_type)> data) noexcept { |
| 127 |
static_assert(std::is_same<base_type, int24>::value || std::is_same<base_type, uint24>::value); |
| 128 |
static_assert(endian == mpt::endian::little || endian == mpt::endian::big); |
| 129 |
static_assert(std::numeric_limits<int_type>::is_integer); |
| 130 |
if constexpr (endian == mpt::endian::native) { |
| 131 |
base_type val = base_type(); |
| 132 |
for (std::size_t i = 0; i < sizeof(base_type); ++i) { |
| 133 |
val.bytes[i] = data[i]; |
| 134 |
} |
| 135 |
return val; |
| 136 |
} else { |
| 137 |
return EndianDecodeImpl<base_type, endian, int_type>(data); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
template <> |
| 144 |
MPT_CONSTEXPRINLINE std::array<std::byte, 3> EndianEncode<int24, mpt::endian::little, int32>(int24 val) noexcept { |
| 145 |
return mpt::EndianEncode24<int24, mpt::endian::little, int32>(val); |
| 146 |
} |
| 147 |
template <> |
| 148 |
MPT_CONSTEXPRINLINE int24 EndianDecode<int24, mpt::endian::little, int32>(std::array<std::byte, 3> data) noexcept { |
| 149 |
return mpt::EndianDecode24<int24, mpt::endian::little, int32>(data); |
| 150 |
} |
| 151 |
template <> |
| 152 |
MPT_CONSTEXPRINLINE std::array<std::byte, 3> EndianEncode<int24, mpt::endian::big, int32>(int24 val) noexcept { |
| 153 |
return mpt::EndianEncode24<int24, mpt::endian::big, int32>(val); |
| 154 |
} |
| 155 |
template <> |
| 156 |
MPT_CONSTEXPRINLINE int24 EndianDecode<int24, mpt::endian::big, int32>(std::array<std::byte, 3> data) noexcept { |
| 157 |
return mpt::EndianDecode24<int24, mpt::endian::big, int32>(data); |
| 158 |
} |
| 159 |
|
| 160 |
template <> |
| 161 |
MPT_CONSTEXPRINLINE std::array<std::byte, 3> EndianEncode<uint24, mpt::endian::little, uint32>(uint24 val) noexcept { |
| 162 |
return mpt::EndianEncode24<uint24, mpt::endian::little, uint32>(val); |
| 163 |
} |
| 164 |
template <> |
| 165 |
MPT_CONSTEXPRINLINE uint24 EndianDecode<uint24, mpt::endian::little, uint32>(std::array<std::byte, 3> data) noexcept { |
| 166 |
return mpt::EndianDecode24<uint24, mpt::endian::little, uint32>(data); |
| 167 |
} |
| 168 |
template <> |
| 169 |
MPT_CONSTEXPRINLINE std::array<std::byte, 3> EndianEncode<uint24, mpt::endian::big, uint32>(uint24 val) noexcept { |
| 170 |
return mpt::EndianEncode24<uint24, mpt::endian::big, uint32>(val); |
| 171 |
} |
| 172 |
template <> |
| 173 |
MPT_CONSTEXPRINLINE uint24 EndianDecode<uint24, mpt::endian::big, uint32>(std::array<std::byte, 3> data) noexcept { |
| 174 |
return mpt::EndianDecode24<uint24, mpt::endian::big, uint32>(data); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
using int24le = packed<int24, mpt::endian::little>; |
| 180 |
using uint24le = packed<uint24, mpt::endian::little>; |
| 181 |
|
| 182 |
using int24be = packed<int24, mpt::endian::big>; |
| 183 |
using uint24be = packed<uint24, mpt::endian::big>; |
| 184 |
|
| 185 |
constexpr bool declare_binary_safe(const int24le &) { |
| 186 |
return true; |
| 187 |
} |
| 188 |
constexpr bool declare_binary_safe(const uint24le &) { |
| 189 |
return true; |
| 190 |
} |
| 191 |
|
| 192 |
constexpr bool declare_binary_safe(const int24be &) { |
| 193 |
return true; |
| 194 |
} |
| 195 |
constexpr bool declare_binary_safe(const uint24be &) { |
| 196 |
return true; |
| 197 |
} |
| 198 |
|
| 199 |
static_assert(mpt::check_binary_size<int24le>(3)); |
| 200 |
static_assert(mpt::check_binary_size<uint24le>(3)); |
| 201 |
|
| 202 |
static_assert(mpt::check_binary_size<int24be>(3)); |
| 203 |
static_assert(mpt::check_binary_size<uint24be>(3)); |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
} // namespace MPT_INLINE_NS |
| 208 |
} // namespace mpt |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
#if !defined(CPPCHECK) |
| 213 |
// work-around crash in cppcheck 2.4.1 |
| 214 |
namespace std { |
| 215 |
template <> |
| 216 |
class numeric_limits<mpt::uint24> : public std::numeric_limits<mpt::uint32> { |
| 217 |
public: |
| 218 |
static constexpr mpt::uint32 min() noexcept { |
| 219 |
return 0; |
| 220 |
} |
| 221 |
static constexpr mpt::uint32 lowest() noexcept { |
| 222 |
return 0; |
| 223 |
} |
| 224 |
static constexpr mpt::uint32 max() noexcept { |
| 225 |
return 0x00ffffff; |
| 226 |
} |
| 227 |
}; |
| 228 |
template <> |
| 229 |
class numeric_limits<mpt::int24> : public std::numeric_limits<mpt::int32> { |
| 230 |
public: |
| 231 |
static constexpr mpt::int32 min() noexcept { |
| 232 |
return 0 - 0x00800000; |
| 233 |
} |
| 234 |
static constexpr mpt::int32 lowest() noexcept { |
| 235 |
return 0 - 0x00800000; |
| 236 |
} |
| 237 |
static constexpr mpt::int32 max() noexcept { |
| 238 |
return 0 + 0x007fffff; |
| 239 |
} |
| 240 |
}; |
| 241 |
} // namespace std |
| 242 |
#endif // !CPPCHECK |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
#endif // MPT_ENDIAN_INT24_HPP |