Re: [PATCH] Optimize int_sqrt for small values for faster idle
From: Thomas Rohwer
Date: Sun Jan 31 2016 - 02:28:37 EST
Hello,
> - m = 1UL << (BITS_PER_LONG - 2);
> + if (x <= 0xffff) {
> + if (m <= 0xff)
> + m = 1UL << (8 - 2);
> + else
> + m = 1UL << (16 - 2);
> + } else if (x <= 0xffffffff)
> + m = 1UL << (32 - 2);
> + else
> + m = 1UL << (BITS_PER_LONG - 2);
> while (m != 0) {
> b = y + m;
> y >>= 1;
>
I think, m can be initialized with
1 << (greatest multiple of 2 less than or equal to (position of most significant bit of x))
i.e. 1 << ((position of most significant bit of x) & 62)
without changing the outcome of the original algorithm (as long as x<m the loop does just m >>= 2).
I believe, that for (position of most significant bit of x) there is an efficient macro, and
some processors directly have an instruction for it. So this would probably be faster than your suggestion
for an initial starting value and give an even better starting value (cutting in some cases further on the number of
while loop interations).
If one just wants to achieve a result with a certain relative error in terms of the fraction of the input, one can
probably only look at the most significant bit and a few following bits of x.
Sincerely,
Thomas