mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] lib/int_sqrt.c: optimize for small argument values
@ 2017-10-19 20:31 Michael Davidson
  2017-10-19 20:42 ` Kees Cook
  2017-10-20  6:13 ` Joe Perches
  0 siblings, 2 replies; 7+ messages in thread
From: Michael Davidson @ 2017-10-19 20:31 UTC (permalink / raw)
  To: Andrew Morton, Ingo Molnar, David Miller, Matthew Wilcox, Kees Cook
  Cc: linux-kernel, Michael Davidson

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-10-20 16:22 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-19 20:31 [PATCH] lib/int_sqrt.c: optimize for small argument values Michael Davidson
2017-10-19 20:42 ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome