Skip to content

Commit

Permalink
Revert "bit: Implement a branchless has_single_bit()"
Browse files Browse the repository at this point in the history
Doesn't work for the highest bit due to overflow.

This reverts commit 208376e.
  • Loading branch information
tavianator committed Nov 11, 2023
1 parent 640fa83 commit 56e3592
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,7 @@ UINT_OVERLOADS(FIRST_TRAILING_ONE)

#define HAS_SINGLE_BIT(type, suffix, width) \
static inline bool has_single_bit##suffix(type n) { \
/** Branchless n && !(n & (n - 1)) */ \
return n < (n ^ (n - 1)) + 1; \
return n && !(n & (n - 1)); \
}

UINT_OVERLOADS(ROTATE_LEFT)
Expand Down
1 change: 1 addition & 0 deletions tests/bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ int main(void) {

bfs_verify(!has_single_bit(0));
bfs_verify(!has_single_bit(UINT32_MAX));
bfs_verify(has_single_bit((uint32_t)1 << (UINT_WIDTH - 1)));

return EXIT_SUCCESS;
}

0 comments on commit 56e3592

Please sign in to comment.