Skip to content

Commit 1f1418a

Browse files
committed
alternative BitScanForward64 implementation
Change-Id: I1d2f388da8414c6cf9401141dfa8e924f6677fd8
1 parent 609a6e8 commit 1f1418a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/cpucounters.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
#ifdef _MSC_VER
4343
#include <intrin.h>
44-
#pragma intrinsic(_BitScanForward64)
4544
#include <windows.h>
4645
#include <comdef.h>
4746
#include <tchar.h>
@@ -7426,9 +7425,19 @@ int32 PCM::mapNUMANodeToSocket(uint32 numa_node_id) const
74267425

74277426
if (mask != 0)
74287427
{
7428+
auto BitScanForward64 = [](unsigned long* Index, uint64_t Mask)
7429+
{
7430+
if (Mask == 0) return 0;
7431+
7432+
// Magic numbers for LSB (chess engine style)
7433+
static const uint64_t magic = 0x03f79d71b4cb0a89ULL;
7434+
uint64_t isolated = Mask & -Mask; // Rightmost set bit
7435+
*Index = (unsigned long)(((isolated * magic) >> 58));
7436+
};
7437+
74297438
// Find first set bit (first processor in this NUMA node within this group)
74307439
DWORD bitPosition = 0;
7431-
_BitScanForward64(&bitPosition, mask);
7440+
BitScanForward64(&bitPosition, mask);
74327441

74337442
// On Windows, we need to find the logical processor ID that corresponds to
74347443
// this bit position in this group. We iterate through topology to find a match.

0 commit comments

Comments
 (0)