Talk:Programming:Integer Multiplication

From CPCWiki - THE Amstrad CPC encyclopedia!
Revision as of 19:57, 6 May 2007 by Executioner (Talk | contribs)

Jump to: navigation, search

The FastMult routine using logs can be optimised somewhat from it's original by removing the slow SET instructions and extra direct addressing operation:

FastMult:
  ld      l,c
  ld      h,&82
  ld      d,(hl)          ; d = 32 * log_2(c)
  
  ld      l,b
  ld      a,(hl)          ; a = 32 * log_2(b)
  
  add     a,d
  ld      l,a
  ld      a,0
  adc     a,0
  ld      h,a             ; hl = d + a
  
  add     hl,hl
  set     2,h             ; hl = hl + $0400
  set     7,h             ; hl = hl + &8000
  
  ld      e,(hl)
  inc     hl
  ld      d,(hl)          ; de = 2^((hl)/32)
  
  ret

to

FastMult:
  ld      l,c
  ld      h,&82
  ld      d,(hl)          ; d = 32 * log_2(c)
  
  ld      l,b
  ld      a,(hl)          ; a = 32 * log_2(b)
  
  add     a,d
  ld      l,a
  adc     &42
  sub l
  ld      h,a             ; hl = d + a
  
  add     hl,hl
  
  ld      e,(hl)
  inc     l
  ld      d,(hl)          ; de = 2^((hl)/32)
  
  ret

The add hl,hl can also be removed if the tables are split with low-byte in one page and high-byte in the other and changing the &42 to &84. Since there are 512 entries in the anti-log table, you'd need two INC L's.