2020-11-29 13:03:50 +01:00
|
|
|
#pragma once // No BOM and only basic ASCII in this header, or a neko will die
|
2015-12-02 10:23:25 +01:00
|
|
|
|
2016-08-14 02:22:19 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <intrin.h>
|
|
|
|
#else
|
|
|
|
#include <x86intrin.h>
|
|
|
|
#endif
|
|
|
|
#include <immintrin.h>
|
|
|
|
#include <emmintrin.h>
|
|
|
|
|
2015-10-04 00:45:26 +02:00
|
|
|
#include <cstdint>
|
2018-08-24 18:07:38 +02:00
|
|
|
#include <cstddef>
|
2019-05-10 19:24:14 +02:00
|
|
|
#include <cstring>
|
2016-04-27 00:27:24 +02:00
|
|
|
#include <type_traits>
|
2016-08-14 02:22:19 +02:00
|
|
|
#include <utility>
|
2017-01-29 00:00:49 +01:00
|
|
|
#include <chrono>
|
2017-04-19 13:31:56 +02:00
|
|
|
#include <array>
|
2016-08-14 02:22:19 +02:00
|
|
|
|
2020-12-11 14:31:32 +01:00
|
|
|
using std::chrono::steady_clock;
|
|
|
|
|
|
|
|
using namespace std::literals;
|
|
|
|
|
2020-08-14 18:11:35 +02:00
|
|
|
#ifdef _MSC_VER
|
2020-08-17 16:19:49 +02:00
|
|
|
#if !defined(__cpp_lib_bitops) && _MSC_VER < 1928
|
2020-08-14 18:11:35 +02:00
|
|
|
#define __cpp_lib_bitops
|
|
|
|
#endif
|
|
|
|
#endif
|
2020-04-13 14:31:41 +02:00
|
|
|
#include <bit>
|
2016-08-14 02:22:19 +02:00
|
|
|
|
2019-02-27 21:10:40 +01:00
|
|
|
#ifndef __has_builtin
|
|
|
|
#define __has_builtin(x) 0
|
|
|
|
#endif
|
|
|
|
|
2016-08-14 02:22:19 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define SAFE_BUFFERS __declspec(safebuffers)
|
|
|
|
#define NEVER_INLINE __declspec(noinline)
|
|
|
|
#define FORCE_INLINE __forceinline
|
2018-09-06 23:04:26 +02:00
|
|
|
#else // not _MSC_VER
|
2020-03-24 19:12:54 +01:00
|
|
|
#define SAFE_BUFFERS __attribute__((no_stack_protector))
|
2020-11-03 02:12:29 +01:00
|
|
|
#define NEVER_INLINE __attribute__((noinline)) inline
|
2016-08-14 02:22:19 +02:00
|
|
|
#define FORCE_INLINE __attribute__((always_inline)) inline
|
2018-09-06 23:04:26 +02:00
|
|
|
#endif // _MSC_VER
|
2016-08-14 02:22:19 +02:00
|
|
|
|
|
|
|
#define CHECK_SIZE(type, size) static_assert(sizeof(type) == size, "Invalid " #type " type size")
|
|
|
|
#define CHECK_ALIGN(type, align) static_assert(alignof(type) == align, "Invalid " #type " type alignment")
|
|
|
|
#define CHECK_MAX_SIZE(type, size) static_assert(sizeof(type) <= size, #type " type size is too big")
|
|
|
|
#define CHECK_SIZE_ALIGN(type, size, align) CHECK_SIZE(type, size); CHECK_ALIGN(type, align)
|
|
|
|
|
|
|
|
#define DECLARE(...) decltype(__VA_ARGS__) __VA_ARGS__
|
|
|
|
|
|
|
|
#define STR_CASE(...) case __VA_ARGS__: return #__VA_ARGS__
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2018-09-06 23:04:26 +02:00
|
|
|
#if defined(_DEBUG) || defined(_AUDIT)
|
2020-12-09 16:04:52 +01:00
|
|
|
#define AUDIT(...) (static_cast<void>(ensure(__VA_ARGS__)))
|
2018-09-06 23:04:26 +02:00
|
|
|
#else
|
2020-12-09 16:04:52 +01:00
|
|
|
#define AUDIT(...) (static_cast<void>(0))
|
2018-09-06 23:04:26 +02:00
|
|
|
#endif
|
|
|
|
|
2020-02-17 18:55:20 +01:00
|
|
|
#if __cpp_lib_bit_cast >= 201806L
|
2019-06-01 23:12:17 +02:00
|
|
|
#include <bit>
|
|
|
|
#else
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template <class To, class From, typename = std::enable_if_t<sizeof(To) == sizeof(From)>>
|
|
|
|
constexpr To bit_cast(const From& from) noexcept
|
|
|
|
{
|
|
|
|
static_assert(sizeof(To) == sizeof(From), "std::bit_cast<>: incompatible type size");
|
|
|
|
|
2020-07-25 10:46:47 +02:00
|
|
|
if constexpr ((std::is_same_v<std::remove_const_t<To>, std::remove_const_t<From>> && std::is_constructible_v<To, From>) || (std::is_integral_v<From> && std::is_integral_v<To>))
|
2020-06-26 05:45:05 +02:00
|
|
|
{
|
|
|
|
return static_cast<To>(from);
|
|
|
|
}
|
|
|
|
|
2019-09-08 18:27:29 +02:00
|
|
|
To result{};
|
2019-06-01 23:12:17 +02:00
|
|
|
std::memcpy(&result, &from, sizeof(From));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2018-09-06 23:04:26 +02:00
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
using schar = signed char;
|
|
|
|
using uchar = unsigned char;
|
2015-10-04 00:45:26 +02:00
|
|
|
using ushort = unsigned short;
|
2016-08-13 15:36:04 +02:00
|
|
|
using uint = unsigned int;
|
|
|
|
using ulong = unsigned long;
|
2015-10-04 00:45:26 +02:00
|
|
|
using ullong = unsigned long long;
|
2016-08-13 15:36:04 +02:00
|
|
|
using llong = long long;
|
2015-10-04 00:45:26 +02:00
|
|
|
|
2018-08-18 03:13:25 +02:00
|
|
|
#if __APPLE__
|
|
|
|
using uptr = std::uint64_t;
|
|
|
|
#else
|
2017-06-22 23:39:31 +02:00
|
|
|
using uptr = std::uintptr_t;
|
2018-08-18 03:13:25 +02:00
|
|
|
#endif
|
2017-06-22 23:39:31 +02:00
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
using u8 = std::uint8_t;
|
2015-12-02 10:23:25 +01:00
|
|
|
using u16 = std::uint16_t;
|
|
|
|
using u32 = std::uint32_t;
|
|
|
|
using u64 = std::uint64_t;
|
2015-10-04 00:45:26 +02:00
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
using s8 = std::int8_t;
|
2015-12-02 10:23:25 +01:00
|
|
|
using s16 = std::int16_t;
|
|
|
|
using s32 = std::int32_t;
|
|
|
|
using s64 = std::int64_t;
|
2015-10-04 00:45:26 +02:00
|
|
|
|
2020-05-17 20:33:36 +02:00
|
|
|
#if __APPLE__
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
|
|
|
|
constexpr int countr_zero(T x) noexcept
|
|
|
|
{
|
|
|
|
if (x == 0)
|
|
|
|
return sizeof(T) * 8;
|
|
|
|
if constexpr (sizeof(T) <= sizeof(uint))
|
|
|
|
return __builtin_ctz(x);
|
|
|
|
else if constexpr (sizeof(T) <= sizeof(ulong))
|
|
|
|
return __builtin_ctzl(x);
|
|
|
|
else if constexpr (sizeof(T) <= sizeof(ullong))
|
|
|
|
return __builtin_ctzll(x);
|
|
|
|
else
|
|
|
|
static_assert(sizeof(T) <= sizeof(ullong));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
|
|
|
|
constexpr int countr_one(T x) noexcept
|
|
|
|
{
|
|
|
|
return countr_zero<T>(~x);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
|
|
|
|
constexpr int countl_zero(T x) noexcept
|
|
|
|
{
|
|
|
|
if (x == 0)
|
|
|
|
return sizeof(T) * 8;
|
|
|
|
if constexpr (sizeof(T) <= sizeof(uint))
|
|
|
|
return __builtin_clz(x) - (sizeof(uint) - sizeof(T)) * 8;
|
|
|
|
else if constexpr (sizeof(T) <= sizeof(ulong))
|
|
|
|
return __builtin_clzl(x) - (sizeof(ulong) - sizeof(T)) * 8;
|
|
|
|
else if constexpr (sizeof(T) <= sizeof(ullong))
|
|
|
|
return __builtin_clzll(x) - (sizeof(ullong) - sizeof(T)) * 8;
|
|
|
|
else
|
|
|
|
static_assert(sizeof(T) <= sizeof(ullong));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
|
|
|
|
constexpr int countl_one(T x) noexcept
|
|
|
|
{
|
|
|
|
return countl_zero<T>(~x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-07-26 18:00:56 +02:00
|
|
|
// Get integral type from type size
|
|
|
|
template <std::size_t N>
|
2019-07-07 10:53:07 +02:00
|
|
|
struct get_int_impl
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2019-07-26 18:00:56 +02:00
|
|
|
template <>
|
2019-07-07 10:53:07 +02:00
|
|
|
struct get_int_impl<sizeof(u8)>
|
|
|
|
{
|
2019-07-26 18:00:56 +02:00
|
|
|
using utype = u8;
|
|
|
|
using stype = s8;
|
2019-07-07 10:53:07 +02:00
|
|
|
};
|
|
|
|
|
2019-07-26 18:00:56 +02:00
|
|
|
template <>
|
2019-07-07 10:53:07 +02:00
|
|
|
struct get_int_impl<sizeof(u16)>
|
|
|
|
{
|
2019-07-26 18:00:56 +02:00
|
|
|
using utype = u16;
|
|
|
|
using stype = s16;
|
2019-07-07 10:53:07 +02:00
|
|
|
};
|
|
|
|
|
2019-07-26 18:00:56 +02:00
|
|
|
template <>
|
2019-07-07 10:53:07 +02:00
|
|
|
struct get_int_impl<sizeof(u32)>
|
|
|
|
{
|
2019-07-26 18:00:56 +02:00
|
|
|
using utype = u32;
|
|
|
|
using stype = s32;
|
2019-07-07 10:53:07 +02:00
|
|
|
};
|
|
|
|
|
2019-07-26 18:00:56 +02:00
|
|
|
template <>
|
2019-07-07 10:53:07 +02:00
|
|
|
struct get_int_impl<sizeof(u64)>
|
|
|
|
{
|
2019-07-26 18:00:56 +02:00
|
|
|
using utype = u64;
|
|
|
|
using stype = s64;
|
2019-07-07 10:53:07 +02:00
|
|
|
};
|
|
|
|
|
2019-07-26 18:00:56 +02:00
|
|
|
template <std::size_t N>
|
|
|
|
using get_uint_t = typename get_int_impl<N>::utype;
|
|
|
|
|
|
|
|
template <std::size_t N>
|
|
|
|
using get_sint_t = typename get_int_impl<N>::stype;
|
2019-07-07 10:53:07 +02:00
|
|
|
|
2020-04-11 20:34:31 +02:00
|
|
|
template <typename T>
|
2020-04-12 07:11:58 +02:00
|
|
|
std::remove_cvref_t<T> as_rvalue(T&& obj)
|
2020-04-11 20:34:31 +02:00
|
|
|
{
|
2020-12-09 08:47:45 +01:00
|
|
|
return std::forward<T>(obj);
|
2020-04-11 20:34:31 +02:00
|
|
|
}
|
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
// Formatting helper, type-specific preprocessing for improving safety and functionality
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T, typename = void>
|
2016-08-03 22:51:05 +02:00
|
|
|
struct fmt_unveil;
|
2016-04-27 00:27:24 +02:00
|
|
|
|
2016-08-15 12:18:47 +02:00
|
|
|
template <typename Arg>
|
|
|
|
using fmt_unveil_t = typename fmt_unveil<Arg>::type;
|
|
|
|
|
2016-08-08 18:01:06 +02:00
|
|
|
struct fmt_type_info;
|
|
|
|
|
|
|
|
namespace fmt
|
|
|
|
{
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename... Args>
|
2016-08-08 18:01:06 +02:00
|
|
|
const fmt_type_info* get_type_info();
|
|
|
|
}
|
|
|
|
|
2020-10-25 22:28:58 +01:00
|
|
|
template <typename T, std::size_t Align>
|
2016-02-01 22:55:43 +01:00
|
|
|
class atomic_t;
|
|
|
|
|
|
|
|
// Extract T::simple_type if available, remove cv qualifiers
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T, typename = void>
|
2016-02-01 22:55:43 +01:00
|
|
|
struct simple_type_helper
|
|
|
|
{
|
|
|
|
using type = typename std::remove_cv<T>::type;
|
|
|
|
};
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T>
|
2018-08-23 23:31:27 +02:00
|
|
|
struct simple_type_helper<T, std::void_t<typename T::simple_type>>
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
|
|
|
using type = typename T::simple_type;
|
|
|
|
};
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T>
|
|
|
|
using simple_t = typename simple_type_helper<T>::type;
|
2016-02-01 22:55:43 +01:00
|
|
|
|
|
|
|
// Bool type equivalent
|
|
|
|
class b8
|
|
|
|
{
|
2016-04-27 00:27:24 +02:00
|
|
|
u8 m_value;
|
2016-02-01 22:55:43 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
b8() = default;
|
|
|
|
|
2020-10-05 14:37:59 +02:00
|
|
|
constexpr b8(bool value) noexcept
|
2016-02-01 22:55:43 +01:00
|
|
|
: m_value(value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-05 14:37:59 +02:00
|
|
|
constexpr operator bool() const noexcept
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
|
|
|
return m_value != 0;
|
|
|
|
}
|
2020-10-05 14:37:59 +02:00
|
|
|
|
|
|
|
constexpr bool set(bool value) noexcept
|
|
|
|
{
|
|
|
|
m_value = value;
|
|
|
|
return value;
|
|
|
|
}
|
2016-02-01 22:55:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
using u128 = __uint128_t;
|
|
|
|
using s128 = __int128_t;
|
|
|
|
#else
|
|
|
|
|
|
|
|
// Unsigned 128-bit integer implementation (TODO)
|
|
|
|
struct alignas(16) u128
|
|
|
|
{
|
2016-04-27 00:27:24 +02:00
|
|
|
u64 lo, hi;
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
u128() noexcept = default;
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2020-12-12 07:40:07 +01:00
|
|
|
template <typename T, std::enable_if_t<std::is_unsigned_v<T>, u64> = 0>
|
|
|
|
constexpr u128(T arg) noexcept
|
|
|
|
: lo(arg)
|
2016-02-01 22:55:43 +01:00
|
|
|
, hi(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-12-12 07:40:07 +01:00
|
|
|
template <typename T, std::enable_if_t<std::is_signed_v<T>, s64> = 0>
|
|
|
|
constexpr u128(T arg) noexcept
|
|
|
|
: lo(s64{arg})
|
|
|
|
, hi(s64{arg} >> 63)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-27 19:34:08 +01:00
|
|
|
constexpr explicit operator bool() const noexcept
|
|
|
|
{
|
|
|
|
return !!(lo | hi);
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr explicit operator u64() const noexcept
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
return lo;
|
2016-02-01 22:55:43 +01:00
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr friend u128 operator+(const u128& l, const u128& r)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
u128 value = l;
|
|
|
|
value += r;
|
2016-02-01 22:55:43 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr friend u128 operator-(const u128& l, const u128& r)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
u128 value = l;
|
|
|
|
value -= r;
|
2016-02-01 22:55:43 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128 operator+() const
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128 operator-() const
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
u128 value{};
|
|
|
|
value -= *this;
|
2016-02-01 22:55:43 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128& operator++()
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
*this += 1;
|
2016-02-01 22:55:43 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128 operator++(int)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
|
|
|
u128 value = *this;
|
2020-11-20 13:01:25 +01:00
|
|
|
*this += 1;
|
2016-02-01 22:55:43 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128& operator--()
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
*this -= 1;
|
2016-02-01 22:55:43 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128 operator--(int)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
|
|
|
u128 value = *this;
|
2020-11-20 13:01:25 +01:00
|
|
|
*this -= 1;
|
2016-02-01 22:55:43 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128 operator<<(u128 shift_value) const
|
2020-10-27 19:34:08 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
u128 value = *this;
|
|
|
|
value <<= shift_value;
|
2020-10-27 19:34:08 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128 operator>>(u128 shift_value) const
|
2020-10-27 19:34:08 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
u128 value = *this;
|
|
|
|
value >>= shift_value;
|
2020-10-27 19:34:08 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128 operator~() const
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
u128 value{};
|
2016-02-01 22:55:43 +01:00
|
|
|
value.lo = ~lo;
|
|
|
|
value.hi = ~hi;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr friend u128 operator&(const u128& l, const u128& r)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
u128 value{};
|
2016-02-01 22:55:43 +01:00
|
|
|
value.lo = l.lo & r.lo;
|
|
|
|
value.hi = l.hi & r.hi;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr friend u128 operator|(const u128& l, const u128& r)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
u128 value{};
|
2016-02-01 22:55:43 +01:00
|
|
|
value.lo = l.lo | r.lo;
|
|
|
|
value.hi = l.hi | r.hi;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr friend u128 operator^(const u128& l, const u128& r)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
u128 value{};
|
2016-02-01 22:55:43 +01:00
|
|
|
value.lo = l.lo ^ r.lo;
|
|
|
|
value.hi = l.hi ^ r.hi;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128& operator+=(const u128& r)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
if (std::is_constant_evaluated())
|
|
|
|
{
|
|
|
|
lo += r.lo;
|
|
|
|
hi += r.hi + (lo < r.lo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_addcarry_u64(_addcarry_u64(0, r.lo, lo, &lo), r.hi, hi, &hi);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr u128& operator-=(const u128& r)
|
|
|
|
{
|
|
|
|
if (std::is_constant_evaluated())
|
|
|
|
{
|
|
|
|
hi -= r.hi + (lo < r.lo);
|
|
|
|
lo -= r.lo;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_subborrow_u64(_subborrow_u64(0, lo, r.lo, &lo), hi, r.hi, &hi);
|
|
|
|
}
|
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128& operator<<=(const u128& r)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2020-11-20 13:01:25 +01:00
|
|
|
if (std::is_constant_evaluated())
|
|
|
|
{
|
|
|
|
if (r.hi == 0 && r.lo < 64)
|
|
|
|
{
|
|
|
|
hi = (hi << r.lo) | (lo >> (64 - r.lo));
|
|
|
|
lo = (lo << r.lo);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
else if (r.hi == 0 && r.lo < 128)
|
|
|
|
{
|
|
|
|
hi = (lo << (r.lo - 64));
|
|
|
|
lo = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const u64 v0 = lo << (r.lo & 63);
|
|
|
|
const u64 v1 = __shiftleft128(lo, hi, static_cast<uchar>(r.lo));
|
|
|
|
lo = (r.lo & 64) ? 0 : v0;
|
|
|
|
hi = (r.lo & 64) ? v0 : v1;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr u128& operator>>=(const u128& r)
|
|
|
|
{
|
|
|
|
if (std::is_constant_evaluated())
|
|
|
|
{
|
|
|
|
if (r.hi == 0 && r.lo < 64)
|
|
|
|
{
|
|
|
|
lo = (lo >> r.lo) | (hi << (64 - r.lo));
|
|
|
|
hi = (hi >> r.lo);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
else if (r.hi == 0 && r.lo < 128)
|
|
|
|
{
|
|
|
|
lo = (hi >> (r.lo - 64));
|
|
|
|
hi = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const u64 v0 = hi >> (r.lo & 63);
|
|
|
|
const u64 v1 = __shiftright128(lo, hi, static_cast<uchar>(r.lo));
|
|
|
|
lo = (r.lo & 64) ? v0 : v1;
|
|
|
|
hi = (r.lo & 64) ? 0 : v0;
|
2016-02-01 22:55:43 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128& operator&=(const u128& r)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
|
|
|
lo &= r.lo;
|
|
|
|
hi &= r.hi;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128& operator|=(const u128& r)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
|
|
|
lo |= r.lo;
|
|
|
|
hi |= r.hi;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:01:25 +01:00
|
|
|
constexpr u128& operator^=(const u128& r)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
|
|
|
lo ^= r.lo;
|
|
|
|
hi ^= r.hi;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Signed 128-bit integer implementation (TODO)
|
|
|
|
struct alignas(16) s128
|
|
|
|
{
|
2016-04-27 00:27:24 +02:00
|
|
|
u64 lo;
|
|
|
|
s64 hi;
|
2016-02-01 22:55:43 +01:00
|
|
|
|
|
|
|
s128() = default;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2020-12-04 16:03:50 +01:00
|
|
|
template <>
|
|
|
|
struct get_int_impl<16>
|
|
|
|
{
|
|
|
|
using utype = u128;
|
|
|
|
};
|
|
|
|
|
2020-02-19 17:34:04 +01:00
|
|
|
// Return magic value for any unsigned type
|
|
|
|
constexpr inline struct umax_helper
|
|
|
|
{
|
|
|
|
constexpr umax_helper() noexcept = default;
|
|
|
|
|
2020-02-20 13:00:22 +01:00
|
|
|
template <typename T, typename S = simple_t<T>, typename = std::enable_if_t<std::is_unsigned_v<S>>>
|
2020-02-19 17:34:04 +01:00
|
|
|
explicit constexpr operator T() const
|
|
|
|
{
|
2020-12-12 10:00:21 +01:00
|
|
|
return static_cast<S>(-1);
|
2020-02-19 17:34:04 +01:00
|
|
|
}
|
|
|
|
|
2020-02-20 13:00:22 +01:00
|
|
|
template <typename T, typename S = simple_t<T>, typename = std::enable_if_t<std::is_unsigned_v<S>>>
|
2020-02-19 17:34:04 +01:00
|
|
|
constexpr bool operator==(const T& rhs) const
|
|
|
|
{
|
2020-12-12 10:00:21 +01:00
|
|
|
return rhs == static_cast<S>(-1);
|
2020-02-19 17:34:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if __cpp_impl_three_way_comparison >= 201711 && !__INTELLISENSE__
|
|
|
|
#else
|
|
|
|
template <typename T>
|
2020-02-20 13:00:22 +01:00
|
|
|
friend constexpr std::enable_if_t<std::is_unsigned_v<simple_t<T>>, bool> operator==(const T& lhs, const umax_helper& rhs)
|
2020-02-19 17:34:04 +01:00
|
|
|
{
|
2020-12-12 10:00:21 +01:00
|
|
|
return lhs == static_cast<simple_t<T>>(-1);
|
2020-02-19 17:34:04 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __cpp_impl_three_way_comparison >= 201711
|
|
|
|
#else
|
2020-02-20 13:00:22 +01:00
|
|
|
template <typename T, typename S = simple_t<T>, typename = std::enable_if_t<std::is_unsigned_v<S>>>
|
2020-02-19 17:34:04 +01:00
|
|
|
constexpr bool operator!=(const T& rhs) const
|
|
|
|
{
|
2020-12-12 10:00:21 +01:00
|
|
|
return rhs != static_cast<S>(-1);
|
2020-02-19 17:34:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-02-20 13:00:22 +01:00
|
|
|
friend constexpr std::enable_if_t<std::is_unsigned_v<simple_t<T>>, bool> operator!=(const T& lhs, const umax_helper& rhs)
|
2020-02-19 17:34:04 +01:00
|
|
|
{
|
2020-12-12 10:00:21 +01:00
|
|
|
return lhs != static_cast<simple_t<T>>(-1);
|
2020-02-19 17:34:04 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} umax;
|
|
|
|
|
2019-05-10 19:24:14 +02:00
|
|
|
using f32 = float;
|
|
|
|
using f64 = double;
|
|
|
|
|
2015-10-04 00:45:26 +02:00
|
|
|
union alignas(2) f16
|
|
|
|
{
|
|
|
|
u16 _u16;
|
|
|
|
u8 _u8[2];
|
2015-10-25 23:40:02 +01:00
|
|
|
|
|
|
|
explicit f16(u16 raw)
|
|
|
|
{
|
|
|
|
_u16 = raw;
|
|
|
|
}
|
|
|
|
|
2019-05-10 19:24:14 +02:00
|
|
|
explicit operator f32() const
|
2015-10-25 23:40:02 +01:00
|
|
|
{
|
|
|
|
// See http://stackoverflow.com/a/26779139
|
|
|
|
// The conversion doesn't handle NaN/Inf
|
2016-08-13 15:36:04 +02:00
|
|
|
u32 raw = ((_u16 & 0x8000) << 16) | // Sign (just moved)
|
2020-12-09 08:47:45 +01:00
|
|
|
(((_u16 & 0x7c00) + 0x1C000) << 13) | // Exponent ( exp - 15 + 127)
|
|
|
|
((_u16 & 0x03FF) << 13); // Mantissa
|
2019-05-10 19:24:14 +02:00
|
|
|
|
2019-06-01 23:12:17 +02:00
|
|
|
return std::bit_cast<f32>(raw);
|
2015-10-25 23:40:02 +01:00
|
|
|
}
|
2015-10-04 00:45:26 +02:00
|
|
|
};
|
|
|
|
|
2016-08-14 02:22:19 +02:00
|
|
|
CHECK_SIZE_ALIGN(f16, 2, 2);
|
|
|
|
|
2019-12-16 20:56:14 +01:00
|
|
|
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value>>
|
|
|
|
constexpr T align(T value, ullong align)
|
|
|
|
{
|
|
|
|
return static_cast<T>((value + (align - 1)) & (0 - align));
|
|
|
|
}
|
|
|
|
|
|
|
|
// General purpose aligned division, the result is rounded up not truncated
|
|
|
|
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value>>
|
|
|
|
constexpr T aligned_div(T value, ullong align)
|
|
|
|
{
|
|
|
|
return static_cast<T>((value + align - 1) / align);
|
|
|
|
}
|
|
|
|
|
|
|
|
// General purpose aligned division, the result is rounded to nearest
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
|
2019-12-16 20:56:14 +01:00
|
|
|
constexpr T rounded_div(T value, std::conditional_t<std::is_signed<T>::value, llong, ullong> align)
|
2016-08-08 18:01:06 +02:00
|
|
|
{
|
2019-12-16 20:56:14 +01:00
|
|
|
if constexpr (std::is_unsigned<T>::value)
|
|
|
|
{
|
|
|
|
return static_cast<T>((value + (align / 2)) / align);
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<T>((value + (value < 0 ? 0 - align : align) / 2) / align);
|
2016-08-08 18:01:06 +02:00
|
|
|
}
|
|
|
|
|
2017-04-19 13:31:56 +02:00
|
|
|
template <typename T, typename T2>
|
|
|
|
inline u32 offset32(T T2::*const mptr)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
2019-06-01 23:12:17 +02:00
|
|
|
return std::bit_cast<u32>(mptr);
|
2017-04-19 13:31:56 +02:00
|
|
|
#elif __GNUG__
|
2019-06-01 23:12:17 +02:00
|
|
|
return std::bit_cast<std::size_t>(mptr);
|
2017-04-19 13:31:56 +02:00
|
|
|
#else
|
2019-06-01 23:12:17 +02:00
|
|
|
static_assert(sizeof(mptr) == 0, "Unsupported pointer-to-member size");
|
2017-04-19 13:31:56 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct offset32_array
|
|
|
|
{
|
|
|
|
static_assert(std::is_array<T>::value, "Invalid pointer-to-member type (array expected)");
|
|
|
|
|
|
|
|
template <typename Arg>
|
|
|
|
static inline u32 index32(const Arg& arg)
|
|
|
|
{
|
2018-09-03 17:46:14 +02:00
|
|
|
return u32{sizeof(std::remove_extent_t<T>)} * static_cast<u32>(arg);
|
2017-04-19 13:31:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, std::size_t N>
|
|
|
|
struct offset32_array<std::array<T, N>>
|
|
|
|
{
|
|
|
|
template <typename Arg>
|
|
|
|
static inline u32 index32(const Arg& arg)
|
|
|
|
{
|
2018-09-03 17:46:14 +02:00
|
|
|
return u32{sizeof(T)} * static_cast<u32>(arg);
|
2017-04-19 13:31:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Arg>
|
|
|
|
struct offset32_detail;
|
|
|
|
|
|
|
|
template <typename T, typename T2, typename Arg, typename... Args>
|
|
|
|
inline u32 offset32(T T2::*const mptr, const Arg& arg, const Args&... args)
|
|
|
|
{
|
|
|
|
return offset32_detail<Arg>::offset32(mptr, arg, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Arg>
|
|
|
|
struct offset32_detail
|
|
|
|
{
|
|
|
|
template <typename T, typename T2, typename... Args>
|
|
|
|
static inline u32 offset32(T T2::*const mptr, const Arg& arg, const Args&... args)
|
|
|
|
{
|
|
|
|
return ::offset32(mptr, args...) + offset32_array<T>::index32(arg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T3, typename T4>
|
|
|
|
struct offset32_detail<T3 T4::*>
|
|
|
|
{
|
|
|
|
template <typename T, typename T2, typename... Args>
|
|
|
|
static inline u32 offset32(T T2::*const mptr, T3 T4::*const mptr2, const Args&... args)
|
|
|
|
{
|
|
|
|
return ::offset32(mptr) + ::offset32(mptr2, args...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-14 02:22:19 +02:00
|
|
|
// Helper function, used by ""_u16, ""_u32, ""_u64
|
2019-12-23 20:51:40 +01:00
|
|
|
constexpr u32 to_u8(char c)
|
2016-08-14 02:22:19 +02:00
|
|
|
{
|
2016-08-15 12:18:47 +02:00
|
|
|
return static_cast<u8>(c);
|
2016-08-14 02:22:19 +02:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:55:20 +01:00
|
|
|
// Convert 1-2-byte string to u16 value like reinterpret_cast does
|
2019-12-26 21:01:48 +01:00
|
|
|
constexpr u16 operator""_u16(const char* s, std::size_t /*length*/)
|
2016-08-14 02:22:19 +02:00
|
|
|
{
|
2020-02-17 18:55:20 +01:00
|
|
|
if constexpr (std::endian::little == std::endian::native)
|
|
|
|
{
|
|
|
|
return static_cast<u16>(to_u8(s[1]) << 8 | to_u8(s[0]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return static_cast<u16>(to_u8(s[0]) << 8 | to_u8(s[1]));
|
|
|
|
}
|
2016-08-14 02:22:19 +02:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:55:20 +01:00
|
|
|
// Convert 3-4-byte string to u32 value like reinterpret_cast does
|
2019-12-26 21:01:48 +01:00
|
|
|
constexpr u32 operator""_u32(const char* s, std::size_t /*length*/)
|
2016-08-14 02:22:19 +02:00
|
|
|
{
|
2020-02-17 18:55:20 +01:00
|
|
|
if constexpr (std::endian::little == std::endian::native)
|
|
|
|
{
|
|
|
|
return to_u8(s[3]) << 24 | to_u8(s[2]) << 16 | to_u8(s[1]) << 8 | to_u8(s[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return to_u8(s[0]) << 24 | to_u8(s[1]) << 16 | to_u8(s[2]) << 8 | to_u8(s[3]);
|
|
|
|
}
|
2016-08-14 02:22:19 +02:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:55:20 +01:00
|
|
|
// Convert 5-6-byte string to u64 value like reinterpret_cast does
|
|
|
|
constexpr u64 operator""_u48(const char* s, std::size_t /*length*/)
|
|
|
|
{
|
|
|
|
if constexpr (std::endian::little == std::endian::native)
|
|
|
|
{
|
|
|
|
return static_cast<u64>(to_u8(s[5]) << 8 | to_u8(s[4])) << 32 | to_u8(s[3]) << 24 | to_u8(s[2]) << 16 | to_u8(s[1]) << 8 | to_u8(s[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return static_cast<u64>(to_u8(s[0]) << 8 | to_u8(s[1])) << 32 | to_u8(s[2]) << 24 | to_u8(s[3]) << 16 | to_u8(s[4]) << 8 | to_u8(s[5]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert 7-8-byte string to u64 value like reinterpret_cast does
|
2019-12-26 21:01:48 +01:00
|
|
|
constexpr u64 operator""_u64(const char* s, std::size_t /*length*/)
|
2016-08-14 02:22:19 +02:00
|
|
|
{
|
2020-02-17 18:55:20 +01:00
|
|
|
if constexpr (std::endian::little == std::endian::native)
|
|
|
|
{
|
|
|
|
return static_cast<u64>(to_u8(s[7]) << 24 | to_u8(s[6]) << 16 | to_u8(s[5]) << 8 | to_u8(s[4])) << 32 | to_u8(s[3]) << 24 | to_u8(s[2]) << 16 | to_u8(s[1]) << 8 | to_u8(s[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return static_cast<u64>(to_u8(s[0]) << 24 | to_u8(s[1]) << 16 | to_u8(s[2]) << 8 | to_u8(s[3])) << 32 | to_u8(s[4]) << 24 | to_u8(s[5]) << 16 | to_u8(s[6]) << 8 | to_u8(s[7]);
|
|
|
|
}
|
2016-08-14 02:22:19 +02:00
|
|
|
}
|
|
|
|
|
2020-12-09 08:47:45 +01:00
|
|
|
#if !defined(__INTELLISENSE__) && !__has_builtin(__builtin_COLUMN) && !defined(_MSC_VER)
|
|
|
|
constexpr unsigned __builtin_COLUMN()
|
2016-08-08 18:01:06 +02:00
|
|
|
{
|
2020-12-09 08:47:45 +01:00
|
|
|
return -1;
|
2016-08-08 18:01:06 +02:00
|
|
|
}
|
2020-12-09 08:47:45 +01:00
|
|
|
#endif
|
2016-08-08 18:01:06 +02:00
|
|
|
|
2020-12-09 08:47:45 +01:00
|
|
|
struct src_loc
|
2016-08-13 15:36:04 +02:00
|
|
|
{
|
2020-12-09 08:47:45 +01:00
|
|
|
u32 line;
|
|
|
|
u32 col;
|
|
|
|
const char* file;
|
|
|
|
const char* func;
|
2016-08-13 15:36:04 +02:00
|
|
|
};
|
|
|
|
|
2020-12-09 08:47:45 +01:00
|
|
|
namespace fmt
|
2016-08-13 15:36:04 +02:00
|
|
|
{
|
2020-12-09 08:47:45 +01:00
|
|
|
[[noreturn]] void raw_verify_error(const src_loc& loc);
|
2020-12-09 14:03:15 +01:00
|
|
|
[[noreturn]] void raw_narrow_error(const src_loc& loc, const fmt_type_info* sup, u64 arg);
|
2016-08-13 15:36:04 +02:00
|
|
|
}
|
|
|
|
|
2020-12-09 08:47:45 +01:00
|
|
|
template <typename T>
|
|
|
|
constexpr decltype(auto) ensure(T&& arg,
|
|
|
|
u32 line = __builtin_LINE(),
|
|
|
|
u32 col = __builtin_COLUMN(),
|
|
|
|
const char* file = __builtin_FILE(),
|
|
|
|
const char* func = __builtin_FUNCTION()) noexcept
|
2016-08-13 15:36:04 +02:00
|
|
|
{
|
2020-12-09 08:47:45 +01:00
|
|
|
if (std::forward<T>(arg)) [[likely]]
|
2016-08-13 15:36:04 +02:00
|
|
|
{
|
2020-12-09 08:47:45 +01:00
|
|
|
return std::forward<T>(arg);
|
2016-08-13 15:36:04 +02:00
|
|
|
}
|
|
|
|
|
2020-12-09 08:47:45 +01:00
|
|
|
fmt::raw_verify_error({line, col, file, func});
|
2016-08-13 15:36:04 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 19:22:07 +02:00
|
|
|
// narrow() function details
|
|
|
|
template <typename From, typename To = void, typename = void>
|
|
|
|
struct narrow_impl
|
|
|
|
{
|
|
|
|
// Temporarily (diagnostic)
|
|
|
|
static_assert(std::is_void<To>::value, "narrow_impl<> specialization not found");
|
|
|
|
|
|
|
|
// Returns true if value cannot be represented in type To
|
|
|
|
static constexpr bool test(const From& value)
|
|
|
|
{
|
|
|
|
// Unspecialized cases (including cast to void) always considered narrowing
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Unsigned to unsigned narrowing
|
|
|
|
template <typename From, typename To>
|
|
|
|
struct narrow_impl<From, To, std::enable_if_t<std::is_unsigned<From>::value && std::is_unsigned<To>::value>>
|
|
|
|
{
|
|
|
|
static constexpr bool test(const From& value)
|
|
|
|
{
|
|
|
|
return sizeof(To) < sizeof(From) && static_cast<To>(value) != value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Signed to signed narrowing
|
|
|
|
template <typename From, typename To>
|
|
|
|
struct narrow_impl<From, To, std::enable_if_t<std::is_signed<From>::value && std::is_signed<To>::value>>
|
|
|
|
{
|
|
|
|
static constexpr bool test(const From& value)
|
|
|
|
{
|
|
|
|
return sizeof(To) < sizeof(From) && static_cast<To>(value) != value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Unsigned to signed narrowing
|
|
|
|
template <typename From, typename To>
|
|
|
|
struct narrow_impl<From, To, std::enable_if_t<std::is_unsigned<From>::value && std::is_signed<To>::value>>
|
|
|
|
{
|
|
|
|
static constexpr bool test(const From& value)
|
|
|
|
{
|
|
|
|
return sizeof(To) <= sizeof(From) && value > (static_cast<std::make_unsigned_t<To>>(-1) >> 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Signed to unsigned narrowing (I)
|
|
|
|
template <typename From, typename To>
|
|
|
|
struct narrow_impl<From, To, std::enable_if_t<std::is_signed<From>::value && std::is_unsigned<To>::value && sizeof(To) >= sizeof(From)>>
|
|
|
|
{
|
|
|
|
static constexpr bool test(const From& value)
|
|
|
|
{
|
|
|
|
return value < static_cast<From>(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Signed to unsigned narrowing (II)
|
|
|
|
template <typename From, typename To>
|
|
|
|
struct narrow_impl<From, To, std::enable_if_t<std::is_signed<From>::value && std::is_unsigned<To>::value && sizeof(To) < sizeof(From)>>
|
|
|
|
{
|
|
|
|
static constexpr bool test(const From& value)
|
|
|
|
{
|
|
|
|
return static_cast<std::make_unsigned_t<From>>(value) > static_cast<To>(-1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-15 12:18:47 +02:00
|
|
|
// Simple type enabled (TODO: allow for To as well)
|
2016-08-14 19:22:07 +02:00
|
|
|
template <typename From, typename To>
|
2018-08-23 23:31:27 +02:00
|
|
|
struct narrow_impl<From, To, std::void_t<typename From::simple_type>>
|
2016-08-14 19:22:07 +02:00
|
|
|
: narrow_impl<simple_t<From>, To>
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename To = void, typename From, typename = decltype(static_cast<To>(std::declval<From>()))>
|
2020-12-09 14:03:15 +01:00
|
|
|
[[nodiscard]] constexpr To narrow(const From& value,
|
|
|
|
u32 line = __builtin_LINE(),
|
|
|
|
u32 col = __builtin_COLUMN(),
|
|
|
|
const char* file = __builtin_FILE(),
|
|
|
|
const char* func = __builtin_FUNCTION())
|
2016-08-08 18:01:06 +02:00
|
|
|
{
|
2016-08-14 19:22:07 +02:00
|
|
|
// Narrow check
|
2020-12-09 14:03:15 +01:00
|
|
|
if (narrow_impl<From, To>::test(value)) [[unlikely]]
|
2016-08-08 18:01:06 +02:00
|
|
|
{
|
|
|
|
// Pack value as formatting argument
|
2020-12-09 14:03:15 +01:00
|
|
|
fmt::raw_narrow_error({line, col, file, func}, fmt::get_type_info<fmt_unveil_t<From>>(), fmt_unveil<From>::get(value));
|
2016-08-08 18:01:06 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 19:22:07 +02:00
|
|
|
return static_cast<To>(value);
|
2016-08-08 18:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns u32 size() for container
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename CT, typename = decltype(static_cast<u32>(std::declval<CT>().size()))>
|
2020-12-09 14:03:15 +01:00
|
|
|
[[nodiscard]] constexpr u32 size32(const CT& container,
|
|
|
|
u32 line = __builtin_LINE(),
|
|
|
|
u32 col = __builtin_COLUMN(),
|
|
|
|
const char* file = __builtin_FILE(),
|
|
|
|
const char* func = __builtin_FUNCTION())
|
2016-08-08 18:01:06 +02:00
|
|
|
{
|
2020-12-09 14:03:15 +01:00
|
|
|
return narrow<u32>(container.size(), line, col, file, func);
|
2016-08-08 18:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns u32 size for an array
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T, std::size_t Size>
|
2020-12-09 14:03:15 +01:00
|
|
|
[[nodiscard]] constexpr u32 size32(const T (&)[Size])
|
2016-08-08 18:01:06 +02:00
|
|
|
{
|
2020-12-09 14:03:15 +01:00
|
|
|
static_assert(Size < UINT32_MAX, "Array is too big for 32-bit");
|
2016-08-08 18:01:06 +02:00
|
|
|
return static_cast<u32>(Size);
|
|
|
|
}
|
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Simplified hash algorithm for pointers. May be used in std::unordered_(map|set).
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T, std::size_t Align = alignof(T)>
|
2016-05-13 16:01:48 +02:00
|
|
|
struct pointer_hash
|
|
|
|
{
|
|
|
|
std::size_t operator()(T* ptr) const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<std::uintptr_t>(ptr) / Align;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T, std::size_t Shift = 0>
|
2016-06-02 17:16:01 +02:00
|
|
|
struct value_hash
|
|
|
|
{
|
|
|
|
std::size_t operator()(T value) const
|
|
|
|
{
|
|
|
|
return static_cast<std::size_t>(value) >> Shift;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-15 16:07:42 +01:00
|
|
|
// Synchronization helper (cache-friendly busy waiting)
|
2018-04-03 21:42:47 +02:00
|
|
|
inline void busy_wait(std::size_t cycles = 3000)
|
2017-02-15 16:07:42 +01:00
|
|
|
{
|
2018-04-03 21:42:47 +02:00
|
|
|
const u64 s = __rdtsc();
|
|
|
|
do _mm_pause(); while (__rdtsc() - s < cycles);
|
2017-02-15 16:07:42 +01:00
|
|
|
}
|
2019-07-07 10:53:07 +02:00
|
|
|
|
|
|
|
// TODO: Remove when moving to c++20
|
|
|
|
template <typename T>
|
|
|
|
inline constexpr uintmax_t floor2(T value)
|
|
|
|
{
|
|
|
|
value >>= 1;
|
|
|
|
|
|
|
|
for (uintmax_t i = 0;; i++, value >>= 1)
|
|
|
|
{
|
|
|
|
if (value == 0)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline constexpr uintmax_t ceil2(T value)
|
|
|
|
{
|
|
|
|
const uintmax_t ispow2 = value & (value - 1); // if power of 2 the result is 0
|
|
|
|
|
|
|
|
value >>= 1;
|
|
|
|
|
|
|
|
for (uintmax_t i = 0;; i++, value >>= 1)
|
|
|
|
{
|
|
|
|
if (value == 0)
|
|
|
|
{
|
|
|
|
return i + std::min<uintmax_t>(ispow2, 1);
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 15:02:12 +02:00
|
|
|
}
|