03-02-2016, 02:04 AM
(This post was last modified: 03-02-2016, 02:05 AM by CrazyGuy108.
Edit Reason: removed unneccessary 'register' keyword
)
For the lazy, I found a decent sqrt algorithm online.
This one's in C++:
And this one, written in C:
There could be faster ones I'm not aware of though.
This one's in C++:
Code:
unsigned short isqrt(unsigned long a)
{
unsigned long rem = 0, root = 0;
for (int i = 0; i < 16; ++i)
{
root <<= 1;
rem = ((rem << 2) + (a >> 30));
a <<= 2;
++root;
if (root <= rem)
{
rem -= root;
++root;
}
else
{
--root;
}
}
return (unsigned short) root >> 1;
}
And this one, written in C:
Code:
uint32 // OR uint16 OR uint8
isqrt32 (uint32 n) // OR isqrt16 ( uint16 n ) OR isqrt8 ( uint8 n ) - respectively
// OR overloaded as isqrt (uint?? n) in C++
{
uint32 // OR register uint16 OR register uint8 - respectively
root, remainder, place;
root = 0;
remainder = n;
place = 0x40000000; // OR place = 0x4000; OR place = 0x40; - respectively
while (place > remainder)
place = place >> 2;
while (place)
{
if (remainder >= root + place)
{
remainder = remainder - root - place;
root = root + (place << 1);
}
root = root >> 1;
place = place >> 2;
}
return root;
}