bit_utils.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <catch2/catch.hpp>
  5. #include <math.h>
  6. #include "common/bit_util.h"
  7. namespace Common {
  8. inline u32 CTZ32(u32 value) {
  9. u32 count = 0;
  10. while (((value >> count) & 0xf) == 0 && count < 32)
  11. count += 4;
  12. while (((value >> count) & 1) == 0 && count < 32)
  13. count++;
  14. return count;
  15. }
  16. inline u64 CTZ64(u64 value) {
  17. u64 count = 0;
  18. while (((value >> count) & 0xf) == 0 && count < 64)
  19. count += 4;
  20. while (((value >> count) & 1) == 0 && count < 64)
  21. count++;
  22. return count;
  23. }
  24. TEST_CASE("BitUtils", "[common]") {
  25. REQUIRE(Common::CountTrailingZeroes32(0) == CTZ32(0));
  26. REQUIRE(Common::CountTrailingZeroes64(0) == CTZ64(0));
  27. REQUIRE(Common::CountTrailingZeroes32(9) == CTZ32(9));
  28. REQUIRE(Common::CountTrailingZeroes32(8) == CTZ32(8));
  29. REQUIRE(Common::CountTrailingZeroes32(0x801000) == CTZ32(0x801000));
  30. REQUIRE(Common::CountTrailingZeroes64(9) == CTZ64(9));
  31. REQUIRE(Common::CountTrailingZeroes64(8) == CTZ64(8));
  32. REQUIRE(Common::CountTrailingZeroes64(0x801000) == CTZ64(0x801000));
  33. REQUIRE(Common::CountTrailingZeroes64(0x801000000000UL) == CTZ64(0x801000000000UL));
  34. }
  35. } // namespace Common