maths: style

This commit is contained in:
Danny Robson 2015-11-13 17:25:21 +11:00
parent 1ec9582240
commit b6a4166c68
3 changed files with 66 additions and 39 deletions

View File

@ -24,7 +24,7 @@
#include <type_traits> #include <type_traits>
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
bool bool
is_pow2 (T value) { is_pow2 (T value) {
@ -39,7 +39,7 @@ template bool is_pow2 (uint32_t);
template bool is_pow2 (uint64_t); template bool is_pow2 (uint64_t);
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
T T
log2up (T v) log2up (T v)
@ -51,7 +51,7 @@ template uint32_t log2up (uint32_t);
template uint64_t log2up (uint64_t); template uint64_t log2up (uint64_t);
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
T T
log2 (T v) { log2 (T v) {
@ -71,18 +71,19 @@ template uint32_t log2 (uint32_t);
template uint64_t log2 (uint64_t); template uint64_t log2 (uint64_t);
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
double double
rootsquare (T a, T b) rootsquare (T a, T b)
{ return sqrt (util::pow2 (a) + util::pow2 (b)); } { return sqrt (util::pow2 (a) + util::pow2 (b)); }
//-----------------------------------------------------------------------------
template double rootsquare (double, double); template double rootsquare (double, double);
template double rootsquare ( int, int); template double rootsquare ( int, int);
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
bool bool
is_integer (const T &value) { is_integer (const T &value) {
@ -92,11 +93,12 @@ is_integer (const T &value) {
} }
//-----------------------------------------------------------------------------
template bool is_integer (const double&); template bool is_integer (const double&);
template bool is_integer (const float&); template bool is_integer (const float&);
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <> template <>
unsigned unsigned
digits (const uint32_t &v) { digits (const uint32_t &v) {
@ -113,7 +115,7 @@ digits (const uint32_t &v) {
} }
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
T T
round_pow2 (T value) { round_pow2 (T value) {
@ -130,23 +132,24 @@ round_pow2 (T value) {
} }
//-----------------------------------------------------------------------------
template uint8_t round_pow2 (uint8_t); template uint8_t round_pow2 (uint8_t);
template uint16_t round_pow2 (uint16_t); template uint16_t round_pow2 (uint16_t);
template uint32_t round_pow2 (uint32_t); template uint32_t round_pow2 (uint32_t);
template uint64_t round_pow2 (uint64_t); template uint64_t round_pow2 (uint64_t);
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template const float PI<float>; template const float PI<float>;
template const double PI<double>; template const double PI<double>;
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
// Simple instantiations. Some functions aren't used internally to the library // Simple instantiations. Some functions aren't used internally to the library
// so it's easier to instantiate early and check for broken code at library // so it's easier to instantiate early and check for broken code at library
// build time. // build time.
template float limit (float, float, float); template float limit (float, float, float);
template float smoothstep (float, float, float); template float smoothstep (float, float, float);
template double smoothstep (double, double, double); template double smoothstep (double, double, double);

View File

@ -59,7 +59,8 @@ namespace util {
} }
} }
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
namespace util { namespace util {
template <typename T> template <typename T>
constexpr T constexpr T
@ -85,14 +86,14 @@ T
log2up (T val); log2up (T val);
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
// Roots // Roots
template <typename T> template <typename T>
double double
rootsquare (T a, T b); rootsquare (T a, T b);
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
// Rounding // Rounding
template <typename T, typename U> template <typename T, typename U>
inline inline
@ -109,32 +110,39 @@ round_to (T value, U size)
} }
//-----------------------------------------------------------------------------
template <typename T> template <typename T>
T T
round_pow2 (T value); round_pow2 (T value);
//-----------------------------------------------------------------------------
template <typename T, typename U> template <typename T, typename U>
constexpr T constexpr T
divup (const T a, const U b) divup (const T a, const U b)
{ return (a + b - 1) / b; } { return (a + b - 1) / b; }
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
// Classification // Properties
template <typename T> template <typename T>
bool bool
is_integer (const T& value); is_integer (const T& value);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Properties
template <typename T> template <typename T>
unsigned unsigned
digits (const T& value); digits (const T& value);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
constexpr int sign (int);
constexpr float sign (float);
constexpr double sign (double);
///////////////////////////////////////////////////////////////////////////////
// factorisation // factorisation
template <typename T> template <typename T>
constexpr T constexpr T
@ -149,21 +157,6 @@ gcd (T a, T b)
} }
//-----------------------------------------------------------------------------
constexpr int sign (int);
constexpr float sign (float);
constexpr double sign (double);
//-----------------------------------------------------------------------------
template <typename T>
const T&
identity (const T& t)
{
return t;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Comparisons // Comparisons
inline bool inline bool
@ -246,16 +239,27 @@ exactly_zero (T a)
{ return exactly_equal (a, T{0}); } { return exactly_equal (a, T{0}); }
//-----------------------------------------------------------------------------
template <typename T>
const T&
identity (const T& t)
{
return t;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// angles, trig // angles, trig
template <typename T> template <typename T>
constexpr T PI = T(3.141592653589793238462643); constexpr T PI = T(3.141592653589793238462643);
//-----------------------------------------------------------------------------
template <typename T> template <typename T>
constexpr T E = T(2.71828182845904523536028747135266250); constexpr T E = T(2.71828182845904523536028747135266250);
//-----------------------------------------------------------------------------
template <typename T> template <typename T>
constexpr T constexpr T
to_degrees (T radians) to_degrees (T radians)
@ -265,6 +269,7 @@ to_degrees (T radians)
} }
//-----------------------------------------------------------------------------
template <typename T> template <typename T>
constexpr T constexpr T
to_radians (T degrees) to_radians (T degrees)
@ -274,6 +279,7 @@ to_radians (T degrees)
} }
//-----------------------------------------------------------------------------
//! Normalised sinc function //! Normalised sinc function
template <typename T> template <typename T>
constexpr T constexpr T
@ -283,6 +289,7 @@ sincn (T x)
} }
//-----------------------------------------------------------------------------
//! Unnormalised sinc function //! Unnormalised sinc function
template <typename T> template <typename T>
constexpr T constexpr T
@ -292,7 +299,9 @@ sincu (T x)
} }
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
// combinatorics
constexpr uintmax_t constexpr uintmax_t
factorial (unsigned i) factorial (unsigned i)
{ {
@ -300,6 +309,7 @@ factorial (unsigned i)
} }
//-----------------------------------------------------------------------------
/// stirlings approximation of factorials /// stirlings approximation of factorials
constexpr uintmax_t constexpr uintmax_t
stirling (unsigned n) stirling (unsigned n)
@ -310,6 +320,7 @@ stirling (unsigned n)
} }
//-----------------------------------------------------------------------------
constexpr uintmax_t constexpr uintmax_t
combination (unsigned n, unsigned k) combination (unsigned n, unsigned k)
{ {
@ -317,7 +328,7 @@ combination (unsigned n, unsigned k)
} }
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
// kahan summation for long floating point sequences // kahan summation for long floating point sequences
template <class InputIt> template <class InputIt>
@ -342,7 +353,7 @@ fsum (InputIt first, InputIt last)
} }
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
/// Variadic minimum /// Variadic minimum
namespace util { namespace util {
template <typename T> template <typename T>
@ -351,6 +362,7 @@ namespace util {
{ return a; } { return a; }
//-------------------------------------------------------------------------
template <typename T, typename U, typename ...Args> template <typename T, typename U, typename ...Args>
constexpr typename std::enable_if< 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_unsigned<typename std::decay<T>::type>::value == std::is_unsigned<typename std::decay<U>::type>::value &&
@ -363,7 +375,7 @@ namespace util {
} }
//----------------------------------------------------------------------------- //-------------------------------------------------------------------------
/// Variadic maximum /// Variadic maximum
template <typename T> template <typename T>
constexpr T constexpr T
@ -371,6 +383,7 @@ namespace util {
{ return a; } { return a; }
//-------------------------------------------------------------------------
template <typename T, typename U, typename ...Args> template <typename T, typename U, typename ...Args>
constexpr typename std::enable_if< 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_unsigned<typename std::decay<T>::type>::value == std::is_unsigned<typename std::decay<U>::type>::value &&
@ -383,7 +396,8 @@ namespace util {
} }
} }
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
// Limiting functions // Limiting functions
// min/max clamping // min/max clamping
@ -399,6 +413,7 @@ limit (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
@ -427,6 +442,7 @@ renormalise (T t)
} }
//-----------------------------------------------------------------------------
// float -> int // float -> int
template <typename T, typename U> template <typename T, typename U>
constexpr constexpr
@ -459,6 +475,7 @@ renormalise (T t)
} }
//-----------------------------------------------------------------------------
// float -> float, avoid identity conversion as we don't want to create // float -> float, avoid identity conversion as we don't want to create
// ambiguous overloads // ambiguous overloads
template <typename T, typename U> template <typename T, typename U>
@ -474,6 +491,7 @@ renormalise (T t)
} }
//-----------------------------------------------------------------------------
// hi_int -> lo_int // hi_int -> lo_int
template <typename T, typename U> template <typename T, typename U>
constexpr constexpr
@ -493,6 +511,7 @@ renormalise (T t)
} }
//-----------------------------------------------------------------------------
// lo_int -> hi_int // lo_int -> hi_int
template <typename T, typename U> template <typename T, typename U>
constexpr constexpr
@ -523,6 +542,7 @@ renormalise (T t)
} }
//-----------------------------------------------------------------------------
template <typename T, typename U> template <typename T, typename U>
constexpr constexpr
typename std::enable_if< typename std::enable_if<
@ -531,6 +551,7 @@ typename std::enable_if<
renormalise (T t) renormalise (T t)
{ return t; } { return t; }
#include "maths.ipp" #include "maths.ipp"
#endif // __MATHS_HPP #endif // __MATHS_HPP

View File

@ -23,7 +23,7 @@
#include <type_traits> #include <type_traits>
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
constexpr T constexpr T
util::pow (T x, unsigned y) util::pow (T x, unsigned y)
@ -35,7 +35,7 @@ util::pow (T x, unsigned y)
} }
///---------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
/// Return a unit type with a sign that matches the provided value /// Return a unit type with a sign that matches the provided value
/// ///
/// We were using __builtin_signbit for the potential speedboost, but it causes /// We were using __builtin_signbit for the potential speedboost, but it causes
@ -47,6 +47,8 @@ sign (int v)
return std::signbit (v) ? -1 : 1; return std::signbit (v) ? -1 : 1;
} }
//-----------------------------------------------------------------------------
constexpr float constexpr float
sign (float v) sign (float v)
{ {
@ -54,6 +56,7 @@ sign (float v)
} }
//-----------------------------------------------------------------------------
constexpr double constexpr double
sign (double v) sign (double v)
{ {