BaseMath.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (C) 2000 Sean Cavanaugh
  2. // This file is licensed under the terms of the Lesser GNU Public License
  3. // (see LPGL.txt, or http://www.gnu.org/copyleft/lesser.txt)
  4. #ifndef BASEMATH_H__
  5. #define BASEMATH_H__
  6. #if _MSC_VER >= 1000
  7. #pragma once
  8. #endif // _MSC_VER >= 1000
  9. #if defined(_WIN32) && !defined(FASTCALL)
  10. #define FASTCALL __fastcall
  11. #endif
  12. //
  13. // MIN/MAX/AVG functions, work best with basic types
  14. //
  15. template < typename T >
  16. inline T FASTCALL MIN(const T a, const T b)
  17. {
  18. return ((a < b) ? a : b);
  19. }
  20. template < typename T >
  21. inline T FASTCALL MIN(const T a, const T b, const T c)
  22. {
  23. return (MIN(MIN(a, b), c));
  24. }
  25. template < typename T >
  26. inline T FASTCALL MAX(const T a, const T b)
  27. {
  28. return ((a > b) ? a : b);
  29. }
  30. template < typename T >
  31. inline T FASTCALL MAX(const T a, const T b, const T c)
  32. {
  33. return (MAX(MAX(a, b), c));
  34. }
  35. template < typename T >
  36. inline T FASTCALL AVG(const T a, const T b)
  37. {
  38. return ((a + b) / 2);
  39. }
  40. //
  41. // MIN/MAX/AVG functions, work best with user-defined types
  42. // (hopefully the compiler will choose the right one in most cases
  43. //
  44. template < typename T >
  45. inline T FASTCALL MIN(const T& a, const T& b)
  46. {
  47. return ((a < b) ? a : b);
  48. }
  49. template < typename T >
  50. inline T FASTCALL MIN(const T& a, const T& b, const T& c)
  51. {
  52. return (MIN(MIN(a, b), c));
  53. }
  54. template < typename T >
  55. inline T FASTCALL MAX(const T& a, const T& b)
  56. {
  57. return ((a > b) ? a : b);
  58. }
  59. template < typename T >
  60. inline T FASTCALL MAX(const T& a, const T& b, const T& c)
  61. {
  62. return (MAX(MAX(a, b), c));
  63. }
  64. template < typename T >
  65. inline T FASTCALL AVG(const T& a, const T& b)
  66. {
  67. return ((a + b) / 2);
  68. }
  69. //
  70. // Generic Array Operations
  71. //
  72. template < typename T >
  73. inline T FASTCALL MIN(const T* array, const int size)
  74. {
  75. assert(size);
  76. T val = array[0];
  77. for (int i = 1; i < size; i++)
  78. {
  79. if (val > array[i])
  80. {
  81. val = array[i];
  82. }
  83. }
  84. return val;
  85. }
  86. template < typename T >
  87. inline T FASTCALL MAX(const T* array, const int size)
  88. {
  89. assert(size);
  90. T val = array[0];
  91. for (int i = 1; i < size; i++)
  92. {
  93. if (val < array[i])
  94. {
  95. val = array[i];
  96. }
  97. }
  98. return val;
  99. }
  100. template < typename T >
  101. inline T FASTCALL AVG(const T* array, const int size)
  102. {
  103. assert(size);
  104. T sum = array[0];
  105. for (int i = 1; i < size; i++)
  106. {
  107. sum += array[i];
  108. }
  109. return sum / num;
  110. }
  111. template < typename T >
  112. inline T FASTCALL SUM(const T* array, const int size)
  113. {
  114. assert(size);
  115. T sum = array[0];
  116. for (int i = 1; i < size; i++)
  117. {
  118. sum += array[i];
  119. }
  120. return sum;
  121. }
  122. // Uses one temp to swap, works best with user-defined types or doubles/long doubles
  123. template < typename T >
  124. inline void FASTCALL SWAP(T& a, T& b)
  125. {
  126. T temp = a;
  127. a = b;
  128. b = temp;
  129. }
  130. // XOR math to swap (no temps), works with integral types very well
  131. template < typename T >
  132. inline void FASTCALL XOR_SWAP(T& a, T& b)
  133. {
  134. a ^= b;
  135. b ^= a;
  136. a ^= b;
  137. }
  138. // Uses two temps to swap, works very well with built-in types on pipelined CPUs
  139. template < typename T >
  140. inline void FASTCALL PIPE_SWAP(T& a, T& b)
  141. {
  142. T tmpA = a;
  143. T tmpB = b;
  144. b = tmpA;
  145. a = tmpB;
  146. }
  147. #endif // BASEMATH_H__