Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS141-L3-1Tarun Soni, Summer ‘03 More ALUs and floating point numbers  Today: The rest of chap 4:  Multiplication, Division and Floating point numbers.

Similar presentations


Presentation on theme: "CS141-L3-1Tarun Soni, Summer ‘03 More ALUs and floating point numbers  Today: The rest of chap 4:  Multiplication, Division and Floating point numbers."— Presentation transcript:

1 CS141-L3-1Tarun Soni, Summer ‘03 More ALUs and floating point numbers  Today: The rest of chap 4:  Multiplication, Division and Floating point numbers

2 CS141-L3-2Tarun Soni, Summer ‘03  Instruction Set Architectures  Performance issues  2s complement, Addition, Subtraction The Story so far: Basically ISA and some ALU stuff

3 CS141-L3-3Tarun Soni, Summer ‘03 CPU: The big picture Instruction Fetch Instruction Decode Operand Fetch Execute Result Store Next Instruction ° Design hardware for each of these steps!!! Execute an entire instruction FetchDecode Fetch Execute Store Next

4 CS141-L3-4Tarun Soni, Summer ‘03 CPU: Clocking Clk Don’t Care SetupHold........................ SetupHold All storage elements are clocked by the same clock edge

5 CS141-L3-5Tarun Soni, Summer ‘03 CPU: Big Picture: Control and Data Path ALUctr RegDst ALUSrc ExtOp MemtoRegMemWr Equal Instruction Imm16RdRsRt nPC_sel Adr Inst Memory DATA PATH Control Op Fun RegWr

6 CS141-L3-6Tarun Soni, Summer ‘03 CPU: The abstract version Logical vs. Physical Structure Data Out Clk 5 RwRaRb 32 32-bit Registers Rd ALU Clk Data In Data Address Ideal Data Memory Instruction Address Ideal Instruction Memory Clk PC 5 Rs 5 Rt 32 A B Next Address Control Datapath Control Signals Conditions

7 CS141-L3-7Tarun Soni, Summer ‘03 Computer Performance Multiplication and Division

8 CS141-L3-8Tarun Soni, Summer ‘03 The 32 bit ALU-limited edition Bit-slice plus extra on the two ends Overflow means number too large for the representation Carry-look ahead and other adder tricks AB M S 32 4 Ovflw ALU0 a0b0 cinco s0 ALU31 a31b31 cinco s31 C/L to produce select, comp, c-in signed-arith and cin xor co

9 CS141-L3-9Tarun Soni, Summer ‘03 The Design Process Divide and Conquer (e.g., ALU) –Formulate a solution in terms of simpler components. –Design each of the components (subproblems) Generate and Test (e.g., ALU) –Given a collection of building blocks, look for ways of putting them together that meets requirement Successive Refinement (e.g., multiplier, divider) –Solve "most" of the problem (i.e., ignore some constraints or special cases), examine and correct shortcomings. Formulate High-Level Alternatives (e.g., shifter) –Articulate many strategies to "keep in mind" while pursuing any one approach. Work on the Things you Know How to Do –The unknown will become “obvious” as you make progress. Optimization Criteria: Delay [Logic levels, Fan in/out], Area [Gate count, Package count, Pin out] Cost, Power, Design time

10 CS141-L3-10Tarun Soni, Summer ‘03 The 32 bit ALU-limited edition Supported Operations 000 = and 001 = or 010 = add 110 = subtract 111 = slt Tuned performance by using Carry-lookahead adders. What about other instructions ? multiply mult $2,$3Hi, Lo = $2 x $364-bit signed product multiply unsignedmultu$2,$3Hi, Lo = $2 x $3 64-bit unsigned product divide div $2,$3Lo = $2 ÷ $3,Lo = quotient, Hi = remainder Hi = $2 mod $3 divide unsigned divu $2,$3Lo = $2 ÷ $3,Unsigned quotient & remainder

11 CS141-L3-11Tarun Soni, Summer ‘03 Grade school Paper and pencil example: Multiplicand 1000 Multiplier x1001 1000 0000 0000 1000 Product 1001000 m bits x n bits = m+n bit product Binary makes it easy: –0 => place 0 ( 0 x multiplicand) –1 => place multiplicand ( 1 x multiplicand) we’ll look at a couple of versions of multiplication hardware

