Skip to content

Commit 0c7ad4e

Browse files
Optimized VmaCountBitsSets to use std::popcount when C++20 is enabled
Closes #251
1 parent 2d2a9e3 commit 0c7ad4e

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

include/vk_mem_alloc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,6 +2574,9 @@ VMA_CALL_PRE void VMA_CALL_POST vmaFreeStatsString(
25742574
#ifdef _MSC_VER
25752575
#include <intrin.h> // For functions like __popcnt, _BitScanForward etc.
25762576
#endif
2577+
#if __cplusplus >= 202002L || _MSVC_LANG >= 202002L // C++20
2578+
#include <bit> // For std::popcount
2579+
#endif
25772580

25782581
/*******************************************************************************
25792582
CONFIGURATION SECTION
@@ -3176,12 +3179,16 @@ But you need to check in runtime whether user's CPU supports these, as some old
31763179
*/
31773180
static inline uint32_t VmaCountBitsSet(uint32_t v)
31783181
{
3182+
#if __cplusplus >= 202002L || _MSVC_LANG >= 202002L // C++20
3183+
return std::popcount(v);
3184+
#else
31793185
uint32_t c = v - ((v >> 1) & 0x55555555);
31803186
c = ((c >> 2) & 0x33333333) + (c & 0x33333333);
31813187
c = ((c >> 4) + c) & 0x0F0F0F0F;
31823188
c = ((c >> 8) + c) & 0x00FF00FF;
31833189
c = ((c >> 16) + c) & 0x0000FFFF;
31843190
return c;
3191+
#endif
31853192
}
31863193

31873194
static inline uint8_t VmaBitScanLSB(uint64_t mask)

0 commit comments

Comments
 (0)