Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1764942AbcLWPYP (ORCPT ); Fri, 23 Dec 2016 10:24:15 -0500 Received: from p3plsmtps2ded01.prod.phx3.secureserver.net ([208.109.80.58]:41984 "EHLO p3plsmtps2ded01.prod.phx3.secureserver.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754069AbcLWPYN (ORCPT ); Fri, 23 Dec 2016 10:24:13 -0500 x-originating-ip: 72.167.245.219 From: Matthew Wilcox To: Yury Norov , Rasmus Villemoes , George Spelvin , Akinobu Mita , Thomas Gleixner , Andrew Morton , linux-kernel@vger.kernel.org Cc: Matthew Wilcox Subject: [PATCH] find_bit: Micro-optimise find_next_*_bit Date: Fri, 23 Dec 2016 09:20:03 -0800 Message-Id: <1482513603-9630-1-git-send-email-mawilcox@linuxonhyperv.com> X-Mailer: git-send-email 1.7.4.1 X-CMAE-Envelope: MS4wfOnv7z0LvhePGQtn3GGAxYZ97YrXzDrue5JAJ1KfjYEs03+YXx94MsnZZksXRiiqmB9/hdwGe0qoIjT4MkHC3g8yUXgISm+yOS8i0lT63l/kZ0dnrwLN U39p098Sg3ZecSuB2MfgpnFwvJnloNjSAAOKONmaDVpb5VZSqG7iYMqU6+6Lv5XXnC5W7dUZVAQFuq8WaJWXJo7IGntW+HmYHvm+OYHdK09DuagWtzSUsFdb QUV16in6rpnq++KajU/VEzVtz/loaB8uKOBcFp0F+6QIxdGOBkm2o+oHJZu7e0nhC4cLSq8xJEvBw+pC2AHpoKQ39oKgIOwShXYhIsHePMLaz5Qv9PTX1UAB ByNt+xZVMX215R0v9FkD1V2yO2S2wfvbOTxCCJk7tDcymeeyoau9tbpT4qnW9Bb6inhMEwzzrwP6vqN3rB3oc2ZLDVSZFQ== Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1110 Lines: 31 From: Matthew Wilcox 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 --- 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