What's the best (fastest) multiplication/division code?
Moderator: Moderators
What's the best (fastest) multiplication/division code?
I need to multiply/divide numbers by arbitrary values so bitwise ops aren't useful here as they only allow for multiplication/division by very specific values.
I know I'll have to do it in software, but I don't feel like reinventing the wheel. Anyone has good, fast code that would allow me to do both? Preferably something I can just jsr into.
I know I'll have to do it in software, but I don't feel like reinventing the wheel. Anyone has good, fast code that would allow me to do both? Preferably something I can just jsr into.
 rainwarrior
 Posts: 7824
 Joined: Sun Jan 22, 2012 12:03 pm
 Location: Canada
 Contact:
Re: What's the best (fastest) multiplication/division code?
Bregalad shared a nice multiply here:
Wiki: 8bit Multiply
CC65 has some multiply/divide routines: (see umul/udiv/imul/idiv)
CC65 GitHub runtime module
Keldon recently shared a multiply using a lookup table:
Forum: Relatively fast multi, ...
In general, you can make it faster with lookup tables, but they take up space.
There's unfortunately a lot of different needs for multiplication (e.g. 8 bit x 8 bit = 16 bit? 16 x 16 = 32? signed/unsigned? remainder or fixed point?) so it might be tricky to find a "generic" one that does exactly what you want efficiently.
If you want to try writing your own, the simple method isn't really that tricky. If you know how to do long multiplication / long division by hand, it's basically just this technique in binary (i.e. for multiply it's a left shift x2 for each row instead of x10, but you still just add up the rows in the end).
Finally, there's an even simpler version where you can implement multiply as just a repeated add, or divide as counting a repeated subtract. This is slow and inefficient in the general case, but if your numbers are small or you're in a situation where the result is cumulatively maintained, they can be pretty effective anyway.
Wiki: 8bit Multiply
CC65 has some multiply/divide routines: (see umul/udiv/imul/idiv)
CC65 GitHub runtime module
Keldon recently shared a multiply using a lookup table:
Forum: Relatively fast multi, ...
In general, you can make it faster with lookup tables, but they take up space.
There's unfortunately a lot of different needs for multiplication (e.g. 8 bit x 8 bit = 16 bit? 16 x 16 = 32? signed/unsigned? remainder or fixed point?) so it might be tricky to find a "generic" one that does exactly what you want efficiently.
If you want to try writing your own, the simple method isn't really that tricky. If you know how to do long multiplication / long division by hand, it's basically just this technique in binary (i.e. for multiply it's a left shift x2 for each row instead of x10, but you still just add up the rows in the end).
Finally, there's an even simpler version where you can implement multiply as just a repeated add, or divide as counting a repeated subtract. This is slow and inefficient in the general case, but if your numbers are small or you're in a situation where the result is cumulatively maintained, they can be pretty effective anyway.
Re: What's the best (fastest) multiplication/division code?
http://6502org.wikidot.com/softwaremathintmul
http://6502org.wikidot.com/softwaremathintdiv
If you absolutely know you need multiplication of a factor of 2, bit shift is much faster.
I've used a table of answers before, for example, if I always need to multiply by 10.
LDX value
LDA table, x
http://6502org.wikidot.com/softwaremathintdiv
If you absolutely know you need multiplication of a factor of 2, bit shift is much faster.
I've used a table of answers before, for example, if I always need to multiply by 10.
LDX value
LDA table, x
nesdoug.com  blog/tutorial on programming for the NES
Re: What's the best (fastest) multiplication/division code?
I've been using versions of this for multiplication, and have avoided division like the plague, but the page where that comes from has a few suggestions for division as well. If I need to divide by a constant, I multiply by the reciprocal of that constant instead, which's faster.
May I ask why do you need multiplication and division in a 6502 program? Most games can get away without them, even when more complex physics is involved. Sometimes you can make do with approximations, so you can use smaller lookup tables and faster code. The only reason I'm using multiplication is because I'm working on a raycaster, which involves trigonometry, but even then I try to use lookup tables as much as possible to avoid/simplify the realtime math.
May I ask why do you need multiplication and division in a 6502 program? Most games can get away without them, even when more complex physics is involved. Sometimes you can make do with approximations, so you can use smaller lookup tables and faster code. The only reason I'm using multiplication is because I'm working on a raycaster, which involves trigonometry, but even then I try to use lookup tables as much as possible to avoid/simplify the realtime math.
Re: What's the best (fastest) multiplication/division code?
Because I want my game to have score multipliers/score dividers.
 GradualGames
 Posts: 1107
 Joined: Sun Nov 09, 2008 9:18 pm
 Location: Pennsylvania, USA
 Contact:
Re: What's the best (fastest) multiplication/division code?
Hmm, if you're just updating a score, you could spread repeated addition or subtraction over multiple frames perhaps, if you don't want to go the lookup table route. That's assuming you have some kind of action game. If it's a puzzle game or rpg where speed is not at a premium I'd probably just use repeated addition/subtraction (all at once rather than spread out)
Re: What's the best (fastest) multiplication/division code?
So I guess that speed isn't such a big concern then, since you'll most likely not be doing several of these operations every frame. In this case, I recommend straightforward shiftandadd and shiftandsubtract routines, which are more than efficient enough to be used every once in a while, instead of large lookup tables and obscure logic.
 rainwarrior
 Posts: 7824
 Joined: Sun Jan 22, 2012 12:03 pm
 Location: Canada
 Contact:
Re: What's the best (fastest) multiplication/division code?
I already linked Wiki: 8bit Multiply but tepples just added an even nicer variation to it.
Re: What's the best (fastest) multiplication/division code?
If you add DEY to the start of the subroutine you can drop the CLC inside the loop. Saves <=14 cycles.rainwarrior wrote:I already linked Wiki: 8bit Multiply but tepples just added an even nicer variation to it.
I think.
Also, unrolling that loop saves like ~40 cycles at the cost of ~50 bytes. Well worth it, IMO. 50 bytes is nothing.
 rainwarrior
 Posts: 7824
 Joined: Sun Jan 22, 2012 12:03 pm
 Location: Canada
 Contact:
Re: What's the best (fastest) multiplication/division code?
I think that's valid, yeah.pubby wrote:If you add DEY to the start of the subroutine you can drop the CLC inside the loop. Saves <=14 cycles.
It would probably need a check for 0 before the DEY, which I guess would add 6 more cycles, so the difference is from 4 to 10 cycles? Though you also get a very quick Y=0 case in the bargain.
Edit: or wait, never mind, 255 + carry is going to be 0 anyway. I guess you don't actually need to check at all. So, 0 to 14 cycles, yeah.
I guess cc65's umul8x8r16 is actually the same as tepples' algorithm, just with slightly less parsimonious return value storage:
https://github.com/cc65/cc65/blob/maste ... ul8x8r16.s
Last edited by rainwarrior on Tue Oct 24, 2017 6:47 pm, edited 2 times in total.
Re: What's the best (fastest) multiplication/division code?
Haven't checked if it's better than the ones on the wiki, but here's another player from atariage: http://atariage.com/forums/topic/71120 ... /?p=896028
Not a new method, just different code.
Not a new method, just different code.
 rainwarrior
 Posts: 7824
 Joined: Sun Jan 22, 2012 12:03 pm
 Location: Canada
 Contact:
Re: What's the best (fastest) multiplication/division code?
It's equivalent to tepples' method with the (nonunrolling) optimization pubby suggested. Also it doesn't bother to initialize A and treats it as an extra "add" input.Kasumi wrote:Haven't checked if it's better than the ones on the wiki, but here's another player from atariage: http://atariage.com/forums/topic/71120 ... /?p=896028
Not a new method, just different code.
Re: What's the best (fastest) multiplication/division code?
prodlo will remain zero, but register A will end up nonzero. So checking for Y=0 is still necessary.rainwarrior wrote:Edit: or wait, never mind, 255 + carry is going to be 0 anyway. I guess you don't actually need to check at all. So, 0 to 14 cycles, yeah.
 FrankenGraphics
 Formerly WheelInventor
 Posts: 2032
 Joined: Thu Apr 14, 2016 2:55 am
 Location: Gothenburg, Sweden
 Contact:
Re: What's the best (fastest) multiplication/division code?
As long as something's to be multiplied/divided by a power of 2, doing this is the simplest way:
Then jsr label
You can avoid the addtional 12 cycles that jsr and rts cost together by hardcoding it into your specific
function. If you can figure out a siginificant var or const to be a value so that the operation requires even fewer lsr/asl:s to get the desired value; even better.
Several integer and floating point math examples can be found at http://6502.org/source/  i haven't looked them up but came across it when i was looking for a reference on how many cycles the above would take.
Code: Select all
mult32:
asl
mult16:
asl
mult8:
asl
mult4:
asl
asl
rts
div32:
lsr
div16:
lsr
div8:
lsr
div4:
lsr
lsr
rts
You can avoid the addtional 12 cycles that jsr and rts cost together by hardcoding it into your specific
function. If you can figure out a siginificant var or const to be a value so that the operation requires even fewer lsr/asl:s to get the desired value; even better.
Several integer and floating point math examples can be found at http://6502.org/source/  i haven't looked them up but came across it when i was looking for a reference on how many cycles the above would take.
http://www.frankengraphics.com  personal NES blog

 Posts: 6
 Joined: Wed Oct 18, 2017 1:33 pm
Re: What's the best (fastest) multiplication/division code?
I made an interesting 8*8=8bit shiftadd multiply routine in 15 bytes (over in Commodore 64 land). It shrinks down to 11 bytes if you initialize .A yourself, for the added portion of a multiplyaccumulate.
The "interesting" part is that It exits early whenever the rightshifting multiplier reaches zero, which is fundamentally faster than performing all the iterations for the bit width if one of your values is small. I also extended it to 8*8=16bit which can also function as 16*8=16bit, still performing the early exit. There's plenty of other multiplication & division algorithms under the math section in there as well, though I think all the other primary mult techniques have been covered here so far.
The "interesting" part is that It exits early whenever the rightshifting multiplier reaches zero, which is fundamentally faster than performing all the iterations for the bit width if one of your values is small. I also extended it to 8*8=16bit which can also function as 16*8=16bit, still performing the early exit. There's plenty of other multiplication & division algorithms under the math section in there as well, though I think all the other primary mult techniques have been covered here so far.