16 #ifndef MATHFU_UTILITIES_H_
17 #define MATHFU_UTILITIES_H_
80 #define MATHFU_COMPILE_WITHOUT_SIMD_SUPPORT
83 #if !defined(MATHFU_COMPILE_WITHOUT_SIMD_SUPPORT)
85 #define MATHFU_COMPILE_WITH_SIMD
86 #elif defined(__ARM_NEON__)
87 #define MATHFU_COMPILE_WITH_SIMD
88 #elif defined(_M_IX86_FP) // MSVC
89 #if _M_IX86_FP >= 1 // SSE enabled
90 #define MATHFU_COMPILE_WITH_SIMD
91 #endif // _M_IX86_FP >= 1
93 #endif // !defined(MATHFU_COMPILE_WITHOUT_SIMD_SUPPORT)
120 #define MATHFU_COMPILE_FORCE_PADDING
124 #ifdef MATHFU_COMPILE_WITH_SIMD
139 #define MATHFU_COMPILE_WITH_PADDING
141 #if defined(MATHFU_COMPILE_FORCE_PADDING)
142 #if MATHFU_COMPILE_FORCE_PADDING == 1
143 #if !defined(MATHFU_COMPILE_WITH_PADDING)
144 #define MATHFU_COMPILE_WITH_PADDING
145 #endif // !defined(MATHFU_COMPILE_WITH_PADDING)
147 #if defined(MATHFU_COMPILE_WITH_PADDING)
148 #undef MATHFU_COMPILE_WITH_PADDING
149 #endif // MATHFU_COMPILE_WITH_PADDING
150 #endif // MATHFU_COMPILE_FORCE_PADDING == 1
151 #endif // MATHFU_COMPILE_FORCE_PADDING
153 #endif // MATHFU_COMPILE_WITH_SIMD
161 #define MATHFU_VERSION_MAJOR 1
165 #define MATHFU_VERSION_MINOR 1
169 #define MATHFU_VERSION_REVISION 0
174 #define MATHFU_STRING_EXPAND(X) #X
175 #define MATHFU_STRING(X) MATHFU_STRING_EXPAND(X)
180 #if defined(MATHFU_COMPILE_WITH_SIMD)
181 #define MATHFU_BUILD_OPTIONS_SIMD "[simd]"
183 #define MATHFU_BUILD_OPTIONS_SIMD "[no simd]"
184 #endif // defined(MATHFU_COMPILE_WITH_SIMD)
185 #if defined(MATHFU_COMPILE_WITH_PADDING)
186 #define MATHFU_BUILD_OPTIONS_PADDING "[padding]"
188 #define MATHFU_BUILD_OPTIONS_PADDING "[no padding]"
189 #endif // defined(MATHFU_COMPILE_WITH_PADDING)
196 #define MATHFU_BUILD_OPTIONS_STRING \
197 (MATHFU_BUILD_OPTIONS_SIMD " " MATHFU_BUILD_OPTIONS_PADDING)
201 #if !defined(_WIN32) && !defined(__CYGWIN__)
221 volatile __attribute__((weak)) const
char *kMathFuVersionString =
226 #endif // !defined(_WIN32) && !defined(__CYGWIN__)
230 struct static_assert_util;
232 struct static_assert_util<true> {};
244 #define MATHFU_STATIC_ASSERT(x) static_assert_util<(x)>()
252 #define MATHFU_UNROLLED_LOOP(iterator, number_of_iterations, operation) \
254 const int iterator = 0; \
256 if ((number_of_iterations) > 1) { \
257 const int iterator = 1; \
259 if ((number_of_iterations) > 2) { \
260 const int iterator = 2; \
262 if ((number_of_iterations) > 3) { \
263 const int iterator = 3; \
265 if ((number_of_iterations) > 4) { \
266 for (int iterator = 4; iterator < (number_of_iterations); \
292 T
Clamp(
const T &x,
const T &lower,
const T &upper) {
293 return std::max<T>(lower, std::min<T>(x, upper));
310 template <
class T,
class T2>
311 T
Lerp(
const T &range_start,
const T &range_end,
const T2 &percent) {
312 const T2 one_minus_percent =
static_cast<T2
>(1.0) - percent;
313 return range_start * one_minus_percent + range_end * percent;
329 T
Lerp(
const T &range_start,
const T &range_end,
const T &percent) {
330 return Lerp<T, T>(range_start, range_end, percent);
343 bool InRange(T val, T range_start, T range_end) {
344 return val >= range_start && val < range_end;
362 return static_cast<T
>(rand()) / static_cast<T>(RAND_MAX);
368 return static_cast<float>(rand() >> 8) /
369 (static_cast<float>((RAND_MAX >> 8) + 1));
376 return static_cast<double>(rand()) / (static_cast<double>(RAND_MAX + 1LL));
392 return (Random<T>() * range * 2) - range;
409 return Lerp(range_start, range_end, Random<T>());
414 inline int RandomInRange<int>(
int range_start,
int range_end) {
415 return static_cast<int>(RandomInRange<float>(
static_cast<float>(range_start),
416 static_cast<float>(range_end)));
426 return static_cast<T
>(
427 pow(static_cast<T>(2), ceil(log(x) / log(static_cast<T>(2)))));
432 inline int32_t RoundUpToPowerOf2<>(int32_t x) {
447 template <
typename T>
449 return (v +
sizeof(T) - 1) & ~(
sizeof(T) - 1);
474 #define MATHFU_ALIGNMENT 16
486 #if defined(_MSC_VER) && _MSC_VER >= 1900 // MSVC 2015
492 if (!buf)
return NULL;
494 uint8_t *aligned_buf =
reinterpret_cast<uint8_t *
>(
500 assert(static_cast<size_t>(aligned_buf - buf) >
sizeof(
void *));
501 *(
reinterpret_cast<uint8_t **
>(aligned_buf) - 1) = buf;
503 #endif // defined(_MSC_VER) && _MSC_VER >= 1900 // MSVC 2015
511 #if defined(_MSC_VER) && _MSC_VER >= 1900 // MSVC 2015
514 if (p == NULL)
return;
515 free(*(reinterpret_cast<uint8_t **>(p) - 1));
516 #endif // defined(_MSC_VER) && _MSC_VER >= 1900 // MSVC 2015
528 template <
typename T>
556 template <
typename _Tp1>
576 #if defined(_MSC_VER)
577 #if _MSC_VER <= 1800 // MSVC 2013
578 #if !defined(noexcept)
580 #endif // !defined(noexcept)
581 #endif // _MSC_VER <= 1800
582 #endif // defined(_MSC_VER)
592 #define MATHFU_DEFINE_GLOBAL_SIMD_AWARE_NEW_DELETE \
593 void *operator new(std::size_t n) { return mathfu::AllocateAligned(n); } \
594 void *operator new[](std::size_t n) { return mathfu::AllocateAligned(n); } \
595 void operator delete(void *p) noexcept { mathfu::FreeAligned(p); } \
596 void operator delete[](void *p) noexcept { mathfu::FreeAligned(p); }
600 #define MATHFU_DEFINE_CLASS_SIMD_AWARE_NEW_DELETE \
601 static void *operator new(std::size_t n) { \
602 return mathfu::AllocateAligned(n); \
604 static void *operator new[](std::size_t n) { \
605 return mathfu::AllocateAligned(n); \
607 static void *operator new(std::size_t , void *p) { return p; } \
608 static void *operator new[](std::size_t , void *p) { return p; } \
609 static void operator delete(void *p) { mathfu::FreeAligned(p); } \
610 static void operator delete[](void *p) { mathfu::FreeAligned(p); } \
611 static void operator delete(void * , void * ) {} \
612 static void operator delete[](void * , void * ) {}
618 #endif // MATHFU_UTILITIES_H_
Obtains an allocator of a different type.
Definition: utilities.h:557
void deallocate(pointer p, size_type)
Definition: utilities.h:573
T RoundUpToPowerOf2(T x)
Round a value up to the nearest power of 2.
Definition: utilities.h:425
void * AllocateAligned(size_t n)
Allocate an aligned block of memory.
Definition: utilities.h:485
uint32_t RoundUpToTypeBoundary(uint32_t v)
Round a value up to the type's size boundary.
Definition: utilities.h:448
simd_allocator(const simd_allocator &a)
Constructs and copies a simd_allocator.
Definition: utilities.h:543
#define MATHFU_VERSION_MINOR
Minor version number of the library.
Definition: utilities.h:165
#define MATHFU_ALIGNMENT
Alignment (in bytes) of memory allocated by AllocateAligned.
Definition: utilities.h:474
const T * const_pointer
Const pointer of type T.
Definition: utilities.h:536
volatile const char * kMathFuVersionString
String which identifies the current version of MathFu.
Definition: utilities.h:221
T RandomRange(T range)
Generate a random value of type T in the range -range...+range.
Definition: utilities.h:391
SIMD-safe memory allocator, for use with STL types like std::vector.
Definition: utilities.h:529
~simd_allocator()
Destructs a simd_allocator.
Definition: utilities.h:551
void FreeAligned(void *p)
Deallocate a block of memory allocated with AllocateAligned().
Definition: utilities.h:510
simd_allocator()
Constructs a simd_allocator.
Definition: utilities.h:539
simd_allocator(const simd_allocator< U > &a)
Constructs and copies a simd_allocator.
Definition: utilities.h:549
bool InRange(T val, T range_start, T range_end)
Check if val is within [range_start..range_end).
Definition: utilities.h:343
size_t size_type
Size type.
Definition: utilities.h:532
T Clamp(const T &x, const T &lower, const T &upper)
Clamp x within [lower, upper].
Definition: utilities.h:292
T Random()
Generate a random value of type T.
Definition: utilities.h:361
T * pointer
Pointer of type T.
Definition: utilities.h:534
simd_allocator< _Tp1 > other
Allocator of type _Tp1.
Definition: utilities.h:559
#define MATHFU_VERSION_REVISION
Revision number of the library.
Definition: utilities.h:169
T Lerp(const T &range_start, const T &range_end, const T2 &percent)
Linearly interpolate between range_start and range_end, based on percent.
Definition: utilities.h:311
T RandomInRange(T range_start, T range_end)
Generate a random number between [range_start, range_end].
Definition: utilities.h:408
pointer allocate(size_type n)
Allocate memory for object T.
Definition: utilities.h:566
#define MATHFU_VERSION_MAJOR
Major version number of the library.
Definition: utilities.h:161