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
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 … ten (π) … ten (e) ten or 1.0 ten x ,155,760,000 ten or ten x 10 9 Scientific Notation: A.AAAAA x 10 yyyy ten x ten x Correct Normalized Notation Incorrect (un-normalized) notation ten x ten x 10 -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 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
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
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 = x 2 17 A = x 2 17 B = x 2 17 B = x 2 17 in 2’s complement representation 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
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 = x 2 17 will be represented as follows: A B Sign + Magnitude Representation suitable for Quickly comparing two FP numbers
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 and B = 1.22 x 2 17 Let’s represent the exponent in two’s complement representation A B Although in fact A B Exponent Two’s Complement Representation Unsuitable for Quickly comparing two FP numbers
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 ( ) If a number K = k 1. 2 e E = e + bias For Single Precision, bias = 127 and for Double Precision, bias = A E = = 110 B E = = 144 A > B because of the biased representation of exponents Biased Representation Suitable
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
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
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 ?
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
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.
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 ± 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.
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 and B = 1.45 x How to proceed ? 1.85 x = x x = 1.45 x 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
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)
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)
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
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
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
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.