BioCMAMC-ST
traits.hpp
1#ifndef __COMMON_TRAITS_HPP__
2#define __COMMON_TRAITS_HPP__
3#include <cmath>
4#include <type_traits>
5
6#if defined __GNUC__
7# define LIKELY(EXPR) __builtin_expect(!!(EXPR), 1)
8#else
9# define LIKELY(EXPR) (!!(EXPR))
10#endif
11
12#if defined NDEBUG
13# define X_ASSERT(CHECK) void(0)
14#else
15# define X_ASSERT(CHECK) \
16 (LIKELY(CHECK) ? void(0) : [] { assert(!(#CHECK)); }())
17#endif
18
19template <typename T>
20concept FloatingPointType = std::is_floating_point_v<std::remove_cvref_t<T> >;
21
22static_assert(FloatingPointType<double>, "double ok");
23static_assert(FloatingPointType<float>, "float ok");
24static_assert(!FloatingPointType<int>, "int ok");
25
26constexpr double tolerance_equality_float = 1e-15;
27
28template <typename T> concept IntegerType = requires(T n)
29{
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> >;
34};
35
36template <typename T>
38
39// General case: by value
40template <NumberType T>
41inline bool
42almost_equal(T val, T val2, T tolerance = tolerance_equality_float)
43{
44 using CommonT = std::common_type_t<T, T>;
45 return std::abs(static_cast<CommonT>(val) - static_cast<CommonT>(val2))
46 < static_cast<CommonT>(tolerance);
47}
48
49// Overload for references
50template <NumberType T>
51inline bool
52almost_equal(const T& val,
53 const T& val2,
54 T tolerance = tolerance_equality_float)
55{
56 using CommonT = std::common_type_t<T, T>;
57 return std::abs(static_cast<CommonT>(val) - static_cast<CommonT>(val2))
58 < static_cast<CommonT>(tolerance);
59}
60
61// Overload for pointers
62template <NumberType T>
63inline bool
64almost_equal(const T* val,
65 const T* val2,
66 T tolerance = tolerance_equality_float)
67{
68 if (!val || !val2)
69 {
70 return false; // Null pointer check
71 }
72 using CommonT = std::common_type_t<T, T>;
73 return std::abs(static_cast<CommonT>(*val) - static_cast<CommonT>(*val2))
74 < static_cast<CommonT>(tolerance);
75}
76
77// Overload for rvalue references
78template <NumberType T>
79inline bool
80almost_equal(T&& val, T&& val2, T tolerance = tolerance_equality_float)
81{
82 using CommonT = std::common_type_t<T, T>;
83 return std::abs(static_cast<CommonT>(val) - static_cast<CommonT>(val2))
84 < static_cast<CommonT>(tolerance);
85}
86
87#endif
Definition traits.hpp:20
Definition traits.hpp:28
Definition traits.hpp:37