Permalink
Browse files

sizeof works just as well as numeric_limits<T>::digits

Summary: ... and is shorter and more readable

Test Plan: folly/test

Reviewed By: andrei.alexandrescu@fb.com

FB internal diff: D545774
  • Loading branch information...
1 parent bf188b3 commit 442e1c0c6ffebf233bae7367bc49a4d90c149b0b @tudor tudor committed Aug 10, 2012
Showing with 10 additions and 20 deletions.
  1. +10 −20 folly/Bits.h
View
@@ -84,8 +84,7 @@ template <class T>
typename std::enable_if<
(std::is_integral<T>::value &&
std::is_unsigned<T>::value &&
- (std::numeric_limits<T>::digits <=
- std::numeric_limits<unsigned int>::digits)),
+ sizeof(T) <= sizeof(unsigned int)),
unsigned int>::type
findFirstSet(T x) {
return __builtin_ffs(x);
@@ -95,10 +94,8 @@ template <class T>
typename std::enable_if<
(std::is_integral<T>::value &&
std::is_unsigned<T>::value &&
- (std::numeric_limits<T>::digits >
- std::numeric_limits<unsigned int>::digits) &&
- (std::numeric_limits<T>::digits <=
- std::numeric_limits<unsigned long>::digits)),
+ sizeof(T) > sizeof(unsigned int) &&
+ sizeof(T) <= sizeof(unsigned long)),
unsigned int>::type
findFirstSet(T x) {
return __builtin_ffsl(x);
@@ -108,10 +105,8 @@ template <class T>
typename std::enable_if<
(std::is_integral<T>::value &&
std::is_unsigned<T>::value &&
- (std::numeric_limits<T>::digits >
- std::numeric_limits<unsigned long>::digits) &&
- (std::numeric_limits<T>::digits <=
- std::numeric_limits<unsigned long long>::digits)),
+ sizeof(T) > sizeof(unsigned long) &&
+ sizeof(T) <= sizeof(unsigned long long)),
unsigned int>::type
findFirstSet(T x) {
return __builtin_ffsll(x);
@@ -134,8 +129,7 @@ template <class T>
typename std::enable_if<
(std::is_integral<T>::value &&
std::is_unsigned<T>::value &&
- (std::numeric_limits<T>::digits <=
- std::numeric_limits<unsigned int>::digits)),
+ sizeof(T) <= sizeof(unsigned int)),
unsigned int>::type
findLastSet(T x) {
return x ? 8 * sizeof(unsigned int) - __builtin_clz(x) : 0;
@@ -145,10 +139,8 @@ template <class T>
typename std::enable_if<
(std::is_integral<T>::value &&
std::is_unsigned<T>::value &&
- (std::numeric_limits<T>::digits >
- std::numeric_limits<unsigned int>::digits) &&
- (std::numeric_limits<T>::digits <=
- std::numeric_limits<unsigned long>::digits)),
+ sizeof(T) > sizeof(unsigned int) &&
+ sizeof(T) <= sizeof(unsigned long)),
unsigned int>::type
findLastSet(T x) {
return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
@@ -158,10 +150,8 @@ template <class T>
typename std::enable_if<
(std::is_integral<T>::value &&
std::is_unsigned<T>::value &&
- (std::numeric_limits<T>::digits >
- std::numeric_limits<unsigned long>::digits) &&
- (std::numeric_limits<T>::digits <=
- std::numeric_limits<unsigned long long>::digits)),
+ sizeof(T) > sizeof(unsigned long) &&
+ sizeof(T) <= sizeof(unsigned long long)),
unsigned int>::type
findLastSet(T x) {
return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;

0 comments on commit 442e1c0

Please sign in to comment.