Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions cpp/csp/core/DynamicBitSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,12 @@ class DynamicBitSet
private:
using nbit_type = uint8_t;

#ifndef WIN32
#define CLZ_CONSTEXPR constexpr
#else
#define CLZ_CONSTEXPR
#endif

template<typename value_type,
std::enable_if_t<std::is_unsigned<value_type>::value, bool> = true>
static constexpr nbit_type nbits() { return sizeof( value_type ) * 8; }

template<typename U, std::enable_if_t<std::is_unsigned<U>::value, bool> = true>
static CLZ_CONSTEXPR nbit_type log2( U n ) { return nbits<uint32_t>() - clz(static_cast<uint32_t>( n )) - 1; } //upcast to 32 bit to avoid truncation for log2
static CLZ_CONSTEXPR nbit_type log2(uint64_t n) { return nbits<uint64_t>() - clz(n) - 1; }
static constexpr nbit_type log2( U n ) { return nbits<U>() - clz( n ) - 1; }

static constexpr node_type mask( nbit_type bitIndex ) { return ( node_type )1 << bitIndex; }

Expand All @@ -182,7 +175,7 @@ class DynamicBitSet
}

static constexpr nbit_type _bits = nbits<NodeT>();
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;
Expand Down
50 changes: 5 additions & 45 deletions cpp/csp/core/Platform.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef _IN_CSP_CORE_PLATFORM_H
#define _IN_CSP_CORE_PLATFORM_H
#include <bit>
#include <type_traits>
#include <stdint.h>
#include <time.h>
Expand Down Expand Up @@ -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<uint32_t>(n)) - 16; }
inline uint8_t clz(uint8_t n) { return clz(static_cast<uint32_t>(n)) - 24; }

template<typename U, std::enable_if_t<std::is_unsigned<U>::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
Expand All @@ -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<uint32_t>(n)) - 16; }
inline constexpr uint8_t clz(uint8_t n) { return clz(static_cast<uint32_t>(n)) - 24; }
template<typename U, std::enable_if_t<std::is_unsigned<U>::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<typename U, std::enable_if_t<std::is_unsigned<U>::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
Loading