From cfe1185621c42a08c9a9dcc050f0d784054c5722 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 22 Dec 2017 12:33:29 +1100 Subject: [PATCH] maths: move log2 into header for constexpr --- maths.cpp | 21 --------------------- maths.hpp | 11 +++++++++-- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/maths.cpp b/maths.cpp index ec39ea19..cc3493e3 100644 --- a/maths.cpp +++ b/maths.cpp @@ -31,27 +31,6 @@ template uint32_t util::log2up (uint32_t); template uint64_t util::log2up (uint64_t); -/////////////////////////////////////////////////////////////////////////////// -template -T -util::log2 (T v) -{ - static_assert (std::is_integral::value, - "log2 is only implemented for integers"); - - T l = 0; - while (v >>= 1) - ++l; - - return l; -} - -template uint8_t util::log2 (uint8_t); -template uint16_t util::log2 (uint16_t); -template uint32_t util::log2 (uint32_t); -template uint64_t util::log2 (uint64_t); - - /////////////////////////////////////////////////////////////////////////////// template const float util::PI; template const double util::PI; diff --git a/maths.hpp b/maths.hpp index d6aa3f19..24ad136b 100644 --- a/maths.hpp +++ b/maths.hpp @@ -242,8 +242,15 @@ namespace util { //----------------------------------------------------------------------------- // Logarithms template - T - log2 (T val); + constexpr + std::enable_if_t, T> + log2 (T val) + { + T tally = 0; + while (val >>= 1) + ++tally; + return tally; + } //-------------------------------------------------------------------------