Util.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef CRYINCLUDE_CRYCOMMONTOOLS_UTIL_H
  9. #define CRYINCLUDE_CRYCOMMONTOOLS_UTIL_H
  10. #pragma once
  11. #include <cstring> // memset()
  12. #ifndef BIT
  13. #define BIT(x) (1 << (x))
  14. #endif
  15. union IntOrPtr
  16. {
  17. int m_int;
  18. unsigned int m_uint;
  19. void* m_pVoid;
  20. char* m_pChar;
  21. void setZero()
  22. {
  23. memset(this, 0, sizeof(*this));
  24. }
  25. bool operator==(const IntOrPtr& a) const
  26. {
  27. return memcmp(this, &a, sizeof(*this)) == 0;
  28. }
  29. bool operator!=(const IntOrPtr& a) const
  30. {
  31. return memcmp(this, &a, sizeof(*this)) != 0;
  32. }
  33. };
  34. namespace Util
  35. {
  36. // note: names 'getMin' and 'getMax' (instead of usual 'min' and 'max') are
  37. // used to avoid conflicts with global and/or user's #define of 'min' and 'max'
  38. template<class T>
  39. inline const T& getMin(const T& a, const T& b)
  40. {
  41. return (a < b) ? a : b;
  42. }
  43. template<class T>
  44. inline const T& getMax(const T& a, const T& b)
  45. {
  46. return (a < b) ? b : a;
  47. }
  48. template<class T>
  49. inline const T& getMin(const T& a, const T& b, const T& c)
  50. {
  51. return (a < b)
  52. ? ((a < c) ? a : c)
  53. : ((b < c) ? b : c);
  54. }
  55. template<class T>
  56. inline const T& getMax(const T& a, const T& b, const T& c)
  57. {
  58. return (a < b)
  59. ? ((b < c) ? c : b)
  60. : ((a < c) ? c : a);
  61. }
  62. template<class T>
  63. inline const T& getClamped(const T& a, const T& a_min, const T& a_max)
  64. {
  65. if (a < a_min)
  66. {
  67. return a_min;
  68. }
  69. if (a_max < a)
  70. {
  71. return a_max;
  72. }
  73. return a;
  74. }
  75. // note: name 'clampMinMax' (instead of usual 'clamp') is used
  76. // to avoid conflicts with global and/or user's #define of 'clamp'
  77. template<class T>
  78. inline void clampMinMax(T& a, const T& a_min, const T& a_max)
  79. {
  80. if (a < a_min)
  81. {
  82. a = a_min;
  83. }
  84. else if (a_max < a)
  85. {
  86. a = a_max;
  87. }
  88. }
  89. template<class T>
  90. inline void clampMin(T& a, const T& a_min)
  91. {
  92. if (a < a_min)
  93. {
  94. a = a_min;
  95. }
  96. }
  97. template<class T>
  98. inline void clampMax(T& a, const T& a_max)
  99. {
  100. if (a_max < a)
  101. {
  102. a = a_max;
  103. }
  104. }
  105. template <class TInteger>
  106. inline bool isPowerOfTwo(TInteger x)
  107. {
  108. return (x & (x - 1)) == 0;
  109. }
  110. template <class TInteger>
  111. inline TInteger getCeiledPowerOfTwo(TInteger x)
  112. {
  113. x = x - 1;
  114. if (sizeof(TInteger) > 0)
  115. {
  116. x |= x >> 1;
  117. }
  118. if (sizeof(TInteger) > 0)
  119. {
  120. x |= x >> 2;
  121. }
  122. if (sizeof(TInteger) > 0)
  123. {
  124. x |= x >> 4;
  125. }
  126. if (sizeof(TInteger) > 1)
  127. {
  128. x |= x >> 8;
  129. }
  130. if (sizeof(TInteger) > 2)
  131. {
  132. x |= x >> 16;
  133. }
  134. if (sizeof(TInteger) > 4)
  135. {
  136. x |= x >> 32;
  137. }
  138. return x + 1;
  139. }
  140. template <class TInteger>
  141. inline TInteger getFlooredPowerOfTwo(TInteger x)
  142. {
  143. if (!isPowerOfTwo(x))
  144. {
  145. x = getCeiledPowerOfTwo(x) >> 1;
  146. }
  147. return x;
  148. }
  149. template <class T>
  150. inline T square(T x)
  151. {
  152. return x * x;
  153. }
  154. template <class T>
  155. inline T cube(T x)
  156. {
  157. return x * x * x;
  158. }
  159. } // namespace Util
  160. #endif // CRYINCLUDE_CRYCOMMONTOOLS_UTIL_H