diff options
-rw-r--r-- | al/auxeffectslot.cpp | 6 | ||||
-rw-r--r-- | al/buffer.cpp | 7 | ||||
-rw-r--r-- | al/effect.cpp | 7 | ||||
-rw-r--r-- | al/filter.cpp | 7 | ||||
-rw-r--r-- | al/source.cpp | 7 | ||||
-rw-r--r-- | alc/alc.cpp | 19 | ||||
-rw-r--r-- | alc/backends/base.cpp | 2 | ||||
-rw-r--r-- | alc/backends/wasapi.cpp | 2 | ||||
-rw-r--r-- | alc/converter.cpp | 2 | ||||
-rw-r--r-- | common/alnumeric.h | 95 |
10 files changed, 72 insertions, 82 deletions
diff --git a/al/auxeffectslot.cpp b/al/auxeffectslot.cpp index 00dce6c4..74005aaa 100644 --- a/al/auxeffectslot.cpp +++ b/al/auxeffectslot.cpp @@ -177,7 +177,7 @@ bool EnsureEffectSlots(ALCcontext *context, size_t needed) size_t count{std::accumulate(context->mEffectSlotList.cbegin(), context->mEffectSlotList.cend(), size_t{0}, [](size_t cur, const EffectSlotSubList &sublist) noexcept -> size_t - { return cur + static_cast<ALuint>(POPCNT64(sublist.FreeMask)); } + { return cur + static_cast<ALuint>(PopCount(sublist.FreeMask)); } )}; while(needed > count) @@ -207,7 +207,7 @@ ALeffectslot *AllocEffectSlot(ALCcontext *context) { return entry.FreeMask != 0; } ); auto lidx = static_cast<ALuint>(std::distance(context->mEffectSlotList.begin(), sublist)); - auto slidx = static_cast<ALuint>(CTZ64(sublist->FreeMask)); + auto slidx = static_cast<ALuint>(CountTrailingZeros(sublist->FreeMask)); ALeffectslot *slot{::new (sublist->EffectSlots + slidx) ALeffectslot{}}; if(ALenum err{slot->init()}) @@ -942,7 +942,7 @@ EffectSlotSubList::~EffectSlotSubList() uint64_t usemask{~FreeMask}; while(usemask) { - ALsizei idx{CTZ64(usemask)}; + const ALsizei idx{CountTrailingZeros(usemask)}; al::destroy_at(EffectSlots+idx); usemask &= ~(1_u64 << idx); } diff --git a/al/buffer.cpp b/al/buffer.cpp index f43c756f..524fb81c 100644 --- a/al/buffer.cpp +++ b/al/buffer.cpp @@ -286,8 +286,7 @@ bool EnsureBuffers(ALCdevice *device, size_t needed) { size_t count{std::accumulate(device->BufferList.cbegin(), device->BufferList.cend(), size_t{0}, [](size_t cur, const BufferSubList &sublist) noexcept -> size_t - { return cur + static_cast<ALuint>(POPCNT64(sublist.FreeMask)); } - )}; + { return cur + static_cast<ALuint>(PopCount(sublist.FreeMask)); })}; while(needed > count) { @@ -316,7 +315,7 @@ ALbuffer *AllocBuffer(ALCdevice *device) ); auto lidx = static_cast<ALuint>(std::distance(device->BufferList.begin(), sublist)); - auto slidx = static_cast<ALuint>(CTZ64(sublist->FreeMask)); + auto slidx = static_cast<ALuint>(CountTrailingZeros(sublist->FreeMask)); ALbuffer *buffer{::new (sublist->Buffers + slidx) ALbuffer{}}; @@ -1582,7 +1581,7 @@ BufferSubList::~BufferSubList() uint64_t usemask{~FreeMask}; while(usemask) { - ALsizei idx{CTZ64(usemask)}; + const ALsizei idx{CountTrailingZeros(usemask)}; al::destroy_at(Buffers+idx); usemask &= ~(1_u64 << idx); } diff --git a/al/effect.cpp b/al/effect.cpp index 350d0e69..a90adf3c 100644 --- a/al/effect.cpp +++ b/al/effect.cpp @@ -145,8 +145,7 @@ bool EnsureEffects(ALCdevice *device, size_t needed) { size_t count{std::accumulate(device->EffectList.cbegin(), device->EffectList.cend(), size_t{0}, [](size_t cur, const EffectSubList &sublist) noexcept -> size_t - { return cur + static_cast<ALuint>(POPCNT64(sublist.FreeMask)); } - )}; + { return cur + static_cast<ALuint>(PopCount(sublist.FreeMask)); })}; while(needed > count) { @@ -174,7 +173,7 @@ ALeffect *AllocEffect(ALCdevice *device) { return entry.FreeMask != 0; } ); auto lidx = static_cast<ALuint>(std::distance(device->EffectList.begin(), sublist)); - auto slidx = static_cast<ALuint>(CTZ64(sublist->FreeMask)); + auto slidx = static_cast<ALuint>(CountTrailingZeros(sublist->FreeMask)); ALeffect *effect{::new (sublist->Effects + slidx) ALeffect{}}; InitEffectParams(effect, AL_EFFECT_NULL); @@ -534,7 +533,7 @@ EffectSubList::~EffectSubList() uint64_t usemask{~FreeMask}; while(usemask) { - ALsizei idx = CTZ64(usemask); + const ALsizei idx{CountTrailingZeros(usemask)}; al::destroy_at(Effects+idx); usemask &= ~(1_u64 << idx); } diff --git a/al/filter.cpp b/al/filter.cpp index 19d972e9..447bea7a 100644 --- a/al/filter.cpp +++ b/al/filter.cpp @@ -321,8 +321,7 @@ bool EnsureFilters(ALCdevice *device, size_t needed) { size_t count{std::accumulate(device->FilterList.cbegin(), device->FilterList.cend(), size_t{0}, [](size_t cur, const FilterSubList &sublist) noexcept -> size_t - { return cur + static_cast<ALuint>(POPCNT64(sublist.FreeMask)); } - )}; + { return cur + static_cast<ALuint>(PopCount(sublist.FreeMask)); })}; while(needed > count) { @@ -351,7 +350,7 @@ ALfilter *AllocFilter(ALCdevice *device) { return entry.FreeMask != 0; } ); auto lidx = static_cast<ALuint>(std::distance(device->FilterList.begin(), sublist)); - auto slidx = static_cast<ALuint>(CTZ64(sublist->FreeMask)); + auto slidx = static_cast<ALuint>(CountTrailingZeros(sublist->FreeMask)); ALfilter *filter{::new(sublist->Filters + slidx) ALfilter{}}; InitFilterParams(filter, AL_FILTER_NULL); @@ -702,7 +701,7 @@ FilterSubList::~FilterSubList() uint64_t usemask{~FreeMask}; while(usemask) { - ALsizei idx = CTZ64(usemask); + const ALsizei idx{CountTrailingZeros(usemask)}; al::destroy_at(Filters+idx); usemask &= ~(1_u64 << idx); } diff --git a/al/source.cpp b/al/source.cpp index 9771fec7..c16b77e6 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -683,8 +683,7 @@ bool EnsureSources(ALCcontext *context, size_t needed) size_t count{std::accumulate(context->mSourceList.cbegin(), context->mSourceList.cend(), size_t{0}, [](size_t cur, const SourceSubList &sublist) noexcept -> size_t - { return cur + static_cast<ALuint>(POPCNT64(sublist.FreeMask)); } - )}; + { return cur + static_cast<ALuint>(PopCount(sublist.FreeMask)); })}; while(needed > count) { @@ -712,7 +711,7 @@ ALsource *AllocSource(ALCcontext *context) { return entry.FreeMask != 0; } ); auto lidx = static_cast<ALuint>(std::distance(context->mSourceList.begin(), sublist)); - auto slidx = static_cast<ALuint>(CTZ64(sublist->FreeMask)); + auto slidx = static_cast<ALuint>(CountTrailingZeros(sublist->FreeMask)); ALsource *source{::new(sublist->Sources + slidx) ALsource{}}; @@ -3410,7 +3409,7 @@ SourceSubList::~SourceSubList() uint64_t usemask{~FreeMask}; while(usemask) { - ALsizei idx{CTZ64(usemask)}; + const ALsizei idx{CountTrailingZeros(usemask)}; al::destroy_at(Sources+idx); usemask &= ~(1_u64 << idx); } diff --git a/alc/alc.cpp b/alc/alc.cpp index 2b43ebf5..1170800d 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -2117,7 +2117,7 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) uint64_t usemask{~sublist.FreeMask}; while(usemask) { - ALsizei idx{CTZ64(usemask)}; + const ALsizei idx{CountTrailingZeros(usemask)}; ALeffectslot *slot{sublist.EffectSlots + idx}; usemask &= ~(1_u64 << idx); @@ -2140,7 +2140,7 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) uint64_t usemask{~sublist.FreeMask}; while(usemask) { - ALsizei idx{CTZ64(usemask)}; + const ALsizei idx{CountTrailingZeros(usemask)}; ALsource *source{sublist.Sources + idx}; usemask &= ~(1_u64 << idx); @@ -2282,22 +2282,19 @@ ALCdevice::~ALCdevice() size_t count{std::accumulate(BufferList.cbegin(), BufferList.cend(), size_t{0u}, [](size_t cur, const BufferSubList &sublist) noexcept -> size_t - { return cur + static_cast<ALuint>(POPCNT64(~sublist.FreeMask)); } - )}; + { return cur + static_cast<ALuint>(PopCount(~sublist.FreeMask)); })}; if(count > 0) WARN("%zu Buffer%s not deleted\n", count, (count==1)?"":"s"); count = std::accumulate(EffectList.cbegin(), EffectList.cend(), size_t{0u}, [](size_t cur, const EffectSubList &sublist) noexcept -> size_t - { return cur + static_cast<ALuint>(POPCNT64(~sublist.FreeMask)); } - ); + { return cur + static_cast<ALuint>(PopCount(~sublist.FreeMask)); }); if(count > 0) WARN("%zu Effect%s not deleted\n", count, (count==1)?"":"s"); count = std::accumulate(FilterList.cbegin(), FilterList.cend(), size_t{0u}, [](size_t cur, const FilterSubList &sublist) noexcept -> size_t - { return cur + static_cast<ALuint>(POPCNT64(~sublist.FreeMask)); } - ); + { return cur + static_cast<ALuint>(PopCount(~sublist.FreeMask)); }); if(count > 0) WARN("%zu Filter%s not deleted\n", count, (count==1)?"":"s"); @@ -2350,8 +2347,7 @@ ALCcontext::~ALCcontext() count = std::accumulate(mSourceList.cbegin(), mSourceList.cend(), size_t{0u}, [](size_t cur, const SourceSubList &sublist) noexcept -> size_t - { return cur + static_cast<ALuint>(POPCNT64(~sublist.FreeMask)); } - ); + { return cur + static_cast<ALuint>(PopCount(~sublist.FreeMask)); }); if(count > 0) WARN("%zu Source%s not deleted\n", count, (count==1)?"":"s"); mSourceList.clear(); @@ -2377,8 +2373,7 @@ ALCcontext::~ALCcontext() count = std::accumulate(mEffectSlotList.cbegin(), mEffectSlotList.cend(), size_t{0u}, [](size_t cur, const EffectSlotSubList &sublist) noexcept -> size_t - { return cur + static_cast<ALuint>(POPCNT64(~sublist.FreeMask)); } - ); + { return cur + static_cast<ALuint>(PopCount(~sublist.FreeMask)); }); if(count > 0) WARN("%zu AuxiliaryEffectSlot%s not deleted\n", count, (count==1)?"":"s"); mEffectSlotList.clear(); diff --git a/alc/backends/base.cpp b/alc/backends/base.cpp index 5c53e3b8..04fc4d64 100644 --- a/alc/backends/base.cpp +++ b/alc/backends/base.cpp @@ -183,7 +183,7 @@ void BackendBase::setChannelOrderFromWFXMask(ALuint chanmask) ALuint idx{0}; while(chanmask) { - const int bit{CTZ32(chanmask)}; + const int bit{CountTrailingZeros(chanmask)}; const ALuint mask{1u << bit}; chanmask &= ~mask; diff --git a/alc/backends/wasapi.cpp b/alc/backends/wasapi.cpp index d7b763c4..ba0d52d5 100644 --- a/alc/backends/wasapi.cpp +++ b/alc/backends/wasapi.cpp @@ -1641,7 +1641,7 @@ HRESULT WasapiCapture::resetProxy() if((InputType.dwChannelMask&SPEAKER_LOW_FREQUENCY)) { constexpr auto lfemask = MaskFromTopBits(SPEAKER_LOW_FREQUENCY); - const int lfeidx{POPCNT32(InputType.dwChannelMask&lfemask) - 1}; + const int lfeidx{PopCount(uint32_t{InputType.dwChannelMask&lfemask}) - 1}; chanmask &= ~(1u << lfeidx); } diff --git a/alc/converter.cpp b/alc/converter.cpp index b07e8619..6e5b2207 100644 --- a/alc/converter.cpp +++ b/alc/converter.cpp @@ -340,7 +340,7 @@ void ChannelConverter::convert(const void *src, float *dst, ALuint frames) const { if(mDstChans == DevFmtMono) { - const float scale{std::sqrt(1.0f / static_cast<float>(POPCNT32(mChanMask)))}; + const float scale{std::sqrt(1.0f / static_cast<float>(PopCount(mChanMask)))}; switch(mSrcType) { #define HANDLE_FMT(T) case T: Multi2Mono<T>(mChanMask, mSrcStep, scale, dst, src, frames); break diff --git a/common/alnumeric.h b/common/alnumeric.h index af8e7b2e..d3d0edfd 100644 --- a/common/alnumeric.h +++ b/common/alnumeric.h @@ -90,34 +90,33 @@ inline size_t RoundUp(size_t value, size_t r) noexcept } -/* Define CTZ macros (count trailing zeros), and POPCNT macros (population - * count/count 1 bits), for 32- and 64-bit integers. The CTZ macros' results - * are *UNDEFINED* if the value is 0. +/* Define CountTrailingZeros (count trailing zero bits, starting from the lsb) + * and PopCount (population count/count 1 bits) methods, for 32- and 64-bit + * integers. The CountTrailingZeros results are *UNDEFINED* if the value is 0. */ -#ifdef __GNUC__ +template<typename T> +inline int PopCount(T val) = delete; +template<typename T> +inline int CountTrailingZeros(T val) = delete; -namespace detail_ { +#ifdef __GNUC__ -template<typename T> -constexpr inline auto popcnt64(T val) = delete; +/* Define variations for unsigned (long (long)) int, since we don't know what + * uint32/64_t are typedef'd to. + */ +template<> +inline int PopCount(unsigned long long val) { return __builtin_popcountll(val); } template<> -constexpr inline auto popcnt64(unsigned long long val) { return __builtin_popcountll(val); } +inline int PopCount(unsigned long val) { return __builtin_popcountl(val); } template<> -constexpr inline auto popcnt64(unsigned long val) { return __builtin_popcountl(val); } +inline int PopCount(unsigned int val) { return __builtin_popcount(val); } -template<typename T> -constexpr inline auto ctz64(T val) = delete; template<> -constexpr inline auto ctz64(unsigned long long val) { return __builtin_ctzll(val); } +inline int CountTrailingZeros(unsigned long long val) { return __builtin_ctzll(val); } template<> -constexpr inline auto ctz64(unsigned long val) { return __builtin_ctzl(val); } - -} // namespace detail_ - -#define POPCNT32 __builtin_popcount -#define CTZ32 __builtin_ctz -#define POPCNT64 detail_::popcnt64<uint64_t> -#define CTZ64 detail_::ctz64<uint64_t> +inline int CountTrailingZeros(unsigned long val) { return __builtin_ctzl(val); } +template<> +inline int CountTrailingZeros(unsigned int val) { return __builtin_ctz(val); } #else @@ -128,69 +127,69 @@ constexpr inline auto ctz64(unsigned long val) { return __builtin_ctzl(val); } * as the ntz2 variant. These likely aren't the most efficient methods, but * they're good enough if the GCC built-ins aren't available. */ -inline int fallback_popcnt32(uint32_t v) +template<> +inline int PopCount(uint32_t v) { v = v - ((v >> 1) & 0x55555555u); v = (v & 0x33333333u) + ((v >> 2) & 0x33333333u); v = (v + (v >> 4)) & 0x0f0f0f0fu; - return (int)((v * 0x01010101u) >> 24); + return static_cast<int>((v * 0x01010101u) >> 24); } -#define POPCNT32 fallback_popcnt32 -inline int fallback_popcnt64(uint64_t v) +template<> +inline int PopCount(uint64_t v) { v = v - ((v >> 1) & 0x5555555555555555_u64); v = (v & 0x3333333333333333_u64) + ((v >> 2) & 0x3333333333333333_u64); v = (v + (v >> 4)) & 0x0f0f0f0f0f0f0f0f_u64; - return (int)((v * 0x0101010101010101_u64) >> 56); + return static_cast<int>((v * 0x0101010101010101_u64) >> 56); } -#define POPCNT64 fallback_popcnt64 #if defined(_WIN64) -inline int msvc64_ctz32(uint32_t v) +template<> +inline int CountTrailingZeros(uint32_t v) { unsigned long idx = 32; _BitScanForward(&idx, v); - return (int)idx; + return static_cast<int>(idx); } -#define CTZ32 msvc64_ctz32 -inline int msvc64_ctz64(uint64_t v) +template<> +inline int CountTrailingZeros(uint64_t v) { unsigned long idx = 64; _BitScanForward64(&idx, v); - return (int)idx; + return static_cast<int>(idx); } -#define CTZ64 msvc64_ctz64 #elif defined(_WIN32) -inline int msvc_ctz32(uint32_t v) +template<> +inline int CountTrailingZeros(uint32_t v) { unsigned long idx = 32; _BitScanForward(&idx, v); - return (int)idx; + return static_cast<int>(idx); } -#define CTZ32 msvc_ctz32 -inline int msvc_ctz64(uint64_t v) +template<> +inline int CountTrailingZeros(uint64_t v) { unsigned long idx = 64; - if(!_BitScanForward(&idx, (uint32_t)(v&0xffffffff))) + if(!_BitScanForward(&idx, static_cast<uint32_t>(v&0xffffffff))) { - if(_BitScanForward(&idx, (uint32_t)(v>>32))) + if(_BitScanForward(&idx, static_cast<uint32_t>(v>>32))) idx += 32; } - return (int)idx; + return static_cast<int>(idx); } -#define CTZ64 msvc_ctz64 #else -inline int fallback_ctz32(uint32_t value) -{ return POPCNT32(~value & (value - 1)); } -#define CTZ32 fallback_ctz32 -inline int fallback_ctz64(uint64_t value) -{ return POPCNT64(~value & (value - 1)); } -#define CTZ64 fallback_ctz64 +template<> +inline int CountTrailingZeros(uint32_t value) +{ return PopCount(~value & (value - 1)); } +template<> +inline int CountTrailingZeros(uint64_t value) +{ return PopCount(~value & (value - 1)); } #endif #endif @@ -240,7 +239,7 @@ inline int float2int(float f) noexcept #elif (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP == 0) \ || ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) \ - && !defined(__SSE_MATH__)) + && !defined(__SSE_MATH__)) int sign, shift, mant; union { float f; @@ -276,7 +275,7 @@ inline int double2int(double d) noexcept #elif (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP < 2) \ || ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) \ - && !defined(__SSE2_MATH__)) + && !defined(__SSE2_MATH__)) int sign, shift; int64_t mant; union { |