Re: [PATCH] lib/int_sqrt.c: Optimize square root function

From: Linus Torvalds
Date: Mon Feb 02 2015 - 14:13:52 EST


On Mon, Feb 2, 2015 at 11:00 AM, Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> (I'm also not entirely sure what uses int_sqrt() that ends up being so
> performance-critical, so it would be good to document that too, since
> that probably also matters for the "what's the normal argument range"
> question..)

... it's also not entirely clear that we need a whole new loop. We
might just instead start off with a better guess for 'm' using some
calculation that might be doable with a single conditional move
instruction instead of a loop. Because I suspect that the inevitable
branch misprediction of a new loop is likely as expensive as a few
iterations through the core one.

IOW, instead of

m = 1UL << (BITS_PER_LONG - 2);

perhaps something like

m = 1UL << (BITS_PER_LONG/2- 2);
if (m < x)
m <<= BITS_PER_LONG/2;

(assuming gcc can change that code into a "cmov") might cut down the
"lots of empty loops" case in half for small values of 'x'.

There's probably some other better cheap initial guess value estimator.

Linus
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/