Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multiplication and Division

Similar presentations


Presentation on theme: "Multiplication and Division"— Presentation transcript:

1 Multiplication and Division
CS/COE 0447 Jarrett Billingsley

2 Class announcements NNNOOOOOOOOO!!!!!!!! CS447

3 Multiplication CS447

4 Monkeys, ladders, bananas, and hoses
here's where most people would teach you Booth's Algorithm there are a few problems with it: it's really complicated it's really confusing most importantly, literally no one uses it anymore and we haven't for decades ever heard the (fake?) story of the monkey-ladder experiment? as far as I can tell, Booth's Algorithm is a waste of time used to torture architecture students and nothing more - scientists put a group of 3 monkeys in a room with a ladder. at the top of the ladder was a banana. - whenever any monkey tried to climb the ladder, the scientists sprayed them with a hose. - eventually all the monkeys learned not to climb the ladder. - then they replaced one monkey with a new one. new monkey tries to climb the ladder, but other 2 monkeys pull him down. - eventually the new monkey learns not to climb the ladder from the other 2 monkeys. - then they replaced another of the original monkeys… same thing. - then they replaced the last original monkey… same thing. - now they had three monkeys who had never been sprayed but nonetheless refused to go up the ladder. - "it's just how we've always done it." - "listen to your elders." - "do you really want to take the risk?" CS447

5 Multiplication by repeated addition
in A × B, the product (answer) is "A copies of B, added together" 6 × 3 = 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 how many additions would it take to calculate 500,000,000 × 2? CS447

6 A B P 1 ✕ 1 Back to grade school remember your multiplication tables?
binary is so much easier if we list 0 too, the product logic looks awfully familiar… 1 2 3 4 5 6 7 8 9 10 12 14 16 18 15 21 24 27 20 28 32 36 25 30 35 40 45 42 48 54 49 56 63 64 72 81 1 A B P 1 - it's an AND! (hey, we don't call it "logical product" for nothing.) CS447

7 Just like you remember you know how to multiply, riiiight? 1011 = × 0101 = 1011 0000 0000___ 11 × 5 Multiplicand Multiplier these are partial products. how many additions are we doing? 55 00 000 wait, what operation are we doing here...? 110111 - for an n-bit multiplier, we have n partial products, and therefore O(n) additions. - each partial product is shifted left one more place than the one above it. - you do this in decimal too! each row of the partial product is multiplied by 10 (shifted left one place). CS447

8 78×54 = 70×50 + 70×4 + 8×50 + 8×4 Wait, why does this work?
what are we actually doing with this technique? remember how positional numbers are really polynomials? FOIL… 78×54 = 70× ×4 + 8×50 + 8×4 we're eliminating many addition steps by grouping them together. we're eliminating many addition steps by grouping them together. we group them together by powers of the base. - we're also "mashing together" some of the inner terms in each partial product. - so even though this FOILing gives us 3 terms, we only have 2 partial products, since the middle term is split across 2 partial products. CS447

9 Grade school algorithm, formalized
for(n bits in multiplier) { if((multiplier & 1) != 0) product += multiplicand multiplicand <<= 1 multiplier >>= 1 } Multiplicand Multiplier Product 1011 0101 +1011 10110 010 1011 + 0 101100 01 1011 shifting the multiplier right is just like going right-to-left through the digits. shifting the multiplicand left is what we do in the partial product rows. how long does this take? 110111 + 0 - "multiplier & 1" is checking if the LSB is 1. (it masks off everything except the LSB.) - the code inside the loop is O(1), so the loop is O(n) where n is the number of bits in the multiplier. 110111 CS447

10 How many bits? 99 × 99 9801 when we added two n-digit/bit numbers, how many digits/bits was the sum? how about for multiplication? when you multiply two n-digit/bit numbers, the product will be at most 2n digits/bits so if we multiply two 32-bit numbers… we could get a 64-bit result! AAAA! if we just ignored those extra 32 bits, or crashed, we'd be losing a lot of info. so we have to store it. 9999 × 1111 × - when adding two n-bit numbers, we could get an n+1-bit result. - technically if you multiply an m-bit number by an n-bit result then the product can be (m+n) bits, but if m = n (commonly), then it's 2n. CS447

