From 0c7ad4e85944b38ef6e4d2becfd58bf58385812e Mon Sep 17 00:00:00 2001 From: Adam Sawicki Date: Tue, 29 Mar 2022 18:07:09 +0200 Subject: [PATCH] Optimized VmaCountBitsSets to use std::popcount when C++20 is enabled Closes #251 --- include/vk_mem_alloc.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/vk_mem_alloc.h b/include/vk_mem_alloc.h index 256b654..98aa40f 100644 --- a/include/vk_mem_alloc.h +++ b/include/vk_mem_alloc.h @@ -2574,6 +2574,9 @@ VMA_CALL_PRE void VMA_CALL_POST vmaFreeStatsString( #ifdef _MSC_VER #include // For functions like __popcnt, _BitScanForward etc. #endif +#if __cplusplus >= 202002L || _MSVC_LANG >= 202002L // C++20 + #include // For std::popcount +#endif /******************************************************************************* 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) { +#if __cplusplus >= 202002L || _MSVC_LANG >= 202002L // C++20 + return std::popcount(v); +#else uint32_t c = v - ((v >> 1) & 0x55555555); c = ((c >> 2) & 0x33333333) + (c & 0x33333333); c = ((c >> 4) + c) & 0x0F0F0F0F; c = ((c >> 8) + c) & 0x00FF00FF; c = ((c >> 16) + c) & 0x0000FFFF; return c; +#endif } static inline uint8_t VmaBitScanLSB(uint64_t mask)