diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2023-11-10 12:28:00 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2023-11-10 21:13:36 -0500 |
commit | 208376ef99da243545efcd6fb02d3469b4c068ed (patch) | |
tree | b7155aa6823ad9488fa23402e689cf131878ef9d /src/bit.h | |
parent | 4efbe8eca395c90fc0053c7ba1038ccb7bf69e61 (diff) | |
download | bfs-208376ef99da243545efcd6fb02d3469b4c068ed.tar.xz |
bit: Implement a branchless has_single_bit()
Diffstat (limited to 'src/bit.h')
-rw-r--r-- | src/bit.h | 3 |
1 files changed, 2 insertions, 1 deletions
@@ -350,7 +350,8 @@ UINT_OVERLOADS(FIRST_TRAILING_ONE) #define HAS_SINGLE_BIT(type, suffix, width) \ static inline bool has_single_bit##suffix(type n) { \ - return n && !(n & (n - 1)); \ + /** Branchless n && !(n & (n - 1)) */ \ + return n < (n ^ (n - 1)) + 1; \ } UINT_OVERLOADS(ROTATE_LEFT) |