Math.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #if defined(JPH_USE_TZCNT)
  84. return _tzcnt_u32(inValue);
  85. #elif defined(JPH_COMPILER_MSVC)
  86. if (inValue == 0)
  87. return 32;
  88. unsigned long result;
  89. _BitScanForward(&result, inValue);
  90. return result;
  91. #else
  92. if (inValue == 0)
  93. return 32;
  94. return __builtin_ctz(inValue);
  95. #endif
  96. #elif defined(JPH_CPU_ARM64)
  97. return __builtin_clz(__builtin_bitreverse32(inValue));
  98. #else
  99. #error Undefined
  100. #endif
  101. }
  102. /// Compute the number of leading zero bits (how many high bits are zero)
  103. inline uint CountLeadingZeros(uint32 inValue)
  104. {
  105. #if defined(JPH_CPU_X64)
  106. #if defined(JPH_USE_LZCNT)
  107. return _lzcnt_u32(inValue);
  108. #elif defined(JPH_COMPILER_MSVC)
  109. if (inValue == 0)
  110. return 32;
  111. unsigned long result;
  112. _BitScanReverse(&result, inValue);
  113. return 31 - result;
  114. #else
  115. if (inValue == 0)
  116. return 32;
  117. return __builtin_clz(inValue);
  118. #endif
  119. #elif defined(JPH_CPU_ARM64)
  120. return __builtin_clz(inValue);
  121. #else
  122. #error Undefined
  123. #endif
  124. }
  125. /// Count the number of 1 bits in a value
  126. inline uint CountBits(uint32 inValue)
  127. {
  128. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  129. return __builtin_popcount(inValue);
  130. #elif defined(JPH_COMPILER_MSVC)
  131. #if defined(JPH_USE_SSE4_2)
  132. return _mm_popcnt_u32(inValue);
  133. #else
  134. inValue = inValue - ((inValue >> 1) & 0x55555555);
  135. inValue = (inValue & 0x33333333) + ((inValue >> 2) & 0x33333333);
  136. inValue = (inValue + (inValue >> 4)) & 0x0F0F0F0F;
  137. return (inValue * 0x01010101) >> 24;
  138. #endif
  139. #else
  140. #error Undefined
  141. #endif
  142. }
  143. /// Get the next higher power of 2 of a value, or the value itself if the value is already a power of 2
  144. inline uint32 GetNextPowerOf2(uint32 inValue)
  145. {
  146. return inValue <= 1? uint32(1) : uint32(1) << (32 - CountLeadingZeros(inValue - 1));
  147. }
  148. } // JPH