blob: 0afa3c8fad6eae0a0a762f212323eba005ef1d35 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#pragma once
#include <cstdlib>
#include <bit>
#include <cmath>
#ifdef __clang__
#pragma clang diagnostic ignored "-Wfloat-equal"
#endif
namespace floormat::math::detail {
template<typename T> struct int_type_for_;
template<> struct int_type_for_<float> { using type = int32_t; };
template<> struct int_type_for_<double> { using type = int64_t; };
template<typename T> using int_type_for = typename int_type_for_<T>::type;
} // namespace floormat::math::detail
namespace floormat::math {
template<typename T>
constexpr inline T abs(T x)
requires std::is_arithmetic_v<T>
{
static_assert(std::is_floating_point_v<T> ||
std::is_integral_v<T> && std::is_signed_v<T>);
return x < T{0} ? -x : x;
}
template <typename T>
requires std::is_arithmetic_v<T>
constexpr inline T sgn(T val)
{
return T(T{0} < val) - T(val < T{0});
}
template<typename T>
constexpr inline T sqrt(T x0)
requires std::is_floating_point_v<T>
{
if (std::is_constant_evaluated())
{
auto x = x0, prev = T{0};
while (x != prev)
{
prev = x;
x = T(0.5) * (x + x0 / x);
}
return x;
}
else
return std::sqrt(x0);
}
template<typename T>
requires std::is_integral_v<T>
constexpr inline double sqrt(T x)
{
return sqrt(double(x));
}
template<typename T>
requires std::is_floating_point_v<T>
constexpr inline T ceil(T x)
{
if (std::is_constant_evaluated())
{
using int_ = detail::int_type_for<T>;
const auto x0 = int_(x);
return x0 + int_{1} * (x > x0);
}
else
return std::ceil(x);
}
template<typename T>
requires std::is_floating_point_v<T>
constexpr inline T floor(T x)
{
if (std::is_constant_evaluated())
{
using int_ = detail::int_type_for<T>;
const auto x0 = int_(x);
return x0 - int_{1} * (x < T{0} && x != x0);
}
else
return std::floor(x);
}
} // namespace floormat::math
|