Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utils.h
Go to the documentation of this file.
1 
18 #ifndef ION_MATH_UTILS_H_
19 #define ION_MATH_UTILS_H_
20 
23 
24 #include <algorithm>
25 #include <cmath>
26 
27 #include "base/integral_types.h"
28 
29 namespace ion {
30 namespace math {
31 
33 template <typename T>
34 inline bool IsFinite(T x) {
35  return (x == x &&
36  x != std::numeric_limits<T>::infinity() &&
37  x != -std::numeric_limits<T>::infinity());
38 }
39 
41 template <typename T>
42 inline const T Abs(const T& val) {
43  return val >= static_cast<T>(0) ? val : -val;
44 }
45 
47 template <typename T>
48 inline const T Square(const T& val) {
49  return val * val;
50 }
51 
58 template <typename T>
59 inline T Sqrt(const T& val) {
60  return static_cast<T>(sqrt(static_cast<double>(val)));
61 }
62 template <>
63 inline float Sqrt(const float& val) {
64  return sqrtf(val);
65 }
66 
68 template <typename T>
69 inline T Cosine(T angle) {
70  return cos(angle);
71 }
73 template <>
74 inline float Cosine(float angle) {
75  return cosf(angle);
76 }
77 
79 template <typename T>
80 inline T Sine(T angle) {
81  return sin(angle);
82 }
84 template <>
85 inline float Sine(float angle) {
86  return sinf(angle);
87 }
88 
90 template <typename T>
91 inline T Tangent(T angle) {
92  return tan(angle);
93 }
95 template <>
96 inline float Tangent(float angle) {
97  return tanf(angle);
98 }
99 
102 template <typename T>
103 inline T Factorial(int x) {
104  if (x < 0) return 0;
105  T result = 1;
106  for (; x > 0; x--) result *= x;
107  return result;
108 }
109 
114 template <typename T>
115 inline T DoubleFactorial(int x) {
116  if (x < 0) return 0;
117  T result = 1;
118  for (; x > 0; x -= 2) result *= x;
119  return result;
120 }
121 
124 inline uint32 NextPowerOf2(uint32 n) {
125  if (n == 0) return 0;
126  n -= 1;
127  n |= n >> 16;
128  n |= n >> 8;
129  n |= n >> 4;
130  n |= n >> 2;
131  n |= n >> 1;
132  return n + 1;
133 }
135  if (n == 0) return 0;
136  n -= 1;
137  n |= n >> 32;
138  n |= n >> 16;
139  n |= n >> 8;
140  n |= n >> 4;
141  n |= n >> 2;
142  n |= n >> 1;
143  return n + 1;
144 }
145 
147 template <typename T>
148 inline T Log2(T n) {
149  static const T kInverseOfLogOf2 = static_cast<T>(1.4426950408889634);
150  return static_cast<T>(log(n) * kInverseOfLogOf2);
151 }
155 template <>
156 inline uint32 Log2(uint32 n) {
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};
160 
161  n |= n >> 1; // First round down to one less than a power of 2.
162  n |= n >> 2;
163  n |= n >> 4;
164  n |= n >> 8;
165  n |= n >> 16;
166 
167  return kMultiplyDeBruijnBitPosition[(n * 0x07C4ACDDU) >> 27];
168 }
169 template <>
170 inline int Log2(int n) {
171  if (n <= 0)
172  return 0;
173  else
174  return Log2(static_cast<uint32>(n));
175 }
176 template <>
177 inline uint64 Log2(uint64 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};
183  n |= n >> 1; // First round down to one less than a power of 2.
184  n |= n >> 2;
185  n |= n >> 4;
186  n |= n >> 8;
187  n |= n >> 16;
188  n |= n >> 32;
189  n -= n >> 1;
190 
191  return kMultiplyDeBruijnBitPosition[(n * 0x07EDD5E59A4E28C2ULL) >> 58];
192 }
193 template <>
194 inline int64 Log2(int64 n) {
195  if (n <= 0)
196  return 0;
197  else
198  return Log2(static_cast<uint64>(n));
199 }
200 
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);
206 }
207 
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));
213 }
214 
216 inline bool IsPowerOfTwo(int value) {
217  return (value != 0) && ((value & (value - 1)) == 0);
218 }
219 
220 } // namespace math
221 } // namespace ion
222 
223 #endif // ION_MATH_UTILS_H_
T Log2(T n)
Returns the base-2 logarithm of n.
Definition: utils.h:148
T Tangent(const ion::math::Angle< T > &angle)
ion::math::Angle specialization of Tangent.
Definition: angleutils.h:92
double value
bool IsFinite(T x)
Tests whether a numeric value is finite.
Definition: utils.h:34
T Cosine(const ion::math::Angle< T > &angle)
ion::math::Angle specialization of Cosine.
Definition: angleutils.h:80
T DoubleFactorial(int x)
Returns the double factorial (!!) of x.
Definition: utils.h:115
T Sqrt(const T &val)
Returns the square root of a value.
Definition: utils.h:59
T Factorial(int x)
Returns the factorial (!) of x.
Definition: utils.h:103
const U Lerp(const U &begin, const U &end, const T &t)
Linearly interpolates between two values.
Definition: utils.h:211
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.
Definition: utils.h:204
uint32 NextPowerOf2(uint32 n)
Returns the next power of 2 greater than or equal to n.
Definition: utils.h:124
bool IsPowerOfTwo(int value)
Returns true if a value is a power of two.
Definition: utils.h:216
T Sine(const ion::math::Angle< T > &angle)
ion::math::Angle specialization of Sine.
Definition: angleutils.h:86
const T Square(const T &val)
Squares a value.
Definition: utils.h:48
const T Abs(const T &val)
Returns the absolute value of a number in a type-safe way.
Definition: utils.h:42