Skip to content

Commit 4cc67b0

Browse files
maciej-w-rozyckiakpm00
authored andcommitted
linux/log2.h: reduce instruction count for is_power_of_2()
Follow an observation that (n ^ (n - 1)) will only ever retain the most significant bit set in the word operated on if that is the only bit set in the first place, and use it to determine whether a number is a whole power of 2, avoiding the need for an explicit check for nonzero. This reduces the sequence produced to 3 instructions only across Alpha, MIPS, and RISC-V targets, down from 4, 5, and 4 respectively, removing a branch in the two latter cases. And it's 5 instructions on POWER and x86-64 vs 8 and 9 respectively. There are no branches now emitted here for targets that have a suitable conditional set operation, although an inline expansion will often end with one, depending on what code a call to this function is used in. Credit goes to GCC authors for coming up with this optimisation used as the fallback for (__builtin_popcountl(n) == 1), equivalent to this code, for targets where the hardware population count operation is considered expensive. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Maciej W. Rozycki <[email protected]> Cc: Jens Axboe <[email protected]> Cc: John Garry <[email protected]> Cc: "Martin K. Petersen" <[email protected]> Cc: Su Hui <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 5e65b5c commit 4cc67b0

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

include/linux/log2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int __ilog2_u64(u64 n)
4444
static __always_inline __attribute__((const))
4545
bool is_power_of_2(unsigned long n)
4646
{
47-
return (n != 0 && ((n & (n - 1)) == 0));
47+
return n - 1 < (n ^ (n - 1));
4848
}
4949

5050
/**

0 commit comments

Comments
 (0)