Optimized Integer SQRT. for upto 3x faster operation

This commit is contained in:
engstk 2017-11-26 16:32:49 +01:00 committed by Ksawlii
parent e0f839a996
commit f8f036ffe5

View file

@ -16,26 +16,34 @@
*
* Computes: floor(sqrt(x))
*/
unsigned long int_sqrt(unsigned long x)
inline unsigned long int_sqrt(unsigned long x)
{
unsigned long b, m, y = 0;
register unsigned long tmp;
register unsigned long place;
register unsigned long root = 0;
if (x <= 1)
return x;
m = 1UL << (__fls(x) & ~1UL);
while (m != 0) {
b = y + m;
y >>= 1;
place = 1UL << (BITS_PER_LONG - 2);
if (x >= b) {
x -= b;
y += m;
do{
place >>= 2;
}while(place > x);
do {
tmp = root + place;
root >>= 1;
if (x >= tmp)
{
x -= tmp;
root += place;
}
m >>= 2;
}
place >>= 2;
}while (place != 0);
return y;
return root;
}
EXPORT_SYMBOL(int_sqrt);