4 ms·
Just for fun... I think this should be on the efficient frontier of 8 x 8 -> 16 bit multipliers, at an average cost of around 43 cycles and memory of 1330 byt
by dzdt 3y ago
Just for fun... I think this should be on the efficient frontier of 8 x 8 -> 16 bit multipliers, at an average cost of around 43 cycles and memory of 1330 bytes or so. Compare 45.5 cycles and 1580 bytes for the mult66.a fastest in the collection linked.
lmul0 = $06 ; pointer into square table low
lmul1 = $08 ; pointer into square table high
labs0 = $0a ; pointer into table of abs(i-1)
prod_low = $0c
\* = $0200
; Align tables to start of page
abstable
!for i, 0, 255 {
!byte <(abs(i-1))
}
squaretable1_lsb
!for i, 0, 511 {
!byte <((i*i)/4)
}
squaretable1_msb
!for i, 0, 511 {
!byte >((i*i)/4)
}
; multiply
;
; f(x) = x*x/4
;
; return f(X+Y) - f(|Y-X|)
;
; 8 bit x 8bit unsigned multiply, 16 bit result
;
; On Entry:
; A: multiplier
; Y: multiplicand
; On Exit:
; (prod_low, A): product
mult
sta lmul0
sta lmul1
eor #ff
sta labs0
ldx (labs0),Y ;X = abs(Y-A)
lda (lmul0),Y
sec
sbc squaretable1_lsb,X
sta prod_low
lda (lmul1),Y
sbc squaretable1_msb,X
rts
;call this once to initialise high bytes of pointers to table
mult_init
lda #>abstable
sta labs0+1
lda #>squaretable1_lsb
; high byte (#2 in this instance)
sta lmul0+1
lda #>squaretable1_msb
; high byte (#4 in this instance)
sta lmul1+1
rts
- djmips 3y agoNice. Bonus is that it doesn't use self modifying code and another plus one for using ldx(labs0),Y - Looks like this one can also be optimized for repeated multiplication by the same factor by skipping the first 4 instructions. Did you write this one?
- dzdt 3y agoOh crap. The LDX (labs0),Y instruction doesn't actually exist! If you are cool with undocumented opcodes you can replace that with LAX (labs0),Y which does exist. Otherwise it would need another 2 cycles for TAX. Yes I wrote, based on mult65.a credited to Nick Jameson. That is one of the better ones; I took a look and thought about how it could be sped up a little.
- djmips 3y agoHaha, just like chatgpt - that's the same mistakes it makes