From: Michael Davidson <md@google.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Ingo Molnar <mingo@kernel.org>,
David Miller <davem@davemloft.net>,
Matthew Wilcox <mawilcox@microsoft.com>,
Kees Cook <keescook@chromium.org>
Cc: linux-kernel@vger.kernel.org, Michael Davidson <md@google.com>
Subject: [PATCH] lib/int_sqrt.c: optimize for small argument values
Date: Thu, 19 Oct 2017 13:31:37 -0700 [thread overview]
Message-ID: <20171019203137.131042-1-md@google.com> (raw)
int_sqrt() currently takes approximately constant time
regardless of the value of the argument. By using the
magnitude of the operand to set the initial conditions
for the calculation the cost becomes proportional to
log2 of the argument with the worst case behavior not
being measurably slower than it currently is.
Signed-off-by: Michael Davidson <md@google.com>
---
lib/int_sqrt.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/int_sqrt.c b/lib/int_sqrt.c
index 1ef4cc344977..8394b0dcecd4 100644
--- a/lib/int_sqrt.c
+++ b/lib/int_sqrt.c
@@ -21,7 +21,7 @@ unsigned long int_sqrt(unsigned long x)
if (x <= 1)
return x;
- m = 1UL << (BITS_PER_LONG - 2);
+ m = 1UL << (__fls(x) & ~1UL);
while (m != 0) {
b = y + m;
y >>= 1;
--
2.15.0.rc1.287.g2b38de12cc-goog
next reply other threads:[~2017-10-19 20:32 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-19 20:31 Michael Davidson [this message]
2017-10-19 20:42 ` [PATCH] lib/int_sqrt.c: optimize for small argument values Kees Cook
2017-10-19 20:56 ` Jason A. Donenfeld
2017-10-19 21:04 ` Kees Cook
2017-10-20 6:13 ` Joe Perches
2017-10-20 14:18 ` Kees Cook
2017-10-20 16:22 ` Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171019203137.131042-1-md@google.com \
--to=md@google.com \
--cc=akpm@linux-foundation.org \
--cc=davem@davemloft.net \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mawilcox@microsoft.com \
--cc=mingo@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.