UI: BitUtil: Use Qt functions

This commit is contained in:
Nodir Temirkhodjaev 2024-02-21 10:41:04 +03:00
parent b515a1329f
commit a943164204
5 changed files with 43 additions and 33 deletions

View File

@ -1,6 +1,7 @@
include(../Common/Common.pri)
HEADERS += \
tst_bitutil.h \
tst_confutil.h \
tst_fileutil.h \
tst_ioccontainer.h \

View File

@ -0,0 +1,31 @@
#pragma once
#include <QDebug>
#include <googletest.h>
#include <util/bitutil.h>
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);
}

View File

@ -1,3 +1,4 @@
#include "tst_bitutil.h"
#include "tst_confutil.h"
#include "tst_fileutil.h"
#include "tst_ioccontainer.h"

View File

@ -1,41 +1,18 @@
#include "bitutil.h"
#define WIN32_LEAN_AND_MEAN
#include <qt_windows.h>
#include <QtAlgorithms>
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);
}

View File

@ -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