Optimized VmaCountBitsSets to use std::popcount when C++20 is enabled

Closes #251
This commit is contained in:
Adam Sawicki 2022-03-29 18:07:09 +02:00
parent 2d2a9e3df3
commit 0c7ad4e859

View File

@ -2574,6 +2574,9 @@ VMA_CALL_PRE void VMA_CALL_POST vmaFreeStatsString(
#ifdef _MSC_VER #ifdef _MSC_VER
#include <intrin.h> // For functions like __popcnt, _BitScanForward etc. #include <intrin.h> // For functions like __popcnt, _BitScanForward etc.
#endif #endif
#if __cplusplus >= 202002L || _MSVC_LANG >= 202002L // C++20
#include <bit> // For std::popcount
#endif
/******************************************************************************* /*******************************************************************************
CONFIGURATION SECTION CONFIGURATION SECTION
@ -3176,12 +3179,16 @@ But you need to check in runtime whether user's CPU supports these, as some old
*/ */
static inline uint32_t VmaCountBitsSet(uint32_t v) static inline uint32_t VmaCountBitsSet(uint32_t v)
{ {
#if __cplusplus >= 202002L || _MSVC_LANG >= 202002L // C++20
return std::popcount(v);
#else
uint32_t c = v - ((v >> 1) & 0x55555555); uint32_t c = v - ((v >> 1) & 0x55555555);
c = ((c >> 2) & 0x33333333) + (c & 0x33333333); c = ((c >> 2) & 0x33333333) + (c & 0x33333333);
c = ((c >> 4) + c) & 0x0F0F0F0F; c = ((c >> 4) + c) & 0x0F0F0F0F;
c = ((c >> 8) + c) & 0x00FF00FF; c = ((c >> 8) + c) & 0x00FF00FF;
c = ((c >> 16) + c) & 0x0000FFFF; c = ((c >> 16) + c) & 0x0000FFFF;
return c; return c;
#endif
} }
static inline uint8_t VmaBitScanLSB(uint64_t mask) static inline uint8_t VmaBitScanLSB(uint64_t mask)