Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic IV CPSC 321 Andreas Klappenecker. Any Questions?

Similar presentations


Presentation on theme: "Arithmetic IV CPSC 321 Andreas Klappenecker. Any Questions?"— Presentation transcript:

1 Arithmetic IV CPSC 321 Andreas Klappenecker

2 Any Questions?

3 Today’s Menu Division Floating Point Numbers

4 Recall: Multiplier

5 The Booth Multiplier Let’s kick it up a notch!

6 Booth Multiplication Current and previous bit 00: middle of run of 0s, no action 01: end of a run of 1s, add multiplicand 10: beginning of a run of 1s, subtract mcnd 11: middle of string of 1s, no action

7 Negative Numbers 0010 2 x 1101 2 = 1111 1010 2 0) Mcnd 0010 Prod 0000 1101,0 1) Mcnd 0010 Prod 1110 1101,0 sub 1) Mcnd 0010 Prod 1111 0110,1 >> 2) Mcnd 0010 Prod 0001 0110,1 add 2) Mcnd 0010 Prod 0000 1011,0 >> 3) Mcnd 0010 Prod 1110 1011,0 sub 3) Mcnd 0010 Prod 1111 0101,1 >> 4) Mcnd 0010 Prod 1111 0101,1 nop 4) Mcnd 0010 Prod 1111 1010,1 >>

8 Exercise Work through some examples 1.Grade school algorithms 2.Booth multiplication Read Chapter 4 Really!

9 MIPS Assembly Language Multiplication of two 32 bit operands x = mult $a1, $s1result is stored in mfhi $v0 special registers mflo $v1high= 32 MSBs low = 32 LSBs highlow

10 MIPS Assembly Language Integer division: divide a by b a = q b + r quotient q = floor(a/b), remainder r = a % b div $a0, $a1 mflo $v0 # quotient mfhi $v1 # remainder rqab/=

11 Exponentiation Suppose you want to raise an integer b to the nth power: (...((b*b)*b)...*b) = b n Way too slow for large n! Use square and multiply algorithm.

12 Square and Multiply Power(x,n) = x if n=1 Power(x 2,n/2) if n even x*Power(x 2,(n-1)/2) if n odd x 6 = (x 2 ) 3 =x 2 (x 2 ) 2

13 Division

14 Recall Decimal Division Grammar school algorithm 93:7= 13 7 23 21 (trial multiplication, 3*7 works, 4*7 doesn’t) 2 - -

15 Binary division Dividend = Quotient x Divisor + Remainder See how big a number can be subtracted Create a quotient bit (0 or 1) in each step Binary division is simpler than decimal division

16 Binary Division 00000111: 0010 = 11 -00100 0011 -0010 0001 7 = 3x2 + 1

17 Division Hardware (Version 1)

18 rem = rem - div if rem < 0 then // divisor too big rem = rem + div quo <<= 1 LSB(quo) = 0 else // can divide quo <<= 1 LSB(quo) = 1 fi div >>= 1 repeat unless done

19 iterationStepQuotDivisorRemainder 0 initial step 00000010 00000000 0111 1 rem -= div rem +div, Q<<=1, Q0=0 div >>= 1 0000 0000 0000 0010 0000 0010 0000 0001 0000 1110 0111 0000 0111 0000 0111 2 rem -= div rem +div, Q<<=1, Q0=0 div >>= 1 0000 0000 0000 0001 0000 0001 0000 0000 1000 1111 0111 0000 0111 0000 0111 3 rem -= div rem +div, Q<<=1, Q0=0 div >>= 10000 0000 0000 1000 0000 1000 0000 0100 1111 1111 0000 0111 0000 0111 4 rem -= div rem Q<<=1, Q0=1 div >>= 1 0001 0001 0001 0000 0100 0000 0100 0000 0010 0000 0011 0000 0011 0000 0011 5 rem -= div rem Q<<=1, Q0=1 div >>= 1 0001 0011 0011 0000 0010 0000 0010 0000 0001 0000 0001 0000 0001 0000 0001

20 Half of the bits in divisor always 0 Half of adder and divisor register is wasted Instead of shifting divisor to the right, shift remainder to the left Shift left first to save one iteration

21 Division hardware (Version 2) ALU and divisor registers are reduced, remainder shifted left

22 3b. Restore the original value by adding the Divisor register to the left half of the Remainder register, &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 (n = 4 here) nth repetition? No: < n repetitions Start: Place Dividend in Remainder

23 Observations (Divide Version 2) Eliminate quotient register => combine with remainder register Start by shifting the remainder left, as before. After that, the loop will contain only two steps (because remainder register shifts both remainder and quotient) Remainder will now be shifted left one time too many. Thus, a final correction step must shift back only the remainder in the left half of the register

24 rem <<= 1 rem -= (div >> 32) if rem < 0 then rem += (div >> 32) rem <<= 1 LSB(rem) = 0 else rem <<= 1 LSB(rem) = 1 fi repeat unless done Correct remainder Version 3

25 Floating Point Numbers

26 IEEE 754 Floating Point Representation Float – 1 sign bit, 8 exponent bits, 23 bits for significand. seeeeeeeefffffffffffffffffffffff value = (-1) s x F x 2 E-127 with F= 1 +.ffffffff.... fff Double – 1 sign bit, 11 exponent bits, 52 bits for significand

27 Floating Point Representation: double 1 bit sign, 11 bits for exponent, 52 bits for significand seeeeeeeeeeeffffffffffffffffffff ffffffffffffffffffffffffffffffff Range of float: 2.0 x 10 -38 … 2.0 x 10 38 Range of double: 2.0 x 10 -308 … 2.0 x 10 308

28 Integer and Floating Point Arithmetic instruction memory PCPC integer register file integer ALU integer multiplier data memory flt pt register file flt pt adder flt pt multiplier HI LO

29 Floating Point Registers in MIPS $f0 - $f3 function-returned values $f4 - $f11 temporary values $f12 - $f15 function arguments $f16 - $f19 temporary values $f20 - $f31 saved values Interpretation: 32 registers of 32 bits or 16 registers of 64 bits (even indices)

30 Floating Point in MIPS.data Pi:.double 3.14159 Rad:.double 12.34567.text main:l.d$f0, Pi# $f0 = Pi; load double l.d$f4, Rad# $f4 = radius mul.d$f12, $f4, $f4 # $f12 = radius squared mul.d$f12, $f12, $f0# multiply by Pi li $v0, 3 # print double $f12 syscall# (= print the area) li $v0, 10# syscall # exit

31 Conclusion We learned how to divide Make sure that you work through some examples Read the section on floating point addition and multiplication in Chapter 4 (What are the challenges here?) To probe further: Knuth’s “Art of Computer Programming” contains numerous MIX assembly language programs; you can get involved into the translation to MMIX.


Download ppt "Arithmetic IV CPSC 321 Andreas Klappenecker. Any Questions?"

Similar presentations


Ads by Google