2011-05-23 17:18:52 +10:00
|
|
|
/*
|
2011-06-21 20:26:32 +10:00
|
|
|
* This file is part of libgim.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2012-04-11 15:18:26 +10:00
|
|
|
* libgim is free software: you can redistribute it and/or modify it under the
|
2011-05-23 17:18:52 +10:00
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
2014-07-07 15:16:04 +10:00
|
|
|
*
|
2012-04-11 15:18:26 +10:00
|
|
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
2011-05-23 17:18:52 +10:00
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
2014-07-07 15:16:04 +10:00
|
|
|
*
|
2011-05-23 17:18:52 +10:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2011-06-21 20:26:32 +10:00
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2014-09-17 18:20:28 +10:00
|
|
|
* Copyright 2010-2014 Danny Robson <danny@nerdcruft.net>
|
2011-05-23 17:18:52 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __MATHS_HPP
|
|
|
|
#define __MATHS_HPP
|
|
|
|
|
2015-02-05 20:30:33 +11:00
|
|
|
#include "debug.hpp"
|
2015-03-20 01:32:56 +11:00
|
|
|
#include "types/traits.hpp"
|
2015-02-05 20:30:33 +11:00
|
|
|
|
2015-04-09 17:44:50 +10:00
|
|
|
#include <cmath>
|
2015-02-02 15:25:22 +11:00
|
|
|
#include <cstdint>
|
2015-04-09 17:44:50 +10:00
|
|
|
#include <limits>
|
2012-04-20 18:02:54 +10:00
|
|
|
#include <type_traits>
|
2014-08-02 21:13:31 +10:00
|
|
|
#include <utility>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
template <typename T>
|
|
|
|
T
|
|
|
|
abs (T value)
|
|
|
|
{ return value > 0 ? value : -value; }
|
|
|
|
|
|
|
|
|
2015-01-21 23:35:08 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Exponentials
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
2014-07-07 15:16:04 +10:00
|
|
|
constexpr T
|
2015-01-21 23:35:08 +11:00
|
|
|
pow2 [[gnu::pure]] (T value)
|
2012-07-30 16:31:19 +10:00
|
|
|
{ return value * value; }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-01-21 23:35:34 +11:00
|
|
|
template <typename T>
|
2015-01-28 14:57:20 +11:00
|
|
|
constexpr T
|
|
|
|
pow [[gnu::pure]] (T x, unsigned y);
|
2015-01-21 23:35:34 +11:00
|
|
|
|
|
|
|
|
2011-08-29 14:31:22 +10:00
|
|
|
template <typename T>
|
|
|
|
bool
|
2014-10-17 19:24:53 +11:00
|
|
|
is_pow2 [[gnu::pure]] (T value);
|
2011-08-29 14:31:22 +10:00
|
|
|
|
|
|
|
|
2015-01-21 23:35:08 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Logarithms
|
2014-09-11 15:34:59 +10:00
|
|
|
template <typename T>
|
|
|
|
T
|
2014-10-17 19:24:53 +11:00
|
|
|
log2 [[gnu::pure]] (T val);
|
2014-09-11 15:34:59 +10:00
|
|
|
|
|
|
|
|
2014-09-17 16:41:38 +10:00
|
|
|
template <typename T>
|
|
|
|
T
|
2014-10-17 19:24:53 +11:00
|
|
|
log2up [[gnu::pure]] (T val);
|
2014-09-17 16:41:38 +10:00
|
|
|
|
|
|
|
|
2015-01-21 23:35:08 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Roots
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
|
|
|
double
|
2014-10-17 19:24:53 +11:00
|
|
|
rootsquare [[gnu::pure]] (T a, T b);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-01-21 23:35:08 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Rounding
|
2014-08-01 20:44:58 +10:00
|
|
|
template <typename T, typename U>
|
|
|
|
typename std::common_type<T, U>::type
|
2014-10-17 19:24:53 +11:00
|
|
|
align [[gnu::pure]] (T value, U size);
|
2011-08-29 14:31:22 +10:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T
|
2014-10-17 19:24:53 +11:00
|
|
|
round_pow2 [[gnu::pure]] (T value);
|
2011-08-29 14:31:22 +10:00
|
|
|
|
|
|
|
|
2015-01-21 23:35:08 +11:00
|
|
|
template <typename T, typename U>
|
2015-01-28 14:57:20 +11:00
|
|
|
constexpr T
|
|
|
|
divup [[gnu::pure]] (const T a, const U b)
|
2015-01-21 23:35:08 +11:00
|
|
|
{ return (a + b - 1) / b; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Classification
|
2012-04-20 18:04:40 +10:00
|
|
|
template <typename T>
|
|
|
|
bool
|
2014-10-17 19:24:53 +11:00
|
|
|
is_integer [[gnu::pure]] (const T& value);
|
2012-04-20 18:04:40 +10:00
|
|
|
|
|
|
|
|
2015-01-21 23:35:08 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Properties
|
2012-07-30 16:31:51 +10:00
|
|
|
template <typename T>
|
|
|
|
unsigned
|
2014-10-17 19:24:53 +11:00
|
|
|
digits [[gnu::pure]] (const T& value);
|
2012-07-30 16:31:51 +10:00
|
|
|
|
2012-04-20 18:04:40 +10:00
|
|
|
|
2015-01-21 23:35:08 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-24 02:42:42 +11:00
|
|
|
constexpr int sign (int);
|
|
|
|
constexpr float sign (float);
|
|
|
|
constexpr double sign (double);
|
2014-04-16 19:17:15 +10:00
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-01-21 23:35:08 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Comparisons
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
|
|
|
bool
|
2015-01-28 14:57:20 +11:00
|
|
|
almost_equal [[gnu::pure]] (const T &a, const T &b)
|
2011-05-23 17:18:52 +10:00
|
|
|
{ return a == b; }
|
|
|
|
|
|
|
|
|
2012-05-26 18:02:38 +10:00
|
|
|
template <>
|
|
|
|
bool
|
2015-01-28 14:57:20 +11:00
|
|
|
almost_equal [[gnu::pure]] (const float &a, const float &b);
|
2012-05-26 18:02:38 +10:00
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
bool
|
2015-01-28 14:57:20 +11:00
|
|
|
almost_equal [[gnu::pure]] (const double &a, const double &b);
|
2012-05-26 18:02:38 +10:00
|
|
|
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename Ta, typename Tb>
|
2012-04-20 18:02:54 +10:00
|
|
|
typename std::enable_if<
|
|
|
|
std::is_arithmetic<Ta>::value && std::is_arithmetic<Tb>::value,
|
|
|
|
bool
|
|
|
|
>::type
|
2015-01-28 14:57:20 +11:00
|
|
|
almost_equal [[gnu::pure]] (Ta a, Tb b) {
|
2011-05-23 17:18:52 +10:00
|
|
|
return almost_equal <decltype(a + b)> (static_cast<decltype(a + b)>(a),
|
|
|
|
static_cast<decltype(a + b)>(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-20 18:02:54 +10:00
|
|
|
template <typename Ta, typename Tb>
|
|
|
|
typename std::enable_if<
|
|
|
|
!std::is_arithmetic<Ta>::value || !std::is_arithmetic<Tb>::value,
|
|
|
|
bool
|
|
|
|
>::type
|
2015-01-28 14:57:20 +11:00
|
|
|
almost_equal [[gnu::pure]] (const Ta &a, const Tb &b)
|
2012-04-20 18:02:54 +10:00
|
|
|
{ return a == b; }
|
|
|
|
|
2012-05-03 18:11:42 +10:00
|
|
|
|
2012-05-08 16:39:58 +10:00
|
|
|
// Useful for explictly ignore equality warnings
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
2014-07-07 15:16:21 +10:00
|
|
|
template <typename T, typename U>
|
2012-05-08 16:39:58 +10:00
|
|
|
bool
|
2015-01-28 14:57:20 +11:00
|
|
|
exactly_equal [[gnu::pure]] (const T &a, const U &b)
|
2012-05-08 16:39:58 +10:00
|
|
|
{ return a == b; }
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
|
|
|
|
2012-05-03 18:11:42 +10:00
|
|
|
template <typename T>
|
|
|
|
bool
|
2015-01-28 14:57:20 +11:00
|
|
|
almost_zero [[gnu::pure]] (T a)
|
2012-05-03 18:11:42 +10:00
|
|
|
{ return almost_equal (a, 0); }
|
|
|
|
|
2012-05-08 16:39:58 +10:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool
|
2015-01-28 14:57:20 +11:00
|
|
|
exactly_zero [[gnu::pure]] (T a)
|
2012-05-08 16:39:58 +10:00
|
|
|
{ return exactly_equal (a, static_cast<T> (0)); }
|
|
|
|
|
|
|
|
|
2015-01-13 18:32:01 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-01-21 23:39:23 +11:00
|
|
|
// angles, trig
|
2015-02-04 15:44:03 +11:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct constants { };
|
|
|
|
|
2015-01-21 23:39:23 +11:00
|
|
|
constexpr double PI_d = 3.141592653589793238462643;
|
|
|
|
constexpr float PI_f = 3.141592653589793238462643f;
|
2011-10-29 23:13:47 +11:00
|
|
|
|
2015-02-04 15:44:03 +11:00
|
|
|
constexpr float E_f = 2.71828182845904523536028747135266250f;
|
|
|
|
constexpr double E_d = 2.71828182845904523536028747135266250;
|
2015-02-02 15:25:22 +11:00
|
|
|
|
2015-02-04 15:44:03 +11:00
|
|
|
template <typename T>
|
|
|
|
constexpr T
|
|
|
|
to_degrees [[gnu::pure]] (T radians)
|
|
|
|
{
|
|
|
|
return radians * 180 / constants<T>::PI;
|
2011-10-29 23:13:47 +11:00
|
|
|
}
|
|
|
|
|
2015-01-21 23:39:23 +11:00
|
|
|
|
2015-02-04 15:44:03 +11:00
|
|
|
template <typename T>
|
|
|
|
constexpr T
|
|
|
|
to_radians [[gnu::pure]] (T degrees)
|
|
|
|
{
|
|
|
|
return degrees / 180 * constants<T>::PI;
|
2015-01-21 23:39:23 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-04 15:44:31 +11:00
|
|
|
//! Normalised sinc function
|
|
|
|
template <typename T>
|
|
|
|
constexpr T
|
|
|
|
sincn [[gnu::pure]] (T x)
|
|
|
|
{
|
2015-02-05 20:29:47 +11:00
|
|
|
return almost_zero (x) ? 1 : std::sin (constants<T>::PI * x) / (constants<T>::PI * x);
|
2015-01-13 18:32:30 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-04 15:44:31 +11:00
|
|
|
//! Unnormalised sinc function
|
|
|
|
template <typename T>
|
|
|
|
constexpr T
|
|
|
|
sincu [[gnu::pure]] (T x)
|
|
|
|
{
|
2015-02-05 20:29:47 +11:00
|
|
|
return almost_zero (x) ? 1 : std::sin (x) / x;
|
2011-10-29 23:13:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-02 15:25:22 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
constexpr uintmax_t
|
|
|
|
factorial [[gnu::pure]] (unsigned i)
|
|
|
|
{
|
|
|
|
return i <= 1 ? 0 : i * factorial (i - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
constexpr uintmax_t
|
|
|
|
stirling [[gnu::pure]] (unsigned n)
|
|
|
|
{
|
|
|
|
return static_cast<uintmax_t> (std::sqrt (2 * PI_f * n) * std::pow (n / E_f, n));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
constexpr uintmax_t
|
|
|
|
combination [[gnu::pure]] (unsigned n, unsigned k)
|
|
|
|
{
|
|
|
|
return factorial (n) / (factorial (k) / (factorial (n - k)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-13 18:32:01 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
/// Variadic minimum
|
2015-03-06 17:49:35 +11:00
|
|
|
namespace util {
|
|
|
|
template <typename T>
|
|
|
|
constexpr T
|
|
|
|
min [[gnu::pure]] (const T a)
|
|
|
|
{ return a; }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-03-06 17:49:35 +11:00
|
|
|
template <typename T, typename U, typename ...Args>
|
|
|
|
constexpr typename std::enable_if<
|
|
|
|
std::is_unsigned<typename std::decay<T>::type>::value == std::is_unsigned<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
|
|
|
|
>::type
|
|
|
|
min [[gnu::pure]] (const T a, const U b, Args ...args)
|
|
|
|
{
|
|
|
|
return min (a < b ? a : b, std::forward<Args> (args)...);
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-01-13 18:32:01 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-06 17:49:35 +11:00
|
|
|
/// Variadic maximum
|
|
|
|
template <typename T>
|
|
|
|
constexpr T
|
|
|
|
max [[gnu::pure]] (const T a)
|
|
|
|
{ return a; }
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T, typename U, typename ...Args>
|
|
|
|
constexpr typename std::enable_if<
|
|
|
|
std::is_unsigned<typename std::decay<T>::type>::value == std::is_unsigned<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
|
|
|
|
>::type
|
|
|
|
max [[gnu::pure]] (const T a, const U b, Args ...args)
|
|
|
|
{
|
|
|
|
return max (a > b ? a : b, std::forward<Args> (args)...);
|
|
|
|
}
|
2015-01-15 14:06:17 +11:00
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-01-13 18:32:01 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-05 20:30:45 +11:00
|
|
|
// Limiting functions
|
|
|
|
|
|
|
|
// min/max clamping
|
2014-08-02 21:13:51 +10:00
|
|
|
template <typename T, typename U, typename V>
|
|
|
|
T
|
2015-02-05 20:30:45 +11:00
|
|
|
limit [[gnu::pure]] (const T val, const U lo, const V hi)
|
2015-01-21 23:35:08 +11:00
|
|
|
{
|
2015-02-05 20:30:45 +11:00
|
|
|
CHECK_LE(
|
|
|
|
decltype (lo+hi) (lo),
|
|
|
|
decltype (hi+lo) (hi)
|
|
|
|
);
|
|
|
|
|
2014-08-02 21:13:51 +10:00
|
|
|
return val > hi ? hi:
|
|
|
|
val < lo ? lo:
|
2015-01-22 14:56:33 +11:00
|
|
|
val;
|
2014-08-02 21:13:51 +10:00
|
|
|
}
|
|
|
|
|
2015-01-21 23:35:08 +11:00
|
|
|
|
2015-02-05 20:30:33 +11:00
|
|
|
// clamped cubic hermite interpolation
|
|
|
|
template <typename T>
|
|
|
|
T
|
|
|
|
smoothstep [[gnu::pure]] (T a, T b, T x)
|
|
|
|
{
|
|
|
|
CHECK_LE(a, b);
|
|
|
|
x = limit ((x - a) / (b - a), T{0}, T{1});
|
|
|
|
return x * x * (3 - 2 * x);
|
|
|
|
}
|
|
|
|
|
2015-04-09 17:44:50 +10:00
|
|
|
#include "types/string.hpp"
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T, typename U>
|
|
|
|
U
|
|
|
|
renormalise [[gnu::pure]] (T t)
|
|
|
|
{
|
|
|
|
static const T T_max = std::numeric_limits<T>::max ();
|
|
|
|
static const U U_max = std::numeric_limits<U>::max ();
|
|
|
|
static const bool shrinking = sizeof (U) < sizeof (T);
|
|
|
|
static const bool T_float = std::is_floating_point<T>::value;
|
|
|
|
static const bool U_float = std::is_floating_point<U>::value;
|
|
|
|
|
|
|
|
if (T_float && U_float)
|
|
|
|
return U (t);
|
|
|
|
|
|
|
|
if (T_float) {
|
|
|
|
return U (limit (t, 0, 1) * U_max);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (U_float)
|
2015-04-09 20:43:14 +10:00
|
|
|
return U(U (t) / T_max);
|
2015-04-09 17:44:50 +10:00
|
|
|
|
|
|
|
if (shrinking)
|
|
|
|
return U (t / (sizeof (T) / sizeof (U)));
|
|
|
|
else
|
|
|
|
return U (t) * (sizeof (U) / sizeof (T));
|
|
|
|
}
|
2015-02-05 20:30:33 +11:00
|
|
|
|
2014-08-01 20:44:58 +10:00
|
|
|
#include "maths.ipp"
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#endif // __MATHS_HPP
|