# Talk:Programming:Integer Multiplication

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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)

ld      l,a
ld      a,0
ld      h,a             ; hl = d + a

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)

ld      l,a
sub l
ld      h,a             ; hl = d + a

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.

So the more optimal way would look like this:
```FastMult:
ld      l,c
ld      h,logtab/256
ld      a,(hl)          ; a = 32 * log_2(c)
ld      l,b
add     (hl)            ; cf,a = 32 * log_2(c) + 32 * log_2(b)
ld      l,a
ld      a,h