Download presentation
Presentation is loading. Please wait.
1
CS/COE 0447 (term 2181) Jarrett Billingsley
Division and Floats CS/COE 0447 (term 2181) Jarrett Billingsley
2
Class announcements I finished Stranger Things 2 :O 11/2/2017
CS/COE 0447 term 2181
3
Division Like multiplication, but not
11/2/2017 CS/COE 0447 term 2181
4
If multiplication is repeated addition…
…is division repeated subtraction? Yes. Yes, it is. In A ÷ B, the quotient (answer) is "how many times can you subtract B from A until you get a number less than B?" 20 ÷ 3 = 6 R 2 1 2 3 What about these lil guys? 4 5 6 Now, how many subtractions would it take to calculate 1,000,000,000 ÷ 2? 11/2/2017 CS/COE 0447 term 2181
5
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 What the fuck? 11/2/2017 CS/COE 0447 term 2181
6
What's going on? Division is multiplication backwards. It's like parallel parking. 77 ×54 308 3850 4158 We're finding the partial products that add up to a total product. Division goes left-to-right because we find the partial products from biggest to smallest. And to make it even more interesting, there might be a remainder. +R51=4209 Division is hard. Like, mathematically proven hard. 11/2/2017 CS/COE 0447 term 2181
7
Thanks, tiny multiplication table. Thable.
At least multiplication in binary is easy, which simplifies the task. 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? 11/2/2017 CS/COE 0447 term 2181
8
Divisor? Dividend? Remainder? (animated)
The divisor divides the dividend. The dividend is the number that is being divided. 1100 Remainder Original dividend The remainder is the smaller number that we're trying to fit the divisor into. It's actually being shifted left. 11/2/2017 CS/COE 0447 term 2181
9
Another way of thinking about it (animated)
We're starting with the divisor shifted all the way left, and sliding it right. 1 1 R1 1100 1 11001 Shifting the divisor right is the same as shifting the remainder/dividend left. 11/2/2017 CS/COE 0447 term 2181
10
noitacilpitluM Divisor 101 Remainder Dividend Quotient 1011 for(n bits in dividend) { quotient <<= 1 remainder << dividend << 1 if(divisor <= remainder) { quotient[0] = 1 remainder -= divisor } 1 0110 10 1100 101 1000 -101 |1 1000 1 1 0000 10 1 10 11/2/2017 CS/COE 0447 term 2181
11
Huh, another similarity
The MIPS div instruction works like mult: div t0, a0 then HI = remainder and LO = quotient. Then we can use mfhi/mflo to get the answers. The div pseudo-op does a div followed by an mflo. The rem pseudo-op does a div followed by an mfhi. So, does this mean the multiplication and division hardware are linked somehow? 11/2/2017 CS/COE 0447 term 2181
12
- Well then FSM control unit 8 shift left shift left LSB 8 8 go!
The divider circuitry is closely related to the multiplier. These are hooked together. 8 Remainder Dividend Quotient write enable shift left shift left LSB Divisor 8 8 FSM control unit go! - negative? 8 I'm done! 11/2/2017 CS/COE 0447 term 2181
13
÷ Rem A Quot B Signed division sign(A)⊕sign(B) abs neg abs
It works just like signed multiplication. Absolute value the inputs, negate the output if the signs differ. Rem A abs ÷ Quot neg B abs sign(A)⊕sign(B) 11/2/2017 CS/COE 0447 term 2181
14
Floating-point number representation
11/2/2017 CS/COE 0447 term 2181
15
This could be a whole unit itself...
Floating-point arithmetic is COMPLEX STUFF. But it's not super useful to know unless you're either: doing lots of high-precision numerical programming, or implementing floating-point arithmetic yourself. However... It's good to have an understanding of why limitations exist It's good to have an appreciation of how complex this is... and how much better things are now than they were in the 1970s and 1980s. 11/2/2017 CS/COE 0447 term 2181
16
This is called fixed-point representation.
Fixing the point If we want to represent decimal places, one way of doing so is by assuming that the lowest n digits are the decimal places. $12.34 1234 This is called fixed-point representation. +$54.32 +5432 It's fast and easy, but limited. You trade off speed for accuracy and range. $66.66 6666 11/2/2017 CS/COE 0447 term 2181
17
IEEE 754 Est'd 1985, updated as recently as 2008 Standard for floating-point representation and arithmetic that virtually every CPU now uses Floating-point representation is based around scientific notation 1,348 = = 1,440,000 = × 10+3 × 10-3 × 10+6 sign exponent significand 11/2/2017 CS/COE 0447 term 2181
18
Binary Scientific Notation
Scientific notation works equally well in any other base! (below uses base-10 exponents for clarity) = = = × 2+7 × 2-3 × 2+15 what do you notice about the digit before the binary point? 11/2/2017 CS/COE 0447 term 2181
19
IEEE 754 Single-precision
Known as float in C/C++/Java etc., 32-bit float format 1 bit for sign, 8 bits for the exponent, 23 bits for the fraction Note that the fraction field only stores the digits after the binary point – the 1 before the binary point is implicit! In effect this gives us a 24-bit significand. The only number with a 0 before the binary point is 0! Floating-point numbers are actually in sign-magnitude! Do you remember the downsides? 11/2/2017 illustration from user Stannered on Wikimedia Commons CS/COE 0447 term 2181
20
The exponent field -127 => -10 => 34 => 117 161 Signed Biased
The exponent field is 8 bits, and can hold positive or negative exponents, but... it doesn't use S-M, 1's, or 2's complement. It uses something called biased notation. Basically, you take the signed number and add a constant to get the biased representation. Single-precision floats use 127. -127 => -10 => 34 => 117 161 Signed Biased The exponent can range from -127 to 127 (0 to 254 biased) Why'd they do this? So you can kinda-sorta sort floating point numbers with integer comparisons. Or something. 11/2/2017 CS/COE 0447 term 2181
21
Special values Some mathematical operations can give infinity values (like n/0) or are undefined (like 0/0). An exponent of 255 (biased) is used to signal these values. If the fraction part is 0, it's positive or negative infinity If the fraction part is nonzero, it's "NaN" (not a number) To sum up... Exponent Fraction Value nonzero "denormalized" number 1..254 anything regular number 255 infinity NaN 11/2/2017 CS/COE 0447 term 2181
22
Other formats The most common other format is double-precision (C/C++/Java double), which uses an 11-bit exponent and 52-bit fraction. GPUs have driven the creation of a half-precision 16-bit floating-point format. Isn't it cute? 11/2/2017 both illustrations from user Codekaizen on Wikimedia Commons CS/COE 0447 term 2181
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.