computer architecturefall 2008 © august 27, 2008 msakr/15447-f08/ cs 447 – computer architecture...

35
Computer Architecture Fall 2008 © August 27, 2008 www.qatar.cmu.edu/~msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Post on 22-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

August 27, 2008

www.qatar.cmu.edu/~msakr/15447-f08/

CS 447 – Computer Architecture

Lecture 4Computer Arithmetic (2)

Page 2: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Last Time

°Representation of finite-width unsigned and signed integers

°Addition and subtraction of integers

°Multiplication of unsigned integers

Page 3: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Unsigned Binary Multiplication

Page 4: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Execution of Example

Page 5: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Flowchart for Unsigned Binary Multiplication

Page 6: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Multiplying Negative Numbers°This does not work!

°Solution 1• Convert to positive if required

• Multiply as above

• If signs were different, negate answer

°Solution 2• Booth’s algorithm

Page 7: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Observation°Which of these two multiplications is more difficult?

98,765 x 10,001

98,765 x 9,999

°Note:

98,765 x 10,001 = 98,765 x (10,000 + 1)

98,765 x 9,999 = 98,765 x (10,000 – 1)

Page 8: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

In BinaryLet Q = d d d 0 1 1 . . . 1 1 0 d d

QL = d d d 1 0 0 . . . 0 0 0 d d

QR = 0 0 0 0 0 0 . . . 0 1 0 0 0

Page 9: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

In BinaryLet Q = d d d 0 1 1 . . . 1 1 0 d d

QL = d d d 1 0 0 . . . 0 0 0 d d

QR = 0 0 0 0 0 0 . . . 0 1 0 0 0

Then Q = QL – QR

And M x Q = M x QL – M x QR

Page 10: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

A bit more explanation

P = M x Q

where M and Q are n-bit two’s complement integers

e.g., Q = -23 q3 + 22 q2 + 21 q1 + 20 q0

which can be re-written as follows:

Q = 20(q-1–q0)+21(q0-q1)+22(q1-q2)+23(q2-q3)

leading to

P = M x 20 x (q-1 - q0) + M x 21 x (q0 - q1) +

M x 22 x (q1 - q2) +M x 23 x (q2 - q3)

Page 11: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Finally!P = M x 20 x (q-1 - q0) +

M x 21 x (q0 - q1) +M x 22 x (q1 - q2) +M x 23 x (q2 - q3)

qi qi-1 qi-1 – qi Action

0 0 0 Noop

0 1 1 Add M

1 0 -1 Subtract M

1 1 0 Noop

Page 12: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Example of Booth’s Algorithm

Page 13: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Booth’s Algorithm

Page 14: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Division

°More complex than multiplication

°Negative numbers are really bad!

°Based on long division

Page 15: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Division of Unsigned Binary Integers

001111

1011

00001101

100100111011001110

1011

1011100

Quotient

Dividend

Remainder

PartialRemainders

Divisor

Page 16: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Flowchart for Unsigned Binary Division

Page 17: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Real Numbers

°Numbers with fractions

°Could be done in pure binary• 1001.1010 = 24 + 20 +2-1 + 2-3 =9.625

°Where is the binary point?

°Fixed?• Very limited

°Moving?• How do you show where it is?

Page 18: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Floating Point

°+/- .significand x 2exponent

°Misnomer

°Point is actually fixed between sign bit and body of mantissa

°Exponent indicates place value (point position)

Sig

n bi

t

BiasedExponent

Significand or Mantissa

Page 19: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Floating Point Examples

Page 20: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Signs for Floating Point

°Sign Magnitude for Mantissa

°Exponent is in excess or biased notation

• e.g. Excess (bias) 127

• 8 bit exponent field

• Pure value range 0-255

• Subtract 127 to get correct value

• Range -127 to +128

Page 21: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Normalization

°FP numbers are usually normalized

° i.e. exponent is adjusted so that leading bit (MSB) of mantissa is 1

°Since it is always 1 there is no need to store it

° (c.f. Scientific notation where numbers are normalized to give a single digit before the decimal point

°e.g. 3.123 x 103)

Page 22: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

FP Ranges

°For a 32 bit number• 8 bit exponent

• +/- 2128 3.4 x 1038

°Accuracy• The effect of changing lsb of mantissa

• 23 bit mantissa 2-23 1.2 x 10-7

• About 6 decimal places

Page 23: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Examples of 32-bit Numbers (wikipedia)

Type Sign Exp Exp+Bias Exponent Significand Value

Zero 0 -127 0 0000 0000 000 0000 0000 0000 0000 0000 0

One 0 0 127 0111 1111 000 0000 0000 0000 0000 0000 1

Minus One 1 0 127 0111 1111 000 0000 0000 0000 0000 0000 -1

Smallest denormalized number * -127 0 0000 0000 000 0000 0000 0000 0000 0001 ±2−23 × 2−126 = ±2−149 ≈ ±1.4×10-45

Middle denormalized number * -127 0 0000 0000 100 0000 0000 0000 0000 0000 ±2-1 × 2-126 = ±2-127 ≈ ±5.88×10-39

Largest denormalized number * -127 0 0000 0000 111 1111 1111 1111 1111 1111 ±(1-2-23) × 2-126 ≈ ±1.18×10-38

Smallest normalized number * -126 1 0000 0001 000 0000 0000 0000 0000 0000 ±2-126 ≈ ±1.18×10-38

Largest normalized number * 127 254 1111 1110 111 1111 1111 1111 1111 1111 ±(2-2-23) × 2127 ≈ ±3.4×1038

Positive infinity 0 128 255 1111 1111 000 0000 0000 0000 0000 0000 +∞

Negative infinity 1 128 255 1111 1111 000 0000 0000 0000 0000 0000 -∞

Not a number * 128 255 1111 1111 non zero NaN

* Sign bit can be either 0 or 1            

Page 24: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Range and Precision (wikipedia)

Exponent Minimum Maximum Precision

0 1 2 1.19E-07

1 2 4 2.38E-07

2 4 8 4.77E-07

10 1024 2048 1.22E-04

11 2048 4096 2.44E-04

23 8388608 16777215 1

24 16777216 33554430 2

127 1.70E+38 3.40E+38 2.03E+31

Page 25: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Expressible Numbers

Page 26: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Density of Floating Point Numbers

Page 27: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

IEEE 754 Formats

Page 28: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

FP Arithmetic +/-

°Check for zeros

°Align significands (adjusting exponents)

°Add or subtract significands

°Normalize result

Page 29: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

FP Addition & Subtraction Flowchart

Page 30: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Floating point adder

Page 31: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Page 32: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Page 33: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

FP Arithmetic x/°Check for zero

°Add/subtract exponents

°Multiply/divide significands (watch sign)

°Normalize

°Round

°All intermediate results should be in double length storage

Page 34: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Floating Point Multiplication

Page 35: Computer ArchitectureFall 2008 © August 27, 2008 msakr/15447-f08/ CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)

Computer Architecture Fall 2008 ©

Floating Point Division