arithmetic and data representation

101
TU-Delft TI1400/11-PDS 1 Arithmetic and Data Representation ttp://www.pds.ewi.tudelft.nl/~iosup/Courses/2011_ti1400_3.ppt

Upload: trent

Post on 21-Jan-2016

42 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

1

Arithmetic and Data Representation

http://www.pds.ewi.tudelft.nl/~iosup/Courses/2011_ti1400_3.ppt

Page 2: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

2

Lecture 1Making functions

time

A,B Y

A

BYADD

nand gates

Page 3: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Lecture 1Making functions

Circuit Diagramhttp://xkcd.com/730/

Page 4: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Lecture 2Programmable device

2,1 3ProgrammableDevice

input stream output stream

programREAD(X)READ(Y)ADD(X,Y,Z)WRITE(Z)

• 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

Page 5: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Lecture 2Von Neumann Architecture

READ(X)READ(Y)ADD(X,Y,Z)WRITE(Z)

X: 1Y: 2Z: 3 • •

TEMP_A: TEMP_B: RESULT:

IR:

PC:

arithmeticunit

Central Processing Unit

CONTROL

Memory

Input

Output

Page 6: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Problem: How to Represent and Use Data?

1. Representation2. Arithmetic3. Conversion

Page 7: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

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)

Page 8: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

The FDIV Problem (1993) Arithmetic (Gone Wrong)

• 4195835.0/3145727.0 = 1.333 820 449 136 241 002 5 4195835.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)

Page 9: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

9

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

Page 10: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

14

Data Type Representation

1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects

Page 11: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

15

Number systems

0-9 8 0 11

277

-15

66 10110

01001110

Page 12: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

16

Question

• Which decimal number is associated with the bitstring <110> ?

Page 13: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

17

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 string X of n elements from : Xn n

• Applying Xn to a valuation function F

- signature: F : n Z

Page 14: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

18

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

Page 15: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

19

Question

What value has 267 ?

Page 16: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

20

Binary representation

• In computer, we have 2 ={0, 1}

• There are, however, many valuation functions F

277

-15

66

1110

0011

1010

F1

F2

F3

Page 17: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

21

Criteria for representations

• Let Xn = < xn-1, xn-2 , ....., x0 >• Range of natural numbers of X• Representation of 010

• Efficiency of implementation- Sign inversion : A -A- Addition: A+B- Extension: n bit m bit representation, m>n

X

Page 18: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

22

Data Type Representation

1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects

1.1. Sign-Magnitude1.2. Two’s Complement (2C)1.3. One’s Complement (1C)1.4. Excess-X

Page 19: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

23

1. Integer Representation

1.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

1 1 0 V = - 21 = -2

0 1 0 V = +21 = 2sign bit

X

Page 20: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

24

1. Integer Representation

1.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 1 0 V = - 22 +21 = -2

0 1 0 V = +21 = 2sign

X

Page 21: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

010

25

1. Integer Representation 1.2. 2C 2C negative

100 101 110 111 000 001 010 011-4 -3 -2 -1 0 1 2 3

Inversion rule:- invert all bits- add 1- General: A + Å = 2n

110 0011

+

Page 22: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

26

1. Integer Representation

1.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 1 0

0 1 0sign

X V = - 22 +1+21 = -1

V = +21 = 2

Page 23: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

27

1. Integer Representation 1.3. 1C 1C 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

Page 24: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

28

1. Integer Representation

1.4. Excess-x

• F: xn-12n-1 + xn-22n-2 + ....+ x020 - 2n-1

• Range: [-2n-1, 2n-1-1]• Zero: 10000.....

1 1 0

0 1 0

X V = 22 + 21 - 22 = 2

V = +21 - 22= -2

Page 25: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

29

Data Type Representation

1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects

1.5. Addition1.6. Sign Extension for 2C1.7. Other Representations1.8. Conversion

Page 26: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

30

1. Integer Representation

1.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

Page 27: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

31

1. Integer Representation 1.5. AdditionExample

X000001010011100101110111

2C0123-4-3-2-1

1C0123-3-2-10

SM01230-1-2-3

Ex-4-4-3-2-10123

Page 28: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

32

1. Integer Representation

1.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+1 bits equal, remove lefmost n-k bits- if not, overflow

