Optimized Integer SQRT. for upto 3x faster operation
This commit is contained in:
parent
e0f839a996
commit
f8f036ffe5
1 changed files with 20 additions and 12 deletions
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue