Changes

Jump to: navigation, search

Programming:Precalculated square

903 bytes added, 23:37, 18 September 2007
Added optimised version
SQTAB DS 512 ;space for the table
</pre>
 
== Performance Improvements ==
 
This routine can be improved somewhat by using a page-aligned table with separate pages for LSB and MSB of the result. In order to do this, the initialisation routine needs to be:
 
<pre>
INITSQ LD DE, 1 ;1st odd number
LD HL, 0 ;HL = 1st square number
LD B, H ;counter = 256
LD IX, SQTAB ;startaddress of the square table
SQLOOP LD (IX + 0), L ;Lowbyte to table
INC HX
LD (IX + 0), H ;Highbyte to table
DEC HX
INC LX
ADD HL, DE ;add odd number
INC DE ;next odd number
INC DE
DJNZ SQLOOP ;256 times
RET
</pre>
 
And the routine to get the square can now be:
 
<pre>
GETSQ LD L, A
LD H, SQTAB / 256 ;HL = pointer to LSB
LD E, (HL) ;E = Lowbyte of the result
INC H
LD D, (HL) ;D = Highbyte of the result
RET
</pre>
 
This is now a very small routine which could easily be defined as a macro for optimal performance (without the RET).
 
[[Category:Programming]]
151
edits