lzham_utils.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // File: lzham_utils.h
  2. // See Copyright Notice and license at the end of include/lzham.h
  3. #pragma once
  4. #define LZHAM_GET_ALIGNMENT(v) ((!sizeof(v)) ? 1 : (__alignof(v) ? __alignof(v) : sizeof(uint32)))
  5. #define LZHAM_MIN(a, b) (((a) < (b)) ? (a) : (b))
  6. #define LZHAM_MAX(a, b) (((a) < (b)) ? (b) : (a))
  7. template<class T, size_t N> T decay_array_to_subtype(T (&a)[N]);
  8. #define LZHAM_ARRAY_SIZE(X) (sizeof(X) / sizeof(decay_array_to_subtype(X)))
  9. namespace lzham
  10. {
  11. namespace utils
  12. {
  13. template<typename T> inline void swap(T& l, T& r)
  14. {
  15. T temp(l);
  16. l = r;
  17. r = temp;
  18. }
  19. template<typename T> inline void zero_object(T& obj)
  20. {
  21. memset(&obj, 0, sizeof(obj));
  22. }
  23. static inline uint32 swap32(uint32 x) { return ((x << 24U) | ((x << 8U) & 0x00FF0000U) | ((x >> 8U) & 0x0000FF00U) | (x >> 24U)); }
  24. inline uint count_leading_zeros16(uint v)
  25. {
  26. LZHAM_ASSERT(v < 0x10000);
  27. uint temp;
  28. uint n = 16;
  29. temp = v >> 8;
  30. if (temp) { n -= 8; v = temp; }
  31. temp = v >> 4;
  32. if (temp) { n -= 4; v = temp; }
  33. temp = v >> 2;
  34. if (temp) { n -= 2; v = temp; }
  35. temp = v >> 1;
  36. if (temp) { n -= 1; v = temp; }
  37. if (v & 1) n--;
  38. return n;
  39. }
  40. } // namespace utils
  41. } // namespace lzham