Debian Codegraph @

Source/Core/Common/MathUtil.h

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <algorithm>
7 #include <bit>
8 #include <cmath>
9 #include <concepts>
10 #include <limits>
11 #include <type_traits>
12 #include <utility>
13
14 #include "Common/CommonTypes.h"
15
16 namespace MathUtil
17 {
18 constexpr double TAU = 6.2831853071795865;
19 constexpr double PI = TAU / 2;
20 constexpr double GRAVITY_ACCELERATION = 9.80665;
21
22 template <typename T>
23 constexpr auto Sign(const T& val) -> decltype((T{} < val) - (val < T{}))
24 {
25 return (T{} < val) - (val < T{});
26 }
27
28 template <typename T, typename F>
29 constexpr auto Lerp(const T& x, const T& y, const F& a) -> decltype(x + (y - x) * a)
30 {
31 return x + (y - x) * a;
32 }
33
34 // Casts the specified value to a Dest. The value will be clamped to fit in the destination type.
35 // Warning: The result of SaturatingCast(NaN) is undefined.
36 template <std::integral Dest, typename T>
37 constexpr Dest SaturatingCast(T value)
38 {
39 constexpr Dest lo = std::numeric_limits<Dest>::min();
40 constexpr Dest hi = std::numeric_limits<Dest>::max();
41
42 if constexpr (std::is_integral_v<T>)
43 {
44 if (std::cmp_less(value, lo))
45 return lo;
46 if (std::cmp_greater(value, hi))
47 return hi;
48 }
49 else
50 {
51 if (value < static_cast<T>(lo))
52 return lo;
53 if (value >= static_cast<T>(hi))
54 return hi;
55 }
56 return static_cast<Dest>(value);
57 }
58
59 template <typename T>
60 constexpr bool IsPow2(T imm)
61 {
62 return imm > 0 && (imm & (imm - 1)) == 0;
63 }
64
65 constexpr u32 NextPowerOf2(u32 value)
66 {
67 --value;
68 value |= value >> 1;
69 value |= value >> 2;
70 value |= value >> 4;
71 value |= value >> 8;
72 value |= value >> 16;
73 ++value;
74
75 return value;
76 }
77
78 template <class T>
79 struct Rectangle
80 {
81 T left{};
82 T top{};
83 T right{};
84 T bottom{};
85
86 constexpr Rectangle() = default;
87
88 constexpr Rectangle(T theLeft, T theTop, T theRight, T theBottom)
89 : left(theLeft), top(theTop), right(theRight), bottom(theBottom)
90 {
91 }
92
93 constexpr bool operator==(const Rectangle& r) const
94 {
95 return left == r.left && top == r.top && right == r.right && bottom == r.bottom;
96 }
97
98 constexpr T GetWidth() const { return GetDistance(left, right); }
99 constexpr T GetHeight() const { return GetDistance(top, bottom); }
100 // If the rectangle is in a coordinate system with a lower-left origin, use
101 // this Clamp.
102 void ClampLL(T x1, T y1, T x2, T y2)
103 {
104 left = std::clamp(left, x1, x2);
105 right = std::clamp(right, x1, x2);
106 top = std::clamp(top, y2, y1);
107 bottom = std::clamp(bottom, y2, y1);
108 }
109
110 // If the rectangle is in a coordinate system with an upper-left origin,
111 // use this Clamp.
112 void ClampUL(T x1, T y1, T x2, T y2)
113 {
114 left = std::clamp(left, x1, x2);
115 right = std::clamp(right, x1, x2);
116 top = std::clamp(top, y1, y2);
117 bottom = std::clamp(bottom, y1, y2);
118 }
119
120 private:
121 constexpr T GetDistance(T a, T b) const
122 {
123 if constexpr (std::is_unsigned<T>())
124 return b > a ? b - a : a - b;
125 else
126 return std::abs(b - a);
127 }
128 };
129
130 template <typename T>
131 class RunningMean
132 {
133 public:
134 constexpr void Clear() { *this = {}; }
135
136 constexpr void Push(T x) { m_mean = m_mean + (x - m_mean) / ++m_count; }
137
138 constexpr size_t Count() const { return m_count; }
139 constexpr T Mean() const { return m_mean; }
140
141 private:
142 size_t m_count = 0;
143 T m_mean{};
144 };
145
146 template <typename T>
147 class RunningVariance
148 {
149 public:
150 constexpr void Clear() { *this = {}; }
151
152 constexpr void Push(T x)
153 {
154 const auto old_mean = m_running_mean.Mean();
155 m_running_mean.Push(x);
156 m_variance += (x - old_mean) * (x - m_running_mean.Mean());
157 }
158
159 constexpr size_t Count() const { return m_running_mean.Count(); }
160 constexpr T Mean() const { return m_running_mean.Mean(); }
161
162 constexpr T Variance() const { return m_variance / (Count() - 1); }
163 T StandardDeviation() const { return std::sqrt(Variance()); }
164
165 constexpr T PopulationVariance() const { return m_variance / Count(); }
166 T PopulationStandardDeviation() const { return std::sqrt(PopulationVariance()); }
167
168 private:
169 RunningMean<T> m_running_mean;
170 T m_variance{};
171 };
172
173 // Rounds down. 0 -> undefined
174 constexpr int IntLog2(u64 val)
175 {
176 return 63 - std::countl_zero(val);
177 }
178 } // namespace MathUtil