• Example: n=6, k=44 = 000100 -> 01009 = 001001 -> overflow-6 = 111010 -> 1010

Page 29: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

33

1. Integer Representation 1.7. Other RepresentationsBinary Coded Decimaldecimal

0123456789

X0000000100100011010001010110011110001001

Page 30: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

34

Radix 2, 8, 10, 16binair

01

1011

100101110111

10001001101010111100110111101111

octal01234567

1011121314151617

dec.0123456789

101112131415

hex0123456789ABCDEF

L33T-Speak

http://www.jayssite.com/stuff/l33t/l33t_translator.html

h4x0r

1007

1337

hacker

money (loot)

elite (leet)

1. Integer Representation 1.7. Other Representations

Page 31: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

35

1. Integer Representation

1.8. Conversion [1/2]

• Simple, if one radix is power of other radix- binary to octal10100101001 = 010 100 101 001 = 2451oct

- binary to hexadecimal10100101001 = 0101 1010 1001 = 5A9hex =

0x05A9

Page 32: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

36

1. Integer Representation

1.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

Page 33: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

37

Data Type Representation

1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects

1.9. Binary Addition1.10. Operation Overflow

Page 34: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

38

1. Integer Representation 1.9. Binary AdditionAddition of binary digits

• Addition of two bits

• Sum s and Carry c

000

+

101

+

011

+

1 1 10

+

x yc s

+

Page 35: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

39

1. Integer Representation 1.9. Binary AdditionMore positions

xi

yi

ci

si

ci+1

.

...

. .

.

xi

001

yi

011

ci

011

si

001

ci+1

011

truth table (partial)

Page 36: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

40

1. Integer Representation 1.9. Binary AdditionBasic Adder

Full Adder

xi yi

ci

si

ci+1

Page 37: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

41

1. Integer Representation 1.9. Binary AdditionRipple Carry Adder

FA FA FA

Page 38: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

42

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)

Page 39: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

43

1. Integer Representation 1.10. Overflow Addition/subtraction

• 1C- all bits can be treated the same- when carry in last position add +1 to

Least Significant Bit (LSB)

0111 (7) 1100 (-3)------- +10011 1-------- + 0100 (4)

Page 40: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

44

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?)

Page 41: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

45

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

OVFL xn 1yn 1s n 1 x n 1y n 1sn 1

Page 42: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

46

Data Type Representation

1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects

2.1. Fixed Point2.2. Floating Point2.3. IEEE 754

Page 43: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

47

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,

Page 44: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

48

Question

Can conversion between decimal and binary numbers always be done exacly in

a. integers ?b. fractions ?

Page 45: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

49

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

Page 46: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

50

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 = radixm = mantissa, significant, or fractione = exponent

Page 47: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

51

2.2. Floating Point (2)

• r is normally 10 or 2• mantissa m is normalized: (1/r) ≤ m < 1

- e.g., if r = 2 then 0.5 ≤ m < 1- e.g., if r = 10 then

6.543 x 1010 is normalized, 654,300 x 105 is not

• Normalization is used to save as much as possible significant digits

Page 48: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

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!

Page 49: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Birth of a Standard (IEEE 754)

53

2.3. FP Representation/IEEE 754

• Need- Many competing hardware makers

IBM, 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

Page 50: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

IEEE Floating point standard (IEEE 754)

54

2.3. FP Representation/IEEE 754

e’

ms

32 bit23 bit8 bit

value = ±1s x 2e’-127 x m

Page 51: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

2.3. FP Representation/IEEE 754

Mantissa

• Implicit 1 bit just left of the point, so 24-bit total

• Mantissa m is normalized: (1/r) ≤ m < 1- So, including implicit 1., 1 ≤ m’ < 2

value = ±1s x 2e’-127 x m’

m’=1.m, m=[01][01]…

Page 52: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

2.3. FP Representation/IEEE 754

Exponent

• e: [1,254] in excess-127 representation->[-126,127]• e=0 and e=255 are reserved values• Floating point (radix point is “floating”)

- e=35 => m+1 means +235 (+34 359 738 368)- e=0 => m+1 means +20 (+1)

value = ±1s x 2e’-127 x m

Page 53: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

2.3. FP Representation/IEEE 754

Sign

• Sign is 0 for positive, 1 for negative• Floating-point numbers that in value differ only in

