22
CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Embed Size (px)

Citation preview

Page 1: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

CSC 4250Computer Architectures

September 5, 2006Appendix H. Computer Arithmetic

Page 2: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Integers

Using an 8-bit pattern, how many different integers can we represent?

28

Correct? How can we represent negative integers? Use a sign bit How many different integers are there?

Page 3: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Small Numbers

How can we represent numbers that are smaller than 1?

Page 4: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Fixed Point Data Type

Bit pattern: 0100 0010 Binary point just to the right of sign bit (in text)

Can be anywhere inside bit pattern Binary point to far right of bit pattern What do we get?

Integer Value of bit pattern as integer: 0100 0010 Value of bit patt. as fixed point: 0100 0010

Page 5: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Blocked Floating Point

Fixed point → Low cost floating point Exponent kept in separate variable Exponent shared by a set of fixed point variables Applications? Overflow?

Page 6: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Saturating Arithmetic

In DSP, if result is too large to be represented, it is set to the largest representable number with the appropriate sign

What happens when we get a floating-point overflow?

Page 7: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

IEEE 754 Floating-Point (32 bit)

Value = (–1)s (1+f) 2(e–127)

Significand = 1+f Compare : scientific notation, say 3.45 × 106

Base = 2 So, 13 is represented by 1.101 × 23

with s=0, e=10000010, f=10100…00 Can you give one advantage of the implicit 1? Can you give one disadvantage of the implicit 1?

31 30 29 … 24 23 22 21 20 19 18 17 … 5 4 3 2 1 0

s e: exponent f: fraction

1 bit 8 bits 23 bits

Page 8: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

IEEE 754 Floating-Point (64 bit)

1 bit 11 bits 52 bits

Value = (–1)s (1+f) 2(e–1023)

Significand = 1+f

63 62 61 … 53 52 51 50 49 48 … 4 3 2 1

s e : exponent f : fraction

Page 9: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Base

IEEE: 2 Other bases: 16 IBM

8 Burroughs Normalize such that the first digit is nonzero For base 16, the first digit could be 1, 2, …, 14, 15 Need new symbols to represent 10,11, 12, 13, 14, 15 What is base 16 called?

Page 10: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Hexadecimal

What is hexa? Greek for six What is decimal? Latin for ten What is Latin prefix for six? Sexa What is old name for base 16? Sexidecimal Which company changed the name?

Page 11: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Hexadecimal System

For base 16, we have

1 = 1/16 × 161 with f = 0001 00…00,

2 = 2/16 × 161 with f = 0010 00…00,

4 = 4/16 × 161 with f = 0100 00…00,

8 = 8/16 × 161 with f = 1000 00…00,

16 = 1/16 × 162 with f = 0001 00…00, Disadvantage: As many as three leading zero bits Advantage: Base larger → Range larger 32 bit FP rep.: 1 sign bit, 7 exp. bits, 24 fraction bits

(exponent bias = 64).

Page 12: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Floating Point Representation of 1 IEEE:

s = 0, e = 01111111, f = 00000…00;

Value = (1) 2(127–127) = 1. IBM Hexadecimal:

s = 0, e = 1000001, f = 00010000…00;

Value = (1/16) 16(65–64) = 1.

Page 13: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

IEEE FP Representation of One to Sixteen Number s e f

1 0 01111111 00000000000000000000000

2 0 10000000 00000000000000000000000

3 0 10000000 10000000000000000000000

4 0 10000001 00000000000000000000000

5 0 10000001 01000000000000000000000

6 0 10000001 10000000000000000000000

7 0 10000001 11000000000000000000000

8 0 10000010 00000000000000000000000

9 0 10000010 00100000000000000000000

10 0 10000010 01000000000000000000000

11 0 10000010 01100000000000000000000

12 0 10000010 10000000000000000000000

13 0 10000010 10100000000000000000000

14 0 10000010 11000000000000000000000

15 0 10000010 11100000000000000000000

16 0 10000011 00000000000000000000000

Page 14: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Integer Comparisons of FP Number Ease to use Sorting Sign bit is most significant:

Easy to check if number is greater than, less than, or equal to zero

Exponent before fraction: Larger exponent → larger number Use bias such that exponent values ≥ 0 1 ≤ e ≤ 254 → –126 ≤ e – bias ≤ 127 e = 0 and e = 255?

Page 15: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Representation of Zero

All bits (except sign bit) equal zero e = 0 → zero exponent field Zero fraction field (no implicit 1) Plus and minus zero

Page 16: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Denormal Numbers

e = 0 → zero exponent field f ≠ 0 → no implicit 1 Value = (–1)s *f *2–126

Gradual underflow

Page 17: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

IEEE Representation

Fill in the blanks

Number s e f

0

1

2–1

2–2

2–125

2–126

2–127

2–128

2–129

2–149

Page 18: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Underflow to Zero

In old days, a FP number that underflowed could be set to zero

An old “fast” test for equality:

If a − b = 0, then a = b Test would fail if a − b underflowed

Page 19: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Infinity

e = 11…1 → maximum exponent field Zero fraction field Plus and minus infinity 1/0 = ∞; 1+∞ = ∞ Useful in trigonometry:

sin(tan–1∞) = sin π/2 = 1

Page 20: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

NaN

Not a Number e = 11…1 → maximum exponent field Nonzero fraction field Can you give an operation that generates NaN? What is 1+NaN?

Page 21: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Examples

Largest positive number:s = 0, e = 11111110, f = 11111…11;Value = (1+2–1+…+2–23) 2(254–127)

= (2−2–23) 2127 ≈ 2128 ≈ 2.56×1038

One:s = 0, e = 01111111, f = 00000…00;Value = (1) 2(127–127) = 1.

Smallest positive normal number:s = 0, e = 00000001, f = 00000…00;Value = (1) 2(1–127) = 2–126 ≈ 1.6×10–38

Page 22: CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic

Examples (2)

Largest positive denormal number:s = 0, e = 00000000, f = 11111…11;Value = (2–1+…+2–23) 2–126

= (1−2–23) 2–126 ≈ 2–126

Differs from smallest normal by 2–149

Smallest positive denormal number:s = 0, e = 00000000, f = 00000…01;Value = (2–23) 2–126 = 2–149 ≈ 2×10–45

Plus zero:s = 0, e = 00000000, f = 00000…00.