11#ifndef _IN_CSP_CORE_PLATFORM_H
22#define _IN_CSP_CORE_PLATFORM_H
3+ #include < bit>
34#include < type_traits>
45#include < stdint.h>
56#include < time.h>
@@ -47,47 +48,11 @@ inline tm * localtime_r( const time_t * timep, tm * result )
4748inline int nanosleep (const timespec* req, timespec* rem)
4849{
4950 assert (rem == nullptr );
50- int64_t millis = req->tv_sec * 1000 + req->tv_nsec * 1000000 ;
51+ int64_t millis = req->tv_sec * 1000 + req->tv_nsec / 1000000 ;
5152 Sleep (millis);
5253 return 0 ;
5354}
5455
55- inline uint8_t clz (uint64_t n)
56- {
57- unsigned long index = 0 ;
58- if (_BitScanReverse64 (&index, n))
59- return 64 - index - 1 ;
60- return 0 ;
61- }
62-
63- inline uint8_t clz (uint32_t n)
64- {
65- unsigned long index = 0 ;
66- if (_BitScanReverse (&index, n))
67- return 32 - index - 1 ;
68- return 0 ;
69- }
70-
71- inline uint8_t clz (uint16_t n) { return clz (static_cast <uint32_t >(n)) - 16 ; }
72- inline uint8_t clz (uint8_t n) { return clz (static_cast <uint32_t >(n)) - 24 ; }
73-
74- template <typename U, std::enable_if_t <std::is_unsigned<U>::value, bool > = true >
75- inline uint8_t ffs (U n)
76- {
77- unsigned long index = 0 ;
78- if (_BitScanForward (&index, n))
79- return index + 1 ;
80- return 0 ;
81- }
82-
83- inline uint8_t ffs (uint64_t n)
84- {
85- unsigned long index = 0 ;
86- if (_BitScanForward64 (&index, n))
87- return index + 1 ;
88- return 0 ;
89- }
90-
9156#else
9257
9358#define CSPIMPL_EXPORT
@@ -100,19 +65,14 @@ inline uint8_t ffs(uint64_t n)
10065
10166#define NO_INLINE __attribute__ ((noinline))
10267
103- inline constexpr uint8_t clz(uint32_t n) { return __builtin_clz (n); }
104- inline constexpr uint8_t clz (uint64_t n) { return __builtin_clzl (n); }
68+ #endif
10569
10670// clz (count leading zeros) returns number of leading zeros before MSB (i.e. clz(00110..) = 2 )
107- // __builtin_clz auto-promotes to 32-bits: need to subtract off extra leading zeros
108- inline constexpr uint8_t clz (uint16_t n) { return clz (static_cast <uint32_t >(n)) - 16 ; }
109- inline constexpr uint8_t clz (uint8_t n) { return clz (static_cast <uint32_t >(n)) - 24 ; }
71+ template <typename U, std::enable_if_t <std::is_unsigned<U>::value, bool > = true >
72+ inline constexpr uint8_t clz ( U n ) { return std::countl_zero (n); }
11073
11174// ffs (find first set) returns offset of first set bit (i.e. ffs(..0110) = 2 ), with ffs(0) = 0
11275template <typename U, std::enable_if_t <std::is_unsigned<U>::value, bool > = true >
113- inline constexpr uint8_t ffs ( U n ) { return __builtin_ffs (n); }
114- inline constexpr uint8_t ffs ( uint64_t n ) { return __builtin_ffsl (n); }
115-
116- #endif
76+ inline constexpr uint8_t ffs ( U n ) { return n ? std::countr_zero (n) + 1 : 0 ; }
11777
11878#endif
0 commit comments