Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1032696yba; Thu, 4 Apr 2019 02:53:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqxUJUl8vrH+FnVCsQ/6qs6DPy3Rc5Y2J1BRGJwGhiKKTZSgBInYyJn2mdAA5LW/yuLHRlFO X-Received: by 2002:a63:fc43:: with SMTP id r3mr4873045pgk.44.1554371618541; Thu, 04 Apr 2019 02:53:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554371618; cv=none; d=google.com; s=arc-20160816; b=z2VTXokUSrNrPE5x9NYpuirXpK3b1rjmbWDXVG9fsPMg4JWisCfhva9GcU0Oj8bGv3 mb/jnuYc8lYFmy5af3RI1/T07dMP2QNqByDSTe8McUuilE4GilBNEdN4rRl8lAjUCAaZ Q5n7Uw4nmZtS/CtwfXpV8VkyI1gLBtACKhqtBWqZHHcVGYngrdfCNma+oRuUXMXv15Ah 1BDD2bNK+VyOHyJpUaQzGiXpZv6GPSsOMzHwj0PYsq5ahFN0w8ZxFPzwepPHSyla8iBL RrfKYZlbrzI+0GNrA+/VF/+6fmeOpN7Kv/YblI3mNh5euDAXLvC+PVyJg7VuXuFFB7Uu aWtg== 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=noYU+bDtpTAcMXKVfFGIy8B6yxWXRa++zAVOZxZhP6Y=; b=jLFT8qTkkpXmvIzuLoJV/8ztVzF5W5I5GyCRk9sArfxqT/izvLvp4XD7PIIXkoEeWq VLk7SdGL9Avoq65P4XGJDIFMpRAkSY4jiOzh5YPHZvdgFNlO9IEZTKbfGX0pXUA51z7N i1a9V2AGtt/FtdZSv965yWaGAVz60Dik0hjpd73r+l/44YSoYytNO1dLlBxDA2yAW51P 56MVfQEgA2ph4PfD+GvFBZSQoRVwWG2QHbemCphutycKx0h9khBEEh/EXaF8I4UNkTfY b04J+Fo3UJVFrgZgvpa0PerFZ/PrgYf1+GDf5V4oqWxaPa+xZz4hhVxNPpAyUC04xcNt o3dQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=hmYT5bdx; 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 g1si8501251pgd.269.2019.04.04.02.53.23; Thu, 04 Apr 2019 02:53:38 -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=hmYT5bdx; 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 S1729922AbfDDIxy (ORCPT + 99 others); Thu, 4 Apr 2019 04:53:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:56286 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729902AbfDDIxt (ORCPT ); Thu, 4 Apr 2019 04:53:49 -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 9804B20882; Thu, 4 Apr 2019 08:53:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1554368029; bh=z0SVYqajzIJ45JNJ8em38ycgwdF6o85f81lFtuJQu7c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hmYT5bdxLPDNy57gmR5chpEnTWek3OzB76ox9ZbbLaIIrfzNqqRvFwCxSr2lVGsWV G3wTxImPmYpMgKQXTS0xcUAl+8ebOmWJdQyBPVWVl4f76DA3uaTZezhdE5sEgqwbbF GJdNwIEWDuL9t8vIBMGfwn05vfBDsgagccXl73gY= 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.14 003/121] lib/int_sqrt: optimize initial value compute Date: Thu, 4 Apr 2019 10:46:31 +0200 Message-Id: <20190404084545.439585397@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190404084545.245659903@linuxfoundation.org> References: <20190404084545.245659903@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.14-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 @@ -8,6 +8,7 @@ #include #include +#include /** * int_sqrt - rough approximation to sqrt @@ -22,10 +23,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;