blob: 4158803671d2a119d8fcf71cd44153b062ac8c7f (
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
|
#pragma once
#include "core/math/concepts.hh"
namespace math
{
template<typename type, std::size_t size>
constexpr std::size_t array_size(const type (&)[size]);
} // namespace math
namespace math
{
template<std::integral scalar>
constexpr scalar log2(const scalar x);
template<std::signed_integral scalar>
constexpr scalar mod_signed(const scalar x, const scalar m);
template<math::signed_arithmetic result_scalar, math::arithmetic scalar>
constexpr result_scalar sign(const scalar x);
} // namespace math
namespace math
{
template<math::arithmetic scalar>
constexpr scalar degrees(const scalar x);
template<math::arithmetic scalar>
constexpr scalar radians(const scalar x);
} // namespace math
template<typename type, std::size_t size>
constexpr std::size_t math::array_size(const type (&)[size])
{
return size;
}
template<std::integral scalar>
constexpr scalar math::log2(const scalar x)
{
if(x < static_cast<scalar>(2))
return static_cast<scalar>(0);
return math::log2<scalar>((x + static_cast<scalar>(1)) >> 1) + static_cast<scalar>(1);
}
template<std::signed_integral scalar>
constexpr scalar math::mod_signed(const scalar x, const scalar m)
{
auto result = static_cast<scalar>(x % m);
if(result < static_cast<scalar>(0))
return result + m;
return result;
}
template<math::signed_arithmetic result_scalar, math::arithmetic scalar>
constexpr result_scalar math::sign(const scalar x)
{
if(x < static_cast<scalar>(0))
return static_cast<result_scalar>(-1);
if(x > static_cast<scalar>(0))
return static_cast<result_scalar>(+1);
return static_cast<result_scalar>(0);
}
template<math::arithmetic scalar>
constexpr scalar math::degrees(const scalar x)
{
return static_cast<scalar>(static_cast<double>(x) * 180.0 / M_PI);
}
template<math::arithmetic scalar>
constexpr scalar math::radians(const scalar x)
{
return static_cast<scalar>(static_cast<double>(x) * M_PI / 180.0);
}
|