1#ifndef __COMMON_TRAITS_HPP__
2#define __COMMON_TRAITS_HPP__
7# define LIKELY(EXPR) __builtin_expect(!!(EXPR), 1)
9# define LIKELY(EXPR) (!!(EXPR))
13# define X_ASSERT(CHECK) void(0)
15# define X_ASSERT(CHECK) \
16 (LIKELY(CHECK) ? void(0) : [] { assert(!(#CHECK)); }())
26constexpr double tolerance_equality_float = 1e-15;
30 requires std::is_integral_v<std::remove_cvref_t<T>>;
31 requires !std::is_same_v<std::remove_cvref_t<T>,
bool>;
32 requires std::is_arithmetic_v<
decltype(n + 1)>;
33 requires !std::is_pointer_v<std::remove_cvref_t<T>>;
40template <NumberType T>
41inline bool almost_equal(T val, T val2, T tolerance = tolerance_equality_float)
43 using CommonT = std::common_type_t<T, T>;
44 return std::abs(
static_cast<CommonT
>(val) -
static_cast<CommonT
>(val2)) <
45 static_cast<CommonT
>(tolerance);
49template <NumberType T>
50inline bool almost_equal(
const T& val,
52 T tolerance = tolerance_equality_float)
54 using CommonT = std::common_type_t<T, T>;
55 return std::abs(
static_cast<CommonT
>(val) -
static_cast<CommonT
>(val2)) <
56 static_cast<CommonT
>(tolerance);
60template <NumberType T>
61inline bool almost_equal(
const T* val,
63 T tolerance = tolerance_equality_float)
69 using CommonT = std::common_type_t<T, T>;
70 return std::abs(
static_cast<CommonT
>(*val) -
static_cast<CommonT
>(*val2)) <
71 static_cast<CommonT
>(tolerance);
75template <NumberType T>
77almost_equal(T&& val, T&& val2, T tolerance = tolerance_equality_float)
79 using CommonT = std::common_type_t<T, T>;
80 return std::abs(
static_cast<CommonT
>(val) -
static_cast<CommonT
>(val2)) <
81 static_cast<CommonT
>(tolerance);