math.inl - Engine C API Reference

math.inl
  1. #include "assert.h"
  2. namespace stingray_plugin_foundation {
  3. __forceinline bool math::is_pow2(unsigned x)
  4. {
  5. return (x & (x - 1)) == 0;
  6. }
  7. __forceinline float math::square_root(float f)
  8. {
  9. return sqrtf(f);
  10. }
  11. __forceinline float math::degrees_to_radians(float f)
  12. {
  13. return f / 180.0f * pi;
  14. }
  15. __forceinline float math::radians_to_degrees(float f)
  16. {
  17. return 180.0f * f / pi;
  18. }
  19. template <class T> __forceinline T math::max(T a, T b)
  20. {
  21. return a > b ? a : b;
  22. }
  23. template <class T> __forceinline T math::max3(T a, T b, T c)
  24. {
  25. return max(a, max(b,c));
  26. }
  27. template <class T> __forceinline T math::min(T a, T b)
  28. {
  29. return a < b ? a : b;
  30. }
  31. template <class T> __forceinline T math::min3(T a, T b, T c)
  32. {
  33. return min(a, min(b,c));
  34. }
  35. template <class T> __forceinline T math::clamp(T x, T min, T max)
  36. {
  37. if(x>max)
  38. return max;
  39. else if(x<min)
  40. return min;
  41. else
  42. return x;
  43. }
  44. template <class T> __forceinline T math::abs(T x)
  45. {
  46. if (x > 0)
  47. return x;
  48. else
  49. return -x;
  50. }
  51. template <class T> __forceinline T math::sign(T x)
  52. {
  53. if (x > 0)
  54. return T(1);
  55. else if (x == 0)
  56. return T(0);
  57. else
  58. return T(-1);
  59. }
  60. __forceinline unsigned math::branchless_max(unsigned a, unsigned b)
  61. {
  62. return a - ((a-b) & (a-b)>>31);
  63. }
  64. __forceinline unsigned math::branchless_min(unsigned a, unsigned b)
  65. {
  66. return b + ((a-b) & (a-b)>>31);
  67. }
  68. __forceinline unsigned math::div_ceil(unsigned a, unsigned b) {
  69. return (a+b-1)/b;
  70. }
  71. __forceinline unsigned math::div_round(unsigned a, unsigned b) {
  72. return (a+b/2)/b;
  73. }
  74. __forceinline float lerp(float a, float b, float p)
  75. {
  76. return a * (1-p) + b * p;
  77. }
  78. inline void Crossfade::update(float dt)
  79. {
  80. if (_fade_in_t < FLT_MAX) {
  81. _fade_in += (1 - _fade_in) * dt / _fade_in_t;
  82. _fade_in_t -= dt;
  83. if (_fade_in_t <= 0) {
  84. _fade_in_t = FLT_MAX;
  85. _fade_in = 1;
  86. }
  87. }
  88. if (_fade_out_t < FLT_MAX) {
  89. _fade_out += (1 - _fade_out) * dt / _fade_out_t;
  90. _fade_out_t -= dt;
  91. if (_fade_out_t <= 0) {
  92. _fade_out_t = FLT_MAX;
  93. _fade_out = 1;
  94. }
  95. }
  96. XENSURE(blend() >= 0.0f);
  97. }
  98. }