Math.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. namespace JPH {
  5. /// The constant \f$\pi\f$
  6. #define JPH_PI 3.14159265358979323846f
  7. /// Convert a value from degrees to radians
  8. inline constexpr float DegreesToRadians(float inV)
  9. {
  10. return inV * (JPH_PI / 180.0f);
  11. }
  12. /// Convert a value from radians to degrees
  13. inline constexpr float RadiansToDegrees(float inV)
  14. {
  15. return inV * (180.0f / JPH_PI);
  16. }
  17. /// Convert angle in radians to the range \f$[-\pi, \pi]\f$
  18. inline float CenterAngleAroundZero(float inV)
  19. {
  20. if (inV < -JPH_PI)
  21. {
  22. do
  23. inV += 2.0f * JPH_PI;
  24. while (inV < -JPH_PI);
  25. }
  26. else if (inV > JPH_PI)
  27. {
  28. do
  29. inV -= 2.0f * JPH_PI;
  30. while (inV > JPH_PI);
  31. }
  32. JPH_ASSERT(inV >= -JPH_PI && inV <= JPH_PI);
  33. return inV;
  34. }
  35. /// Clamp a value between two values
  36. template <typename T>
  37. inline constexpr T Clamp(T inV, T inMin, T inMax)
  38. {
  39. return min(max(inV, inMin), inMax);
  40. }
  41. /// Square a value
  42. template <typename T>
  43. inline constexpr T Square(T inV)
  44. {
  45. return inV * inV;
  46. }
  47. /// Returns \f$inV^3\f$.
  48. template <typename T>
  49. inline constexpr T Cubed(T inV)
  50. {
  51. return inV * inV * inV;
  52. }
  53. /// Get the sign of a value
  54. template <typename T>
  55. inline constexpr T Sign(T inV)
  56. {
  57. return inV < 0? T(-1) : T(1);
  58. }
  59. /// Check if inV is a power of 2
  60. template <typename T>
  61. inline constexpr bool IsPowerOf2(T inV)
  62. {
  63. return (inV & (inV - 1)) == 0;
  64. }
  65. /// Align inV up to the next inAlignment bytes
  66. template <typename T>
  67. inline T AlignUp(T inV, uint64 inAlignment)
  68. {
  69. JPH_ASSERT(IsPowerOf2(inAlignment));
  70. return T((uint64(inV) + inAlignment - 1) & ~(inAlignment - 1));
  71. }
  72. /// Check if inV is inAlignment aligned
  73. template <typename T>
  74. inline bool IsAligned(T inV, uint64 inAlignment)
  75. {
  76. JPH_ASSERT(IsPowerOf2(inAlignment));
  77. return (uint64(inV) & (inAlignment - 1)) == 0;
  78. }
  79. /// Compute number of trailing zero bits (how many low bits are zero)
  80. inline uint CountTrailingZeros(uint32 inValue)
  81. {
  82. #if defined(JPH_CPU_X64)
  83. #ifdef JPH_USE_LZCNT
  84. return _tzcnt_u32(inValue);
  85. #else
  86. if (inValue == 0)
  87. return 32;
  88. unsigned long result;
  89. _BitScanForward(&result, inValue);
  90. return result;
  91. #endif
  92. #elif defined(JPH_CPU_ARM64)
  93. return __builtin_clz(__builtin_bitreverse32(inValue));
  94. #else
  95. #error Undefined
  96. #endif
  97. }
  98. /// Compute the number of leading zero bits (how many high bits are zero)
  99. inline uint CountLeadingZeros(uint32 inValue)
  100. {
  101. #if defined(JPH_CPU_X64)
  102. #ifdef JPH_USE_LZCNT
  103. return _lzcnt_u32(inValue);
  104. #else
  105. if (inValue == 0)
  106. return 32;
  107. unsigned long result;
  108. _BitScanReverse(&result, inValue);
  109. return 31 - result;
  110. #endif
  111. #elif defined(JPH_CPU_ARM64)
  112. return __builtin_clz(inValue);
  113. #else
  114. #error Undefined
  115. #endif
  116. }
  117. /// Count the number of 1 bits in a value
  118. inline uint CountBits(uint32 inValue)
  119. {
  120. #if defined(JPH_CPU_X64)
  121. return _mm_popcnt_u32(inValue);
  122. #elif defined(JPH_CPU_ARM64)
  123. return __builtin_popcount(inValue);
  124. #else
  125. #error Undefined
  126. #endif
  127. }
  128. /// Get the next higher power of 2 of a value, or the value itself if the value is already a power of 2
  129. inline uint32 GetNextPowerOf2(uint32 inValue)
  130. {
  131. return inValue <= 1? uint32(1) : uint32(1) << (32 - CountLeadingZeros(inValue - 1));
  132. }
  133. } // JPH