12 CS141-L3-12Tarun Soni, Summer ‘03 Unsigned basic multiplier B0B0 A0A0 A1A1 A2A2 A3A3 A0A0 A1A1 A2A2 A3A3 A0A0 A1A1 A2A2 A3A3 A0A0 A1A1 A2A2 A3A3 B1B1 B2B2 B3B3 P0P0 P1P1 P2P2 P3P3 P4P4 P5P5 P6P6 P7P7 0000 Stage i accumulates A * 2 i if B i == 1

13 CS141-L3-13Tarun Soni, Summer ‘03 Unsigned basic multiplier at each stage shift A left ( x 2) use next bit of B to determine whether to add in shifted multiplicand accumulate 2n bit partial product at each stage B0B0 A0A0 A1A1 A2A2 A3A3 A0A0 A1A1 A2A2 A3A3 A0A0 A1A1 A2A2 A3A3 A0A0 A1A1 A2A2 A3A3 B1B1 B2B2 B3B3 P0P0 P1P1 P2P2 P3P3 P4P4 P5P5 P6P6 P7P7 0000 000

14 CS141-L3-14Tarun Soni, Summer ‘03 Unsigned basic multiplier for(i=0; i<32; i++) { If ( mulitplier[0] == 1 ) // we could do multiplier[i] and skip the shift { product += multiplicand ; // product is 64 bit register // adder is 64 bit. ! } multiplicand << 1; // shift multiplicand to prepare for next add // multiplicand is in a 64 bit register mulitplier >> 1; // position the i’th bit on lsb for test. } The algorithm

15 CS141-L3-15Tarun Soni, Summer ‘03 Unsigned basic multiplier 64-bit Multiplicand reg, 64-bit ALU, 64-bit Product reg, 32-bit multiplier reg Multiplier = datapath + control Product Multiplier Multiplicand 64-bit ALU Shift Left Shift Right Write Control 32 bits 64 bits ProductMultiplierMultiplicand 0000 0000 00110000 0010 0000 001000010000 0100 0000 011000000000 1000 0000 0110

16 CS141-L3-16Tarun Soni, Summer ‘03 Some observations 1 clock per cycle => ­ 100 clocks per multiply –Ratio of multiply to add 5:1 to 100:1 1/2 the bits in multiplicand always 0 => 64-bit adder is wasted 0’s inserted in left of multiplicand as shifted => least significant bits of product never changed once formed Instead of shifting multiplicand to left, shift product to right? Speed ? Power/efficiency of the adder ? Pattern of result on product register ?

17 CS141-L3-17Tarun Soni, Summer ‘03 Multiplier 2.0 32-bit Multiplicand reg, 32 -bit ALU, 64-bit Product reg, 32-bit Multiplier reg Product Multiplier Multiplicand 32-bit ALU Shift Right Write Control 32 bits 64 bits Shift Right

18 CS141-L3-18Tarun Soni, Summer ‘03 Multiplier 2.0 3. Shift the Multiplier register right 1 bit. Done Yes: 32 repetitions 2. Shift the Product register right 1 bit. No: < 32 repetitions 1. Test Multiplier0 Multiplier0 = 0 Multiplier0 = 1 1a. Add multiplicand to the left half of product & place the result in the left half of Product register 32nd repetition? Start for(i=0; i<32; i++) { If ( mulitplier[0] == 1 ) { product[31:16] += multiplicand ; // product is 64 bit register // adder is 32 bit. ! } product >> 1; // shift product right // saving product[i:0] for final result mulitplier >> 1; // position the i’th bit on lsb for test. }

19 CS141-L3-19Tarun Soni, Summer ‘03 Multiplier 2.0 Product Multiplier Multiplicand 32-bit ALU Shift Right Write Control 32 bits 64 bits Shift Right ProductMultiplierMultiplicand NextProduct 0000 0000 001100100000+0010 = 0010 0000 0001 0000000100100001+0010 = 0011 0000 0001 1000000000100001+0000 = 0001 1000 0000 1100000000100000+0000 = 0000 1100 0000 0110

20 CS141-L3-20Tarun Soni, Summer ‘03 Multiplier 3.0 Product register wastes space that exactly matches size of multiplier => combine Multiplier register and Product register 32-bit Multiplicand reg, 32 -bit ALU, 64-bit Product reg, (0-bit Multiplier reg) Product (Multiplier) Multiplicand 32-bit ALU Write Control 32 bits 64 bits Shift Right

21 CS141-L3-21Tarun Soni, Summer ‘03 Multiplier 3.0 Done Yes: 32 repetitions 2. Shift the Product register right 1 bit. No: < 32 repetitions 1. Test Product0 Product0 = 0 Product0 = 1 1a. Add multiplicand to the left half of product & place the result in the left half of Product register 32nd repetition? Start for(i=0; i<32; i++) { If ( product[0] == 1 ) { product[31:16] += multiplicand ; // product is 64 bit register // adder is 32 bit. ! } product >> 1; // shift product right // saving product[i:0] for final result }

22 CS141-L3-22Tarun Soni, Summer ‘03 More observations ? 2 steps per bit because Multiplier & Product combined MIPS registers Hi and Lo are left and right half of Product Gives us MIPS instruction MultU How can you make it faster? What about signed multiplication? –easiest solution is to make both positive & remember whether to complement product when done (leave out the sign bit, run for 31 steps) –apply definition of 2’s complement need to sign-extend partial products and subtract at the end –Booth’s Algorithm is elegant way to multiply signed numbers using same hardware as before and save cycles can handle multiple bits at a time

23 CS141-L3-23Tarun Soni, Summer ‘03 Booths algorithm Example 2 x 6 = 0010 x 0110: 0010 x 0110 + 0000shift (0 in multiplier) + 0010 add (1 in multiplier) + 0100 add (1 in multiplier) +0000 shift (0 in multiplier) 00001100 ALU with add or subtract gets same result in more than one way: 6= – 2 + 8 0110 = – 00010 + 01000 = 11110 + 01000 For example 0010 x 0110 0000 shift (0 in multiplier) – 0010 sub (first 1 in multpl.). 0000 shift (mid string of 1s). +0010 add (prior step had last 1) 00001100

24 CS141-L3-24Tarun Soni, Summer ‘03 Booths algorithm Current BitBit to the RightExplanationExampleOp 10Begins run of 1s0001111000sub 11Middle of run of 1s0001111000none 01End of run of 1s0001111000add 00Middle of run of 0s0001111000none Originally for Speed (when shift was faster than add) Replace a string of 1s in multiplier with an initial subtract when we first see a one and then later add for the bit after the last one –1 + 10000 01111

25 CS141-L3-25Tarun Soni, Summer ‘03 Booths algorithm Booths Example (2 x 7) 1a. P = P - m1110 +1110 1110 0111 0shift P (sign ext) 1b. 00101111 0011 111 -> nop, shift 2.00101111 1001 111 -> nop, shift 3.00101111 1100 101 -> add 4a.0010 +0010 0001 1100 1shift 4b.00100000 1110 0done OperationMultiplicandProductnext? 0. initial value00100000 0111 010 -> sub

26 CS141-L3-26Tarun Soni, Summer ‘03 Booths algorithm Booths Example (2 x -3) 1a. P = P - m1110 +1110 1110 1101 0shift P (sign ext) 1b. 00101111 0110 101 -> add + 0010 2a.0001 0110 1shift P 2b.00100000 1011 010 -> sub +1110 3a.00101110 1011 0shift 3b.0010 1111 0101 111 -> nop 4a1111 0101 1 shift 4b.00101111 1010 1 done OperationMultiplicandProductnext? 0. initial value00100000 1101 010 -> sub

27 CS141-L3-27Tarun Soni, Summer ‘03 Division 1001 Quotient Divisor 1000 1001010 Dividend –1000 10 101 1010 –1000 10 Remainder (or Modulo result) See how big a number can be subtracted, creating quotient bit on each step Binary => 1 * divisor or 0 * divisor Dividend = Quotient x Divisor + Remainder => sizeof( Dividend ) = sizeof( Quotient ) + sizeof( Divisor ) 3 versions of divide, successive refinement

28 CS141-L3-28Tarun Soni, Summer ‘03 Division 1.0 64-bit Divisor reg, 64-bit ALU, 64-bit Remainder reg, 32-bit Quotient reg Remainder Quotient Divisor 64-bit ALU Shift Right Shift Left Write Control 32 bits 64 bits

29 CS141-L3-29Tarun Soni, Summer ‘03 Division 1.0 1. Subtract the Divisor register from the Remainder register, and place the result in the Remainder register. Test Remainder Remainder < 0 Remainder >= 0 2a. Shift the Quotient register to the left setting the new rightmost bit to 1. 2b. Restore the original value by adding the Divisor register to the Remainder register, and place the sum in the Remainder register. Also shift the Quotient register to the left, setting the new least significant bit to 0. 3. Shift the Divisor register right 1 bit. 33rd repetition? No: < 33 repetitions Done Yes: 33 repetitions Start Takes n+1 steps for n-bit Quotient & Rem. QuotientDivisorRemainder 00000010 00000000 0111

30 CS141-L3-30Tarun Soni, Summer ‘03 Division 2.0 1/2 bits in divisor always 0 => 1/2 of 64-bit adder is wasted => 1/2 of divisor is wasted Instead of shifting divisor to right, shift remainder to left? 1st step cannot produce a 1 in quotient bit (otherwise too big) => switch order to shift first and then subtract, can save 1 iteration 32-bit Divisor reg, 32-bit ALU, 64-bit Remainder reg, 32-bit Quotient reg Remainder Quotient Divisor 32-bit ALU Shift Left Write Control 32 bits 64 bits Shift Left

31 CS141-L3-31Tarun Soni, Summer ‘03 Division 2.0 3b. Restore the original value by adding the Divisor register to the left half of the Remainderregister, &place the sum in the left half of the Remainder register. Also shift the Quotient register to the left, setting the new least significant bit to 0. Test Remainder Remainder < 0 Remainder >= 0 2. Subtract the Divisor register from the left half of the Remainder register, & place the result in the left half of the Remainder register. 3a. Shift the Quotient register to the left setting the new rightmost bit to 1. 1. Shift the Remainder register left 1 bit. Done Yes: n repetitions nth repetition? No: < n repetitions Start: Place Dividend in Remainder

32 CS141-L3-32Tarun Soni, Summer ‘03 Division 3.0 Eliminate Quotient register by combining with Remainder as shifted left –Start by shifting the Remainder left as before. –Thereafter loop contains only two steps because the shifting of the Remainder register shifts both the remainder in the left half and the quotient in the right half –The consequence of combining the two registers together and the new order of the operations in the loop is that the remainder will shifted left one time too many. – Thus the final correction step must shift back only the remainder in the left half of the register 32-bit Divisor reg, 32 -bit ALU, 64-bit Remainder reg, (0-bit Quotient reg) Remainder (Quotient) Divisor 32-bit ALU Write Control 32 bits 64 bits Shift Left “HI”“LO”

33 CS141-L3-33Tarun Soni, Summer ‘03 Division 3.0 Remainder Divisor 0000 01110010 3b. Restore the original value by adding the Divisor register to the left half of the Remainderregister, &place the sum in the left half of the Remainder register. Also shift the Remainder register to the left, setting the new least significant bit to 0. Test Remainder Remainder < 0 Remainder  0 2. Subtract the Divisor register from the left half of the Remainder register, & place the result in the left half of the Remainder register. 3a. Shift the Remainder register to the left setting the new rightmost bit to 1. 1. Shift the Remainder register left 1 bit. Done. Shift left half of Remainder right 1 bit. Yes: n repetitions (n = 4 here) nth repetition? No: < n repetitions Start: Place Dividend in Remainder

34 CS141-L3-34Tarun Soni, Summer ‘03 Sign of remainder = ? 7/4 = (Q=1, R=3) 7/4 = (Q=2, R=-1) Which do you prefer? Convention: a/b = (Q, R) Sign(R) <= Sign(a) Thus 7/4 = (Q=1, R=3) -7/4 = (Q=-1,R=-3) Division: some signed details -a 0 a Q*b + R + Q*b R a =Q*b + R

35 CS141-L3-35Tarun Soni, Summer ‘03 Floating Point What can be represented in N bits? Unsigned0to2 2s Complement- 2to2 - 1 1s Complement-2 +1to2 -1 But, what about? –very large numbers?9,349,398,989,787,762,244,859,087,678 –very small number?0.0000000000000000000000045691 –rationals 2/3 – irrationals 2 – transcendentalse N N-1

36 CS141-L3-36Tarun Soni, Summer ‘03 Floating Point 6.02 x 10 1.673 x 10 23 -24 exponent radix (base) Mantissa decimal point IEEE F.P. ± 1.M x 2 e - 127 Issues: ° Arithmetic (+, -, *, / ) ° Representation, Normal form ° Range and Precision ° Rounding ° Exceptions (e.g., divide by zero, overflow, underflow) ° Errors ° Properties ( negation, inversion, if A ° B then A - B ° 0 )

37 CS141-L3-37Tarun Soni, Summer ‘03 Floating Point Binary Fractions 1011 2 = 1x2 3 + 0x2 2 + 1x2 1 + 1x2 0 so... 101.011 2 = 1x2 2 + 0x2 1 + 1x2 0 + 0x2 -1 + 1x2 -2 + 1x2 -3 e.g.,.75 = 3/4 = 3/2 2 = 1/2 + 1/4 =.11

38 CS141-L3-38Tarun Soni, Summer ‘03 Floating Point Representation of floating point numbers in IEEE 754 standard: single precision 1823 sign exponent: excess 127 binary integer mantissa: sign + magnitude, normalized binary significand w/ hidden integer bit: 1.M actual exponent is e = E - 127 SE M N = (-1) 2 (1.M) S E-127 0 < E < 255 0 = 0 00000000 0... 0 -1.5 = 1 01111111 10... 0 Magnitude of numbers that can be represented is in the range: 2 -126 (1.0) to2 127 (2 - 2 23 ) which is approximately: 1.8 x 10 -38 to3.40 x 10 38 integer comparison valid on IEEE Fl.Pt. numbers of same sign!

39 CS141-L3-39Tarun Soni, Summer ‘03 Floating Point Leading “1” bit of significand is implicit Exponent is “biased” to make sorting easier –all 0s is smallest exponent all 1s is largest –bias of 127 for single precision and 1023 for double precision –summary: (–1) sign ´ (1+significand) ´ 2 exponent – bias Example: –decimal: -.75 = -3/4 = -3/2 2 –binary: -.11 = -1.1 x 2 -1 –floating point: exponent = 126 = 01111110 –IEEE single precision: 10111111010000000000000000000000 Sign Exponent Significand

40 CS141-L3-40Tarun Soni, Summer ‘03 Floating Point Floating Point Addition How do you add in scientific notation? 9.962 x 10 4 + 5.231 x 10 2 Basic Algorithm 1. Align 2. Add 3. Normalize 4. Round Approximate algorithm. While (Exp(A) > Exp(B) ) { shift Mantissa(B) right; Exp(B)++; } Mantissa(Result) = Mantissa(A) + Mantissa(B); Exp(Result) = Exp(A); // or Exp(B) While (Mantissa(Result)[msb] !=1!) { Exp(Result)--; } Round(Mantissa); Round(Exponent);

41 CS141-L3-41Tarun Soni, Summer ‘03 Floating Point

42 CS141-L3-42Tarun Soni, Summer ‘03 Floating Point Addition

43 CS141-L3-43Tarun Soni, Summer ‘03 Floating Point Floating Point Multiplication How do you multiply in scientific notation? (9.9 x 10 4 )(5.2 x 10 2 ) = 5.148 x 10 7 Basic Algorithm 1. Add exponents 1a. Correct for bias in exponent representation (Exp -= 127); 2. Multiply 3. Normalize 4. Round 5. Set Sign

44 CS141-L3-44Tarun Soni, Summer ‘03 Floating Point Accuracy Issues FP Accuracy Extremely important in scientific calculations Very tiny errors can accumulate over time IEEE 754 FP standard has four rounding modes –always round up –always round down –truncate –round to nearest => in case of tie, round to nearest even Requires extra bits in intermediate representations

45 CS141-L3-45Tarun Soni, Summer ‘03 Floating Point Accuracy Issues Guard bits -- bits to the right of the least significant bit of the significand computed for use in normalization (could become significant at that point) and rounding. IEEE 754 has three extra bits and calls them guard, round, and sticky. How many extra bits? IEEE Spec: As if computed the result exactly and rounded.

46 CS141-L3-46Tarun Soni, Summer ‘03 Floating Point Overflows Infinity and NaNs result of operation overflows, i.e., is larger than the largest number that can be represented overflow is not the same as divide by zero (raises a different exception) +/- infinity S 1... 1 0... 0 It may make sense to do further computations with infinity e.g., X/0 > Y may be a valid comparison Not a number, but not infinity (e.q. sqrt(-4)) invalid operation exception (unless operation is = or =) NaN S 1... 1 non-zero NaNs propagate: f(NaN) = NaN HW decides what goes here

47 CS141-L3-47Tarun Soni, Summer ‘03 Multiplication and division take much longer than addition, requiring multiple addition steps. Floating Point extends the range of numbers that can be represented, at the expense of precision (accuracy). FP operations are very similar to integer, but with pre- and post- processing. Rounding implementation is critical to accuracy over time. Summary


Download ppt "CS141-L3-1Tarun Soni, Summer ‘03 More ALUs and floating point numbers  Today: The rest of chap 4:  Multiplication, Division and Floating point numbers."

Similar presentations


Ads by Google