maths: allow any integral exponent for pow()

This commit is contained in:
Danny Robson 2018-01-01 15:45:56 +11:00
parent ed7ab6cb9e
commit 4bd693a799
4 changed files with 23 additions and 15 deletions

View File

@ -30,10 +30,10 @@ namespace util {
CHECK_GE (t, 0); CHECK_GE (t, 0);
CHECK_LE (t, 1); CHECK_LE (t, 1);
auto v0 = pow (1 - t, 3) * m_points[0]; auto v0 = pow (1 - t, 3u) * m_points[0];
auto v1 = 3 * pow2 (1 - t) * t * m_points[1]; auto v1 = 3 * pow2 (1 - t) * t * m_points[1];
auto v2 = 3 * pow2 (1 - t) * t * m_points[2]; auto v2 = 3 * pow2 (1 - t) * t * m_points[2];
auto v3 = pow (t, 3) * m_points[3]; auto v3 = pow (t, 3u) * m_points[3];
return { return {
v0.x + v1.x + v2.x + v3.x, v0.x + v1.x + v2.x + v3.x,

View File

@ -192,27 +192,27 @@ suffix_to_multiplier (char c)
switch (c) { switch (c) {
case 'e': case 'e':
case 'E': case 'E':
return util::pow (1024UL, 6); return util::pow (1024UL, 6u);
case 'p': case 'p':
case 'P': case 'P':
return util::pow (1024UL, 5); return util::pow (1024UL, 5u);
case 't': case 't':
case 'T': case 'T':
return util::pow (1024UL, 4); return util::pow (1024UL, 4u);
case 'g': case 'g':
case 'G': case 'G':
return util::pow (1024UL, 3); return util::pow (1024UL, 3u);
case 'm': case 'm':
case 'M': case 'M':
return util::pow (1024UL, 2); return util::pow (1024UL, 2u);
case 'k': case 'k':
case 'K': case 'K':
return util::pow (1024UL, 1); return util::pow (1024UL, 1u);
default: default:
const char str[2] = { c, '\0' }; const char str[2] = { c, '\0' };

View File

@ -221,11 +221,19 @@ namespace util {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
template <typename T> template <
constexpr T typename BaseT,
pow [[gnu::const]] (T x, unsigned y) typename ExponentT,
typename = std::enable_if_t<
std::is_unsigned_v<ExponentT>,
void
>
>
constexpr BaseT
pow [[gnu::const]] (BaseT base, ExponentT exponent)
{ {
return y == 0 ? T{1} : x * pow (x, y - 1); assert (exponent >= 0);
return exponent == 0 ? BaseT{1} : base * pow (base, exponent - 1);
} }

View File

@ -8,9 +8,9 @@ using util::pow;
constexpr float order2 (float x) { return x * x + 3 * x - 7.f; } constexpr float order2 (float x) { return x * x + 3 * x - 7.f; }
constexpr float order4 (float x) { return 10 * pow (x, 4u) constexpr float order4 (float x) { return 10 * util::pow (x, 4u)
-270 * pow (x, 2u) -270 * util::pow (x, 2u)
-140 * pow (x, 1u) -140 * util::pow (x, 1u)
+1200; } +1200; }
struct { struct {