11 How (and why) MIPS does it
MIPS has two more 32-bit registers, HI and LO. if you do this: mult t0, a0 then HI = upper 32 bits of the product and LO = lower 32 bits to actually get the product, we use these: mfhi t0 # move From HI (t0 = HI) mflo t1 # move From LO (t1 = LO) the mul pseudo-op does a mult followed by an mflo MIPS does this for 2 reasons: multiplication can take longer than addition we'd otherwise have to change two different registers at once if you wanted to check for 32-bit multiplication overflow, how could you do it? - you could use mfhi to get the upper 32 bits and see if it's nonzero. - if all the upper bits are 0, then the result fits comfortably in 32 bits. - if all the upper bits are 1, then the result is negative… and may be either 32 or 33 bits… so more checks… ugh… CS447

12 Signed multiplication
CS447

13 Grade school (but like, 6th, instead of 3rd)
if you multiply two signed numbers, what's the rule? Product Sign A B P 3 5 15 -5 -15 -3 A B S + - if the signs of the operands differ, the output is negative. - you may or may not recognize this logic as exclusive OR. CS447

14 long prod = unsigned_mult(abs(A), abs(B)); if(sgn(A) == sgn(B))
Don't repeat yourself we already have an algorithm to multiply unsigned numbers multiplying signed numbers is exactly the same (except for the signs) so why not use what we already made? long prod = unsigned_mult(abs(A), abs(B)); if(sgn(A) == sgn(B)) return prod; else return –prod; - abs(x) = {-x if x < 0; x otherwise} - sgn(x) = {-1 if x < 0; 1 if x > 0; 0 otherwise} CS447

15 Division Like multiplication, but… not
CS447

16 If multiplication is repeated addition…
…is division repeated subtraction? yes. it is. in A ÷ B, the quotient (answer) is "how many times can you subtract B from A until you can't anymore?" 20 ÷ 3 = 6 R 2 1 2 3 what about these lil guys? 4 5 6 how many subtractions would it take to calculate 1,000,000,000 ÷ 2? CS447

17 That's not what you learned in school, was it
you learned something a tiiiiiny bit more complicated 5 4 R51 4÷77? 4209 77 42÷77? -385 =77×5 9 420÷77! oh, that's like 77×4 35 uhh…how many times? like, 5-ish? guess and check -308 =77×4 51 wtf? CS447

18 What's going on? division is multiplication backwards. it's like parallel parking. 77 × we're finding the partial products that add up to a total product 5 4 division goes left-to-right because we find the partial products from biggest to smallest 308 3850 and to make it even more interesting, there might be a remainder 4158 and then there's division by 0. +R51 multiplication is multiplying polynomials. division is factoring polynomials. 4209 CS447

19 Another way of looking at it (animated)
let's say we want to do… idk, 696÷4 40 40 40 4 first we ask how many 400s fit 1 400 4 40 7 then how many 40s 4 4 then how many 4s 40 4 so we're saving time by doing groups of subtractions from biggest to smallest 40 40 - could we go smallest to biggest? - how would you tell how many 4s before you knew how many 400s and 40s? - (YOU CAN'T) CS447

20 Thanks, tiny multiplication table. Thable.
at least multiplication in binary is easy, which simplifies division 1 1 R1 1100≤1? 1100≤10? 1100≤100? 1100 1100≤1001? -1100 1 1100≤10010! 110 1100≤1100! -1100 in binary, each step becomes a yes-no choice: does the divisor fit into the remainder? 1100≤1? CS447

21 Divisor? Dividend? Remainder?
the divisor divides the dividend the dividend is the number that is being divided 1100 -1100 1 the remainder is the number we're trying to fit the divisor into 110 this is the new remainder. but really, when we subtract something, we are making the remainder smaller. it starts off as the dividend… - the remainder isn't just a number you get at the end. it is the number that you start off with and chop parts off of. - it's the uncovered (white) part of the graph paper. - the remainder went from to 11001, and the next step will reduce it to 1. - we usually don't drop more than 1 digit at a time… just to save some writing. but they are there. CS447

22 Finding partial products, biggest to smallest (animated)
essentially we're starting with the divisor shifted all the way left, and sliding it right 1 1 R1 1100 1 11001 so let's ALGORITHM-IZE IT CS447

23 noitacilpitluM let's do 11 ÷ 5. Divisor Remainder Quotient divisor <<= n (bits in dividend) remainder = dividend quotient = 0 for(n bits in dividend) { divisor >>= 1 if(divisor > remainder) { append 0 to quotient } else { remainder -= divisor append 1 to quotient } 1011 101000 1011 10100 1011 1010 -1010 1 101 1 10 1 10 - 11 ÷ 5 is indeed 2R1. CS447


Download ppt "Multiplication and Division"

Similar presentations


Ads by Google