maths: annotate with gnu::const where appropriate
This commit is contained in:
parent
02a42f282e
commit
c76e0716c4
76
maths.hpp
76
maths.hpp
@ -43,16 +43,16 @@ namespace util {
|
|||||||
namespace util {
|
namespace util {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr T
|
constexpr T
|
||||||
pow2 [[gnu::pure]] (T value)
|
pow2 [[gnu::const]] (T value)
|
||||||
{ return value * value; }
|
{ return value * value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> constexpr T pow2 [[gnu::pure]] (T value) { return util::pow2 (value); }
|
template <typename T> constexpr T pow2 [[gnu::const]] (T value) { return util::pow2 (value); }
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr T
|
constexpr T
|
||||||
pow [[gnu::pure]] (T x, unsigned y);
|
pow [[gnu::const]] (T x, unsigned y);
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
|
|
||||||
@ -64,26 +64,26 @@ namespace util {
|
|||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool
|
bool
|
||||||
is_pow2 [[gnu::pure]] (T value);
|
is_pow2 [[gnu::const]] (T value);
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// Logarithms
|
// Logarithms
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T
|
T
|
||||||
log2 [[gnu::pure]] (T val);
|
log2 [[gnu::const]] (T val);
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T
|
T
|
||||||
log2up [[gnu::pure]] (T val);
|
log2up [[gnu::const]] (T val);
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// Roots
|
// Roots
|
||||||
template <typename T>
|
template <typename T>
|
||||||
double
|
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 <typename T>
|
template <typename T>
|
||||||
T
|
T
|
||||||
round_pow2 [[gnu::pure]] (T value);
|
round_pow2 [[gnu::const]] (T value);
|
||||||
|
|
||||||
|
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
constexpr T
|
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; }
|
{ return (a + b - 1) / b; }
|
||||||
|
|
||||||
|
|
||||||
@ -118,14 +118,14 @@ divup [[gnu::pure]] (const T a, const U b)
|
|||||||
// Classification
|
// Classification
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool
|
bool
|
||||||
is_integer [[gnu::pure]] (const T& value);
|
is_integer [[gnu::const]] (const T& value);
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// Properties
|
// Properties
|
||||||
template <typename T>
|
template <typename T>
|
||||||
unsigned
|
unsigned
|
||||||
digits [[gnu::pure]] (const T& value);
|
digits [[gnu::const]] (const T& value);
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
@ -162,18 +162,18 @@ identity (const T& t)
|
|||||||
// Comparisons
|
// Comparisons
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool
|
bool
|
||||||
almost_equal [[gnu::pure]] (const T &a, const T &b)
|
almost_equal [[gnu::const]] (const T &a, const T &b)
|
||||||
{ return a == b; }
|
{ return a == b; }
|
||||||
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
bool
|
bool
|
||||||
almost_equal [[gnu::pure]] (const float &a, const float &b);
|
almost_equal [[gnu::const]] (const float &a, const float &b);
|
||||||
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
bool
|
bool
|
||||||
almost_equal [[gnu::pure]] (const double &a, const double &b);
|
almost_equal [[gnu::const]] (const double &a, const double &b);
|
||||||
|
|
||||||
|
|
||||||
template <typename Ta, typename Tb>
|
template <typename Ta, typename Tb>
|
||||||
@ -181,7 +181,7 @@ typename std::enable_if<
|
|||||||
std::is_arithmetic<Ta>::value && std::is_arithmetic<Tb>::value,
|
std::is_arithmetic<Ta>::value && std::is_arithmetic<Tb>::value,
|
||||||
bool
|
bool
|
||||||
>::type
|
>::type
|
||||||
almost_equal [[gnu::pure]] (Ta a, Tb b) {
|
almost_equal [[gnu::const]] (Ta a, Tb b) {
|
||||||
return almost_equal <decltype(a + b)> (static_cast<decltype(a + b)>(a),
|
return almost_equal <decltype(a + b)> (static_cast<decltype(a + b)>(a),
|
||||||
static_cast<decltype(a + b)>(b));
|
static_cast<decltype(a + b)>(b));
|
||||||
}
|
}
|
||||||
@ -192,7 +192,7 @@ typename std::enable_if<
|
|||||||
!std::is_arithmetic<Ta>::value || !std::is_arithmetic<Tb>::value,
|
!std::is_arithmetic<Ta>::value || !std::is_arithmetic<Tb>::value,
|
||||||
bool
|
bool
|
||||||
>::type
|
>::type
|
||||||
almost_equal [[gnu::pure]] (const Ta &a, const Tb &b)
|
almost_equal [[gnu::const]] (const Ta &a, const Tb &b)
|
||||||
{ return a == b; }
|
{ return a == b; }
|
||||||
|
|
||||||
|
|
||||||
@ -201,20 +201,20 @@ almost_equal [[gnu::pure]] (const Ta &a, const Tb &b)
|
|||||||
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
bool
|
bool
|
||||||
exactly_equal [[gnu::pure]] (const T &a, const U &b)
|
exactly_equal [[gnu::const]] (const T &a, const U &b)
|
||||||
{ return a == b; }
|
{ return a == b; }
|
||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool
|
bool
|
||||||
almost_zero [[gnu::pure]] (T a)
|
almost_zero [[gnu::const]] (T a)
|
||||||
{ return almost_equal (a, 0); }
|
{ return almost_equal (a, 0); }
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool
|
bool
|
||||||
exactly_zero [[gnu::pure]] (T a)
|
exactly_zero [[gnu::const]] (T a)
|
||||||
{ return exactly_equal (a, static_cast<T> (0)); }
|
{ return exactly_equal (a, static_cast<T> (0)); }
|
||||||
|
|
||||||
|
|
||||||
@ -230,7 +230,7 @@ constexpr T E = T(2.71828182845904523536028747135266250);
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr T
|
constexpr T
|
||||||
to_degrees [[gnu::pure]] (T radians)
|
to_degrees [[gnu::const]] (T radians)
|
||||||
{
|
{
|
||||||
static_assert (std::is_floating_point<T>::value, "undefined for integral types");
|
static_assert (std::is_floating_point<T>::value, "undefined for integral types");
|
||||||
return radians * 180 / PI<T>;
|
return radians * 180 / PI<T>;
|
||||||
@ -239,7 +239,7 @@ to_degrees [[gnu::pure]] (T radians)
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr T
|
constexpr T
|
||||||
to_radians [[gnu::pure]] (T degrees)
|
to_radians [[gnu::const]] (T degrees)
|
||||||
{
|
{
|
||||||
static_assert (std::is_floating_point<T>::value, "undefined for integral types");
|
static_assert (std::is_floating_point<T>::value, "undefined for integral types");
|
||||||
return degrees / 180 * PI<T>;
|
return degrees / 180 * PI<T>;
|
||||||
@ -249,7 +249,7 @@ to_radians [[gnu::pure]] (T degrees)
|
|||||||
//! Normalised sinc function
|
//! Normalised sinc function
|
||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr T
|
constexpr T
|
||||||
sincn [[gnu::pure]] (T x)
|
sincn [[gnu::const]] (T x)
|
||||||
{
|
{
|
||||||
return almost_zero (x) ? 1 : std::sin (PI<T> * x) / (PI<T> * x);
|
return almost_zero (x) ? 1 : std::sin (PI<T> * x) / (PI<T> * x);
|
||||||
}
|
}
|
||||||
@ -258,7 +258,7 @@ sincn [[gnu::pure]] (T x)
|
|||||||
//! Unnormalised sinc function
|
//! Unnormalised sinc function
|
||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr T
|
constexpr T
|
||||||
sincu [[gnu::pure]] (T x)
|
sincu [[gnu::const]] (T x)
|
||||||
{
|
{
|
||||||
return almost_zero (x) ? 1 : std::sin (x) / x;
|
return almost_zero (x) ? 1 : std::sin (x) / x;
|
||||||
}
|
}
|
||||||
@ -266,7 +266,7 @@ sincu [[gnu::pure]] (T x)
|
|||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
constexpr uintmax_t
|
constexpr uintmax_t
|
||||||
factorial [[gnu::pure]] (unsigned i)
|
factorial [[gnu::const]] (unsigned i)
|
||||||
{
|
{
|
||||||
return i <= 1 ? 0 : i * factorial (i - 1);
|
return i <= 1 ? 0 : i * factorial (i - 1);
|
||||||
}
|
}
|
||||||
@ -274,7 +274,7 @@ factorial [[gnu::pure]] (unsigned i)
|
|||||||
|
|
||||||
/// stirlings approximation of factorials
|
/// stirlings approximation of factorials
|
||||||
constexpr uintmax_t
|
constexpr uintmax_t
|
||||||
stirling [[gnu::pure]] (unsigned n)
|
stirling [[gnu::const]] (unsigned n)
|
||||||
{
|
{
|
||||||
return static_cast<uintmax_t> (
|
return static_cast<uintmax_t> (
|
||||||
std::sqrt (2 * PI<float> * n) * std::pow (n / E<float>, n)
|
std::sqrt (2 * PI<float> * n) * std::pow (n / E<float>, n)
|
||||||
@ -283,7 +283,7 @@ stirling [[gnu::pure]] (unsigned n)
|
|||||||
|
|
||||||
|
|
||||||
constexpr uintmax_t
|
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)));
|
return factorial (n) / (factorial (k) / (factorial (n - k)));
|
||||||
}
|
}
|
||||||
@ -319,7 +319,7 @@ fsum (InputIt first, InputIt last)
|
|||||||
namespace util {
|
namespace util {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr T
|
constexpr T
|
||||||
min [[gnu::pure]] (const T a)
|
min [[gnu::const]] (const T a)
|
||||||
{ return a; }
|
{ return a; }
|
||||||
|
|
||||||
|
|
||||||
@ -329,7 +329,7 @@ namespace util {
|
|||||||
std::is_integral<typename std::decay<T>::type>::value == std::is_integral<typename std::decay<U>::type>::value,
|
std::is_integral<typename std::decay<T>::type>::value == std::is_integral<typename std::decay<U>::type>::value,
|
||||||
typename std::common_type<T,U>::type
|
typename std::common_type<T,U>::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> (args)...);
|
return min (a < b ? a : b, std::forward<Args> (args)...);
|
||||||
}
|
}
|
||||||
@ -339,7 +339,7 @@ namespace util {
|
|||||||
/// Variadic maximum
|
/// Variadic maximum
|
||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr T
|
constexpr T
|
||||||
max [[gnu::pure]] (const T a)
|
max [[gnu::const]] (const T a)
|
||||||
{ return a; }
|
{ return a; }
|
||||||
|
|
||||||
|
|
||||||
@ -349,7 +349,7 @@ namespace util {
|
|||||||
std::is_integral<typename std::decay<T>::type>::value == std::is_integral<typename std::decay<U>::type>::value,
|
std::is_integral<typename std::decay<T>::type>::value == std::is_integral<typename std::decay<U>::type>::value,
|
||||||
typename std::common_type<T,U>::type
|
typename std::common_type<T,U>::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> (args)...);
|
return max (a > b ? a : b, std::forward<Args> (args)...);
|
||||||
}
|
}
|
||||||
@ -361,7 +361,7 @@ namespace util {
|
|||||||
// min/max clamping
|
// min/max clamping
|
||||||
template <typename T, typename U, typename V>
|
template <typename T, typename U, typename V>
|
||||||
constexpr T
|
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 ();
|
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
|
// clamped cubic hermite interpolation
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T
|
T
|
||||||
smoothstep [[gnu::pure]] (T a, T b, T x)
|
smoothstep [[gnu::const]] (T a, T b, T x)
|
||||||
{
|
{
|
||||||
CHECK_LE(a, b);
|
CHECK_LE(a, b);
|
||||||
x = limit ((x - a) / (b - a), T{0}, T{1});
|
x = limit ((x - a) / (b - a), T{0}, T{1});
|
||||||
@ -393,7 +393,7 @@ constexpr
|
|||||||
typename std::enable_if<
|
typename std::enable_if<
|
||||||
!std::is_floating_point<T>::value && std::is_floating_point<U>::value, U
|
!std::is_floating_point<T>::value && std::is_floating_point<U>::value, U
|
||||||
>::type
|
>::type
|
||||||
renormalise [[gnu::pure]] (T t)
|
renormalise [[gnu::const]] (T t)
|
||||||
{
|
{
|
||||||
return t / static_cast<U> (std::numeric_limits<T>::max ());
|
return t / static_cast<U> (std::numeric_limits<T>::max ());
|
||||||
}
|
}
|
||||||
@ -405,7 +405,7 @@ constexpr
|
|||||||
typename std::enable_if<
|
typename std::enable_if<
|
||||||
std::is_floating_point<T>::value && !std::is_floating_point<U>::value, U
|
std::is_floating_point<T>::value && !std::is_floating_point<U>::value, U
|
||||||
>::type
|
>::type
|
||||||
renormalise [[gnu::pure]] (T t)
|
renormalise [[gnu::const]] (T t)
|
||||||
{
|
{
|
||||||
// Ideally std::ldexp would be involved but it complicates handing
|
// Ideally std::ldexp would be involved but it complicates handing
|
||||||
// integers with greater precision than our floating point type. Also it
|
// integers with greater precision than our floating point type. Also it
|
||||||
@ -440,7 +440,7 @@ typename std::enable_if<
|
|||||||
std::is_floating_point<U>::value &&
|
std::is_floating_point<U>::value &&
|
||||||
!std::is_same<T,U>::value, U
|
!std::is_same<T,U>::value, U
|
||||||
>::type
|
>::type
|
||||||
renormalise [[gnu::pure]] (T t)
|
renormalise [[gnu::const]] (T t)
|
||||||
{
|
{
|
||||||
return static_cast<U> (t);
|
return static_cast<U> (t);
|
||||||
}
|
}
|
||||||
@ -454,7 +454,7 @@ typename std::enable_if<
|
|||||||
std::is_integral<U>::value &&
|
std::is_integral<U>::value &&
|
||||||
(sizeof (T) > sizeof (U)), U
|
(sizeof (T) > sizeof (U)), U
|
||||||
>::type
|
>::type
|
||||||
renormalise [[gnu::pure]] (T t)
|
renormalise [[gnu::const]] (T t)
|
||||||
{
|
{
|
||||||
static_assert (sizeof (T) > sizeof (U),
|
static_assert (sizeof (T) > sizeof (U),
|
||||||
"assumes right shift is sufficient");
|
"assumes right shift is sufficient");
|
||||||
@ -473,7 +473,7 @@ typename std::enable_if<
|
|||||||
std::is_integral<U>::value &&
|
std::is_integral<U>::value &&
|
||||||
sizeof (T) < sizeof (U), U
|
sizeof (T) < sizeof (U), U
|
||||||
>::type
|
>::type
|
||||||
renormalise [[gnu::pure]] (T t)
|
renormalise [[gnu::const]] (T t)
|
||||||
{
|
{
|
||||||
static_assert (sizeof (T) < sizeof (U),
|
static_assert (sizeof (T) < sizeof (U),
|
||||||
"assumes bit creation is required to fill space");
|
"assumes bit creation is required to fill space");
|
||||||
@ -500,7 +500,7 @@ constexpr
|
|||||||
typename std::enable_if<
|
typename std::enable_if<
|
||||||
std::is_same<T,U>::value, U
|
std::is_same<T,U>::value, U
|
||||||
>::type
|
>::type
|
||||||
renormalise [[gnu::pure]] (T t)
|
renormalise [[gnu::const]] (T t)
|
||||||
{ return t; }
|
{ return t; }
|
||||||
|
|
||||||
#include "maths.ipp"
|
#include "maths.ipp"
|
||||||
|
Loading…
Reference in New Issue
Block a user