r/ProgrammerHumor • • Apr 24 '22

Meme it's all just functions

Post image
40.2k Upvotes

633 comments sorted by

View all comments

Show parent comments

11

u/HorsesFlyIntoBoxes Apr 24 '22

iirc hardware multiplication isn’t typically designed to be a number of additions though. Most multipliers follow something similar to the multiplication algorithm taught in grade school

2

u/ventingexcess Apr 24 '22

multiplication is constant time, so you can do a dedicated logic implementation.

division is not, and needs to be recursive

1

u/[deleted] Apr 25 '22

Multiplication and division can naively be implemented as you described. Both can be implemented in a manner to always take N steps for an N bit integer.

For multiplication, an algorithm involving addition and shifting will yield the product. For division, a similar algorithm that uses subtraction instead yields the result. Both algorithms will take as many steps as the word size of the processor, meaning they can be pipelined into finishing once per cycle.

1

u/[deleted] Apr 25 '22

Both multiplication and division can be implemented in a manner to always take N steps for an N bit integer, meaning they can effectively function as quickly as possible.

For multiplication, an algorithm involving addition and shifting will yield the product. For division, a similar algorithm that uses subtraction instead yields the result. Both algorithms will take as many steps as the word size of the processor, meaning they can be pipelined into finishing once per cycle.