10#ifndef MATH_FUNCTIONS_HEADER
11#define MATH_FUNCTIONS_HEADER
36gaussian (T
const& x, T
const& sigma)
38 return std::exp(-((x * x) / (T(2) * sigma * sigma)));
51 return std::exp(-(xx / (T(2) * sigma * sigma)));
59gaussian_2d (T
const& x, T
const& y, T
const& sigma_x, T
const& sigma_y)
61 return std::exp(-(x * x) / (T(2) * sigma_x * sigma_x)
62 - (y * y) / (T(2) * sigma_y * sigma_y));
72 return x > T(0) ? std::floor(x + T(0.5)) : std::ceil(x - T(0.5));
90 return (
unsigned char)((float)v1 * w1 + 0.5f);
98 return v1 * w1 + v2 * w2;
107 return (
unsigned char)((float)v1 * w1 + (
float)v2 * w2 + 0.5f);
114 float w1,
float w2,
float w3)
116 return v1 * w1 + v2 * w2 + v3 * w3;
123 unsigned char const& v3,
float w1,
float w2,
float w3)
125 return (
unsigned char)((float)v1 * w1 + (
float)v2 * w2
126 + (float)v3 * w3 + 0.5f);
133 float w1,
float w2,
float w3,
float w4)
135 return v1 * w1 + v2 * w2 + v3 * w3 + v4 * w4;
142 unsigned char const& v3,
unsigned char const& v4,
143 float w1,
float w2,
float w3,
float w4)
145 return (
unsigned char)((float)v1 * w1 + (
float)v2 * w2
146 + (float)v3 * w3 + (
float)v4 * w4 + 0.5f);
158 return (x == T(0) ? T(1) : std::sin(x) / x);
168 static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value,
169 "T must be an unsigned integral type.");
170 return std::bitset<sizeof(T) * 8>(x).count();
175popcount (std::uint32_t
const x)
177# if !(__amd64__ || __x86_64__ || _WIN64 || _M_X64)
178 __pragma (message(
"Warning: CPU support for _mm_popcnt_u32 is assumed, "
179 "but could not be verified."))
181 return static_cast<std::size_t
>(_mm_popcnt_u32(x));
187# if __amd64__ || __x86_64__ || _WIN64 || _M_X64
188 return static_cast<std::size_t
>(_mm_popcnt_u64(x));
190 std::uint32_t
const low_bits =
static_cast<std::uint32_t
>(x);
191 std::uint32_t
const high_bits =
static_cast<std::uint32_t
>(x >> 32);
204clamp (T
const& v, T
const& min = T(0), T
const& max = T(1))
219min (T
const& a, T
const& b, T
const& c)
221 return std::min(a, std::min(b, c));
227max (T
const& a, T
const& b, T
const& c)
229 return std::max(a, std::max(b, c));
237 T ret = (exp == 0 ? T(1) : base);
238 unsigned int const exp2 = exp / 2;
240 for (; i <= exp2; i = i * 2)
250 return bin ^ (bin >> 1);
257 for (
int i = 31; i >= 0; --i)
259 b = (b + ((gc & (1 << i)) >> i)) % 2;
#define MATH_NAMESPACE_BEGIN
#define MATH_NAMESPACE_END
T sinc(T const &x)
Sinc function.
T const & clamp(T const &v, T const &min=T(0), T const &max=T(1))
Returns value 'v' clamped to the interval specified by 'min' and 'max'.
T round(T const &x)
Removes the fractional part of the value to the closest integer.
T fastpow(T const &base, unsigned int exp)
Takes base to the integer power of 'exp'.
T const & max(T const &a, T const &b, T const &c)
Returns the maximum value of three arguments.
T interpolate(T const &v1, float w1)
Generic interpolation (weighting) of a single value.
int from_gray_code(int gc)
std::size_t constexpr popcount(T const x)
Returns the number of one bits of an integer.
T const & min(T const &a, T const &b, T const &c)
Returns the minimum value of three arguments.
int to_gray_code(int bin)
T gaussian_xx(T const &xx, T const &sigma)
Gaussian function that expects x to be squared.
T bound_mirror(T const &v, T const &min, T const &max)
T gaussian_2d(T const &x, T const &y, T const &sigma_x, T const &sigma_y)
Gaussian function in 2D.