2 #include <ossia/detail/config.hpp>
4 #include <ossia/detail/ssize.hpp>
19 #if defined(__ANDROID_API__)
34 const constexpr
auto pi = 3.141592653589793238462643383279502884;
35 const constexpr
auto two_pi = 6.283185307179586476925286766559005768;
36 const constexpr
auto half_pi = 1.570796326794896619231321691639751442;
37 const constexpr
auto rad_to_deg = 5.729577951308232087679815481410517033e+01;
38 const constexpr
auto deg_to_rad = 1.745329251994329576923690768488612713e-02;
39 const constexpr
auto ln_2 = 6.931471805599453094172321214581765680e-01;
40 const constexpr
auto sqrt_2 = 1.414213562373095048801688724209698078e+00;
43 T ipow(T x,
int N) noexcept
46 for(
int i = 1; i < N; i++)
54 auto norm(T x, T y) noexcept
56 return std::sqrt(ipow(x, 2) + ipow(y, 2));
60 auto norm(T x, T y, T z) noexcept
62 return std::sqrt(ipow(x, 2) + ipow(y, 2) + ipow(z, 2));
65 inline constexpr
size_t constexpr_log2(
size_t n) noexcept
67 return ((n < 2) ? 0 : 1 + constexpr_log2(n / 2));
71 inline constexpr
size_t constexpr_abs(T n) noexcept
73 return n < 0 ? -n : n;
78 inline constexpr int64_t constexpr_floor(T val) noexcept
80 const auto val_int = (int64_t)val;
81 const T fval_int = (T)val_int;
82 return (val >= (T)0 ? fval_int : (val == fval_int ? val : fval_int - 1));
86 inline constexpr int64_t constexpr_ceil(T f)
89 return f > i ? i + 1 : i;
95 template <
class T,
class U>
96 OSSIA_INLINE constexpr
auto max(
const T a,
const U b) noexcept ->
97 typename std::conditional<(
sizeof(T) >
sizeof(U)), T, U>::type
99 static_assert(std::is_integral_v<T> && std::is_integral_v<U>);
100 static_assert(std::is_signed_v<T> == std::is_signed_v<U>);
101 return a < b ? b : a;
104 static OSSIA_INLINE constexpr
float max(
float a,
const float b) noexcept
106 return a < b ? b : a;
108 static OSSIA_INLINE constexpr
double max(
double a,
const double b) noexcept
110 return a < b ? b : a;
112 static OSSIA_INLINE constexpr
double max(
float a,
const double b) noexcept
114 return a < b ? b : a;
116 static OSSIA_INLINE constexpr
double max(
double a,
const float b) noexcept
118 return a < b ? b : a;
124 template <
class T,
class U>
125 OSSIA_INLINE constexpr
auto min(
const T a,
const U b) noexcept ->
126 typename std::conditional<(
sizeof(T) >
sizeof(U)), T, U>::type
128 static_assert(std::is_integral_v<T> && std::is_integral_v<U>);
129 static_assert(std::is_signed_v<T> == std::is_signed_v<U>);
130 return a > b ? b : a;
133 static OSSIA_INLINE constexpr
float min(
float a,
const float b) noexcept
135 return a > b ? b : a;
137 static OSSIA_INLINE constexpr
double min(
double a,
const double b) noexcept
139 return a > b ? b : a;
141 static OSSIA_INLINE constexpr
double min(
float a,
const double b) noexcept
143 return a > b ? b : a;
145 static OSSIA_INLINE constexpr
double min(
double a,
const float b) noexcept
147 return a > b ? b : a;
154 OSSIA_INLINE constexpr T
clamp(T d,
const T
min,
const T
max) noexcept
159 static OSSIA_INLINE constexpr int64_t
160 clamp(int64_t d,
const int64_t
min,
const int64_t
max) noexcept
164 static OSSIA_INLINE constexpr int64_t
165 clamp(int64_t d,
const int32_t
min,
const int64_t
max) noexcept
169 static OSSIA_INLINE constexpr int64_t
170 clamp(int64_t d,
const int32_t
min,
const int32_t
max) noexcept
174 static OSSIA_INLINE constexpr int64_t
175 clamp(int64_t d,
const int64_t
min,
const int32_t
max) noexcept
179 static OSSIA_INLINE constexpr int32_t
180 clamp(int32_t d,
const int32_t
min,
const int32_t
max) noexcept
184 static OSSIA_INLINE constexpr int64_t
185 clamp(int32_t d,
const int64_t
min,
const int64_t
max) noexcept
189 static OSSIA_INLINE constexpr int64_t
190 clamp(int32_t d,
const int32_t
min,
const int64_t
max) noexcept
194 static OSSIA_INLINE constexpr int64_t
195 clamp(int32_t d,
const int64_t
min,
const int32_t
max) noexcept
200 static OSSIA_INLINE constexpr uint64_t
201 clamp(uint64_t d,
const uint64_t
min,
const uint64_t
max) noexcept
205 static OSSIA_INLINE constexpr uint64_t
206 clamp(uint64_t d,
const uint32_t
min,
const uint64_t
max) noexcept
210 static OSSIA_INLINE constexpr uint64_t
211 clamp(uint64_t d,
const uint32_t
min,
const uint32_t
max) noexcept
215 static OSSIA_INLINE constexpr uint64_t
216 clamp(uint64_t d,
const uint64_t
min,
const uint32_t
max) noexcept
220 static OSSIA_INLINE constexpr uint32_t
221 clamp(uint32_t d,
const uint32_t
min,
const uint32_t
max) noexcept
225 static OSSIA_INLINE constexpr uint64_t
226 clamp(uint32_t d,
const uint64_t
min,
const uint64_t
max) noexcept
230 static OSSIA_INLINE constexpr uint64_t
231 clamp(uint32_t d,
const uint32_t
min,
const uint64_t
max) noexcept
235 static OSSIA_INLINE constexpr uint64_t
236 clamp(uint32_t d,
const uint64_t
min,
const uint32_t
max) noexcept
241 static OSSIA_INLINE constexpr
double
242 clamp(
double d,
const double min,
const double max) noexcept
246 static OSSIA_INLINE constexpr
double
247 clamp(
double d,
const float min,
const double max) noexcept
251 static OSSIA_INLINE constexpr
double
252 clamp(
double d,
const float min,
const float max) noexcept
256 static OSSIA_INLINE constexpr
double
257 clamp(
double d,
const double min,
const float max) noexcept
261 static OSSIA_INLINE constexpr
float
262 clamp(
float d,
const float min,
const float max) noexcept
266 static OSSIA_INLINE constexpr
double
267 clamp(
float d,
const double min,
const double max) noexcept
271 static OSSIA_INLINE constexpr
double
272 clamp(
float d,
const float min,
const double max) noexcept
276 static OSSIA_INLINE constexpr
double
277 clamp(
float d,
const double min,
const float max) noexcept
285 OSSIA_INLINE constexpr T wrap(
const T val,
const T low,
const T high) noexcept
287 if((val >= low) && (val < high))
290 return low + std::fmod(val - low, std::fabs(low - high));
292 return high - std::fmod(low - val, std::fabs(low - high));
296 OSSIA_INLINE constexpr T fold(
const T val,
const T low,
const T high) noexcept
298 if((val >= low) && (val <= high))
302 return low + std::fabs(ossia::remainder(val - low, 2. * std::fabs(low - high)));
307 OSSIA_INLINE constexpr std::pair<T, T> div(T num, T denom) noexcept
309 return {num / denom, num % denom};
313 OSSIA_INLINE
float log_to_normalized(
float min,
float range,
float val) noexcept
315 return val <=
min ? 0.f : std::log1pf(val -
min) / std::log1pf(range);
317 OSSIA_INLINE
float normalized_to_log(
float min,
float range,
float val) noexcept
319 const float res = std::expm1f(val * std::log1pf(range)) +
min;
320 return std::min(res,
min + range);
323 template <
typename T,
typename R>
324 constexpr OSSIA_INLINE
bool valid_index(T idx,
const R& range) noexcept
326 using range_size_type =
typename R::size_type;
327 if constexpr(std::is_signed_v<T>)
329 if constexpr(std::is_signed_v<range_size_type>)
330 return idx >= 0 && idx < range.size();
332 return idx >= 0 && idx < std::ssize(range);
336 if constexpr(std::is_signed_v<range_size_type>)
337 return idx < static_cast<std::size_t>(range.size());
339 return idx < range.size();
constexpr OSSIA_INLINE T clamp(T d, const T min, const T max) noexcept
clamp Returns the value bounded by a min and a max
Definition: math.hpp:154
constexpr OSSIA_INLINE auto min(const T a, const U b) noexcept -> typename std::conditional<(sizeof(T) > sizeof(U)), T, U >::type
min function tailored for values
Definition: math.hpp:125
constexpr OSSIA_INLINE auto max(const T a, const U b) noexcept -> typename std::conditional<(sizeof(T) > sizeof(U)), T, U >::type
max function tailored for values
Definition: math.hpp:96