Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1035920yba; Thu, 4 Apr 2019 02:58:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqxzaOVoWso1KwTIXairGs79gMnaMz3DG67NU9vT0A2BzxpTgYWTRILxiQVg7WaoxygiF7RQ X-Received: by 2002:a63:6941:: with SMTP id e62mr4671678pgc.99.1554371911857; Thu, 04 Apr 2019 02:58:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554371911; cv=none; d=google.com; s=arc-20160816; b=V1z8f/xYz078npiV8rW5pppz2uTUnONgW56S6DDRwOKOOU9/6w8n0Byc9VqO0fXtSi ag4oHVZJjXwn6tKeHpZivPIMQBUsHn6wlo6/raGfYNu1z3jcGJaUVegFTmd/4s0gSduf bCrVQE/NfVHKOGaqbhV3ZFsOsDIWdeqn6oIEqZA4j2T3JfmUugPR4PLXdSwRsQmCKcrr wWVRoP0l8W22Ce1rrLmmV9aMIy1uU6GBn+ZfD/wZewUIBwmpQldEYhfpPi+uDAqxU7Nt oivpAws/MGPw0aLL0rYFBKrCANcjEo7h17z+vCDLjhr84P6dp2a88x1Aul3ypiegenlb ZGvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=QBL0J9akwBNcS6hhVYzwsgxpWl6bufv/cwsWfGHyKas=; b=iyoA4n4ligsAPGbfiD+rm8FxyOLEkTgoVNInLDvzzyVQpGDCTYAbNV6UmEGtj8DEy/ NSXaTmOixi2PH5koXPGeSlHhYKTPcJXNiamd6K6tgbU0gzDxfGopVLSMMkwzCxDkfkBh dVwBBP2A1KBE8oHkqcY/lmQ9w4RJIt5jp12UPSd2lshdo7YX+ToHhB0JI3Lueg4XMYvE 9WXo0i+JrH3COW7O5GLKaFTOkBbXSVHXJV5mV/JqqKeqhmdw1CcL2yyjNShpm3Qt8hSS GDOeTclEbk8ibGsMkVsObFzgHT7CF9fnPalUgwxPq1y1qY8k2QgrQn5E5ZSuYtiAk+VM mIjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=HtF8GTrQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a1si7943600plp.120.2019.04.04.02.58.16; Thu, 04 Apr 2019 02:58:31 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=HtF8GTrQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728883AbfDDJ5o (ORCPT + 99 others); Thu, 4 Apr 2019 05:57:44 -0400 Received: from mail.kernel.org ([198.145.29.99]:50710 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728777AbfDDIty (ORCPT ); Thu, 4 Apr 2019 04:49:54 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 649E221734; Thu, 4 Apr 2019 08:49:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1554367793; bh=nruYE71tMKdBSwTmSMpR5FkoGU0hAg+y1YyI6b2VNlQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HtF8GTrQ0nblvwsCyQNCYOUCyp4pO2knAj563SnbbzQNGHeSwrol/HObJ26gHhW0J 0pwaWXU/Ee2pVGeNmtKZQg5Wigwa3ICU6Y6MfFX7imbHcXzcnkBzOMc4KM88rNz6Ta Ql0NnMBXYcFZeF4B9XHM8WQfyqJdwDtpYjPAF5fI= 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.9 04/91] lib/int_sqrt: optimize initial value compute Date: Thu, 4 Apr 2019 10:46:48 +0200 Message-Id: <20190404084535.672303864@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190404084535.450029272@linuxfoundation.org> References: <20190404084535.450029272@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ 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;