Skip to content

Commit

Permalink
Updated clz implementation to use lzcnt implementation based on BitSc…
Browse files Browse the repository at this point in the history
…anReverse (#36)

* Updated clz implementation to use lzcnt implementation based on BitScanReverse
* Minor stylistic adaption

Co-authored-by: Jakob Riedle <[email protected]>
  • Loading branch information
revel8n and DuffsDevice committed Jan 17, 2020
1 parent 4fc49c8 commit 4df1047
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions tinyutf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,31 @@ namespace tiny_utf8_detail
#ifndef TINY_UTF8_HAS_CLZ
#define TINY_UTF8_HAS_CLZ true
#endif
static inline unsigned int clz( unsigned int val ){ return __builtin_clz( val ); }
static inline unsigned int clz( unsigned long int val ){ return __builtin_clzl( val ); }
static inline unsigned int clz( char32_t val ){
return sizeof(char32_t) == sizeof(unsigned long int) ? __builtin_clzl( val ) : __builtin_clz( val );
static inline unsigned int clz( unsigned int value ){ return __builtin_clz( value ); }
static inline unsigned int clz( unsigned long int value ){ return __builtin_clzl( value ); }
static inline unsigned int clz( char32_t value ){
return sizeof(char32_t) == sizeof(unsigned long int) ? __builtin_clzl( value ) : __builtin_clz( value );
}
#elif defined(_MSC_VER)
#ifndef TINY_UTF8_HAS_CLZ
#define TINY_UTF8_HAS_CLZ true
#endif
static inline unsigned int clz( uint16_t val ){ return __lzcnt16( val ); }
static inline unsigned int clz( uint32_t val ){ return __lzcnt( val ); }
template<typename T>
static inline unsigned int lzcnt( T value ){
unsigned long value_log2;
#ifndef WIN32
_BitScanReverse64( &value_log2 , value );
#else
_BitScanReverse( &value_log2 , value );
#endif
return sizeof(T) * 8 - value_log2 - 1;
}
static inline unsigned int clz( uint16_t value ){ return lzcnt( value ); }
static inline unsigned int clz( uint32_t value ){ return lzcnt( value ); }
#ifndef WIN32
static inline unsigned int clz( uint64_t val ){ return __lzcnt64(val); }
static inline unsigned int clz( uint64_t value ){ return lzcnt( value ); }
#endif // WIN32
static inline unsigned int clz( char32_t val ){ return __lzcnt( val ); }
static inline unsigned int clz( char32_t value ){ return lzcnt( value ); }
#endif

//! Helper to detect little endian
Expand Down

0 comments on commit 4df1047

Please sign in to comment.