1 / 133

COMPUTER ARITHMETIC

COMPUTER ARITHMETIC. Jehan-François Pâris jparis@uh.edu. Chapter Organization. Representing negative numbers Integer addition and subtraction Integer multiplication and division Floating point operations Examples of implementation IBM 360, RISC, x86. A warning.

minner
Télécharger la présentation

COMPUTER ARITHMETIC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPUTER ARITHMETIC Jehan-François Pâris jparis@uh.edu

  2. Chapter Organization • Representing negative numbers • Integer addition and subtraction • Integer multiplication and division • Floating point operations • Examples of implementation • IBM 360, RISC, x86

  3. A warning • Binary addition, subtraction, multiplication and division are very easy

  4. ADDITION AND SUBTRACTION

  5. Decimal addition (carry) 1_ 19 + 7 26 Binary addition ( carry)111_ 10011 + 111 11010 16+8+2 = 26 General concept

  6. Realization • Simplest solution is a battery of full adders o s3 s2 s1 s0 x3 y3 x2 y2 x1 y1 x0 y0

  7. Observations • Adder add four-bit values • Output o indicates if there is an overflow • A result that cannot be represented using 4 bits • Happens when x + y > 15 • Operation is slowed down bycarry propagation • Faster solutions (not discussed here)

  8. Unsigned addition in 4-bit arithmetic ( carry) 11_ 1011 + 0011 1110 11 + 3 = 14(8 + 4 + 2) Signed and unsigned additions • Signed addition in4-bit arithmetic ( carry) 11_ 1011 + 0011 1110 • -5 + 3 = -2

  9. Signed and unsigned additions • Same rules apply even though bit strings represent different values • Sole difference is overflow handling

  10. No overflow in signed arithmetic ( carry)111_ 1110 + 0011 0001 -2 + 3 = 1(correct) Overflow handling (I) • Signed addition in4-bit arithmetic ( carry) 1__ 0110 + 0011 1001 • 6 + 3  -7(false)

  11. Overflow handling (II) • In signed arithmetic an overflow happens when • The sum of two positive numbers exceeds the maximum positive value that can be represented using n bits: 2n – 1 – 1 • The sum of two negative numbers falls below the minimum negative value that can be represented using n bits: – 2n – 1

  12. Example • Four-bit arithmetic: • Sixteen possible values • Positive overflow happens when result > 7 • Negative overflow happens when result < -8 • Eight-bit arithmetic: • 256 possible values • Positive overflow happens when result > 127 • Negative overflow happens when result < -128

  13. Overflow handling (III) • MIPS architecture handles signed and unsigned overflows in a very different fashion: • Ignores unsigned overflows • Implements modulo 2n arithmetic • Generates an interrupt whenever it detects a signed overflows • Lets the OS handled the condition

  14. Why? • To keep the CPU as simple and regular as possible

  15. An interesting consequence • Most C compilers ignore overflows • C compilers must use unsigned arithmetic for their integer operations • Fortran compilers expect overflow conditions to be detected • Fortran compilers must use signed arithmetic for their integer operations

  16. Subtraction • Can be implementing by • Specific hardware • Negating the subtrahend

  17. Negating a number • Toggle all bits then add one

  18. In 4-bit arithmetic (I)

  19. In 4-bit arithmetic (II)

  20. MULTIPLICATION

  21. (carry) 1_37 x 12 74 370444 What are the rules? Successively multiply the multiplicand by each digit of the multiplier starting at the right shifting the result left by an extra left position each time each time but the first Sum all partial results Decimal multiplication

  22. (carry)111 _1101 x 101 1101 00 1101001000001 What are the rules? Successively multiply the multiplicand by each digit of the multiplier starting at the right shifting the result left by an extra left position each time each time but the first Sum all partial results Binary multiplication is easy! Binary multiplication

  23. Binary multiplication table

  24. Algorithm • Clear contents of 64-bit product register • For (i = 0; i <32; i++) { • If (LSB of multiplier_register ==1) • Add contents of multiplicand register to product register • Shift right one position multiplier register • Shift left one position multiplicand register • } / / for loop

  25. Shift Left Shift Right Multiplier: First version Multiplicand (64 bits) Multiplier 64-bitALU Control Product (64 bits)

  26. Shift Left Shift Right Multiplier: First version As we learnedin grade school Multiplicand (64 bits) Multiplier To get next bit ( LSB to MSB) 64-bitALU Control Product (64 bits)

  27. Explanations • Multiplicand register must be 64-bit wide because 32-bit multiplicand will be shifted 32 times to the left • Requires a 64-bit ALU • Product register must be 64-bit wide to accommodate the result • Contents of multiplier register is shifted 32 times to the right so that each bit successively becomes its least significant bit (LSB)

  28. Example (I) • Multiply 0011 by 0011 • StartMultiplicand Multiplier Product0011 0011 0000 • First additionMultiplicand Multiplier Product0011 0011 0011

  29. Example (II) • Shift right and leftMultiplicand Multiplier Product0110 0001 0011 • Second additionMultiplicand Multiplier Product0110 0001 1001 • 0110 + 011 = 1001

  30. Example (III) • Shift right and leftMultiplicand Multiplier Product1100 0000 1001 • Multiplier is all zeroes: we are done

  31. First Optimization • Must have a 64-bit ALU • More complex than a 32-bit ALU • Solution is not to shift the multiplicand • After each cycle, the LSB being added remains unchanged • Will save that bit elsewhere and shift the product register one position to the left after each iteration

  32. 1101 x 101 1101 00 1101001000101 Observe that the least significant bit added during each cycle remains unchanged Binary multiplication

  33. Algorithm • Clear contents of 64-bit product register • For (i = 0; i <32; i++) { • If (LSB of multiplier_register ==1) • Add contents of multiplicand register to product register • Save LSB of product register • Shift right one position both multiplier register and product register • } / / for loop

  34. Shift Right Multiplier: Second version Multiplicand Multiplier 32-bitALU Control+ Test Product (64 bits) Shift Right and Save

  35. Decimal Example (I) • Multiply 27 by 12 • StartMultiplicand Multiplier Product Result27 12 -- -- • First digitMultiplicand Multiplier Product Result27 12 54 --

  36. Decimal Example (II) • Shift right multiplier and productMultiplicand Multiplier Product Result27 1 5 4 • Second digitMultiplicand Multiplier Product Result27 1 32 4

  37. Decimal Example (III) • Shift right multiplier and productMultiplicand Multiplier Product Result27 0 3 24 • Multiplier equals zeroResult is obtained by concatenating contents of product and result registers • 324

  38. How did it work? • We learned • 2712 = 2710 + 272 = 2710 + 54 = 270 + 54 • Algorithm uses another decomposition • 2712 = 2710 + 272 = 2710 + 50 + 4 = (2710 + 50) + 4 = 320 + 4

  39. Example (I) • Multiply 0011 by 0011 • StartMultiplicand Multiplier Product Result0011 0011 -- -- • First bitMultiplicand Multiplier Product Result0011 0011 0011 --

  40. Example (II) • Shift right multiplier and productMultiplicand Multiplier Product Result0011 0001 0001 1- • Second bitMultiplicand Multiplier Product Result0011 0001 0100 1- Product register contains 0011 + 001 = 0100

  41. Example (III) • Shift right multiplier and productMultiplicand Multiplier Product Result0011 0000 010 01- • Multiplier equals zeroResult is obtained by concatenating contents of product and result registers • 1001 = 9

  42. Second Optimization • Both multiplier and product must be shifted to one position to the right after each iteration • Both are now 32-bit quantities • Can store both quantities in the product register

  43. Multiplier: Third version Multiplicand Control+ Test 32-bitALU Multiplier + Product Shift Right and Save

  44. Third Optimization • Multiplication requires 32 additions and 32 shift operations • Can have two or more partial multiplications • One using bits 0-15 of multiplier • A second using bits 16-31 then add together the partial results

  45. Multiplying negative numbers • Can use the same algorithm as before but we must extend the sign bit of the product

  46. Related MIPS instructions (I) • Integer multiplication uses a separate pair of registers(hi and lo) • mult $s0, $s1 • multiply contents of register $s0 by contents of register $s1 and store results in register pair hi-lo • multu $s0, $s1 • same but unsigned

  47. Related MIPS instructions (II) • mflo $s9 • Move contents of register lo to register $s0 • mfhi $s9 • Move contents of register hi to register $s0

  48. DIVISION

  49. Division • Implemented by successive subtractions • Result must verify the equality Dividend = Multiplier× Quotient + Remainder

  50. Decimal division (long division • What are the rules? • Repeatedly try to subtract smaller multiple of divisor from dividend • Record multiple (or zero) • At each step, repeat with a lower power of ten • Stop when remainder is smaller than divisor

More Related