sign differ in IEEE 754 representation in the s bit

value = ±1s x 2e’-127 x m

Page 54: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Recap

Page 55: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Lecture 2Von Neumann Architecture

READ(X)READ(Y)ADD(X,Y,Z)WRITE(Z)

X: 1Y: 2Z: 3 • •

TEMP_A: TEMP_B: RESULT:

IR:

PC:

arithmeticunit

Central Processing Unit

CONTROL

Memory

Input

Output

Page 56: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

60

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 string X of n elements from : Xn n

• Applying Xn to a valuation function F

- signature: F : n Z

Page 57: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

61

Criteria for representations

• Let Xn = < xn-1, xn-2 , ....., x0 >• Range of natural numbers of X• Representation of 010

• Efficiency of implementation- Sign inversion : A -A- Addition: A+B- Extension: n bit m bit representation, m>n

X

Page 58: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

62

Data Type Representation

1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects

1.1. Sign-Magnitude1.2. Two’s Complement (2C)1.3. One’s Complement (1C)1.4. Excess-X

Page 59: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

63

1. Integer Representation 1.9. Binary AdditionBasic Adder

Full Adder

xi yi

ci

si

ci+1

Page 60: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

64

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,

Page 61: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

IEEE Floating point standard (IEEE 754)

65

2.3. FP Representation/IEEE 754

e’

ms

32 bit23 bit8 bit

value = ±1s x 2e’-127 x m

Page 62: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Recap over

Page 63: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Memory Jog 1: Select the correct answer

1 0011 0010 1101 1011 1101 11012 is …1.321BE0010

2.123EBDD16

3.231DB9916

4.132DBDD16

Page 64: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Memory Jog 2: Select the correct answer

10510 is to 101012 as 07FD16 is to …1.40910

2.204510

3.90410

4.204511

Page 65: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

2.3. FP Representation/IEEE 754

An Example: Number to IEEE 754• Input: 8.75• Step 1 (binary fixed-point): +1000.11 (.75=1x2-1+1x2-2)• Step 2 (normalize): 1.0001100… x 23

• Step 3 (bias the exponent): 3 + 127 = 130 (10000010)

Source: C. Hecker, Let’s Go to the (Floating) Point, Game Developer, 1996

Page 66: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

2.3. FP Representation/IEEE 754

An Example: IEEE 754 to Number

• Step 1: Split into IEEE 754 components

• Step 2 (Sign): 0 is positive, 1 is negative. Sign is …?• Step 3 (exponent): 128 (10000000)-127 (bias)=1• Step 4 (mantissa): 1.01010000… = 1.3125

(1x2-1=.5 1x2-2 =.25 1x2-3 =.125 1x2-4 =.0625)• Step 5 (result): (-1)1 x 1.3125 x 21 = -2.625

1 10000000 01010000 00000000 0000000

110000000 01010000 00000000 0000000In:

Page 67: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Excercise 1: Select the correct answer6.510 in IEEE 754 Single Precision is …

1.0 10000100 10101000000000000000000

2.0 10000001 10100000000000000000000

3.1 01000010 00101010100000000000000

4.0 01000010 00101010100000000000002

Page 68: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Excercise 2: Select the correct answer

0 1000 0100 0101 0000 0000 0000 0000 000 is the IEEE 754 Single Precision for …1.3210

2.4216

3.4210

4.3216Hint: http://www.h-schmidt.net/FloatApplet/IEEE754.html

Page 69: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

73

2.3. FP Representation/IEEE 754

FP exceptions [see wiki ieee 754]

0 00000000 00000000000000000000000 = +01 00000000 00000000000000000000000 = - 00 11111111 00000000000000000000000 = +Infinity1 11111111 00000000000000000000000 = - Infinity0 11111111 00000000000001000001000 = NaN

= ±1s x 2e’-126 x 0.fraction = ±1s x 2e’-127 x 1.fraction

Page 70: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

2.3. FP Representation/IEEE 754

Smallest Denormalized Positive Number

• Step 1: Split into IEEE 754 components

• Step 2 (Sign): 0 is positive, 1 is negative. Sign is +

• Step 3 (exponent): 0 (00000000)-126 (bias)=-126• Step 4 (mantissa): 0.00…1 = 2-23(23 bits)• Step 5 (result): (-1)0 x 2-23 x 2-126 = 2-149 ~ 10-45

0 00000000 00000000 00000000 0000001

000000000 00000000 00000000 0000001In:

Page 71: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

2.3. FP Representation/IEEE 754

Smallest Normalized Positive Number

• Step 1: Split into IEEE 754 components

• Step 2 (Sign): 0 is positive, 1 is negative. Sign is +• Step 3 (exponent): 1 (00000001)-127 (bias)=-126• Step 4 (mantissa): 1.00…0 = 1• Step 5 (result): (-1)0 x 1 x 2-126 = 2-126 ~ 10-38

(smallest denormalized was 2-149 ~ 10-45)

0 00000001 00000000 00000000 0000000

000000001 00000000 00000000 0000000In:

Page 72: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

76

2.3. FP Representation/IEEE 754

FP Operations

• Multiplication f1 x f2:

• Addition f1 + f2 (e2>e1, shift f1 by e1-e2):

m1 re1 m2 re2 m1 m2 re1 e2

m1 re1 m2 re2 m1 r shift re2 m2 re2

(m1 r shift m2)re2

Page 73: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

77

2.3. FP Representation/IEEE 754

FP accuracy

• Rounding: danger of false accuracy• e.g., 1.5*106 + 0.1 *10-3

1.5000000.000000 0001------------------- +1.500000

Page 74: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

78

2.3. FP Representation/IEEE 754

FP rounding

• Rounding = convert to lower precision (w/ error)

• Operations use guard bits -> used for rounding

0.1010.110--------- *0.011 110

Page 75: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

79

2.3. FP Representation/IEEE 754

FP-Rounding [1/2]

• Chopping: cut at guards bits (Round to zero)- error: 0 to almost -1 in LSB + Bias towards zero

• 0.011110 0.011• Von Neumann Rounding/Round to Nearest

- all guard bits 0 cut; else LSB:=1- error (-1,1) in LSB

• 0.01000 0.010 error: 0• 0.01110 0.011 error: 1/16• 0.0101110 0.011

Truncation error grows with the #ops

Page 76: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

80

2.3. FP Representation/IEEE 754

FP-Rounding [2/2]

• Round to nearest EVEN number (default IE3754)- Add 1 to LSB if truncated MSB is 1- error [-1/2,1/2] in LSB

• 0.001000 0.001 • 0.001011 0.001• 0.000111 0.001• 0.001111 0.010• 0.001100 0.010 (to nearest EVEN number)

• Round downward/upward/to nearest ODD number

Decimal:• 10.5 10• 11.5 12• -10.5 -10• -11.5 -12

Page 77: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

2.2. Floating PointFixed or Floating Point? (Revisited)

• Problem- What range is needed?- What precision is needed?- Need to replicate results later? (standard format needed)

• Platform AND Algorithm AND Input- How to optimize instructions/memory requirements?- What can the Platform do for this Algorithm AND Input?

• There’s no free lunch in computer science!

staff.ee.sun.ac.za/whsteyn/Papers2/CONPRA2299_final.pdf

Page 78: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

82

Data Type Representation

1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects

Page 79: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

83

3. Booleans

• Booleans have two values: true/false• Used in programming languages

- if COND then ACTION1 else ACTION2

• Storage takes usually a complete byte- false is often represented as 00000000- true by any other pattern

Page 80: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

84

Data Type Representation

1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects

4.1. ASCII4.2. Unicode4.3. Processing Characters

Page 81: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

85

4. Characters

4.1. ASCII

• American Standard Code for Information Interchange

• ASCII set 128 Characters (7 bit)

• Alphabet {a-zA-Z}• Numbers {0,...,9}• Special symbols, eg. <, =, >, $• Punctuation marks, ! ?, .• Data transmission codes (non printable

characters)

Page 82: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

86

4. Characters

4.1. ASCII• Designed 1960—1963 • Underlies the WWW (actually, as common part of UTF-8)

Page 83: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

4. Characters 4.1. ASCII An Example

The Demo, http://sloan.stanford.edu/MouseSite/1968Demo.html

Page 84: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

4. Characters 4.1. ASCII Raster Images as ASCII (Example)P3 # RGB.ppm 3 3 255 0 0 255 255 255 0 0 0 255 255 255 0 255 0 255 255 2550 0 0 255 255 255 0 0 255

PPM Format Specification, http://netpbm.sourceforge.net/doc/ppm.html

Page 85: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

89

4. Characters

4.2. Unicode

• Large set of international alphabets represented• Subset ISO 10646• 16 bits representation (see Joseph Becker, Unicode 1988, http://www.unicode.org/history/unicode88.pdf)

“a simple unambiguous fixed-length character encoding”

Page 86: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

90

4. Characters

4.2. Unicode/UTF-8

• Variable-width encoding for Unicode (1-4 bytes)

• 1 byte: First 128 characters are ASCII (US-ASCII)• 2 bytes: Next 1,920 characters encode Latin+diacritics,

Greek, Cyrillic, Coptic, …• 3 bytes: Most others (Chinese, Hindi, tagalog/PH, …)• 4 bytes: Historical/Exotic variants (Klingon? Actually,

Klingon has an unofficial 2-byte Unicode encryption, see www.evertype.com/standards/csur/klingon.html )

Page 87: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

91

4. Characters 4.2. Unicode/UTF-8 An Example: The Romanian Alphabet

• HTML:&#258; or&#x102;

http://bucovina.chem.tue.nl/romanian.utf8.htm

Page 88: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

92

4. Characters

4.3. Processing of a number [1/3]

• User types a number, e.g. 123 (decimal)• Computer receives ASCII codes

- ASCII-code of 1: 00110001- ASCII-code of 2: 00110010- ASCII-code of 3: 00110011

Page 89: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

93

4. Characters

4.3. Processing of a number [2/3]

• Program converts codes to binary values1*100 011001002*10 000101003*1 00000011123 01111011

• Perform operation (e.g. subtract 50)123 0111101150 0011001073 01001001

Page 90: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

94

4. Characters

4.3. Processing of a number [3/3]

• The number 73 must be fed back to the screen0 (*100) 000000007 (*10) 000001113 (*1) 00000011

• Computer sends following ASCII codes to screen- ASCII code of 0: 00110000- ASCII code of 7: 00110111

Page 91: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

95

Data Type Representation

1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects

Page 92: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

96

5. Arrays [1/3]

• Most programming languages support arraysA: array[0..N,0..M] of integer;

• Must be mapped onto linear memory• Row major order• Column major order

Page 93: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

97

5. Arrays [2/3]

row major

column major

array

Page 94: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

98

5. Arrays [3/3]

• Storage method is stored as part of array descriptor: so called dope vector.

begin addressno of dimensionsdim #1: low bounddim #1: high bounddim #1: multiplierdim #2: low boundetc

address fixed elementno of dimensionsstride (skipped memory

elements)

Page 95: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

99

Data Type Representation

1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects

Page 96: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

100

6. Objects

Records

Record

field 1: type 1;

field 2: type 2;

end record• Records can be stored with fixed off-set • Records can contain pointers to other data

structures. Results in linked lists

Page 97: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

6. Objects

Raster Images as Records (Example)• Simple, fixed representation:

- Height, Width- RGB(Alpha) data

03 03 FF 00 00 FF FF FF 00 00 00 FF FF FF 00 FF 00 FF FF FF00 00 00 FF FF FF 00 00 FF

Page 98: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

6. Objects

Raster Images as Records (Example)• Fixed-format representation with look-up

table:- Height, Width- Color map/look-up table (here, RGB)- Image = index in color map per pixel03 03

FF FF FF 00 00 00 FF 00 00 00 FF 00 00 00 FF

02 00 0100 03 0001 00 04

Map[02] = FF 00 00

Page 99: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

6. Objects

Raster Images as Records (Example)• Variable-format representation with look-up

table:- Image:

Height, Width, number of bits per color, …

- Color map structure: RGB/ARGB, number of entries, …

- Color map/look-up table- Image = index in color map per pixel

Page 100: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Fun With Hexadecimal NumbersColor Codes [1/2] http://en.wikibooks.org/wiki/LaTeX/Colors

Page 101: Arithmetic and Data Representation

TU-DelftTI1400/11-PDS

Fun With Hexadecimal NumbersColor Codes [2/2] http://johncfish.com/bggallery/otherchart/index.htm

http://www.visibone.com/