lzham_math.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // File: lzham_math.h
  2. // See Copyright Notice and license at the end of include/lzham.h
  3. #pragma once
  4. #if defined(LZHAM_USE_MSVC_INTRINSICS) && !defined(__MINGW32__)
  5. #include <intrin.h>
  6. #if defined(_MSC_VER)
  7. #pragma intrinsic(_BitScanReverse)
  8. #endif
  9. #endif
  10. namespace lzham
  11. {
  12. namespace math
  13. {
  14. // Yes I know these should probably be pass by ref, not val:
  15. // http://www.stepanovpapers.com/notes.pdf
  16. // Just don't use them on non-simple (non built-in) types!
  17. template<typename T> inline T minimum(T a, T b) { return (a < b) ? a : b; }
  18. template<typename T> inline T minimum(T a, T b, T c) { return minimum(minimum(a, b), c); }
  19. template<typename T> inline T maximum(T a, T b) { return (a > b) ? a : b; }
  20. template<typename T> inline T maximum(T a, T b, T c) { return maximum(maximum(a, b), c); }
  21. template<typename T> inline T clamp(T value, T low, T high) { return (value < low) ? low : ((value > high) ? high : value); }
  22. inline bool is_power_of_2(uint32 x) { return x && ((x & (x - 1U)) == 0U); }
  23. inline bool is_power_of_2(uint64 x) { return x && ((x & (x - 1U)) == 0U); }
  24. template<typename T> inline T align_up_pointer(T p, uint alignment)
  25. {
  26. LZHAM_ASSERT(is_power_of_2(alignment));
  27. ptr_bits_t q = reinterpret_cast<ptr_bits_t>(p);
  28. q = (q + alignment - 1) & (~((uint_ptr)alignment - 1));
  29. return reinterpret_cast<T>(q);
  30. }
  31. // From "Hackers Delight"
  32. // val remains unchanged if it is already a power of 2.
  33. inline uint32 next_pow2(uint32 val)
  34. {
  35. val--;
  36. val |= val >> 16;
  37. val |= val >> 8;
  38. val |= val >> 4;
  39. val |= val >> 2;
  40. val |= val >> 1;
  41. return val + 1;
  42. }
  43. // val remains unchanged if it is already a power of 2.
  44. inline uint64 next_pow2(uint64 val)
  45. {
  46. val--;
  47. val |= val >> 32;
  48. val |= val >> 16;
  49. val |= val >> 8;
  50. val |= val >> 4;
  51. val |= val >> 2;
  52. val |= val >> 1;
  53. return val + 1;
  54. }
  55. inline uint floor_log2i(uint v)
  56. {
  57. uint l = 0;
  58. while (v > 1U)
  59. {
  60. v >>= 1;
  61. l++;
  62. }
  63. return l;
  64. }
  65. inline uint ceil_log2i(uint v)
  66. {
  67. uint l = floor_log2i(v);
  68. if ((l != cIntBits) && (v > (1U << l)))
  69. l++;
  70. return l;
  71. }
  72. // Returns the total number of bits needed to encode v.
  73. inline uint total_bits(uint v)
  74. {
  75. unsigned long l = 0;
  76. #if defined(__MINGW32__)
  77. if (v)
  78. {
  79. l = 32 -__builtin_clz(v);
  80. }
  81. #elif defined(LZHAM_USE_MSVC_INTRINSICS)
  82. if (_BitScanReverse(&l, v))
  83. {
  84. l++;
  85. }
  86. else
  87. {
  88. l = 0;
  89. }
  90. #else
  91. while (v > 0U)
  92. {
  93. v >>= 1;
  94. l++;
  95. }
  96. #endif
  97. return static_cast<uint>(l);
  98. }
  99. inline uint compute_mask_size(uint x)
  100. {
  101. uint l = 0;
  102. while (x)
  103. {
  104. x &= (x - 1);
  105. l++;
  106. }
  107. return l;
  108. }
  109. inline uint compute_mask_shift(uint x)
  110. {
  111. if (!x)
  112. return 0;
  113. uint l = 0;
  114. while ((x & 1) == 0)
  115. {
  116. x >>= 1;
  117. l++;
  118. }
  119. return l;
  120. }
  121. }
  122. } // namespace lzham