From a9431642040926f8664f351437c0abd609f15a3d Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Wed, 21 Feb 2024 10:41:04 +0300 Subject: [PATCH] UI: BitUtil: Use Qt functions --- src/tests/UtilTest/UtilTest.pro | 1 + src/tests/UtilTest/tst_bitutil.h | 31 ++++++++++++++++++++++++++ src/tests/UtilTest/tst_main.cpp | 1 + src/ui/util/bitutil.cpp | 37 ++++++-------------------------- src/ui/util/bitutil.h | 6 +++--- 5 files changed, 43 insertions(+), 33 deletions(-) create mode 100644 src/tests/UtilTest/tst_bitutil.h diff --git a/src/tests/UtilTest/UtilTest.pro b/src/tests/UtilTest/UtilTest.pro index 622d64eb..cae5cc6d 100644 --- a/src/tests/UtilTest/UtilTest.pro +++ b/src/tests/UtilTest/UtilTest.pro @@ -1,6 +1,7 @@ include(../Common/Common.pri) HEADERS += \ + tst_bitutil.h \ tst_confutil.h \ tst_fileutil.h \ tst_ioccontainer.h \ diff --git a/src/tests/UtilTest/tst_bitutil.h b/src/tests/UtilTest/tst_bitutil.h new file mode 100644 index 00000000..8011a7ba --- /dev/null +++ b/src/tests/UtilTest/tst_bitutil.h @@ -0,0 +1,31 @@ +#pragma once + +#include + +#include + +#include + +class BitUtilTest : public Test +{ + // Test interface +protected: + void SetUp(); + void TearDown(); +}; + +void BitUtilTest::SetUp() { } + +void BitUtilTest::TearDown() { } + +TEST_F(BitUtilTest, bitCount) +{ + ASSERT_EQ(BitUtil::bitCount(0x03), 2); + ASSERT_EQ(BitUtil::bitCount(0x8F), 5); +} + +TEST_F(BitUtilTest, firstZeroBit) +{ + ASSERT_EQ(BitUtil::firstZeroBit(0x03), 2); + ASSERT_EQ(BitUtil::firstZeroBit(0x8F), 4); +} diff --git a/src/tests/UtilTest/tst_main.cpp b/src/tests/UtilTest/tst_main.cpp index 5cd7fd0b..b6128b89 100644 --- a/src/tests/UtilTest/tst_main.cpp +++ b/src/tests/UtilTest/tst_main.cpp @@ -1,3 +1,4 @@ +#include "tst_bitutil.h" #include "tst_confutil.h" #include "tst_fileutil.h" #include "tst_ioccontainer.h" diff --git a/src/ui/util/bitutil.cpp b/src/ui/util/bitutil.cpp index f9501686..73b15c08 100644 --- a/src/ui/util/bitutil.cpp +++ b/src/ui/util/bitutil.cpp @@ -1,41 +1,18 @@ #include "bitutil.h" -#define WIN32_LEAN_AND_MEAN -#include +#include -int BitUtil::firstZeroBit(quint32 u) +int BitUtil::bitCount(quint32 v) { - const qint32 i = ~u; - return bitCount((i & (-i)) - 1); + return qPopulationCount(v); } -int BitUtil::bitCount(quint32 u) +int BitUtil::firstZeroBit(quint32 v) { -#if 0 // TODO: COMPAT: Enable after Win 11 24H2 -// #if (defined(_M_IX86) || defined(_M_X64)) && QT_VERSION >= QT_VERSION_CHECK(6, 6, 0) - return __popcnt(u); -#elif defined(_M_ARM64) - __n64 num; - num.n64_u64[0] = u; - - num = neon_cnt(num); - return num.n64_u32[0]; -#else - // From http://tekpool.wordpress.com/category/bit-count/ - const quint32 uCount = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111); - return ((uCount + (uCount >> 3)) & 030707070707) % 63; - - // The following code is optimized to __popcnt by MSVC 17.9 - /* - u = u - ((u >> 1) & 0x55555555); - u = (u & 0x33333333) + ((u >> 2) & 0x33333333); - return ((u + (u >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; - */ -#endif + return bitScanForward(~v); } -int BitUtil::bitScanForward(quint32 mask) +int BitUtil::bitScanForward(quint32 v) { - unsigned long index; - return _BitScanForward(&index, mask) ? index : -1; + return qCountTrailingZeroBits(v); } diff --git a/src/ui/util/bitutil.h b/src/ui/util/bitutil.h index d20ab046..cc66b617 100644 --- a/src/ui/util/bitutil.h +++ b/src/ui/util/bitutil.h @@ -6,11 +6,11 @@ class BitUtil { public: - static int firstZeroBit(quint32 u); + static int bitCount(quint32 v); - static int bitCount(quint32 u); + static int firstZeroBit(quint32 v); - static int bitScanForward(quint32 mask); + static int bitScanForward(quint32 v); }; #endif // BITUTIL_H