230 likes | 245 Views
Dive into the world of multiplication and division with essential techniques and insights to enhance your understanding. Learn about Booth's Algorithm, understand binary multiplication, grasp the power of partial products, and explore MIPS implementations. Uncover the mysteries of signed multiplication, optimize calculations with efficient algorithms, and unravel the complexities of division. From basic grade school principles to advanced computational methods, this guide is your key to mastering these fundamental operations.
E N D
Multiplication and Division CS/COE 0447 Jarrett Billingsley
Class announcements • NNNOOOOOOOOO!!!!!!!! CS447
Multiplication CS447
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 CS447
Multiplication by repeated addition • in A × B, the product (answer) is "A copies of B, added together" 6 × 3 = 18 how many additions would it take to calculate 500,000,000 × 2? CS447
Back to grade school • remember your multiplication tables? • binary is so much easier • if we list 0 too, the product logic looks awfully familiar… CS447
Just like you remember • you know how to multiply, riiiight? 1011 = × 0101 = 1011 0000 1011 0000___ 11 × 5 Multiplicand Multiplier 55 these are partial products.how many additions are we doing? 0 00 000 wait, what operation are we doing here...? 110111 CS447
Wait, why does this work? • what are we actually doing with this technique? • remember how positional numbers are really polynomials? FOIL… 78×54 = 70×50 + 70×4 + 8×50 + 8×4 we're eliminating many addition steps by grouping them together. we group them together by powers of the base. CS447
Grade school algorithm, formalized for(n bits in multiplier) { if((multiplier & 1) != 0) product += multiplicand multiplicand <<= 1 multiplier >>= 1 } +1011 + 0 • 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? +101100 + 0 CS447
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 × 9999 99980001 1111 × 1111 11100001 CS447
How (and why) MIPS does it • MIPS has two more 32-bit registers, HI and LO. if you do this: multt0, a0 • then HI = upper 32 bits of the product and LO = lower 32 bits • to actually get the product, we use these: mfhit0 # move From HI (t0 = HI) mflot1 # 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? CS447
Signed multiplication CS447
Grade school (but like, 6th, instead of 3rd) • if you multiply two signed numbers, what's the rule? Product Sign if the signs of the operands differ, the output is negative. CS447
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; CS447
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 what about these lil guys? how many subtractions would it take to calculate 1,000,000,000 ÷ 2? CS447
That's not what you learned in school, was it • you learned something a tiiiiiny bit more complicated 0 4 R51 5 0 4÷77? 77 4209 42÷77? =77×5 -385 420÷77! oh, that's like 77×4 35 9 uhh…how many times? like, 5-ish? guess and check =77×4 -308 51 wtf? CS447
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 4 5 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
Another way of looking at it (animated) • let's say we want to do… idk, 696÷4 40 40 4 40 first we ask how many 400s fit 1 4 400 40 7 then how many 40s 4 4 40 then how many 4s 4 so we're saving time by doing groups of subtractions from biggest to smallest 40 • 40 CS447
Thanks, tiny multiplication table. Thable. • at least multiplication in binary is easy, which simplifies division 0 1 1 R1 0 0 0 0 1100≤1? 1100≤10? 1100≤100? 1100 1001001 1100≤1001? -1100 1100≤10010! 110 0 1 1100≤1100! -1100 in binary, each step becomes a yes-no choice: does the divisor fit into the remainder? 0 1100≤1? CS447
Divisor? Dividend? Remainder? the divisor divides the dividend the dividend is the number that is beingdivided -1100 1100 1001001 the remainder is the number we're trying to fit the divisor into 110 this is the new remainder. 0 1 but really, when we subtract something, we are making the remainder smaller. it starts off as the dividend… CS447
Finding partial products, biggest to smallest (animated) essentially we're starting with the divisor shifted all the way left, and sliding it right 0 1 1 R1 0 0 0 0 1100 1 1001001 11001 1100000000 so let's ALGORITHM-IZE IT CS447
noitacilpitluM 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 } } let's do 11 ÷ 5. -1010 CS447