Skip to content

Commit 486952d

Browse files
Added usage of std::countr_zero, std::countl_zero
When C++20 is available. Copied from VMA.
1 parent e2699db commit 486952d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/D3D12MemAlloc.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@
4949
////////////////////////////////////////////////////////////////////////////////
5050
#ifndef _D3D12MA_CONFIGURATION
5151

52+
#if !defined(D3D12MA_CPP20)
53+
#if __cplusplus >= 202002L || _MSVC_LANG >= 202002L // C++20
54+
#define D3D12MA_CPP20 1
55+
#else
56+
#define D3D12MA_CPP20 0
57+
#endif
58+
#endif
59+
5260
#ifdef _WIN32
5361
#if !defined(WINVER) || WINVER < 0x0600
5462
#error Required at least WinAPI version supporting: client = Windows Vista, server = Windows Server 2008.
@@ -71,6 +79,10 @@
7179
#define D3D12MA_ASSERT(cond) assert(cond)
7280
#endif
7381

82+
#if D3D12MA_CPP20
83+
#include <bit>
84+
#endif
85+
7486
// Assert that will be called very often, like inside data structures e.g. operator[].
7587
// Making it non-empty can make program slow.
7688
#ifndef D3D12MA_HEAVY_ASSERT
@@ -303,6 +315,10 @@ static UINT8 BitScanLSB(UINT64 mask)
303315
if (_BitScanForward64(&pos, mask))
304316
return static_cast<UINT8>(pos);
305317
return UINT8_MAX;
318+
#elif D3D12MA_CPP20
319+
if (mask != 0)
320+
return static_cast<uint8_t>(std::countr_zero(mask));
321+
return UINT8_MAX;
306322
#elif defined __GNUC__ || defined __clang__
307323
return static_cast<UINT8>(__builtin_ffsll(mask)) - 1U;
308324
#else
@@ -325,6 +341,10 @@ static UINT8 BitScanLSB(UINT32 mask)
325341
if (_BitScanForward(&pos, mask))
326342
return static_cast<UINT8>(pos);
327343
return UINT8_MAX;
344+
#elif D3D12MA_CPP20
345+
if (mask != 0)
346+
return static_cast<uint8_t>(std::countr_zero(mask));
347+
return UINT8_MAX;
328348
#elif defined __GNUC__ || defined __clang__
329349
return static_cast<UINT8>(__builtin_ffs(mask)) - 1U;
330350
#else
@@ -347,6 +367,9 @@ static UINT8 BitScanMSB(UINT64 mask)
347367
unsigned long pos;
348368
if (_BitScanReverse64(&pos, mask))
349369
return static_cast<UINT8>(pos);
370+
#elif D3D12MA_CPP20
371+
if (mask != 0)
372+
return 63 - static_cast<uint8_t>(std::countl_zero(mask));
350373
#elif defined __GNUC__ || defined __clang__
351374
if (mask)
352375
return 63 - static_cast<UINT8>(__builtin_clzll(mask));
@@ -369,6 +392,9 @@ static UINT8 BitScanMSB(UINT32 mask)
369392
unsigned long pos;
370393
if (_BitScanReverse(&pos, mask))
371394
return static_cast<UINT8>(pos);
395+
#elif D3D12MA_CPP20
396+
if (mask != 0)
397+
return 31 - static_cast<uint8_t>(std::countl_zero(mask));
372398
#elif defined __GNUC__ || defined __clang__
373399
if (mask)
374400
return 31 - static_cast<UINT8>(__builtin_clz(mask));

0 commit comments

Comments
 (0)