Skip to content

Commit 4df1047

Browse files
revel8nDuffsDevice
andcommitted
Updated clz implementation to use lzcnt implementation based on BitScanReverse (#36)
* Updated clz implementation to use lzcnt implementation based on BitScanReverse * Minor stylistic adaption Co-authored-by: Jakob Riedle <[email protected]>
1 parent 4fc49c8 commit 4df1047

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

tinyutf8.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,31 @@ namespace tiny_utf8_detail
6969
#ifndef TINY_UTF8_HAS_CLZ
7070
#define TINY_UTF8_HAS_CLZ true
7171
#endif
72-
static inline unsigned int clz( unsigned int val ){ return __builtin_clz( val ); }
73-
static inline unsigned int clz( unsigned long int val ){ return __builtin_clzl( val ); }
74-
static inline unsigned int clz( char32_t val ){
75-
return sizeof(char32_t) == sizeof(unsigned long int) ? __builtin_clzl( val ) : __builtin_clz( val );
72+
static inline unsigned int clz( unsigned int value ){ return __builtin_clz( value ); }
73+
static inline unsigned int clz( unsigned long int value ){ return __builtin_clzl( value ); }
74+
static inline unsigned int clz( char32_t value ){
75+
return sizeof(char32_t) == sizeof(unsigned long int) ? __builtin_clzl( value ) : __builtin_clz( value );
7676
}
7777
#elif defined(_MSC_VER)
7878
#ifndef TINY_UTF8_HAS_CLZ
7979
#define TINY_UTF8_HAS_CLZ true
8080
#endif
81-
static inline unsigned int clz( uint16_t val ){ return __lzcnt16( val ); }
82-
static inline unsigned int clz( uint32_t val ){ return __lzcnt( val ); }
81+
template<typename T>
82+
static inline unsigned int lzcnt( T value ){
83+
unsigned long value_log2;
84+
#ifndef WIN32
85+
_BitScanReverse64( &value_log2 , value );
86+
#else
87+
_BitScanReverse( &value_log2 , value );
88+
#endif
89+
return sizeof(T) * 8 - value_log2 - 1;
90+
}
91+
static inline unsigned int clz( uint16_t value ){ return lzcnt( value ); }
92+
static inline unsigned int clz( uint32_t value ){ return lzcnt( value ); }
8393
#ifndef WIN32
84-
static inline unsigned int clz( uint64_t val ){ return __lzcnt64(val); }
94+
static inline unsigned int clz( uint64_t value ){ return lzcnt( value ); }
8595
#endif // WIN32
86-
static inline unsigned int clz( char32_t val ){ return __lzcnt( val ); }
96+
static inline unsigned int clz( char32_t value ){ return lzcnt( value ); }
8797
#endif
8898

8999
//! Helper to detect little endian

0 commit comments

Comments
 (0)