#ifndef COREMATH_H #define COREMATH_H #include #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #ifndef _GNU_SOURCE void sincosf(float a, float* s, float* c); #endif namespace Math { template T interpolate(const T& a, const T& b, float f) { return a * (1.0f - f) + b * f; } template int popCount(const T& t) { static constexpr int map[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}; int sum = 0; for(int i = 0; i < static_cast(sizeof(T) * 8); i += 4) { sum += map[(t >> i) & 0xF]; } return sum; } constexpr int roundUpLog2(int i) { if(i <= 0) { return 0; } int c = 1; while(((i - 1) >> c) > 0) { c++; } return c; } template const T& min(const T& t) { return t; } template const T& min(const T& t, Args&&... args) { const T& o = min(args...); return t < o ? t : o; } template const T& max(const T& t) { return t; } template const T& max(const T& t, Args&&... args) { const T& o = max(args...); return o < t ? t : o; } template const T& clamp(const T& t, const T& borderA, const T& borderB) { const T& low = min(borderA, borderB); const T& high = max(borderA, borderB); return max(low, min(high, t)); } } #endif