Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11351 from JosJuice/bitset-cpp20
Common: Use C++20 <bit> header in BitSet.h
  • Loading branch information
lioncash committed Dec 20, 2022
2 parents fb8aa97 + 547d956 commit 43366d2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 86 deletions.
82 changes: 4 additions & 78 deletions Source/Core/Common/BitSet.h
Expand Up @@ -2,89 +2,15 @@

#pragma once

#include <bit>
#include <cstddef>
#include <initializer_list>
#include <type_traits>
#include "Common/CommonTypes.h"

#ifdef _WIN32

#include <intrin.h>
#include "Common/CommonTypes.h"

namespace Common
{
template <typename T>
constexpr int CountSetBits(T v)
{
// from https://graphics.stanford.edu/~seander/bithacks.html
// GCC has this built in, but MSVC's intrinsic will only emit the actual
// POPCNT instruction, which we're not depending on
v = v - ((v >> 1) & (T) ~(T)0 / 3);
v = (v & (T) ~(T)0 / 15 * 3) + ((v >> 2) & (T) ~(T)0 / 15 * 3);
v = (v + (v >> 4)) & (T) ~(T)0 / 255 * 15;
return (T)(v * ((T) ~(T)0 / 255)) >> (sizeof(T) - 1) * 8;
}
inline int LeastSignificantSetBit(u8 val)
{
unsigned long index;
_BitScanForward(&index, val);
return (int)index;
}
inline int LeastSignificantSetBit(u16 val)
{
unsigned long index;
_BitScanForward(&index, val);
return (int)index;
}
inline int LeastSignificantSetBit(u32 val)
{
unsigned long index;
_BitScanForward(&index, val);
return (int)index;
}
inline int LeastSignificantSetBit(u64 val)
{
unsigned long index;
_BitScanForward64(&index, val);
return (int)index;
}
#else
namespace Common
{
constexpr int CountSetBits(u8 val)
{
return __builtin_popcount(val);
}
constexpr int CountSetBits(u16 val)
{
return __builtin_popcount(val);
}
constexpr int CountSetBits(u32 val)
{
return __builtin_popcount(val);
}
constexpr int CountSetBits(u64 val)
{
return __builtin_popcountll(val);
}
inline int LeastSignificantSetBit(u8 val)
{
return __builtin_ctz(val);
}
inline int LeastSignificantSetBit(u16 val)
{
return __builtin_ctz(val);
}
inline int LeastSignificantSetBit(u32 val)
{
return __builtin_ctz(val);
}
inline int LeastSignificantSetBit(u64 val)
{
return __builtin_ctzll(val);
}
#endif

// Similar to std::bitset, this is a class which encapsulates a bitset, i.e.
// using the set bits of an integer to represent a set of integers. Like that
// class, it acts like an array of bools:
Expand Down Expand Up @@ -146,7 +72,7 @@ class BitSet
}
else
{
int bit = LeastSignificantSetBit(m_val);
int bit = std::countr_zero(m_val);
m_val &= ~(1 << bit);
m_bit = bit;
}
Expand Down Expand Up @@ -203,7 +129,7 @@ class BitSet
// Dolphin's official builds do not currently assume POPCNT support on x86,
// so slower explicit bit twiddling is generated. Still should generally
// be faster than a loop.
constexpr unsigned int Count() const { return CountSetBits(m_val); }
constexpr unsigned int Count() const { return std::popcount(m_val); }
constexpr Iterator begin() const { return ++Iterator(m_val, 0); }
constexpr Iterator end() const { return Iterator(m_val, -1); }
IntTy m_val;
Expand Down
12 changes: 6 additions & 6 deletions Source/Core/VideoBackends/D3D/D3DState.cpp
Expand Up @@ -5,9 +5,9 @@

#include <algorithm>
#include <array>
#include <bit>

#include "Common/Assert.h"
#include "Common/BitSet.h"
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"
Expand Down Expand Up @@ -163,8 +163,8 @@ void StateManager::Apply()

void StateManager::ApplyTextures()
{
const int textureMaskShift = Common::LeastSignificantSetBit((u32)DirtyFlag_Texture0);
const int samplerMaskShift = Common::LeastSignificantSetBit((u32)DirtyFlag_Sampler0);
const int textureMaskShift = std::countr_zero((u32)DirtyFlag_Texture0);
const int samplerMaskShift = std::countr_zero((u32)DirtyFlag_Sampler0);

u32 dirtyTextures =
(m_dirtyFlags &
Expand All @@ -178,7 +178,7 @@ void StateManager::ApplyTextures()
samplerMaskShift;
while (dirtyTextures)
{
const int index = Common::LeastSignificantSetBit(dirtyTextures);
const int index = std::countr_zero(dirtyTextures);
if (m_current.textures[index] != m_pending.textures[index])
{
D3D::context->PSSetShaderResources(index, 1, &m_pending.textures[index]);
Expand All @@ -190,7 +190,7 @@ void StateManager::ApplyTextures()

while (dirtySamplers)
{
const int index = Common::LeastSignificantSetBit(dirtySamplers);
const int index = std::countr_zero(dirtySamplers);
if (m_current.samplers[index] != m_pending.samplers[index])
{
D3D::context->PSSetSamplers(index, 1, &m_pending.samplers[index]);
Expand Down Expand Up @@ -221,7 +221,7 @@ void StateManager::SetTextureByMask(u32 textureSlotMask, ID3D11ShaderResourceVie
{
while (textureSlotMask)
{
const int index = Common::LeastSignificantSetBit(textureSlotMask);
const int index = std::countr_zero(textureSlotMask);
SetTexture(index, srv);
textureSlotMask &= ~(1 << index);
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/VideoCommon/VertexLoaderBase.cpp
Expand Up @@ -4,6 +4,7 @@
#include "VideoCommon/VertexLoaderBase.h"

#include <array>
#include <bit>
#include <cstring>
#include <memory>
#include <string>
Expand All @@ -12,7 +13,6 @@
#include <fmt/format.h>

#include "Common/Assert.h"
#include "Common/BitSet.h"
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"
Expand Down Expand Up @@ -99,7 +99,7 @@ u32 VertexLoaderBase::GetVertexSize(const TVtxDesc& vtx_desc, const VAT& vtx_att
u32 size = 0;

// Each enabled TexMatIdx adds one byte, as does PosMatIdx
size += Common::CountSetBits(vtx_desc.low.Hex & 0x1FF);
size += std::popcount(vtx_desc.low.Hex & 0x1FF);

const u32 pos_size = VertexLoader_Position::GetSize(vtx_desc.low.Position, vtx_attr.g0.PosFormat,
vtx_attr.g0.PosElements);
Expand Down

0 comments on commit 43366d2

Please sign in to comment.