]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
lib/find_bit.c: micro-optimise find_next_*_bit
authorMatthew Wilcox <mawilcox@microsoft.com>
Fri, 24 Feb 2017 23:00:58 +0000 (15:00 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sat, 25 Feb 2017 01:46:57 +0000 (17:46 -0800)
This saves 32 bytes on my x86-64 build, mostly due to alignment
considerations and sharing more code between find_next_bit and
find_next_zero_bit, but it does save a couple 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.

[mawilcox@microsoft.com: v2]
Link: http://lkml.kernel.org/r/1483709016-1834-1-git-send-email-mawilcox@linuxonhyperv.com
Link: http://lkml.kernel.org/r/1483709016-1834-1-git-send-email-mawilcox@linuxonhyperv.com
Signed-off-by: Matthew Wilcox <mawilcox@microsoft.com>
Acked-by: Yury Norov <ynorov@caviumnetworks.com>
Acked-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
lib/find_bit.c
tools/lib/find_bit.c

index 18072ea9c20ebba59aaa3fdf69ee6bfc1db395b6..6ed74f78380ce1f6e69568fad0c28d685a5f2048 100644 (file)
@@ -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;
@@ -151,7 +151,7 @@ static unsigned long _find_next_bit_le(const unsigned long *addr,
 {
        unsigned long tmp;
 
-       if (!nbits || start >= nbits)
+       if (unlikely(start >= nbits))
                return nbits;
 
        tmp = addr[start / BITS_PER_LONG] ^ invert;
index 6d8b8f22cf55d8a825d6dd4d56f992dd5e1d63b8..42c15f906aac5baf9debab89e881003b60c889c4 100644 (file)
@@ -34,7 +34,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;