Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1150443yba; Wed, 24 Apr 2019 16:12:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqwI2HFHE96uwKOk2N7V4G84/FnhGIjF21PkedsQoAQKWCQR5rcLFnarbvN9M+p7OXG4eUvn X-Received: by 2002:a65:4185:: with SMTP id a5mr33118245pgq.82.1556147562459; Wed, 24 Apr 2019 16:12:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556147562; cv=none; d=google.com; s=arc-20160816; b=emy1Yi7nrbxlPkwUm2lciy9JQJEKLjb034k9kll4JFOsnt9SzNL1ZEHhZL673LQSrc ksFJOOtypDoDWmu8wCYrs/wbDNI57XJRe+nDVeiqKwB6xvKFD4oLpy0yuw/WRtTWNqN+ kdM+AEX1Kxs+LZVNFTcsbwdCUea9JEC5d1Lc/PzsAjpNvpJKQtcFZjv36clyIF6qEhGn kIXWtcpet1H5BLEJjb1tOv5cUnsrZYE8F+APHzJ428yjbrzAV5xg2sr8nHYfZZawBZvg QSE1vq2lUZfcapQOYpcwcAZAi0XFHebzJII1PmFe7Bgpsrrx+sYs6T1FKEVw6cbR2APl buag== 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=L/ygDObnjsycwhaK/m60BWijGOcEhx5jzxjiMIsy5XQ=; b=VQ7r4LfKhdgDrTwkmjdGX21SYvYHQSKRjMtXCUhX1hXsDYjCKGsAR4/JCnLwAQwUCE N8CVFLvrhm/aGJ8VxIKAkEAFyjL++9UcNqF9RmmeTt9ui/D5l4cI5i5G7RARWWcx0Uwe 1TBijxFkojse9OGYum6vklb5usUOq/AghEa32B/S0/4IyLbvVFDxAXlGfDBcTe9MJwFt HCSU4G7dFVDGKReHNrqyxp9J5oNsc9B/lSQgOihajxRoDfcSvD+PvMUt5YuVJnn8XAs0 143+BfJmasDrQdK18TlhgILbbpgIIoqDK2W+MJnIGliaeRYOA8yD71aQJl1YyR78Qtpo whlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Ksyz7dPn; 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 k1si20323225pfj.188.2019.04.24.16.12.27; Wed, 24 Apr 2019 16:12:42 -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=Ksyz7dPn; 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 S2388614AbfDXRRX (ORCPT + 99 others); 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;