utils.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // This code is in the public domain -- Ignacio Castaño <[email protected]>
  2. #ifndef NV_CORE_UTILS_H
  3. #define NV_CORE_UTILS_H
  4. #include "debug.h" // nvdebugcheck
  5. #include <new> // for placement new
  6. // Just in case. Grrr.
  7. #undef min
  8. #undef max
  9. #define NV_INT8_MIN (-128)
  10. #define NV_INT8_MAX 127
  11. #define NV_UINT8_MAX 255
  12. #define NV_INT16_MIN (-32767-1)
  13. #define NV_INT16_MAX 32767
  14. #define NV_UINT16_MAX 0xffff
  15. #define NV_INT32_MIN (-2147483647-1)
  16. #define NV_INT32_MAX 2147483647
  17. #define NV_UINT32_MAX 0xffffffff
  18. #define NV_INT64_MAX POSH_I64(9223372036854775807)
  19. #define NV_INT64_MIN (-POSH_I64(9223372036854775807)-1)
  20. #define NV_UINT64_MAX POSH_U64(0xffffffffffffffff)
  21. #define NV_HALF_MAX 65504.0F
  22. #define NV_FLOAT_MAX 3.402823466e+38F
  23. #define NV_INTEGER_TO_FLOAT_MAX 16777217 // Largest integer such that it and all smaller integers can be stored in a 32bit float.
  24. namespace nv
  25. {
  26. // Less error prone than casting. From CB:
  27. // http://cbloomrants.blogspot.com/2011/06/06-17-11-c-casting-is-devil.html
  28. // These intentionally look like casts.
  29. // uint32 casts:
  30. template <typename T> inline uint32 U32(T x) { return x; }
  31. template <> inline uint32 U32<uint64>(uint64 x) { nvDebugCheck(x <= NV_UINT32_MAX); return (uint32)x; }
  32. template <> inline uint32 U32<int64>(int64 x) { nvDebugCheck(x >= 0 && x <= NV_UINT32_MAX); return (uint32)x; }
  33. //template <> inline uint32 U32<uint32>(uint32 x) { return x; }
  34. template <> inline uint32 U32<int32>(int32 x) { nvDebugCheck(x >= 0); return (uint32)x; }
  35. //template <> inline uint32 U32<uint16>(uint16 x) { return x; }
  36. template <> inline uint32 U32<int16>(int16 x) { nvDebugCheck(x >= 0); return (uint32)x; }
  37. //template <> inline uint32 U32<uint8>(uint8 x) { return x; }
  38. template <> inline uint32 U32<int8>(int8 x) { nvDebugCheck(x >= 0); return (uint32)x; }
  39. // int32 casts:
  40. template <typename T> inline int32 I32(T x) { return x; }
  41. template <> inline int32 I32<uint64>(uint64 x) { nvDebugCheck(x <= NV_INT32_MAX); return (int32)x; }
  42. template <> inline int32 I32<int64>(int64 x) { nvDebugCheck(x >= NV_INT32_MIN && x <= NV_UINT32_MAX); return (int32)x; }
  43. template <> inline int32 I32<uint32>(uint32 x) { nvDebugCheck(x <= NV_INT32_MAX); return (int32)x; }
  44. //template <> inline int32 I32<int32>(int32 x) { return x; }
  45. //template <> inline int32 I32<uint16>(uint16 x) { return x; }
  46. //template <> inline int32 I32<int16>(int16 x) { return x; }
  47. //template <> inline int32 I32<uint8>(uint8 x) { return x; }
  48. //template <> inline int32 I32<int8>(int8 x) { return x; }
  49. // uint16 casts:
  50. template <typename T> inline uint16 U16(T x) { return x; }
  51. template <> inline uint16 U16<uint64>(uint64 x) { nvDebugCheck(x <= NV_UINT16_MAX); return (uint16)x; }
  52. template <> inline uint16 U16<int64>(int64 x) { nvDebugCheck(x >= 0 && x <= NV_UINT16_MAX); return (uint16)x; }
  53. template <> inline uint16 U16<uint32>(uint32 x) { nvDebugCheck(x <= NV_UINT16_MAX); return (uint16)x; }
  54. template <> inline uint16 U16<int32>(int32 x) { nvDebugCheck(x >= 0 && x <= NV_UINT16_MAX); return (uint16)x; }
  55. //template <> inline uint16 U16<uint16>(uint16 x) { return x; }
  56. template <> inline uint16 U16<int16>(int16 x) { nvDebugCheck(x >= 0); return (uint16)x; }
  57. //template <> inline uint16 U16<uint8>(uint8 x) { return x; }
  58. template <> inline uint16 U16<int8>(int8 x) { nvDebugCheck(x >= 0); return (uint16)x; }
  59. // int16 casts:
  60. template <typename T> inline int16 I16(T x) { return x; }
  61. template <> inline int16 I16<uint64>(uint64 x) { nvDebugCheck(x <= NV_INT16_MAX); return (int16)x; }
  62. template <> inline int16 I16<int64>(int64 x) { nvDebugCheck(x >= NV_INT16_MIN && x <= NV_UINT16_MAX); return (int16)x; }
  63. template <> inline int16 I16<uint32>(uint32 x) { nvDebugCheck(x <= NV_INT16_MAX); return (int16)x; }
  64. template <> inline int16 I16<int32>(int32 x) { nvDebugCheck(x >= NV_INT16_MIN && x <= NV_UINT16_MAX); return (int16)x; }
  65. template <> inline int16 I16<uint16>(uint16 x) { nvDebugCheck(x <= NV_INT16_MAX); return (int16)x; }
  66. //template <> inline int16 I16<int16>(int16 x) { return x; }
  67. //template <> inline int16 I16<uint8>(uint8 x) { return x; }
  68. //template <> inline int16 I16<int8>(int8 x) { return x; }
  69. // uint8 casts:
  70. template <typename T> inline uint8 U8(T x) { return x; }
  71. template <> inline uint8 U8<uint64>(uint64 x) { nvDebugCheck(x <= NV_UINT8_MAX); return (uint8)x; }
  72. template <> inline uint8 U8<int64>(int64 x) { nvDebugCheck(x >= 0 && x <= NV_UINT8_MAX); return (uint8)x; }
  73. template <> inline uint8 U8<uint32>(uint32 x) { nvDebugCheck(x <= NV_UINT8_MAX); return (uint8)x; }
  74. template <> inline uint8 U8<int32>(int32 x) { nvDebugCheck(x >= 0 && x <= NV_UINT8_MAX); return (uint8)x; }
  75. template <> inline uint8 U8<uint16>(uint16 x) { nvDebugCheck(x <= NV_UINT8_MAX); return (uint8)x; }
  76. template <> inline uint8 U8<int16>(int16 x) { nvDebugCheck(x >= 0 && x <= NV_UINT8_MAX); return (uint8)x; }
  77. //template <> inline uint8 U8<uint8>(uint8 x) { return x; }
  78. template <> inline uint8 U8<int8>(int8 x) { nvDebugCheck(x >= 0); return (uint8)x; }
  79. //template <> inline uint8 U8<float>(int8 x) { nvDebugCheck(x >= 0.0f && x <= 255.0f); return (uint8)x; }
  80. // int8 casts:
  81. template <typename T> inline int8 I8(T x) { return x; }
  82. template <> inline int8 I8<uint64>(uint64 x) { nvDebugCheck(x <= NV_INT8_MAX); return (int8)x; }
  83. template <> inline int8 I8<int64>(int64 x) { nvDebugCheck(x >= NV_INT8_MIN && x <= NV_UINT8_MAX); return (int8)x; }
  84. template <> inline int8 I8<uint32>(uint32 x) { nvDebugCheck(x <= NV_INT8_MAX); return (int8)x; }
  85. template <> inline int8 I8<int32>(int32 x) { nvDebugCheck(x >= NV_INT8_MIN && x <= NV_UINT8_MAX); return (int8)x; }
  86. template <> inline int8 I8<uint16>(uint16 x) { nvDebugCheck(x <= NV_INT8_MAX); return (int8)x; }
  87. template <> inline int8 I8<int16>(int16 x) { nvDebugCheck(x >= NV_INT8_MIN && x <= NV_UINT8_MAX); return (int8)x; }
  88. template <> inline int8 I8<uint8>(uint8 x) { nvDebugCheck(x <= NV_INT8_MAX); return (int8)x; }
  89. //template <> inline int8 I8<int8>(int8 x) { return x; }
  90. // float casts:
  91. template <typename T> inline float F32(T x) { return x; }
  92. template <> inline float F32<uint64>(uint64 x) { nvDebugCheck(x <= NV_INTEGER_TO_FLOAT_MAX); return (float)x; }
  93. template <> inline float F32<int64>(int64 x) { nvDebugCheck(x >= -NV_INTEGER_TO_FLOAT_MAX && x <= NV_INTEGER_TO_FLOAT_MAX); return (float)x; }
  94. template <> inline float F32<uint32>(uint32 x) { nvDebugCheck(x <= NV_INTEGER_TO_FLOAT_MAX); return (float)x; }
  95. template <> inline float F32<int32>(int32 x) { nvDebugCheck(x >= -NV_INTEGER_TO_FLOAT_MAX && x <= NV_INTEGER_TO_FLOAT_MAX); return (float)x; }
  96. // The compiler should not complain about these conversions:
  97. //template <> inline float F32<uint16>(uint16 x) { nvDebugCheck(return (float)x; }
  98. //template <> inline float F32<int16>(int16 x) { nvDebugCheck(return (float)x; }
  99. //template <> inline float F32<uint8>(uint8 x) { nvDebugCheck(return (float)x; }
  100. //template <> inline float F32<int8>(int8 x) { nvDebugCheck(return (float)x; }
  101. /// Swap two values.
  102. template <typename T>
  103. inline void swap(T & a, T & b)
  104. {
  105. T temp(a);
  106. a = b;
  107. b = temp;
  108. }
  109. /// Return the maximum of the two arguments. For floating point values, it returns the second value if the first is NaN.
  110. template <typename T>
  111. //inline const T & max(const T & a, const T & b)
  112. inline T max(const T & a, const T & b)
  113. {
  114. return (b < a) ? a : b;
  115. }
  116. /// Return the maximum of the four arguments.
  117. template <typename T>
  118. //inline const T & max4(const T & a, const T & b, const T & c)
  119. inline T max4(const T & a, const T & b, const T & c, const T & d)
  120. {
  121. return max(max(a, b), max(c, d));
  122. }
  123. /// Return the maximum of the three arguments.
  124. template <typename T>
  125. //inline const T & max3(const T & a, const T & b, const T & c)
  126. inline T max3(const T & a, const T & b, const T & c)
  127. {
  128. return max(a, max(b, c));
  129. }
  130. /// Return the minimum of two values.
  131. template <typename T>
  132. //inline const T & min(const T & a, const T & b)
  133. inline T min(const T & a, const T & b)
  134. {
  135. return (a < b) ? a : b;
  136. }
  137. /// Return the maximum of the three arguments.
  138. template <typename T>
  139. //inline const T & min3(const T & a, const T & b, const T & c)
  140. inline T min3(const T & a, const T & b, const T & c)
  141. {
  142. return min(a, min(b, c));
  143. }
  144. /// Clamp between two values.
  145. template <typename T>
  146. //inline const T & clamp(const T & x, const T & a, const T & b)
  147. inline T clamp(const T & x, const T & a, const T & b)
  148. {
  149. return min(max(x, a), b);
  150. }
  151. /** Return the next power of two.
  152. * @see http://graphics.stanford.edu/~seander/bithacks.html
  153. * @warning Behaviour for 0 is undefined.
  154. * @note isPowerOfTwo(x) == true -> nextPowerOfTwo(x) == x
  155. * @note nextPowerOfTwo(x) = 2 << log2(x-1)
  156. */
  157. inline uint nextPowerOfTwo( uint x )
  158. {
  159. nvDebugCheck( x != 0 );
  160. #if 1 // On modern CPUs this is supposed to be as fast as using the bsr instruction.
  161. x--;
  162. x |= x >> 1;
  163. x |= x >> 2;
  164. x |= x >> 4;
  165. x |= x >> 8;
  166. x |= x >> 16;
  167. return x+1;
  168. #else
  169. uint p = 1;
  170. while( x > p ) {
  171. p += p;
  172. }
  173. return p;
  174. #endif
  175. }
  176. /// Return true if @a n is a power of two.
  177. inline bool isPowerOfTwo( uint n )
  178. {
  179. return (n & (n-1)) == 0;
  180. }
  181. // @@ Move this to utils?
  182. /// Delete all the elements of a container.
  183. template <typename T>
  184. void deleteAll(T & container)
  185. {
  186. for (typename T::PseudoIndex i = container.start(); !container.isDone(i); container.advance(i))
  187. {
  188. delete container[i];
  189. }
  190. }
  191. // @@ Specialize these methods for numeric, pointer, and pod types.
  192. template <typename T>
  193. void construct_range(T * restrict ptr, uint new_size, uint old_size) {
  194. for (uint i = old_size; i < new_size; i++) {
  195. new(ptr+i) T; // placement new
  196. }
  197. }
  198. template <typename T>
  199. void construct_range(T * restrict ptr, uint new_size, uint old_size, const T & elem) {
  200. for (uint i = old_size; i < new_size; i++) {
  201. new(ptr+i) T(elem); // placement new
  202. }
  203. }
  204. template <typename T>
  205. void construct_range(T * restrict ptr, uint new_size, uint old_size, const T * src) {
  206. for (uint i = old_size; i < new_size; i++) {
  207. new(ptr+i) T(src[i]); // placement new
  208. }
  209. }
  210. template <typename T>
  211. void destroy_range(T * restrict ptr, uint new_size, uint old_size) {
  212. for (uint i = new_size; i < old_size; i++) {
  213. (ptr+i)->~T(); // Explicit call to the destructor
  214. }
  215. }
  216. template <typename T>
  217. void fill(T * restrict dst, uint count, const T & value) {
  218. for (uint i = 0; i < count; i++) {
  219. dst[i] = value;
  220. }
  221. }
  222. template <typename T>
  223. void copy_range(T * restrict dst, const T * restrict src, uint count) {
  224. for (uint i = 0; i < count; i++) {
  225. dst[i] = src[i];
  226. }
  227. }
  228. template <typename T>
  229. bool find(const T & element, const T * restrict ptr, uint begin, uint end, uint * index) {
  230. for (uint i = begin; i < end; i++) {
  231. if (ptr[i] == element) {
  232. if (index != NULL) *index = i;
  233. return true;
  234. }
  235. }
  236. return false;
  237. }
  238. } // nv namespace
  239. #endif // NV_CORE_UTILS_H