[PATCH] find_bit: Micro-optimise find_next_*_bit

From: Matthew Wilcox
Date: Fri Dec 23 2016 - 10:24:36 EST


From: Matthew Wilcox <mawilcox@xxxxxxxxxxxxx>

This saves 20 bytes on my x86-64 build, mostly due to alignment
considerations ... I think it actually saves about five bytes of
instructions. There's really two parts to this commit. First, the
first half of the test: (!nbits || start >= nbits) is trivially a subset
of the second half, since nbits and start are both unsigned. Second,
while looking at the disassembly, I noticed that GCC was predicting the
branch taken. Since this is a failure case, it's clearly the less likely
of the two branches, so add an unlikely() to override GCC's heuristics.

Signed-off-by: Matthew Wilcox <mawilcox@xxxxxxxxxxxxx>
---
lib/find_bit.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/find_bit.c b/lib/find_bit.c
index 18072ea9c20e..7d4a681d625f 100644
--- a/lib/find_bit.c
+++ b/lib/find_bit.c
@@ -33,7 +33,7 @@ static unsigned long _find_next_bit(const unsigned long *addr,
{
unsigned long tmp;

- if (!nbits || start >= nbits)
+ if (unlikely(start >= nbits))
return nbits;

tmp = addr[start / BITS_PER_LONG] ^ invert;
--
2.11.0