diff --git a/cpp/csp/core/DynamicBitSet.h b/cpp/csp/core/DynamicBitSet.h index 9ab7c62ed..3d55679ba 100644 --- a/cpp/csp/core/DynamicBitSet.h +++ b/cpp/csp/core/DynamicBitSet.h @@ -155,19 +155,12 @@ class DynamicBitSet private: using nbit_type = uint8_t; -#ifndef WIN32 -#define CLZ_CONSTEXPR constexpr -#else -#define CLZ_CONSTEXPR -#endif - template::value, bool> = true> static constexpr nbit_type nbits() { return sizeof( value_type ) * 8; } template::value, bool> = true> - static CLZ_CONSTEXPR nbit_type log2( U n ) { return nbits() - clz(static_cast( n )) - 1; } //upcast to 32 bit to avoid truncation for log2 - static CLZ_CONSTEXPR nbit_type log2(uint64_t n) { return nbits() - clz(n) - 1; } + static constexpr nbit_type log2( U n ) { return nbits() - clz( n ) - 1; } static constexpr node_type mask( nbit_type bitIndex ) { return ( node_type )1 << bitIndex; } @@ -182,7 +175,7 @@ class DynamicBitSet } static constexpr nbit_type _bits = nbits(); - static inline CLZ_CONSTEXPR nbit_type _logBits = log2( _bits ); + static inline constexpr nbit_type _logBits = log2( _bits ); node_type * m_nodes; index_type m_size; diff --git a/cpp/csp/core/Platform.h b/cpp/csp/core/Platform.h index 37474faf6..3f3cdd0db 100644 --- a/cpp/csp/core/Platform.h +++ b/cpp/csp/core/Platform.h @@ -1,5 +1,6 @@ #ifndef _IN_CSP_CORE_PLATFORM_H #define _IN_CSP_CORE_PLATFORM_H +#include #include #include #include @@ -52,42 +53,6 @@ inline int nanosleep(const timespec* req, timespec* rem) return 0; } -inline uint8_t clz(uint64_t n) -{ - unsigned long index = 0; - if (_BitScanReverse64(&index, n)) - return 64 - index - 1; - return 0; -} - -inline uint8_t clz(uint32_t n) -{ - unsigned long index = 0; - if (_BitScanReverse(&index, n)) - return 32 - index - 1; - return 0; -} - -inline uint8_t clz(uint16_t n) { return clz(static_cast(n)) - 16; } -inline uint8_t clz(uint8_t n) { return clz(static_cast(n)) - 24; } - -template::value, bool> = true> -inline uint8_t ffs(U n) -{ - unsigned long index = 0; - if (_BitScanForward(&index, n)) - return index + 1; - return 0; -} - -inline uint8_t ffs(uint64_t n) -{ - unsigned long index = 0; - if (_BitScanForward64(&index, n)) - return index + 1; - return 0; -} - #else #define CSPIMPL_EXPORT @@ -100,19 +65,14 @@ inline uint8_t ffs(uint64_t n) #define NO_INLINE __attribute__ ((noinline)) -inline constexpr uint8_t clz(uint32_t n) { return __builtin_clz(n); } -inline constexpr uint8_t clz(uint64_t n) { return __builtin_clzl(n); } +#endif // clz (count leading zeros) returns number of leading zeros before MSB (i.e. clz(00110..) = 2 ) -// __builtin_clz auto-promotes to 32-bits: need to subtract off extra leading zeros -inline constexpr uint8_t clz(uint16_t n) { return clz(static_cast(n)) - 16; } -inline constexpr uint8_t clz(uint8_t n) { return clz(static_cast(n)) - 24; } +template::value, bool> = true> +inline constexpr uint8_t clz( U n ) { return std::countl_zero(n); } // ffs (find first set) returns offset of first set bit (i.e. ffs(..0110) = 2 ), with ffs(0) = 0 template::value, bool> = true> -inline constexpr uint8_t ffs( U n ) { return __builtin_ffs(n); } -inline constexpr uint8_t ffs( uint64_t n ) { return __builtin_ffsl(n); } - -#endif +inline constexpr uint8_t ffs( U n ) { return n ? std::countr_zero(n) + 1 : 0; } #endif