Changes

Jump to: navigation, search

Programming:Fast Square Root

683 bytes added, 00:11, 19 September 2007
Added optimised version (much much faster!)
done:
LD A,(Bsqr)
RET
</pre>
 
== Faster Again ==
 
Since there are only 15 possible results for the square root of an 8 bit number, it's really just a series of comparisons, optimised to use a binary comparison. This routine also leaves all other registers intact:
 
<pre>
CP 8 * 8
JR NC,ge8
CP 4 * 4
JR NC,ge4
CP 2 * 2
JR NC,ge2
OR A
RET Z
INC A
RET
ge2: CP 3 * 3
LD A, 3
SBC 0
RET
ge4: CP 6 * 6
JR NC,ge6
CP 5 * 5
LD A, 5
SBC 0
RET
ge6: CP 7 * 7
LD A, 7
SBC 0
RET
ge8: CP 12 * 12
JR NC,ge12
CP 10 * 10
JR NC,ge10
CP 9 * 9
LD A,9
SBC 0
RET
ge10: CP 11 * 11
LD A,11
SBC 0
RET
ge12: CP 14 * 14
JR NC,ge14
CP 13 * 13
LD A,13
SBC 0
RET
ge14: CP 15 * 15
LD A,15
SBC 0
RET
</pre>
[[Category:Programming]]
151
edits