Functions.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. /// @file
  6. /// Contains misc functions
  7. #ifndef ANKI_UTIL_FUNCTIONS_H
  8. #define ANKI_UTIL_FUNCTIONS_H
  9. #include "anki/util/StdTypes.h"
  10. #include "anki/util/Assert.h"
  11. #include <cmath>
  12. #include <utility>
  13. #include <new>
  14. namespace anki {
  15. /// @addtogroup util_other
  16. /// @{
  17. /// Pick a random number from min to max
  18. extern I32 randRange(I32 min, I32 max);
  19. /// Pick a random number from min to max
  20. extern U32 randRange(U32 min, U32 max);
  21. /// Pick a random number from min to max
  22. extern F32 randRange(F32 min, F32 max);
  23. /// Pick a random number from min to max
  24. extern F64 randRange(F64 min, F64 max);
  25. extern F32 randFloat(F32 max);
  26. /// Get min of two values.
  27. template<typename T>
  28. inline T min(T a, T b)
  29. {
  30. return (a < b) ? a : b;
  31. }
  32. /// Get max of two values.
  33. template<typename T>
  34. inline T max(T a, T b)
  35. {
  36. return (a > b) ? a : b;
  37. }
  38. template<typename T>
  39. inline T clamp(T v, T minv, T maxv)
  40. {
  41. return min<T>(max<T>(minv, v), maxv);
  42. }
  43. /// Check if a number os a power of 2
  44. template<typename Int>
  45. inline Bool isPowerOfTwo(Int x)
  46. {
  47. while(((x % 2) == 0) && x > 1)
  48. {
  49. x /= 2;
  50. }
  51. return (x == 1);
  52. }
  53. /// Get the next power of two number. For example if x is 130 this will return
  54. /// 256
  55. template<typename Int>
  56. inline Int nextPowerOfTwo(Int x)
  57. {
  58. return pow(2, ceil(log(x) / log(2)));
  59. }
  60. /// Get align number
  61. /// @param alignment The bytes of alignment
  62. /// @param value The value to align
  63. template<typename Type>
  64. inline Type getAlignedRoundUp(PtrSize alignment, Type value)
  65. {
  66. ANKI_ASSERT(isPowerOfTwo(alignment));
  67. PtrSize v = (PtrSize)value;
  68. v = (v + alignment - 1) & ~(alignment - 1);
  69. return (Type)v;
  70. }
  71. /// Align number
  72. /// @param alignment The bytes of alignment
  73. /// @param value The value to align
  74. template<typename Type>
  75. inline void alignRoundUp(PtrSize alignment, Type& value)
  76. {
  77. value = getAlignedRoundUp(alignment, value);
  78. }
  79. /// Get align number
  80. /// @param alignment The bytes of alignment
  81. /// @param value The value to align
  82. template<typename Type>
  83. inline Type getAlignedRoundDown(PtrSize alignment, Type value)
  84. {
  85. ANKI_ASSERT(isPowerOfTwo(alignment));
  86. PtrSize v = (PtrSize)value;
  87. v &= ~(alignment - 1);
  88. return (Type)v;
  89. }
  90. /// Align number
  91. /// @param alignment The bytes of alignment
  92. /// @param value The value to align
  93. template<typename Type>
  94. inline void alignRoundDown(PtrSize alignment, Type& value)
  95. {
  96. value = getAlignedRoundDown(alignment, value);
  97. }
  98. /// Check if a number is aligned
  99. template<typename Type>
  100. inline Bool isAligned(PtrSize alignment, Type value)
  101. {
  102. return ((PtrSize)value % alignment) == 0;
  103. }
  104. /// A simple template trick to remove the pointer from one type
  105. ///
  106. /// Example:
  107. /// @code
  108. /// double a = 1234.456;
  109. /// RemovePointer<decltype(&a)>::Type b = a;
  110. /// @endcode
  111. /// The b is of type double
  112. template<typename T>
  113. struct RemovePointer;
  114. template<typename T>
  115. struct RemovePointer<T*>
  116. {
  117. typedef T Type;
  118. };
  119. /// Perform a static cast of a pointer
  120. template<typename To, typename From>
  121. To staticCastPtr(From from)
  122. {
  123. #if ANKI_DEBUG == 1
  124. To ptr = dynamic_cast<To>(from);
  125. ANKI_ASSERT(ptr != nullptr);
  126. return ptr;
  127. #else
  128. return static_cast<To>(from);
  129. #endif
  130. }
  131. /// Count bits
  132. inline U32 countBits(U32 number)
  133. {
  134. #if defined(__GNUC__)
  135. return __builtin_popcount(number);
  136. #else
  137. # error "Unimplemented"
  138. #endif
  139. }
  140. /// Call an object constructor.
  141. template<typename T, typename... TArgs>
  142. void construct(T* p, TArgs&&... args)
  143. {
  144. // Placement new
  145. ::new(reinterpret_cast<void*>(p)) T(std::forward<TArgs>(args)...);
  146. }
  147. /// Call destructor.
  148. template<typename T>
  149. void destruct(T* p)
  150. {
  151. p->~T();
  152. }
  153. /// @}
  154. } // end namespace anki
  155. #endif