Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Times routine. Just checkin if this is worth sharing. :)
2012-01-27 17:19
terric
Account closed

Registered: Feb 2009
Posts: 47
Times routine. Just checkin if this is worth sharing. :)

I lately came back to an thought how to multiply two 8bit decimal only number.
This routine basically multiply 0.5 x 0.5 and gives the result of 0.25 in product.

First let me introduce you to a table generator that creates a table at
$6000 to $67ff.

ldx #$00
-
txa
lsr a
sta $6000,x
lsr a
sta $6100,x
lsr a
sta $6200,x
lsr a
sta $6300,x
lsr a
sta $6400,x
lsr a
sta $6500,x
lsr a
sta $6600,x
lsr a
sta $6700,x
inx
bne -
Obviously it takes some mem, what is the gain??? More constant time.
Now if i use a byte of %11000000 that would represent a 0.75, got my format ?
And multiply it width %01000000 (that's 0.25).
If we make this on a calculator 0.75 x 0.25, it would equal 0.1875(%00110000) lda #$00
ldx multiplier ; %01000000 0.25

lsr multiplicand ; %01100000 0->c
bcc +
clc
adc $6700,x
+
lsr multiplicand ; %00110000 0->c
bcc +
clc
adc $6600,x
+
lsr multiplicand ; %00011000 0->c
bcc +
clc
adc $6500,x
+
lsr multiplicand ; %00001100 0->c
bcc +
adc $6400,x
+
lsr multiplicand ; %00000110 0->c
bcc +
clc
adc $6300,x
+
lsr multiplicand ; %00000011 0->c
bcc +
clc
adc $6200,x
+
lsr multiplicand ; %00000001 1->c
bcc +
clc
adc $6100,x ; add 0.25 shifted right 2 times
+
lsr multiplicand ; %00000000 1->c
bcc +
clc
adc $6000,x ; add 0.25 shifted right 1 time
+

and now in the accu we have %00110000 Just what we want.
Maybe i could turn it other way around to undo some branches.
If you manage to optimize or want to say something about it, please do!

/Terric




2012-01-27 17:33
terric
Account closed

Registered: Feb 2009
Posts: 47
Now i am thinking of throwing the table away. :)

Obviously one can

asl multiplicand
bcc nextbit
lsr multiplier
clc
adc multiplier
nextbit

:)
2012-01-27 18:49
JackAsser

Registered: Jun 2002
Posts: 2014
Nice. But this is "just" the shift+add algorithm. Check out codebase for much faster algorithms. Your last exemple though is Nice when memory is sparse.
2012-01-28 07:51
terric
Account closed

Registered: Feb 2009
Posts: 47
@jackasser
I saw the seriously fast multiplication routine. I adapted it for my need, but still a little unsure that i ever could write one on my own.
And that is probably because of that i not fully understand it. :)

So still the 0.75x 0.25 scenario. 8bit dec x 8bit dec = 8bit dec
And after the adaption it looked like this, and works like a charm.
ldx T2
lda T1
sta sm1+1
eor #$ff
sta sm2+1


sec
sm1 lda $6800,x
sm2 sbc $6a00,x
sta answer

*= $6800
.for i=0,i<512,i=i+1
.byte <(i*i/1024)
.next
*= $6a00
.for i=0,i<512,i=i+1
.byte <((i-255)*(i-255)/1024)
.next


2012-01-28 10:07
JackAsser

Registered: Jun 2002
Posts: 2014
Yup Nice! The theory behind is very simple. I'll explain it someday... ;) regarding the tables, use the generator from the article to save disk space and loading times.
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Matt
Nordischsound/Hokuto..
Guests online: 97
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Triad  (9.3)
5 Censor Design  (9.3)
Top Webmasters
1 Slaygon  (9.6)
2 Perff  (9.6)
3 Sabbi  (9.5)
4 Morpheus  (9.4)
5 CreaMD  (9.1)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.041 sec.