Re: A89: TI-GCC
[Prev][Next][Index][Thread]
Re: A89: TI-GCC
Here's one I wrote a week or so ago which I haven't tested, but I believe
works well. Will always return the square root of num rounded down to the
next lowest integer. If num is negative - ie, the root would be imaginary -
it returns zero. It's actually Niklas' algorithm, but I don't think he'd
recoginize it if he saw it after how I've compressed this code into a nice
little block =)
If anyone wants an explanation as to why this works, just ask now. I'd be
more than happy to explain, since I'm proud of how optimized this code is
(in other words: please, ask!)
This should be a lot faster than the algorithm below - assuming that this
works - especially on a 68k with slow shifting.
int sqroot(int num)
{
int cntr = -1, root = 0;
while((num-=(cntr+=2))>=0)
root++;
return root;
}
-Scott
> The only one I have seen is this:
>
> unsigned short sqrt(unsigned long a){
> unsigned long rem = 0;
> unsigned long root = 0;
> for(int i=0; i> 30));
> a <<= 2;
> root ++;
> if(root <= rem){
> rem -= root;
> root++;
> }
> else
> root<;
> }
> return (unsigned short)(root >> 1);
> }
Follow-Ups: