From cc7d66416b91e0972c1022d0a835804a63ab7ab1 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 13 Nov 2023 15:13:27 -0500 Subject: bit: Implement a branchless has_single_bit() --- src/bit.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bit.h b/src/bit.h index 21a8076..7f71d04 100644 --- a/src/bit.h +++ b/src/bit.h @@ -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 - 1 < (n ^ (n - 1)); \ } UINT_OVERLOADS(ROTATE_LEFT) -- cgit v1.2.3