// count trailing zeros.
// NOTE: the builtin is nondeterministic on 0 input
-static inline unsigned ctz(unsigned v) {
+template<class T>
+ inline typename std::enable_if<
+ (std::is_integral<T>::value &&
+ sizeof(T) <= sizeof(unsigned)),
+ unsigned>::type ctz(T v) {
if (v == 0)
return sizeof(v) * 8;
return __builtin_ctz(v);
}
-static inline unsigned ctzl(unsigned long v) {
+
+template<class T>
+ inline typename std::enable_if<
+ (std::is_integral<T>::value &&
+ sizeof(T) > sizeof(unsigned int) &&
+ sizeof(T) <= sizeof(unsigned long)),
+ unsigned>::type ctz(T v) {
if (v == 0)
return sizeof(v) * 8;
return __builtin_ctzl(v);
}
-static inline unsigned ctzll(unsigned long long v) {
+
+template<class T>
+ inline typename std::enable_if<
+ (std::is_integral<T>::value &&
+ sizeof(T) > sizeof(unsigned long) &&
+ sizeof(T) <= sizeof(unsigned long long)),
+ unsigned>::type ctz(T v) {
if (v == 0)
return sizeof(v) * 8;
return __builtin_ctzll(v);
// count leading zeros
// NOTE: the builtin is nondeterministic on 0 input
-static inline unsigned clz(unsigned v) {
+template<class T>
+ inline typename std::enable_if<
+ (std::is_integral<T>::value &&
+ sizeof(T) <= sizeof(unsigned)),
+ unsigned>::type clz(T v) {
if (v == 0)
return sizeof(v) * 8;
return __builtin_clz(v);
}
-static inline unsigned clzl(unsigned long v) {
+
+template<class T>
+ inline typename std::enable_if<
+ (std::is_integral<T>::value &&
+ sizeof(T) > sizeof(unsigned int) &&
+ sizeof(T) <= sizeof(unsigned long)),
+ unsigned>::type clz(T v) {
if (v == 0)
return sizeof(v) * 8;
return __builtin_clzl(v);
}
-static inline unsigned clzll(unsigned long long v) {
+
+template<class T>
+ inline typename std::enable_if<
+ (std::is_integral<T>::value &&
+ sizeof(T) > sizeof(unsigned long) &&
+ sizeof(T) <= sizeof(unsigned long long)),
+ unsigned>::type clz(T v) {
if (v == 0)
return sizeof(v) * 8;
return __builtin_clzll(v);
}
// count bits (set + any 0's that follow)
-static inline unsigned cbits(unsigned v) {
+template<class T>
+ inline typename std::enable_if<
+ (std::is_integral<T>::value &&
+ sizeof(T) <= sizeof(unsigned)),
+ unsigned>::type cbits(T v) {
if (v == 0)
return 0;
return (sizeof(v) * 8) - __builtin_clz(v);
}
-static inline unsigned cbitsl(unsigned long v) {
+
+template<class T>
+ inline typename std::enable_if<
+ (std::is_integral<T>::value &&
+ sizeof(T) > sizeof(unsigned int) &&
+ sizeof(T) <= sizeof(unsigned long)),
+ unsigned>::type cbits(T v) {
if (v == 0)
return 0;
return (sizeof(v) * 8) - __builtin_clzl(v);
}
-static inline unsigned cbitsll(unsigned long long v) {
+
+template<class T>
+ inline typename std::enable_if<
+ (std::is_integral<T>::value &&
+ sizeof(T) > sizeof(unsigned long) &&
+ sizeof(T) <= sizeof(unsigned long long)),
+ unsigned>::type cbits(T v) {
if (v == 0)
return 0;
return (sizeof(v) * 8) - __builtin_clzll(v);