Changes

Jump to: navigation, search

Programming:Fast 16 bit Square Root

1,470 bytes added, 05:20, 19 September 2007
New page
This routine was written by [[User:Executioner|The Executioner]] based on the fast pre-calculated square routine and an algorithm by Milos "baze" Bazelides, with 16 bit arithmetic removed for faster performance.

The initialisation routine, used to build the squares table (same as for the Pre-calculated Square table (my version)).

<pre>
ld de,1
ld hl,0
ld ix,sqtab
sqloop: ld (ix + 0),l
inc hx
ld (ix + 0),h
dec hx
add hl,de
inc de
inc de
inc lx
jr nz,sqloop
</pre>

The actual 16 bit square root routine.

'''Input:''' DE = ''The number you want to find the square root of''

'''Output:''' A = ''Square root of the number supplied in DE''

'''Destroys:''' HL

'''Call:''' CALL SqrtDE

<pre>
SqrtDE: ld hl,sqtab + #180
ld a,d
cp (hl)
jr nz,$ + 6
dec h
ld a,e
cp (hl)
inc h
set 6,l
jr nc,$ + 4
res 7,l

ld a,d
cp (hl)
jr nz,$ + 6
dec h
ld a,e
cp (hl)
inc h
set 5,l
jr nc,$ + 4
res 6,l

ld a,d
cp (hl)
jr nz,$ + 6
dec h
ld a,e
cp (hl)
inc h
set 4,l
jr nc,$ + 4
res 5,l

ld a,d
cp (hl)
jr nz,$ + 6
dec h
ld a,e
cp (hl)
inc h
set 3,l
jr nc,$ + 4
res 4,l

ld a,d
cp (hl)
jr nz,$ + 6
dec h
ld a,e
cp (hl)
inc h
set 2,l
jr nc,$ + 4
res 3,l

ld a,d
cp (hl)
jr nz,$ + 6
dec h
ld a,e
cp (hl)
inc h
set 1,l
jr nc,$ + 4
res 2,l

ld a,d
cp (hl)
jr nz,$ + 6
dec h
ld a,e
cp (hl)
inc h
inc l
jr nc,$ + 4
res 1,l

ld a,d
cp (hl)
jr nz,$ + 6
dec h
ld a,e
cp (hl)
ld a,l
ret nc
dec a
ret
</pre>
151
edits