Presentation is loading. Please wait.

Presentation is loading. Please wait.

King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department.

Similar presentations


Presentation on theme: "King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department."— Presentation transcript:

1 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 1 COE 308 Floating Point

2 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 2 COE 308 The World is not just Integers 3.14159265… ten (π) 2.71828… ten (e) 0.0000000001 ten or 1.0 ten x 10 -9 3,155,760,000 ten or 3.15576 ten x 10 9 Scientific Notation: A.AAAAA x 10 yyyy 3.15576 ten x 10 9 1.0 ten x 10 -9 Correct Normalized Notation Incorrect (un-normalized) notation 31.5576 ten x 10 8 0.1 ten x 10 -8

3 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 3 COE 308 Floating Point Scientific Notation in binary: 1.XXXXXXXXX. 2 yyyyyyy Representation: Sign: S Exponent: E Significand: F (-1) S x (1.0 + F) x 2 E 1.0 > F ≥ 0

4 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 4 COE 308 Floating Point Representation SignExponent Significand:F GOAL  Quickly compare two FP numbers By considering them unsigned integers Options: Significand: Sign + Magnitude Two’s Complement Exponent: Sign + Magnitude Exponent Biased Exponent Significand:F Two’s Complement Sign + Magnitude

5 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 5 COE 308 Floating Point Representation - Two’s Complement - Example: Consider the following two numbers: A = 1.32 x 2 17 B = - 1.22 x 2 17 A = 10101000111101011100001 x 2 17 B = - 00111000011010001111010 x 2 17 B = 11000111100101110000110 x 2 17 in 2’s complement representation 0 00010001 10101000111101011100001 0 00010001 11000111100101110000110 A B Although in fact A > B because A>0 and B A if we consider them as two 32-bits unsigned integers. Two’s Complement Representation Unsuitable for Quickly comparing two FP numbers

6 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 6 COE 308 Floating Point Representation - Sign + Magnitude - A > B because of the sign bit. In Sign + Magnitude representation, the two numbers A = 1.32 x 2 17 and B = - 1.22 x 2 17 will be represented as follows: 0 00010001 10101000111101011100001 1 00010001 00111000011010001111010 A B Sign + Magnitude Representation suitable for Quickly comparing two FP numbers

7 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 7 COE 308 How about the Exponent - Two’s Complement - In previous examples, A and B exponents were positive. How about if one of the exponents is negative? Example:A = 1.32 x 2 -17 and B = 1.22 x 2 17 Let’s represent the exponent in two’s complement representation 0 11101111 10101000111101011100001 0 00010001 00111000011010001111010 A B Although in fact A B Exponent Two’s Complement Representation Unsuitable for Quickly comparing two FP numbers

8 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 8 COE 308 How about the Exponent - Biased Representation - In a biased representation, we add an offset to the exponent so that: The lowest negative exponent is represented with the value ONE (00000001) If a number K = k 1. 2 e  E = e + bias For Single Precision, bias = 127 and for Double Precision, bias = 1023 0 01101110 10101000111101011100001 0 10010000 00111000011010001111010 A E = -17 + 127 = 110 B E = 17 + 127 = 144 A > B because of the biased representation of exponents Biased Representation Suitable

9 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 9 COE 308 Floating Point Representation - Summary - Sign:SExponent:ESignificand:F Sign + Magnitude better than 2’s complement Exponent represented in a bias notation (-1) S x (1.0 + F) x 2 E-bias IEEE-754 Standard

10 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 10 COE 308 MIPS Floating Point Formats S:1 bitE: 8 bitsF: 23 bits Follows IEEE-754 floating point standard Single Precision S:1 bit E: 11 bitsF: 20 bits Double Precision F: 32 bits

11 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 11 COE 308 Conversion How to convert from decimal to FP ?

12 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 12 COE 308 Representation of Zero Zero is represented with: –Sign bit at 0 –Exponent field composed of all bits at 0 –Significand bits are too all at 0 0 00000000 00000000000000000000000

13 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 13 COE 308 Two Issues with FP Overflow occurs when the exponent of the result is larger than the available bits for the exponent field Underflow occurs when the result is smaller than the smallest number that can be represented and will yield a significand of 0s.

14 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 14 COE 308 Representation of Exceptions Infinity –Represented as a number with an exponent of 255 (Single Precision) or 2047 (Double Precision) –The sign determines whether it is ±  0 11111111 00000000000000000000000 +  1 11111111 00000000000000000000000 -  NaN: Not a Number. Used to represent errors and exceptions –Represented with maximum E and F≠0 –Result of exception like division by 0 or square root of negative number –Operation on a NaN will result in a NaN.

15 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 15 COE 308 Floating Point Addition Example: Add two numbers A = 1.85 x 10 25 and B = 1.45 x 10 17 How to proceed ? 1.85 x 10 25 = 185000000.00 x 10 17 1.45 x 10 17 = 1.45 x 10 17 ------------------------------------------------------ Need to align the two significands. Alignment of significands to have same exponents So that Addition becomes POSSIBLE Alignment of significands in binary is performed by shifts Smaller exponent number is shifted to the right

16 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 16 COE 308 Addition or Subtraction ? A and B may not be of the same sign Need also want to simply provide subtraction Need to Select between A and B to determine which one needs to be shifted right Need to Select between A and B to know which one needs to be complemented (2’s complement) Needs also to determine the sign of the result (whether to complement the result or not)

17 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 17 COE 308 Floating Point Addition Compare exponents Shift smaller number to the right (increment its exponent) until its exponent matches the larger exponent Complement one of the two operands (if needed) Add the two significands Loop to Normalize the result –Shift (left/right) to normalize the result –Detect overflow/underflow –Round the significand to the appropriate number of bits Complement the result (if needed)

18 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 18 COE 308 Floating Point Addition Circuit - complement + Sign Determination Unit Normalization and Rounding complement Exponent Compare Unit Shift

19 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 19 COE 308 Floating Point Multiplication Add the two biased exponents Subtract one time a bias to get a biased exponent –Because E 1 = e 1 + bias and E 2 = e 2 + bias –E 1 + E 2 = e 1 + e 2 + 2x bias Multiply the significands Loop to Normalize the result –Shift (left/right) to normalize the result –Detect overflow/underflow –Round the significand to the appropriate number of bits Set the sign of the product

20 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 20 COE 308 Floating Point Multiplication Circuit + Sign Determination Unit Normalization and Rounding Exponent Addition Unit Bias - Integer Multiplication Circuit

21 King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 6 – Floating Point 21 COE 308 Floating Point Instructions in MIPS 32 Floating Point Registers $f0, …, $f31 add.s, sub.s, mul.s and div.s: single precision add.d, sub.d, mul.d and div.d: double precision lwc1, swc1: load/store fp to/from memory bc1t, bc1f: branch if FP cond true/false c.lt.s, c.lt.d: compare single/double precision.


Download ppt "King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department."

Similar presentations


Ads by Google