| |
Voltage Account closed
Registered: Jul 2008 Posts: 14 |
Seriously Fast Multiply - Codebase
So I've used the Seriously Fast Multiplier routine from codebase ... http://codebase64.org/doku.php?id=base:seriously_fast_multiplic..
Works great and I mostly understand it. The unsigned multiplies I get, but the sign conversion process I don't understand.
; Apply sign (See C=Hacking16 for details).
lda T1
bpl :+
sec
lda PRODUCT+1
sbc T2
sta PRODUCT+1
:
lda T2
bpl :+
sec
lda PRODUCT+1
sbc T1
sta PRODUCT+1
:
I'm trying to do a 24bit (signed) x 8bit (signed) multiply.
I have the unsigned part working and would like to use the same method as about to sort out the signs (after the multiplication).
I can work around it by getting the ABS values before multiplying and then apply the sign after, but this seems wasteful when the above method doesn't apply ABS before the mult.
I read and re-read issue 16 of C=Hacking but can't find an explanation of how this works.
I also tried it out "on paper" and can see that it works for 8bit x 8bit and also for 16bit x 16bit, but I don't understand why it works. |
|
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
maybe the reference is incorrect, I remember this method is by stephen judd, and is explained in one of the C= hackings, try to look into issues around 16 looking for judd's articles. |
| |
j0x
Registered: Mar 2004 Posts: 215 |
A negative number, say -30, will be represented in two's complement as a=256-30
Multiply that by b (assumed to be positive):
(256-30)*b = 256*b - 30*b
What we want is "-30*b", so we need to subtract 256*b, which is exactly what the code does.
If b is also negative, we need to subtract 256*a from the result to correct for this. |
| |
Voltage Account closed
Registered: Jul 2008 Posts: 14 |
ok that makes sense. Thanks j0x, much appreciated. |
| |
JackAsser
Registered: Jun 2002 Posts: 2014 |
Quote: ok that makes sense. Thanks j0x, much appreciated.
Sorry for not elaborating the details of sign reconstruction in the article. :P I used that method extensively in the 3D-part of Andropolis. Great that j0x could fill in the details. I did the same experiments on paper to convince myself it actually works. :D |
| |
Voltage Account closed
Registered: Jul 2008 Posts: 14 |
Hey JackAsser, big fan of the 3d in Andropolis, very nice.
Thx for sharing the details on codebase.
I hope to have something nice to show you all by Syntax 2014... |