From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.0 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3AB07C282E1 for ; Wed, 24 Apr 2019 17:17:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 09A7A21907 for ; Wed, 24 Apr 2019 17:17:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1556126245; bh=GXA9xdz13reXHER/mDTvwROLSlaMVpyXXwYTRcI/7oA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=NUTzYJPS5ncNxMSvdELPAm/nnTU8QLtFWBCjdX74VV/0yZhT42zgIsMNEI2Bwwjcl KIxorGHe5ATQMqMX1yVHzsdYH8ysGdexkstotJwp3gy4kRe3W4Zlv9RG2nxgg8nEEA TZeAIqqR9qIa6+Sz8aq3Z4CoAY2NMlGiBOAwnFww= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388617AbfDXRRX (ORCPT ); Wed, 24 Apr 2019 13:17:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:42184 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388602AbfDXRRV (ORCPT ); Wed, 24 Apr 2019 13:17:21 -0400 Received: from localhost (62-193-50-229.as16211.net [62.193.50.229]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D4952218B0; Wed, 24 Apr 2019 17:17:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1556126240; bh=GXA9xdz13reXHER/mDTvwROLSlaMVpyXXwYTRcI/7oA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ksyz7dPnXB81eA0AnFC3uJhzElUdTxbB7fjsjxGEjR6FqtmzuDNhwC7ImCz8YdaUZ 8cT5iBtSKENzU8JrtKt4cv/Ag9NvGQJbTPYEx4SRQLEQo2+wvBofhC+DdhH5n9l2aE UQ9tJPt5l5g6ycMAYbDCEa3WW0bR6Z6eDc1DpzD0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Peter Zijlstra (Intel)" , Joe Perches , Will Deacon , Linus Torvalds , Anshul Garg , Davidlohr Bueso , David Miller , Ingo Molnar , Kees Cook , Matthew Wilcox , Michael Davidson , Thomas Gleixner , Andrew Morton Subject: [PATCH 4.4 004/168] lib/int_sqrt: optimize initial value compute Date: Wed, 24 Apr 2019 19:07:28 +0200 Message-Id: <20190424170923.713010990@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190424170923.452349382@linuxfoundation.org> References: <20190424170923.452349382@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Zijlstra commit f8ae107eef209bff29a5816bc1aad40d5cd69a80 upstream. The initial value (@m) compute is: m = 1UL << (BITS_PER_LONG - 2); while (m > x) m >>= 2; Which is a linear search for the highest even bit smaller or equal to @x We can implement this using a binary search using __fls() (or better when its hardware implemented). m = 1UL << (__fls(x) & ~1UL); Especially for small values of @x; which are the more common arguments when doing a CDF on idle times; the linear search is near to worst case, while the binary search of __fls() is a constant 6 (or 5 on 32bit) branches. cycles: branches: branch-misses: PRE: hot: 43.633557 +- 0.034373 45.333132 +- 0.002277 0.023529 +- 0.000681 cold: 207.438411 +- 0.125840 45.333132 +- 0.002277 6.976486 +- 0.004219 SOFTWARE FLS: hot: 29.576176 +- 0.028850 26.666730 +- 0.004511 0.019463 +- 0.000663 cold: 165.947136 +- 0.188406 26.666746 +- 0.004511 6.133897 +- 0.004386 HARDWARE FLS: hot: 24.720922 +- 0.025161 20.666784 +- 0.004509 0.020836 +- 0.000677 cold: 132.777197 +- 0.127471 20.666776 +- 0.004509 5.080285 +- 0.003874 Averages computed over all values <128k using a LFSR to generate order. Cold numbers have a LFSR based branch trace buffer 'confuser' ran between each int_sqrt() invocation. Link: http://lkml.kernel.org/r/20171020164644.936577234@infradead.org Signed-off-by: Peter Zijlstra (Intel) Suggested-by: Joe Perches Acked-by: Will Deacon Acked-by: Linus Torvalds Cc: Anshul Garg Cc: Davidlohr Bueso Cc: David Miller Cc: Ingo Molnar Cc: Kees Cook Cc: Matthew Wilcox Cc: Michael Davidson Cc: Thomas Gleixner Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Cc: Joe Perches Signed-off-by: Greg Kroah-Hartman --- lib/int_sqrt.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) --- a/lib/int_sqrt.c +++ b/lib/int_sqrt.c @@ -7,6 +7,7 @@ #include #include +#include /** * int_sqrt - rough approximation to sqrt @@ -21,10 +22,7 @@ unsigned long int_sqrt(unsigned long x) if (x <= 1) return x; - m = 1UL << (BITS_PER_LONG - 2); - while (m > x) - m >>= 2; - + m = 1UL << (__fls(x) & ~1UL); while (m != 0) { b = y + m; y >>= 1;