[Prev][Next][Index][Thread]
Re: A85: 16-bit integer Square Root
On Thu, 12 Mar 1998, Jarno Rajala wrote:
>
> The GUESS=(GUESS+X/GUESS)/2 algorithm works fine on floating points, but
> is no good for intergers. I used a binary search kind of method. It
> calculates square root of an integer and rounds it down.
> #include "ti-85.h"
>
> VAR1 = TEXT_MEM
>
> .org
> .db "SQRT by Jarno Rajala",0
>
> ld hl,0
...
>
> .end
I finished the 16-bit version of my sqrt algorithym. I'm not sure how
fast Jarno's Sqrt is but I can say that my Sqrt is smaller an instead of
rounding down it rounds to the nearest integer.
Speedwise I tested the worst case and my calc on 8 contrast did 256 worst
case sqrt16s in about .7 seconds.
The worst case by the way is sqrt of 1.
Anyways here it is:
Sqrt16:
push de
push bc
ld de,0
ld bc,0
call CP_HL_DE
jr z,Sqrt16Done ;0 is a special case.
ld de,65280
ld bc,256
sbc hl,de
jr nc,Sqrt16done
dec bc ;bc=255
scf
rr h
rr l ;shift hl
call Sqrt16loop
Sqrt16done:
ld h,b
ld l,c
pop bc
pop de
ret
Sqrt16loop:
add hl,bc
ret c
dec c
jp Sqrt16loop
That's it.
If anyone tested the other algorithym see which is faster.
-Humberto Yeverino Jr.
"Small potatos make the steak look bigger."
http://www.engr.csufresno.edu/~humberto/Home.html
humberto@engr.csufresno.edu
<font size=3>For browsers:<br>
<a href="http://www.engr.csufresno.edu/~humberto/Home.html">
Have a look.</a></font><br>
Follow-Ups:
References: