1.03k likes | 1.19k Views
Arithmetic and Data Representation. http://www.pds.ewi.tudelft.nl/~iosup/Courses/2011_ti1400_3.ppt. Lecture 1 Making functions. nand gates. A. Y. ADD. B. Y. A,B. . time. 2. Lecture 1 Making functions. Circuit Diagram http://xkcd.com/730/. Lecture 2 Programmable device.
E N D
Arithmetic and Data Representation http://www.pds.ewi.tudelft.nl/~iosup/Courses/2011_ti1400_3.ppt
Lecture 1Making functions nandgates A Y ADD B Y A,B time 2
Lecture 1Making functions Circuit Diagram http://xkcd.com/730/
Lecture 2Programmable device Programmable Device 3 2,1 • READ(X) means read next input value from input stream and store it internally as variable X • WRITE(X) means put value in variable X on output stream • ADD(X,Y,Z) means assign value of X+Y to Z input stream output stream program READ(X) READ(Y) ADD(X,Y,Z) WRITE(Z)
X: 1 Y: 2 Z: 3 • • READ(X) READ(Y) ADD(X,Y,Z) WRITE(Z) Lecture 2Von Neumann Architecture Memory Central Processing Unit TEMP_A: TEMP_B: RESULT: arithmetic unit Input IR: CONTROL Output PC:
Problem: How to Represent and Use Data? • Representation • Arithmetic • Conversion
The Y2K Problem/The Millenium Bug (2000)Representation (Gone Wrong) • Abbreviating the year as two-digits instead of four • 99 instead of 1999 • 00 instead of 2000 • Cost saving (space is money) • How to represent 2096? 98 < 99, but 99 < 00?! • Problem identified by Spencer Bolles in 1985 • Y2K acronym used by David Eddy in 1995 • $150B in US (IDC estimate)
The FDIV Problem (1993) Arithmetic (Gone Wrong) • 4195835.0/3145727.0 = 1.333 820 449 136 241 002 54195835.0/3145727.0 = 1.333 739 068 902 037 589 4 • Pentium used the radix-4 SRT algorithm (which is fast) • “The [problem] can be traced to five missing entries in a lookup table” • Solution: recall defective processors ($350M) or scale operands and use 80-bit extended-precision internal Pentium format (formats are discussed in this lecture) • … oh, and hardware errors still happen (latest, Intel’s Cougar/ Sandy Chipsets in February 1, 2011—est. cost $700M)
Ariane 5 (1996)Conversion (Gone Wrong) June 4, 1996: Ariane 501 Satellite Launch explodes 40s into the flight sequence Cost: $370M “The internal SRI* software exception was caused during execution of a data conversion from 64-bit floating point to 16-bit signed integer value. The floating point number which was converted had a value greater than what could be represented by a 16-bit signed integer.” Inquiry Board Report 9
Data Type Representation • Integer numbers • Real numbers • Booleans • Characters • Composite types (e.g. arrays) • Objects
Number systems 0 -9 0 1 8 1 10110 66 277 0100 -15 1110
Question • Which decimal number is associated with the bitstring <110>?
Definition and Representation Integers (Z) = Set of natural numbers including 0 and their non-zero negatives Integer numbers are generally represented by: • An alfabet • A stringX of n elements from : Xn n • Applying Xn to a valuation functionF • signature: F : nZ
Examples • R = {I, V, M, ....} • VI = 6 • IV = 4 • 10 = {0, 1, 2, 3, .....,9} • 27710 = 2*102 + 7*101 + 7*100 • Many sets and valuation functions F possible radix
Question What value has 267?
Binary representation • In computer, we have 2 ={0, 1} • There are, however, many valuation functions F 1110 F1 66 F2 0011 277 -15 F3 1010
Criteria for representations • LetXn = < xn-1, xn-2 , ....., x0 > • Range of natural numbers of X • Representationof 010 • Efficiencyof implementation • Sign inversion : A -A • Addition: A+B • Extension: n bitm bit representation, m>n
Data Type Representation Integer numbers Real numbers Booleans Characters Composite types (e.g. arrays) Objects 1.1. Sign-Magnitude 1.2. Two’s Complement (2C) 1.3. One’s Complement (1C) 1.4. Excess-X 22
X 1 0 1 V = - 21 = -2 sign bit V = +21 = 2 1 0 0 1. Integer Representation1.1. Sign Magnitude • F: sign(xn-1){xn-22n-2 + ....+ x020} sign(1) = - and sign(0)= + • Range: [-(2n-1 -1),2n-1 -1] • Zero: 100...and 000... • Inversion: change only sign bit
X 1 0 1 V = - 22 +21 = -2 sign V = +21 = 2 1 0 0 1. Integer Representation1.2. Two’s complement (2C) • F:-xn-12n-1+xn-22n-2+ ....+ x020 • Range: [-2n-1,2n-1-1] • Zero: 000...(only a single representation)
1. Integer Representation 1.2. 2C2C negative 100 101 110 111 000 001 010 011 -4 -3 -2 -1 0 1 2 3 Inversion rule: - invert all bits - add1 - General:A + Å = 2n 110 001 1 + 010
X V = - 22 +1+21 = -1 1 0 1 sign V = +21 = 2 1 0 0 1. Integer Representation1.3. One’s Complement (1C) • F:-xn-1(2n-1-1)+xn-22n-2+ ....+ x020 • Range:[-(2n-1-1),2n-1-1] • Zero: 111...and 000...
1. Integer Representation 1.3. 1C1C negative 100 101 110 111 000 001 010 011 -3 -2 -1 0 0 1 2 3 Inversion rule: - invert all bits - General:A + Å = 2n-1 110 001
X V = 22 + 21 - 22 = 2 1 0 1 V = +21 - 22= -2 1 0 0 1. Integer Representation1.4. Excess-x • F:xn-12n-1+xn-22n-2+ ....+ x020- 2n-1 • Range:[-2n-1,2n-1-1] • Zero: 10000.....
Data Type Representation Integer numbers Real numbers Booleans Characters Composite types (e.g. arrays) Objects 1.5. Addition 1.6. Sign Extension for 2C 1.7. Other Representations 1.8. Conversion 29
1. Integer Representation1.5. Addition • SM • Sign and Magnitude are processed separately • Opposite sign operands: subtraction • 2C • All bits can be added, regardless of position • 1C • All bits can be added, regardless of position • When carry at position 2n, add 1 to result
1. Integer Representation1.6. Sign Extension (for 2C) • Represent n-bit in k-bit number • If k>n,extend sign bit to the left • If k<n, • n-k+1bits equal, remove lefmost n-k bits • if not, overflow • Example: n=6, k=4 4 = 000100 -> 0100 9 = 001001 -> overflow -6 = 111010 -> 1010
1. Integer Representation 1.7. Other RepresentationsBinary Coded Decimal
http://www.jayssite.com/stuff/l33t/l33t_translator.html h4x0r 1007 1337 hacker money (loot) elite (leet) 1. Integer Representation 1.7. Other Representations Radix 2, 8, 10, 16 L33T-Speak
1. Integer Representation1.8. Conversion [1/2] • Simple, if one radix is power of other radix • binary to octal 10100101001 = 010 100 101 001 = 2451oct • binary to hexadecimal 10100101001 = 0101 1010 1001 = 5A9hex = 0x05A9
1. Integer Representation1.8. Conversion [2/2] • From binary to decimal 10110101001 = 210 + 28 + 27 + 25+ 23+ 20 = 1449 • From decimal to binary (first method) 1449 = 210 +425 = 210 +28 +169 =..= 10110101001 • From decimal to binary (second method) 1449 = 2*724 +1 724 = 2*362 +0 ...... =...... 1 = 0*1 +1 LSB
Data Type Representation Integer numbers Real numbers Booleans Characters Composite types (e.g. arrays) Objects 1.9. Binary Addition 1.10. Operation Overflow 37
1. Integer Representation 1.9. Binary AdditionAddition of binary digits • Addition of two bits • Sum s and Carry c 1 1 10 x y c s 1 0 1 0 1 1 0 0 0 + + + + +
. xi . . . yi . ci+1 ci . . si 1. Integer Representation 1.9. Binary AdditionMore positions truth table (partial)
1. Integer Representation 1.9. Binary AdditionBasic Adder xi yi ci Full Adder ci+1 si
FA FA FA 1. Integer Representation 1.9. Binary AdditionRipple Carry Adder
1. Integer Representation 1.10. Overflow Addition/subtraction • 2C • Simple, all bits can be treated the same • Carry last position can be ignored • Subtraction by first complementing the subtrahend(minuend – subtrahend = difference) • Example: 0111 (7) 1101 (-3) ------- + 10100 (4)
1. Integer Representation 1.10. Overflow Addition/subtraction 0111 (7) 1100 (-3) ------- + 10011 1 -------- + 0100 (4) • 1C • all bits can be treated the same • when carry in last position add +1toLeast Significant Bit (LSB)
1. Integer Representation 1.10. Overflow Overflow detection • Suppose we have 2C notation • Carry-out MSB is not sufficient to indicate overflow 0111 (7) 0100 (4) -------- + 01011 (-5?)
1. Integer Representation 1.10. Overflow Overflow • Overflow occurs in 2C iff • Both operand have the same sign • The sum bit in the MSB position has the opposite sign
Data Type Representation Integer numbers Real numbers Booleans Characters Composite types (e.g. arrays) Objects 2.1. Fixed Point 2.2. Floating Point 2.3. IEEE 754 46
2.1. Fixed Point (Fractions) • Real numbers (R) = value in a continuous space (vs. Integers—discrete space) • F: -xn-120 + xn-22-1+ ....+x 02-n+1 • Range: [-1, 1-2-n+1] • But: very small numbers cannot be represented, …
Question Can conversion between decimal and binary numbers always be done exacly in a. integers ? b. fractions ?
Answer a) Can conversion between decimal and binary numbers always be done exacly in integers? -> Yes b) Can conversion between decimal and binary numbers always be done exacly in fractions? -> No • Counter-example: 0.210 =0.00110011001100.....2
2.2. Floating Point (1) • Need a represention for: • very large numbers • real numbers • With a limited number of bits • Floating point notation: G = m.re r = radix m = mantissa, significant, or fraction e = exponent
2.2. Floating Point (2) • r is normally 10 or 2 • mantissa m is normalized: (1/r) ≤ m < 1 • e.g., if r = 2then 0.5 ≤ m < 1 • e.g.,if r = 10then 6.543 x 1010 is normalized,654,300 x 105 is not • Normalization is used to save as much as possible significant digits
2.2. Floating PointFixed or Floating Point? • Metrics • Range = ratio largest:smallest number • Precision = how many significant bits for representation • Floating point idea: separate the exponent from the mantissa. Vs Fixed point: • Wider range (through exponent) • Lower precision (mantissa has fewer bits) • There’s no free lunch in computer science!
Birth of a Standard (IEEE 754) 2.3. FP Representation/IEEE 754 • Need • Many competing hardware makersIBM, Cray, CDC, Intel, … • Various range and accuracy • Work Group initiated by Intel 1976, led by W. Kahan • Correct FP • Developers can write portable code AND prove correctness • Result: IEEE 754 Read: IEEE 754: An Interview with William Kahan, IEEE Computer 1998 53
2.3. FP Representation/IEEE 754 IEEE Floating point standard (IEEE 754) value = ±1sx 2e’-127 xm s e’ m 8 bit 23 bit 32 bit