Math.h 1.3 KB

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