From c76e0716c4415d39e095feb02a09bce2d3d4d815 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 13 Nov 2015 13:50:58 +1100 Subject: [PATCH] maths: annotate with gnu::const where appropriate --- maths.hpp | 76 +++++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/maths.hpp b/maths.hpp index 277b19b5..a4f0e28a 100644 --- a/maths.hpp +++ b/maths.hpp @@ -43,16 +43,16 @@ namespace util { namespace util { template constexpr T - pow2 [[gnu::pure]] (T value) + pow2 [[gnu::const]] (T value) { return value * value; } } -template constexpr T pow2 [[gnu::pure]] (T value) { return util::pow2 (value); } +template constexpr T pow2 [[gnu::const]] (T value) { return util::pow2 (value); } //----------------------------------------------------------------------------- template constexpr T -pow [[gnu::pure]] (T x, unsigned y); +pow [[gnu::const]] (T x, unsigned y); namespace util { @@ -64,26 +64,26 @@ namespace util { //----------------------------------------------------------------------------- template bool -is_pow2 [[gnu::pure]] (T value); +is_pow2 [[gnu::const]] (T value); //----------------------------------------------------------------------------- // Logarithms template T -log2 [[gnu::pure]] (T val); +log2 [[gnu::const]] (T val); template T -log2up [[gnu::pure]] (T val); +log2up [[gnu::const]] (T val); //----------------------------------------------------------------------------- // Roots template double -rootsquare [[gnu::pure]] (T a, T b); +rootsquare [[gnu::const]] (T a, T b); //----------------------------------------------------------------------------- @@ -105,12 +105,12 @@ round_to [[gnu::const]] (T value, U size) template T -round_pow2 [[gnu::pure]] (T value); +round_pow2 [[gnu::const]] (T value); template constexpr T -divup [[gnu::pure]] (const T a, const U b) +divup [[gnu::const]] (const T a, const U b) { return (a + b - 1) / b; } @@ -118,14 +118,14 @@ divup [[gnu::pure]] (const T a, const U b) // Classification template bool -is_integer [[gnu::pure]] (const T& value); +is_integer [[gnu::const]] (const T& value); //----------------------------------------------------------------------------- // Properties template unsigned -digits [[gnu::pure]] (const T& value); +digits [[gnu::const]] (const T& value); //----------------------------------------------------------------------------- @@ -162,18 +162,18 @@ identity (const T& t) // Comparisons template bool -almost_equal [[gnu::pure]] (const T &a, const T &b) +almost_equal [[gnu::const]] (const T &a, const T &b) { return a == b; } template <> bool -almost_equal [[gnu::pure]] (const float &a, const float &b); +almost_equal [[gnu::const]] (const float &a, const float &b); template <> bool -almost_equal [[gnu::pure]] (const double &a, const double &b); +almost_equal [[gnu::const]] (const double &a, const double &b); template @@ -181,7 +181,7 @@ typename std::enable_if< std::is_arithmetic::value && std::is_arithmetic::value, bool >::type -almost_equal [[gnu::pure]] (Ta a, Tb b) { +almost_equal [[gnu::const]] (Ta a, Tb b) { return almost_equal (static_cast(a), static_cast(b)); } @@ -192,7 +192,7 @@ typename std::enable_if< !std::is_arithmetic::value || !std::is_arithmetic::value, bool >::type -almost_equal [[gnu::pure]] (const Ta &a, const Tb &b) +almost_equal [[gnu::const]] (const Ta &a, const Tb &b) { return a == b; } @@ -201,20 +201,20 @@ almost_equal [[gnu::pure]] (const Ta &a, const Tb &b) #pragma GCC diagnostic ignored "-Wfloat-equal" template bool -exactly_equal [[gnu::pure]] (const T &a, const U &b) +exactly_equal [[gnu::const]] (const T &a, const U &b) { return a == b; } #pragma GCC diagnostic pop template bool -almost_zero [[gnu::pure]] (T a) +almost_zero [[gnu::const]] (T a) { return almost_equal (a, 0); } template bool -exactly_zero [[gnu::pure]] (T a) +exactly_zero [[gnu::const]] (T a) { return exactly_equal (a, static_cast (0)); } @@ -230,7 +230,7 @@ constexpr T E = T(2.71828182845904523536028747135266250); template constexpr T -to_degrees [[gnu::pure]] (T radians) +to_degrees [[gnu::const]] (T radians) { static_assert (std::is_floating_point::value, "undefined for integral types"); return radians * 180 / PI; @@ -239,7 +239,7 @@ to_degrees [[gnu::pure]] (T radians) template constexpr T -to_radians [[gnu::pure]] (T degrees) +to_radians [[gnu::const]] (T degrees) { static_assert (std::is_floating_point::value, "undefined for integral types"); return degrees / 180 * PI; @@ -249,7 +249,7 @@ to_radians [[gnu::pure]] (T degrees) //! Normalised sinc function template constexpr T -sincn [[gnu::pure]] (T x) +sincn [[gnu::const]] (T x) { return almost_zero (x) ? 1 : std::sin (PI * x) / (PI * x); } @@ -258,7 +258,7 @@ sincn [[gnu::pure]] (T x) //! Unnormalised sinc function template constexpr T -sincu [[gnu::pure]] (T x) +sincu [[gnu::const]] (T x) { return almost_zero (x) ? 1 : std::sin (x) / x; } @@ -266,7 +266,7 @@ sincu [[gnu::pure]] (T x) //----------------------------------------------------------------------------- constexpr uintmax_t -factorial [[gnu::pure]] (unsigned i) +factorial [[gnu::const]] (unsigned i) { return i <= 1 ? 0 : i * factorial (i - 1); } @@ -274,7 +274,7 @@ factorial [[gnu::pure]] (unsigned i) /// stirlings approximation of factorials constexpr uintmax_t -stirling [[gnu::pure]] (unsigned n) +stirling [[gnu::const]] (unsigned n) { return static_cast ( std::sqrt (2 * PI * n) * std::pow (n / E, n) @@ -283,7 +283,7 @@ stirling [[gnu::pure]] (unsigned n) constexpr uintmax_t -combination [[gnu::pure]] (unsigned n, unsigned k) +combination [[gnu::const]] (unsigned n, unsigned k) { return factorial (n) / (factorial (k) / (factorial (n - k))); } @@ -319,7 +319,7 @@ fsum (InputIt first, InputIt last) namespace util { template constexpr T - min [[gnu::pure]] (const T a) + min [[gnu::const]] (const T a) { return a; } @@ -329,7 +329,7 @@ namespace util { std::is_integral::type>::value == std::is_integral::type>::value, typename std::common_type::type >::type - min [[gnu::pure]] (const T a, const U b, Args ...args) + min [[gnu::const]] (const T a, const U b, Args ...args) { return min (a < b ? a : b, std::forward (args)...); } @@ -339,7 +339,7 @@ namespace util { /// Variadic maximum template constexpr T - max [[gnu::pure]] (const T a) + max [[gnu::const]] (const T a) { return a; } @@ -349,7 +349,7 @@ namespace util { std::is_integral::type>::value == std::is_integral::type>::value, typename std::common_type::type >::type - max [[gnu::pure]] (const T a, const U b, Args ...args) + max [[gnu::const]] (const T a, const U b, Args ...args) { return max (a > b ? a : b, std::forward (args)...); } @@ -361,7 +361,7 @@ namespace util { // min/max clamping template constexpr T -limit [[gnu::pure]] (const T val, const U lo, const V hi) +limit [[gnu::const]] (const T val, const U lo, const V hi) { lo <= hi ? (void)0 : panic (); @@ -374,7 +374,7 @@ limit [[gnu::pure]] (const T val, const U lo, const V hi) // clamped cubic hermite interpolation template T -smoothstep [[gnu::pure]] (T a, T b, T x) +smoothstep [[gnu::const]] (T a, T b, T x) { CHECK_LE(a, b); x = limit ((x - a) / (b - a), T{0}, T{1}); @@ -393,7 +393,7 @@ constexpr typename std::enable_if< !std::is_floating_point::value && std::is_floating_point::value, U >::type -renormalise [[gnu::pure]] (T t) +renormalise [[gnu::const]] (T t) { return t / static_cast (std::numeric_limits::max ()); } @@ -405,7 +405,7 @@ constexpr typename std::enable_if< std::is_floating_point::value && !std::is_floating_point::value, U >::type -renormalise [[gnu::pure]] (T t) +renormalise [[gnu::const]] (T t) { // Ideally std::ldexp would be involved but it complicates handing // integers with greater precision than our floating point type. Also it @@ -440,7 +440,7 @@ typename std::enable_if< std::is_floating_point::value && !std::is_same::value, U >::type -renormalise [[gnu::pure]] (T t) +renormalise [[gnu::const]] (T t) { return static_cast (t); } @@ -454,7 +454,7 @@ typename std::enable_if< std::is_integral::value && (sizeof (T) > sizeof (U)), U >::type -renormalise [[gnu::pure]] (T t) +renormalise [[gnu::const]] (T t) { static_assert (sizeof (T) > sizeof (U), "assumes right shift is sufficient"); @@ -473,7 +473,7 @@ typename std::enable_if< std::is_integral::value && sizeof (T) < sizeof (U), U >::type -renormalise [[gnu::pure]] (T t) +renormalise [[gnu::const]] (T t) { static_assert (sizeof (T) < sizeof (U), "assumes bit creation is required to fill space"); @@ -500,7 +500,7 @@ constexpr typename std::enable_if< std::is_same::value, U >::type -renormalise [[gnu::pure]] (T t) +renormalise [[gnu::const]] (T t) { return t; } #include "maths.ipp"