Skip to content

Commit

Permalink
Optimized Integer SQRT. for upto 3x faster operation
Browse files Browse the repository at this point in the history
(Bold clain, didn't benchmark it tho)
  • Loading branch information
engstk authored and polesapart committed Jul 2, 2020
1 parent 36211e4 commit adb9a2f
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/int_sqrt.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,34 @@
*
* A very rough approximation to the sqrt() function.
*/
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 << (BITS_PER_LONG - 2);
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);

0 comments on commit adb9a2f

Please sign in to comment.