libcruft-util/maths.hpp

642 lines
18 KiB
C++
Raw Normal View History

2011-05-23 17:18:52 +10:00
/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2011-05-23 17:18:52 +10:00
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
2014-07-07 15:16:04 +10:00
*
2015-04-13 18:05:28 +10:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
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
#include "./debug.hpp"
#include "./types/traits.hpp"
#include "./float.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>
#include <type_traits>
#include <utility>
2011-05-23 17:18:52 +10:00
///////////////////////////////////////////////////////////////////////////////
// NOTE: You may be tempted to add all sorts of performance enhancing
// attributes (like gnu::const or gnu::pure). DO NOT DO THIS WITHOUT EXTENSIVE
// TESTING. Just about everything will break in some way with these attributes.
//
// In particular: it is safest to apply these only to leaf functions
///////////////////////////////////////////////////////////////////////////////
2015-09-15 21:06:23 +10:00
namespace util {
///////////////////////////////////////////////////////////////////////////
// Comparisons
inline bool
almost_equal (const float &a, const float &b)
{
return ieee_single::almost_equal (a, b);
}
//-----------------------------------------------------------------------------
inline bool
almost_equal (const double &a, const double &b)
{
return ieee_double::almost_equal (a, b);
}
//-----------------------------------------------------------------------------
template <typename A, typename B>
inline
typename std::enable_if_t<
std::is_floating_point<A>::value &&
std::is_floating_point<B>::value,
bool
>
almost_equal (const A &a, const B &b)
{
using common_t = std::common_type_t<A,B>;
return almost_equal<common_t> (static_cast<common_t> (a),
static_cast<common_t> (b));
}
//-----------------------------------------------------------------------------
template <typename A, typename B>
inline
typename std::enable_if_t<
std::is_integral<A>::value &&
std::is_integral<B>::value &&
std::is_signed<A>::value == std::is_signed<B>::value,
bool
>
almost_equal (const A &a, const B &b) {
using common_t = std::common_type_t<A,B>;
return static_cast<common_t> (a) == static_cast<common_t> (b);
}
//-----------------------------------------------------------------------------
template <typename Ta, typename Tb>
inline
typename std::enable_if<
!std::is_arithmetic<Ta>::value ||
!std::is_arithmetic<Tb>::value,
bool
>::type
almost_equal (const Ta &a, const Tb &b)
{ return a == b; }
//-----------------------------------------------------------------------------
// Useful for explictly ignore equality warnings
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
template <typename Ta, typename Tb>
constexpr
typename std::enable_if_t<
std::is_arithmetic<Ta>::value &&
std::is_arithmetic<Tb>::value,
bool
>
exactly_equal (const Ta a, const Tb b)
{
return a == b;
}
//-------------------------------------------------------------------------
template <typename Ta, typename Tb>
inline
typename std::enable_if_t<
!std::is_arithmetic<Ta>::value ||
!std::is_arithmetic<Tb>::value,
bool
>
exactly_equal (const Ta &a, const Tb &b)
{
return a == b;
}
#pragma GCC diagnostic pop
//-----------------------------------------------------------------------------
template <typename T>
constexpr
std::enable_if_t<
std::is_integral<T>::value, bool
>
almost_zero (T t)
{
return t == 0;
}
template <typename T>
std::enable_if_t<
!std::is_integral<T>::value, bool
>
almost_zero (T a)
{ return almost_equal (a, T{0}); }
//-------------------------------------------------------------------------
template <typename T>
constexpr
typename std::enable_if_t<
std::is_integral<T>::value, bool
>
exactly_zero (T t)
{
return exactly_equal (t, T{0});
}
template <typename T>
constexpr
typename std::enable_if_t<
!std::is_integral<T>::value, bool
>
exactly_zero (T t)
{
return exactly_equal (t, T{0});
}
///////////////////////////////////////////////////////////////////////////
template <typename T>
T
abs [[gnu::const]] (T t)
{
return t > 0 ? t : -t;
}
2015-09-15 21:06:23 +10:00
///////////////////////////////////////////////////////////////////////////
// exponentials
2015-09-16 02:24:49 +10:00
2015-10-06 15:21:48 +11:00
template <typename T>
constexpr T
pow2 [[gnu::const]] (T value)
{
return value * value;
}
2011-05-23 17:18:52 +10:00
2015-11-13 17:25:21 +11:00
///////////////////////////////////////////////////////////////////////////
2015-10-06 15:21:48 +11:00
template <typename T>
constexpr T
pow [[gnu::const]] (T x, unsigned y)
{
return y == 0 ? T{1} : x * pow (x, y - 1);
}
2015-10-06 15:21:48 +11:00
2015-01-21 23:35:34 +11:00
//-------------------------------------------------------------------------
template <typename T>
2016-02-02 11:32:29 +11:00
constexpr
std::enable_if_t<std::is_integral<T>::value, bool>
is_pow2 [[gnu::const]] (T value)
{
return value && !(value & (value - 1));
}
2011-08-29 14:31:22 +10:00
//-----------------------------------------------------------------------------
// Logarithms
template <typename T>
T
log2 (T val);
2014-09-11 15:34:59 +10:00
2015-11-16 13:21:38 +11:00
//-------------------------------------------------------------------------
template <typename T>
T
log2up (T val);
2014-09-17 16:41:38 +10:00
///////////////////////////////////////////////////////////////////////////////
// Rounding
template <typename T, typename U>
inline
typename std::common_type<
std::enable_if_t<std::is_integral<T>::value,T>,
std::enable_if_t<std::is_integral<U>::value,U>
>::type
round_to (T value, U size)
{
if (value % size == 0)
return value;
2011-05-23 17:18:52 +10:00
return value + (size - value % size);
}
2011-05-23 17:18:52 +10:00
2011-08-29 14:31:22 +10:00
//-----------------------------------------------------------------------------
template <typename T>
std::enable_if_t<
std::is_integral<T>::value, T
>
round_pow2 (T value);
2011-08-29 14:31:22 +10:00
//-----------------------------------------------------------------------------
template <typename T, typename U>
constexpr std::enable_if_t<
std::is_integral<T>::value &&
std::is_integral<U>::value,
T
>
divup (const T a, const U b)
{
return (a + b - 1) / b;
}
2011-08-29 14:31:22 +10:00
2015-01-21 23:35:08 +11:00
///////////////////////////////////////////////////////////////////////////////
// Properties
template <typename T>
bool
is_integer (const T& value);
2015-01-21 23:35:08 +11:00
//-----------------------------------------------------------------------------
template <typename T>
unsigned
digits (const T& value);
2015-11-16 13:21:38 +11:00
2015-11-16 17:57:53 +11:00
///----------------------------------------------------------------------------
/// return positive or negative unit value corresponding to the input.
template <typename T>
constexpr std::enable_if_t<
std::is_signed<T>::value && std::is_integral<T>::value, T
>
sign (T t)
{
return t < 0 ? -1 : 1;
}
2015-11-16 17:57:53 +11:00
///------------------------------------------------------------------------
/// return positive or negative unit value corresponding to the input.
/// guaranteed to give correct results for signed zeroes, use another
/// method if extreme speed is important.
template <typename T>
constexpr std::enable_if_t<
std::is_floating_point<T>::value, T
>
sign (T t)
{
return std::signbit (t) ? -1 : 1;
}
//-------------------------------------------------------------------------
template <typename T>
constexpr
bool
samesign (T a, T b)
{
return a < 0 && b < 0 || a > 0 && b > 0;
}
///////////////////////////////////////////////////////////////////////////////
// factorisation
template <typename T>
constexpr T
gcd (T a, T b)
{
CHECK_NEZ (a);
CHECK_NEZ (b);
2012-05-26 18:02:38 +10:00
while (a != b) {
if (a > b)
a -= b;
else if (b > a)
b -= a;
}
2012-05-26 18:02:38 +10:00
return a;
}
//-----------------------------------------------------------------------------
template <typename T>
const T&
identity (const T& t)
{
return t;
}
2012-05-08 16:39:58 +10:00
///////////////////////////////////////////////////////////////////////////////
// angles, trig
template <typename T>
constexpr T PI = T(3.141592653589793238462643);
2012-05-08 16:39:58 +10:00
2015-11-16 13:21:38 +11:00
//-----------------------------------------------------------------------------
template <typename T>
constexpr T E = T(2.71828182845904523536028747135266250);
2015-11-13 17:25:21 +11:00
//-----------------------------------------------------------------------------
template <typename T>
constexpr T
to_degrees (T radians)
{
static_assert (std::is_floating_point<T>::value, "undefined for integral types");
return radians * 180 / PI<T>;
}
2015-02-04 15:44:03 +11:00
//-----------------------------------------------------------------------------
template <typename T>
constexpr T
to_radians (T degrees)
{
static_assert (std::is_floating_point<T>::value, "undefined for integral types");
return degrees / 180 * PI<T>;
}
2011-10-29 23:13:47 +11:00
2015-02-02 15:25:22 +11:00
//-----------------------------------------------------------------------------
//! Normalised sinc function
template <typename T>
constexpr T
sincn (T x)
{
return almost_zero (x) ? 1 : std::sin (PI<T> * x) / (PI<T> * x);
}
2011-10-29 23:13:47 +11:00
2015-01-21 23:39:23 +11:00
//-----------------------------------------------------------------------------
//! Unnormalised sinc function
template <typename T>
constexpr T
sincu (T x)
{
return almost_zero (x) ? 1 : std::sin (x) / x;
}
2015-01-21 23:39:23 +11:00
///////////////////////////////////////////////////////////////////////////////
// combinatorics
constexpr uintmax_t
factorial (unsigned i)
{
return i <= 1 ? 0 : i * factorial (i - 1);
}
2011-10-29 23:13:47 +11:00
//-----------------------------------------------------------------------------
/// stirlings approximation of factorials
constexpr uintmax_t
stirling (unsigned n)
{
return static_cast<uintmax_t> (
std::sqrt (2 * PI<float> * n) * std::pow (n / E<float>, n)
);
}
2011-10-29 23:13:47 +11:00
2015-11-13 17:25:21 +11:00
//-----------------------------------------------------------------------------
constexpr uintmax_t
combination (unsigned n, unsigned k)
{
return factorial (n) / (factorial (k) / (factorial (n - k)));
}
2015-02-02 15:25:22 +11:00
///////////////////////////////////////////////////////////////////////////////
// kahan summation for long floating point sequences
2015-02-02 15:25:22 +11:00
template <class InputIt>
typename std::iterator_traits<InputIt>::value_type
fsum (InputIt first, InputIt last)
{
using T = typename std::iterator_traits<InputIt>::value_type;
static_assert (std::is_floating_point<T>::value,
"fsum only works for floating point types");
2015-02-02 15:25:22 +11:00
T sum = 0;
T c = 0;
2015-02-02 15:25:22 +11:00
for (auto cursor = first; cursor != last; ++cursor) {
T y = *cursor - c;
T t = sum + y;
c = (t - sum) - y;
sum = t;
}
2015-02-02 15:25:22 +11:00
return sum;
2015-08-25 17:25:55 +10:00
}
///////////////////////////////////////////////////////////////////////////
/// Variadic minimum
template <typename T>
constexpr T
min (const T a)
{ return a; }
2011-05-23 17:18:52 +10:00
2015-11-13 17:25:21 +11:00
//-------------------------------------------------------------------------
template <typename T, typename U, typename ...Args>
2015-11-13 18:00:15 +11:00
constexpr std::enable_if_t<
std::is_unsigned<std::decay_t<T>>::value == std::is_unsigned<std::decay_t<U>>::value &&
std::is_integral<std::decay_t<T>>::value == std::is_integral<std::decay_t<U>>::value,
std::common_type_t<T,U>
>
min (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-11-13 17:25:21 +11:00
//-------------------------------------------------------------------------
/// Variadic maximum
template <typename T>
constexpr T
max (const T a)
{ return a; }
2015-11-13 17:25:21 +11:00
//-------------------------------------------------------------------------
template <typename T, typename U, typename ...Args>
2015-11-13 18:00:15 +11:00
constexpr std::enable_if_t<
std::is_unsigned<std::decay_t<T>>::value == std::is_unsigned<std::decay_t<U>>::value &&
std::is_integral<std::decay_t<T>>::value == std::is_integral<std::decay_t<U>>::value,
std::common_type_t<T,U>
>
max (const T a, const U b, Args ...args)
{
return max (a > b ? a : b, std::forward<Args> (args)...);
}
2011-05-23 17:18:52 +10:00
2015-11-13 17:25:21 +11:00
///////////////////////////////////////////////////////////////////////////
// Limiting functions
// min/max clamping
template <typename T, typename U, typename V>
constexpr T
limit (const T val, const U lo, const V hi)
{
CHECK_LE (lo, hi);
2015-01-21 23:35:08 +11:00
return val > hi ? hi:
val < lo ? lo:
val;
}
2015-02-05 20:30:33 +11:00
//-------------------------------------------------------------------------
// clamped cubic hermite interpolation
template <typename T>
T
smoothstep (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
2015-10-20 16:50:44 +11:00
2015-04-09 17:44:50 +10:00
///////////////////////////////////////////////////////////////////////////
// renormalisation of unit floating point and/or normalised integers
2015-04-09 17:44:50 +10:00
// int -> float
template <typename T, typename U>
constexpr
typename std::enable_if<
!std::is_floating_point<T>::value && std::is_floating_point<U>::value, U
>::type
renormalise (T t)
{
return t / static_cast<U> (std::numeric_limits<T>::max ());
}
2015-04-09 17:44:50 +10:00
//-------------------------------------------------------------------------
// float -> int
template <typename T, typename U>
constexpr
typename std::enable_if<
std::is_floating_point<T>::value && !std::is_floating_point<U>::value, U
>::type
renormalise (T t)
{
// Ideally std::ldexp would be involved but it complicates handing
// integers with greater precision than our floating point type. Also it
// would prohibit constexpr and involve errno.
size_t usable = std::numeric_limits<T>::digits;
size_t available = sizeof (U) * 8;
size_t shift = std::max (available, usable) - usable;
t = limit (t, 0, 1);
// construct an integer of the float's mantissa size, multiply it by our
// parameter, then shift it back into the full range of the integer type.
U in = std::numeric_limits<U>::max () >> shift;
U mid = static_cast<U> (t * in);
U out = mid << shift;
// use the top bits of the output to fill the bottom bits which through
// shifting would otherwise be zero. this gives us the full extent of the
// integer range, while varying predictably through the entire output
// space.
return out | out >> (available - shift);
}
2015-02-05 20:30:33 +11:00
2015-10-20 16:50:44 +11:00
//-------------------------------------------------------------------------
// float -> float, avoid identity conversion as we don't want to create
// ambiguous overloads
template <typename T, typename U>
constexpr
typename std::enable_if<
std::is_floating_point<T>::value &&
std::is_floating_point<U>::value &&
!std::is_same<T,U>::value, U
>::type
renormalise (T t)
{
return static_cast<U> (t);
}
//-------------------------------------------------------------------------
// hi_int -> lo_int
template <typename T, typename U>
constexpr
typename std::enable_if<
std::is_integral<T>::value &&
std::is_integral<U>::value &&
(sizeof (T) > sizeof (U)), U
>::type
renormalise (T t)
{
static_assert (sizeof (T) > sizeof (U),
"assumes right shift is sufficient");
2015-10-20 16:50:44 +11:00
// we have excess bits ,just shift and return
constexpr auto shift = 8 * (sizeof (T) - sizeof (U));
return t >> shift;
}
2015-10-20 16:50:44 +11:00
//-------------------------------------------------------------------------
// lo_int -> hi_int
template <typename T, typename U>
constexpr
typename std::enable_if<
std::is_integral<T>::value &&
std::is_integral<U>::value &&
sizeof (T) < sizeof (U), U
>::type
renormalise (T t)
{
static_assert (sizeof (T) < sizeof (U),
"assumes bit creation is required to fill space");
2015-10-20 16:50:44 +11:00
// we need to create bits. fill the output integer with copies of ourself.
// this is approximately correct in the general case (introducing a small
// linear positive bias), but allows us to fill the output space in the
// case of input maximum.
2015-10-20 16:50:44 +11:00
static_assert (sizeof (U) % sizeof (T) == 0,
"assumes integer multiple of sizes");
2015-10-20 16:50:44 +11:00
U out = 0;
2015-10-20 16:50:44 +11:00
for (size_t i = 0; i < sizeof (U) / sizeof (T); ++i)
out |= U (t) << sizeof (T) * 8 * i;
return out;
}
2015-10-20 16:50:44 +11:00
//-------------------------------------------------------------------------
template <typename T, typename U>
constexpr
typename std::enable_if<
std::is_same<T,U>::value, U
>::type
renormalise (T t)
{ return t; }
}
2015-10-20 16:50:44 +11:00
2015-11-13 17:25:21 +11:00
2011-05-23 17:18:52 +10:00
#endif // __MATHS_HPP