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)); }())
22constexpr double tolerance_equality_float = 1e-15;
26 requires std::is_integral_v<std::remove_cvref_t<T>>;
27 requires !std::is_same_v<std::remove_cvref_t<T>,
bool>;
28 requires std::is_arithmetic_v<
decltype(n + 1)>;
29 requires !std::is_pointer_v<std::remove_cvref_t<T>>;
36template <NumberType T>
37inline bool almost_equal(T val, T val2, T tolerance = tolerance_equality_float)
39 using CommonT = std::common_type_t<T, T>;
40 return std::abs(
static_cast<CommonT
>(val) -
static_cast<CommonT
>(val2)) <
41 static_cast<CommonT
>(tolerance);
45template <NumberType T>
46inline bool almost_equal(
const T& val,
48 T tolerance = tolerance_equality_float)
50 using CommonT = std::common_type_t<T, T>;
51 return std::abs(
static_cast<CommonT
>(val) -
static_cast<CommonT
>(val2)) <
52 static_cast<CommonT
>(tolerance);
56template <NumberType T>
57inline bool almost_equal(
const T* val,
59 T tolerance = tolerance_equality_float)
65 using CommonT = std::common_type_t<T, T>;
66 return std::abs(
static_cast<CommonT
>(*val) -
static_cast<CommonT
>(*val2)) <
67 static_cast<CommonT
>(tolerance);
71template <NumberType T>
73almost_equal(T&& val, T&& val2, T tolerance = tolerance_equality_float)
75 using CommonT = std::common_type_t<T, T>;
76 return std::abs(
static_cast<CommonT
>(val) -
static_cast<CommonT
>(val2)) <
77 static_cast<CommonT
>(tolerance);