From 8bcbd2729388edc63c82d77d314b583af1447c49 Mon Sep 17 00:00:00 2001 From: untodesu Date: Sun, 14 Sep 2025 19:16:44 +0500 Subject: Cleanup math with qfengine ports again --- core/math/constexpr.hh | 210 ++++++++++--------------------------------------- 1 file changed, 42 insertions(+), 168 deletions(-) (limited to 'core/math/constexpr.hh') diff --git a/core/math/constexpr.hh b/core/math/constexpr.hh index e6c4b6d..6641f23 100644 --- a/core/math/constexpr.hh +++ b/core/math/constexpr.hh @@ -4,195 +4,69 @@ namespace math { -template -constexpr static inline const T abs(const T x); -template -constexpr static inline const std::size_t array_size(const T (&)[L]); -template -constexpr static inline const T ceil(const F x); -template -constexpr static inline const T degrees(const T x); -template -constexpr static inline const T floor(const F x); -template -constexpr static inline const T clamp(const T x, const T min, const T max); -template -constexpr static inline const T lerp(const T x, const T y, const F a); -template -constexpr static inline const T log2(const T x); -template -constexpr static inline const T max(const T x, const T y); -template -constexpr static inline const T min(const T x, const T y); -template -requires std::is_signed_v -constexpr static inline const T mod_signed(const T x, const T m); -template -constexpr static inline const T pow2(const T x); -template -constexpr static inline const T radians(const T x); -template -constexpr static inline const bool range(const T x, const T min, const T max); -template -constexpr static inline const T sign(const F x); -template -constexpr static inline const T smoothstep(const T x, const T y, const F a); +template +constexpr std::size_t array_size(const type (&)[size]); } // namespace math -template -constexpr static inline const T math::abs(const T x) -{ - if(x < static_cast(0)) { - return -x; - } - else { - return x; - } -} - -template -constexpr static inline const std::size_t math::array_size(const T (&)[L]) -{ - return L; -} - -template -constexpr static inline const T math::ceil(const F x) -{ - const T ival = static_cast(x); - - if(ival < x) { - return ival + static_cast(1); - } - else { - return ival; - } -} - -template -constexpr static inline const T math::degrees(const T x) -{ - return x * static_cast(180.0) / static_cast(M_PI); -} - -template -constexpr static inline const T math::floor(const F x) -{ - const T ival = static_cast(x); - - if(ival > x) { - return ival - static_cast(1); - } - else { - return ival; - } -} - -template -constexpr static inline const T math::clamp(const T x, const T min, const T max) -{ - if(x < min) { - return min; - } - else if(x > max) { - return max; - } - else { - return x; - } -} - -template -constexpr static inline const T math::lerp(const T x, const T y, const F a) +namespace math { - return static_cast(static_cast(x) * (static_cast(1.0f) - a) + static_cast(y) * a); -} +template +constexpr scalar log2(const scalar x); +template +constexpr scalar mod_signed(const scalar x, const scalar m); +template +constexpr result_scalar sign(const scalar x); +} // namespace math -template -constexpr static inline const T math::log2(const T x) +namespace math { - if(x < 2) { - return 0; - } - else { - return math::log2((x + 1) >> 1) + 1; - } -} +template +constexpr scalar degrees(const scalar x); +template +constexpr scalar radians(const scalar x); +} // namespace math -template -constexpr static inline const T math::max(const T x, const T y) +template +constexpr std::size_t math::array_size(const type (&)[size]) { - if(x < y) { - return y; - } - else { - return x; - } + return size; } -template -constexpr static inline const T math::min(const T x, const T y) +template +constexpr scalar math::log2(const scalar x) { - if(x > y) { - return y; - } - else { - return x; - } + if(x < static_cast(2)) + return static_cast(0); + return math::log2((x + static_cast(1)) >> 1) + static_cast(1); } -template -requires std::is_signed_v -constexpr static inline const T math::mod_signed(const T x, const T m) +template +constexpr scalar math::mod_signed(const scalar x, const scalar m) { - auto result = static_cast(x % m); - - if(result < T(0)) { + auto result = static_cast(x % m); + if(result < static_cast(0)) return result + m; - } - else { - return result; - } -} - -template -constexpr static inline const T math::pow2(const T x) -{ - T value = static_cast(1); - while(value < x) - value *= static_cast(2); - return value; -} - -template -constexpr static inline const T math::radians(const T x) -{ - return x * static_cast(M_PI) / static_cast(180.0); + return result; } -template -constexpr static inline const bool math::range(const T x, const T min, const T max) +template +constexpr result_scalar math::sign(const scalar x) { - return ((x >= min) && (x <= max)); + if(x < static_cast(0)) + return static_cast(-1); + if(x > static_cast(0)) + return static_cast(+1); + return static_cast(0); } -template -constexpr static inline const T math::sign(const F x) +template +constexpr scalar math::degrees(const scalar x) { - if(x < F(0)) { - return T(-1); - } - else if(x > F(0)) { - return T(+1); - } - else { - return T(0); - } + return static_cast(static_cast(x) * 180.0 / M_PI); } -template -constexpr static inline const T math::smoothstep(const T x, const T y, const F a) +template +constexpr scalar math::radians(const scalar x) { - const F t = math::clamp((a - x) / (y - x), F(0), F(1)); - return static_cast(t * t * (F(3) - F(2) * t)); + return static_cast(static_cast(x) * M_PI / 180.0); } -- cgit