# 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.6799074,"math_prob":0.9961074,"size":1453,"snap":"2020-10-2020-16","text_gpt3_token_len":519,"char_repetition_ratio":0.14216702,"word_repetition_ratio":0.25333333,"special_character_ratio":0.39366826,"punctuation_ratio":0.17902814,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99385333,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-18T02:58:43Z\",\"WARC-Record-ID\":\"<urn:uuid:6680eb43-6d37-4789-bacc-504033f2e3ee>\",\"Content-Length\":\"20415\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:41eebf75-c5a9-4649-a93a-59423f51bc95>\",\"WARC-Concurrent-To\":\"<urn:uuid:f6b2bc67-17f3-46a6-89f6-5814ed132b93>\",\"WARC-IP-Address\":\"142.4.215.89\",\"WARC-Target-URI\":\"http://www.cpcwiki.eu/index.php?title=Talk:Programming:Integer_Multiplication&oldid=16251\",\"WARC-Payload-Digest\":\"sha1:YQFTRVIP6COCFMSHSDETZDXMHNUPCCT5\",\"WARC-Block-Digest\":\"sha1:VCMXPMYRBIDY2L2HODPOEIEIO3KFGJF2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875143505.60_warc_CC-MAIN-20200218025323-20200218055323-00215.warc.gz\"}"}