Math.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef CORE_MATH_H
  2. #define CORE_MATH_H
  3. #include <math.h>
  4. #ifndef M_PI
  5. #define M_PI 3.14159265358979323846
  6. #endif
  7. #ifndef _GNU_SOURCE
  8. void sincosf(float a, float* s, float* c);
  9. #endif
  10. namespace Core::Math {
  11. template<typename T>
  12. T interpolate(const T& a, const T& b, float f) {
  13. return a * (1.0f - f) + b * f;
  14. }
  15. constexpr int roundUpLog2(int i) {
  16. if(i <= 0) {
  17. return 0;
  18. }
  19. int c = 1;
  20. while(((i - 1) >> c) > 0) {
  21. c++;
  22. }
  23. return c;
  24. }
  25. template<typename T>
  26. const T& min(const T& t) {
  27. return t;
  28. }
  29. template<typename T, typename... Args>
  30. const T& min(const T& t, Args&&... args) {
  31. const T& o = min(args...);
  32. return t < o ? t : o;
  33. }
  34. template<typename T>
  35. const T& max(const T& t) {
  36. return t;
  37. }
  38. template<typename T, typename... Args>
  39. const T& max(const T& t, Args&&... args) {
  40. const T& o = max(args...);
  41. return o < t ? t : o;
  42. }
  43. template<typename T>
  44. const T& clamp(const T& t, const T& borderA, const T& borderB) {
  45. const T& low = min(borderA, borderB);
  46. const T& high = max(borderA, borderB);
  47. return max(low, min(high, t));
  48. }
  49. }
  50. #endif