Difference between revisions of "Fast signed multiply"
From Nesdev wiki
Jump to navigationJump to search (Created the page) 
m (1 revision: Rest of pages not related to reference) 
(No difference)

Revision as of 04:22, 13 June 2009
The following code multiplies two 8bit signed integers, of range (64..+64). The output is a signed 16bit result, in range (4096..4096). It uses a 256 byte lokup table, but is quite fast.
;Fast table driven signed multiply routine ;It multiplies Y with A, and relies on the mathematical fact (a+b)^2(ab)^2 = 4*a*b ;Also both inputs and outputs are SIGNED and are never supposed to be outside of the range (64; +64) ;Return with YA = 4*A*Y (result can be shifted as necessary) Multiply sty Temp pha clc adc Temp ;Add two multiplicands together bpl + eor #$ff clc adc #$01 ;If result is negative, force it to be positive + asl A tax ;The square of a negative number is equal to its pos counterpart anyways pla sec sbc Temp ;Compute difference of two multiplicands bpl + eor #$ff ;Again, force the result to be positive clc adc #$01 + asl A tay lda SquareTbl,X sec sbc SquareTbl,Y ;Compute (a+y)^2(ay)^2 pha lda SquareTbl+1,X sbc SquareTbl+1,Y tay pla rts ;This is a square table for the multiply routine ;Note that this macro is for WLADX, but can be adapted ;For most assemblers SquareTbl .def i = 0 .rept 128 .dw i^2 ;Create a square table with 128 word entries .redef i i+1 .endr .ends