-
Notifications
You must be signed in to change notification settings - Fork 786
Optimize bit count polyfills #2914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
749d0f8
f0f1d78
cd47277
c4049c3
5fc72c6
29a8c8c
e7b5088
d81ce1a
78633e2
15bff4a
abcc646
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,17 +65,21 @@ template<typename T> int CountTrailingZeroes(T v) { | |
template<typename T> int CountLeadingZeroes(T v) { | ||
return CountLeadingZeroes(typename std::make_unsigned<T>::type(v)); | ||
} | ||
template<typename T> bool IsPowerOf2(T v) { return v != 0 && PopCount(v) == 1; } | ||
template<typename T> bool IsPowerOf2(T v) { | ||
return v != 0 && (v & (v - 1)) == 0; | ||
} | ||
Comment on lines
+68
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice 👍 I like that this lets us delete code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usually |
||
|
||
template<typename T, typename U> inline static T RotateLeft(T val, U count) { | ||
T mask = sizeof(T) * CHAR_BIT - 1; | ||
auto value = typename std::make_unsigned<T>::type(val); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pretty important cast to |
||
U mask = sizeof(T) * CHAR_BIT - 1; | ||
count &= mask; | ||
return (val << count) | (val >> (-count & mask)); | ||
return (value << count) | (value >> (-count & mask)); | ||
} | ||
template<typename T, typename U> inline static T RotateRight(T val, U count) { | ||
T mask = sizeof(T) * CHAR_BIT - 1; | ||
auto value = typename std::make_unsigned<T>::type(val); | ||
U mask = sizeof(T) * CHAR_BIT - 1; | ||
count &= mask; | ||
return (val >> count) | (val << (-count & mask)); | ||
return (value >> count) | (value << (-count & mask)); | ||
} | ||
|
||
extern uint32_t Log2(uint32_t v); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
__has_builtin(__builtin_popcount)
instead__has_builtin(__builtin_popcountll)
due to clang-format forcing line terminator and carrydefined(_MSC_VER)
to new line and this looks weird.