diff options
| author | untodesu <kirill@untode.su> | 2025-12-11 15:14:26 +0500 |
|---|---|---|
| committer | untodesu <kirill@untode.su> | 2025-12-11 15:14:26 +0500 |
| commit | f40d09cb8f712e87691af4912f3630d92d692779 (patch) | |
| tree | 7ac3a4168ff722689372fd489c6f94d0a2546e8f /src/core/math/constexpr.hh | |
| parent | 8bcbd2729388edc63c82d77d314b583af1447c49 (diff) | |
| download | voxelius-f40d09cb8f712e87691af4912f3630d92d692779.tar.bz2 voxelius-f40d09cb8f712e87691af4912f3630d92d692779.zip | |
Shuffle stuff around
- Use the new and improved hierarchy I figured out when making Prospero chat
- Re-add NSIS scripts, again from Prospero
- Update most build and utility scripts with their most recent versions
Diffstat (limited to 'src/core/math/constexpr.hh')
| -rw-r--r-- | src/core/math/constexpr.hh | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/core/math/constexpr.hh b/src/core/math/constexpr.hh new file mode 100644 index 0000000..4158803 --- /dev/null +++ b/src/core/math/constexpr.hh @@ -0,0 +1,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); +} |
