diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-10-17 02:58:16 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-10-17 02:59:12 +0100 |
commit | cfec8308a39ae4a2a1a1b1fe0df0a099470d9c78 (patch) | |
tree | fb406218d6d7f79e02331d89e6f0567c386d98e7 | |
parent | Don't dump stencil texture (diff) | |
download | ilt-cfec8308a39ae4a2a1a1b1fe0df0a099470d9c78.tar.bz2 ilt-cfec8308a39ae4a2a1a1b1fe0df0a099470d9c78.tar.xz ilt-cfec8308a39ae4a2a1a1b1fe0df0a099470d9c78.zip |
Simple constexpr pow function
-rw-r--r-- | lib/maths.cpp | 12 | ||||
-rw-r--r-- | lib/maths.h | 13 |
2 files changed, 25 insertions, 0 deletions
diff --git a/lib/maths.cpp b/lib/maths.cpp index ea245f0..f527881 100644 --- a/lib/maths.cpp +++ b/lib/maths.cpp @@ -116,6 +116,18 @@ normalize(float ang) return ang; } +static_assert(pow(1, 0) == 1); +static_assert(pow(1, 1) == 1); +static_assert(pow(1, 2) == 1); +static_assert(pow(2, 0) == 1); +static_assert(pow(2, 1) == 2); +static_assert(pow(2, 2) == 4); +static_assert(pow(2, 3) == 8); +static_assert(pow(3, 0) == 1); +static_assert(pow(3, 1) == 3); +static_assert(pow(3, 2) == 9); +static_assert(pow(pi, 3) == 31.006278991699219F); + float operator"" _mph(const long double v) { diff --git a/lib/maths.h b/lib/maths.h index 32f2884..97b7ef5 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -261,6 +261,19 @@ midpoint(const std::pair<T, T> & v) return std::midpoint(v.first, v.second); } +// std::pow is not constexpr +template<typename T> + requires requires(T n) { n *= n; } +constexpr inline T +pow(const T base, std::integral auto exp) +{ + T res {1}; + while (exp--) { + res *= base; + } + return res; +} + // Conversions template<typename T> inline constexpr auto |