bit_util.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <climits>
  6. #include <cstddef>
  7. #ifdef _MSC_VER
  8. #include <intrin.h>
  9. #endif
  10. #include "common/common_types.h"
  11. namespace Common {
  12. /// Gets the size of a specified type T in bits.
  13. template <typename T>
  14. constexpr std::size_t BitSize() {
  15. return sizeof(T) * CHAR_BIT;
  16. }
  17. #ifdef _MSC_VER
  18. inline u32 CountLeadingZeroes32(u32 value) {
  19. unsigned long leading_zero = 0;
  20. if (_BitScanReverse(&leading_zero, value) != 0) {
  21. return 31 - leading_zero;
  22. }
  23. return 32;
  24. }
  25. inline u64 CountLeadingZeroes64(u64 value) {
  26. unsigned long leading_zero = 0;
  27. if (_BitScanReverse64(&leading_zero, value) != 0) {
  28. return 63 - leading_zero;
  29. }
  30. return 64;
  31. }
  32. #else
  33. inline u32 CountLeadingZeroes32(u32 value) {
  34. if (value == 0) {
  35. return 32;
  36. }
  37. return __builtin_clz(value);
  38. }
  39. inline u64 CountLeadingZeroes64(u64 value) {
  40. if (value == 0) {
  41. return 64;
  42. }
  43. return __builtin_clzll(value);
  44. }
  45. #endif
  46. } // namespace Common