mirror of
https://github.com/tnodir/fort
synced 2024-11-15 04:55:48 +00:00
UI: BitUtil: Use Qt functions
This commit is contained in:
parent
b515a1329f
commit
a943164204
@ -1,6 +1,7 @@
|
||||
include(../Common/Common.pri)
|
||||
|
||||
HEADERS += \
|
||||
tst_bitutil.h \
|
||||
tst_confutil.h \
|
||||
tst_fileutil.h \
|
||||
tst_ioccontainer.h \
|
||||
|
31
src/tests/UtilTest/tst_bitutil.h
Normal file
31
src/tests/UtilTest/tst_bitutil.h
Normal 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);
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
#include "tst_bitutil.h"
|
||||
#include "tst_confutil.h"
|
||||
#include "tst_fileutil.h"
|
||||
#include "tst_ioccontainer.h"
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user