18 #ifndef ION_MATH_UTILS_H_
19 #define ION_MATH_UTILS_H_
27 #include "base/integral_types.h"
36 x != std::numeric_limits<T>::infinity() &&
37 x != -std::numeric_limits<T>::infinity());
42 inline const T Abs(
const T& val) {
43 return val >=
static_cast<T>(0) ? val : -val;
60 return static_cast<T>(sqrt(static_cast<double>(val)));
63 inline float Sqrt(
const float& val) {
85 inline float Sine(
float angle) {
102 template <
typename T>
106 for (; x > 0; x--) result *= x;
114 template <
typename T>
118 for (; x > 0; x -= 2) result *= x;
125 if (n == 0)
return 0;
135 if (n == 0)
return 0;
147 template <
typename T>
149 static const T kInverseOfLogOf2 =
static_cast<T>(1.4426950408889634);
150 return static_cast<T>(log(n) * kInverseOfLogOf2);
157 static const uint32 kMultiplyDeBruijnBitPosition[32] = {
158 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
159 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31};
167 return kMultiplyDeBruijnBitPosition[(n * 0x07C4ACDDU) >> 27];
174 return Log2(static_cast<uint32>(n));
178 const uint32 kMultiplyDeBruijnBitPosition[64] = {
179 63, 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3,
180 61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4,
181 62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21,
182 56, 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5};
191 return kMultiplyDeBruijnBitPosition[(n * 0x07EDD5E59A4E28C2ULL) >> 58];
198 return Log2(static_cast<uint64>(n));
203 template <
typename T>
204 inline const T Clamp(
const T& val,
const T& min_val,
const T& max_val) {
205 return std::min(std::max(val, min_val), max_val);
210 template<
typename T,
typename U>
211 inline const U
Lerp(
const U& begin,
const U& end,
const T& t) {
212 return static_cast<U
>(begin + t * (end - begin));
217 return (value != 0) && ((value & (value - 1)) == 0);
223 #endif // ION_MATH_UTILS_H_
T Log2(T n)
Returns the base-2 logarithm of n.
T Tangent(const ion::math::Angle< T > &angle)
ion::math::Angle specialization of Tangent.
bool IsFinite(T x)
Tests whether a numeric value is finite.
T Cosine(const ion::math::Angle< T > &angle)
ion::math::Angle specialization of Cosine.
T DoubleFactorial(int x)
Returns the double factorial (!!) of x.
T Sqrt(const T &val)
Returns the square root of a value.
T Factorial(int x)
Returns the factorial (!) of x.
const U Lerp(const U &begin, const U &end, const T &t)
Linearly interpolates between two values.
const T Clamp(const T &val, const T &min_val, const T &max_val)
Clamps a value to lie between a minimum and maximum, inclusive.
uint32 NextPowerOf2(uint32 n)
Returns the next power of 2 greater than or equal to n.
bool IsPowerOfTwo(int value)
Returns true if a value is a power of two.
T Sine(const ion::math::Angle< T > &angle)
ion::math::Angle specialization of Sine.
const T Square(const T &val)
Squares a value.
const T Abs(const T &val)
Returns the absolute value of a number in a type-safe way.