Lecture07 - 0306-381 Applied Programming • Floating-Point...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 0306-381 Applied Programming • Floating-Point Addition • Floating-Point Division Floating-Point Nomenclature Decimal Scientific Notation X = a ´ 10b – a: Significand or mantissa – b: Exponent • Binary Scientific Notation X = a ´ 2b – a: Binary significand or mantissa – b: Binary exponent Basis of binary floating-point representations 2 IEEE-754 Floating Point Format Normalized Value = (-1)S ´ 2E - Bias ´ 1.F • S: sign • E: biased exponent • F: fractional part of significand Special case: zero value—detection • Only normalized allowed E = 0 for normalized values • Denormalized allowed E = 0 and F = 0 3 Floating-Point Addition: Operands Two floating-point numbers • A = (-1)SA ´ 2EA - Bias ´ 1.FA • B = (-1)SB ´ 2EB - Bias ´ 1.FB Sum • C =A + B = (-1)SC ´ 2EC - Bias ´ 1.FC 4 Floating-Point Addition A + B = [- 1] ´ 2 SA SA ( = ( [- 1] E A - Bias ´1.F A + [- 1] ´ 2 EB - Bias ´1.F B SB ´ 2 E A - Bias ´1.F A )( )+ ) ) = ( [(- 1) æ ö 2 E A - Bias SB E B - Bias ç [- 1] ´ E - Bias ´ 2 ´ 1.F B ÷ ç ÷ 2A è ø SA ´ 1.F A ´ 2 E A - Bias + ( [(- 1) ´1.F ]´ 2 )+ ( [(- 1) ´1.F ´ 2 ]´ 2 ) = ( [ (- 1) ´1.F ]+ [ (- 1) ´ 1.F ´ 2 = SA A E A - Bias EB - E A SB B E A - Bias SA SB A B æé 2 EB - Bias ù E A - Bias ö ç ê(- 1)S B ´ 1.FB ´ E - Bias ú ´ 2 ÷ ç ÷ 2A û èë ø EB - E A )´ 2 E A - Bias 5 Floating-Point Addition Algorithm: Sign A + B = [(-1) S A ´1.F A] + [(-1) S B ´1.FB ´ 2 EB - E A ] ´ 2 E A - Bias C = (- 1) ´ 2 SC EC - Bias ( ) ´1.F C If EA > EB • A and B have same sign (SA == SB) C has that same sign (SC = SA; SC = SB) • A and B have different signs (SA != SB) C has sign of A or B with maximum magnitude (SC = SMAX(A,B)) • A and B have different exponents (EA != EB) C has sign of number with largest exponent (SC = SMAX(EA,EB)) • A and B have same exponent (EA == EB) C has sign of number with largest significand (SC = SMAX(FA,FB)) 6 Floating-Point Addition Algorithm: Exponent (perhaps not final value) A + B = [(-1) S A ´1.F A] + [(-1) S B ´1.FB ´ 2 EB - E A ] ´ 2 E A - Bias C = (- 1)SC ´ 2 EC - Bias ´1.F C If EA > EB ( ) Choose largest exponent (augend or addend) • EC = Maximum of EA and EB Exponent may need adjustment later. Why? 7 Floating-Point Addition Algorithm: Significand Alignment A + B = [(-1) S A ´1.F A] + [(-1) S B ´1.FB ´ 2 EB - E A ] ´ 2 E A - Bias C = (- 1)SC ´ 2 EC - Bias ´1.F C – Shift smaller number right |EB-EA| bits – Maximum exponent now applies to both numbers If EA > EB ( ) • Align binary point of smaller number to larger number • Significand outcome – Maximum of A and B unchanged – Minimum of A and B aligned to maximum • Minimum = Minimum ´ 2-|EB-EA| • Minimum = Minimum >> |EB-EA| 8 Floating-Point Addition Algorithm: Aligned Significand Addition A + B = [(-1) S A ´1.F A] + [(-1) S B ´1.FB ´ 2 EB - E A ] ´ 2 E A - Bias C = (- 1)SC ´ 2 EC - Bias ´1.F C If EA > EB “One” position ( ) • Add significands (not just fractional parts) – Both of same sign: add – Different signs: subtract smaller from larger • Normalize significand result, if necessary If most significant “1” digit is not in “one’s” position • Shift significand bits to get first significant “1” in “one’s” position • Adjust result exponent (EC) to account for shifting • FC is fractional part of normalized significand 9 Floating-Point Addition Algorithm: Check Result A + B = [(-1) S A ´1.F A] + [(-1) S B ´1.FB ´ 2 EB - E A ] ´ 2 E A - Bias C = (- 1)SC ´ 2 EC - Bias ´1.F C Include in Homework Four Ignore for Homework Four If EA > EB ( ) • Check for zero (C == 0) – Significand is zero – Set exponent to zero (EC = 0) • Overflow Exponent greater than 127 (EC > 254) • Underflow Exponent less than -126 (EC < 1) 10 Floating-Point Addition Example IEEE-754 Single Precision Add 2048.12510 and -.7510 S 100000000000.001 + -.11 ----------------11111111111.011 E F 0 10001010 00000000000001000000000 1 01111110 10000000000000000000000 • Sign Positive: (EA > EB) Þ SC ¬ SA • Exponent (for addition) 138: (EA > EB) Þ EC ¬ EA • Fraction 1.00000000000001000000000 -0.00000000000110000000000 Significand alignment -------------------------0.11111111111011000000000 2048.125 -.75 --------2047.375 Normalization 1.11111111110110000000000 11 S=0; E=10001001; F=11111111110110000000000 0306-381 Applied Programming • Floating-Point Addition FFloating-Point Division Floating-Point Division Two floating-point numbers • A = (-1)SA ´ 2EA - Bias ´ 1.FA • B = (-1)SB ´ 2EB - Bias ´ 1.FB Quotient • C =A ¸ B = (-1)SC ´ 2EC - Bias ´ 1.FC A ´ B = [- 1]S A ´ 2 E A - Bias ´1.F A ¸ [- 1]S B ´ 2 EB - Bias ´ 1.F B SA ( = ([- 1] ¸ [- 1]S B ´ 2 E A - Bias ¸ 2 EB - Bias ´ (1.F A¸1.F B ) ´2 E A - Bias - EB + Bias )( )( ) ) = (- 1) S A -SB 1.F A ´ 1.F B 13 Floating-Point Division Algorithm: Sign A ¸ B = (- 1) S A -SB ´2 E A - Bias - EB + Bias 1.F A ´ 1.F B C = (- 1)SC ´ 2 EC - Bias ´ 1.F C • Look at signs of factors (dividend and divisor) – Same signs ® Positive result – Different signs ® Negative result • S field outcome: (SA, SB) ® SC – Same signs ® Positive result (0,0) ® 0 (0,1) ® 1 or or (1,1) ® 0 (1,0) ® 1 – Different signs ® Negative result Integer operation on SA and SB: SC ¬ SA Å SB C bit-wise XOR: SC = SA ^ SB 14 Floating-Point Division Algorithm: Exponent (perhaps not final value) A ¸ B = (- 1) S A -SB SC ´2 E A - Bias - EB + Bias 1.F A ´ 1.F B C = (- 1) ´ 2 EC - Bias ´ 1.F C Subtract exponents and adjust for bias • EC - Bias = EA - Bias - EB + Bias EC = EA - EB + Bias Exponent may need adjustment later. 15 Floating-Point Division Algorithm Significand A ¸ B = (- 1) S A -SB ´2 E A - Bias - EB + Bias 1.F A ´ 1.F B C = (- 1)SC ´ 2 EC - Bias ´ 1.F C • Divide significands (not just fractional parts)? What result can be expected? 16 Floating-Point Division Faster Algorithm Motivation A ¸ B = (- 1) S A -SB ´2 E A - Bias - EB + Bias 1.F A ´ 1.F B C = (- 1)SC ´ 2 EC - Bias ´ 1.F C Relative operational timings – Integer add: 1 time unit – Floating-point add: 4 time units – Floating-point multiply: 6 time units – Floating-point divide: 13 time units Why not implement as multiplication? 17 Floating-Point Division Faster Algorithm Divisor ® Multiplier A ¸ B = [- 1] ´ 2 SA ( E A - Bias ´ 1.F A ´ ) 1 (- 1) SB ´ 2 EB - Bias ´ 1.F B C = (- 1) ´ 2 SC EC - Bias ´ 1.F C • Determine reciprocal of divisor • Multiply dividend by reciprocal How is the reciprocal of a floating-point number computed? 18 Reciprocal Calculation A ¸ B = [- 1] ´ 2 SA ( E A - Bias ´ 1.F A ´ ) 1 (- 1) SB ´2 EB - Bias ´ 1.F B C = (- 1) ´ 2 SC EC - Bias ´ 1.F C Reciprocal Þ raised to the power of -1 1 n -1 =x = x -1´n = x -n xn () \ Negate exponent of floating-point number? 1 (- 1) SB ´2 EB - Bias ´ 1.F B = (- 1) ´ 2 SB ? -( EB - Bias ) ´ 1.F B 19 No, but this idea is motivation for an approximation. Reciprocal Approximation "N³0 1 1.F ´ 2 N > 1 2.0 ´ 2 -N N = 1 1.0 ´ 2 N +1 = 1.0 ´ 2 - N -1 1.0 ´ 2 1.0 ´ 2 1 1 = ³ N 1.0 ´ 2 1.F ´ 2 N 1 ³ > 1.0 ´ 2 - N -1 1.F ´ 2 N -N 1.0 ´ 2 - N > 1. 1 ´ 2 - N -1 > 1.0 ´ 2 - N -1 An approximation for F of a number’s reciprocal can be found between F = 0 and F = 1. 20 Reciprocal Approximation Algorithm X = (- 1) SX ´2 E X - Bias 1 1 ´1.F X = = B (- 1)S B ´ 2 EB - Bias ´ 1.F B • Keep same sign for reciprocal SX = SB • Negate exponent and subtract one for reciprocal EX - Bias = -(EB - Bias) - 1 EX = -EB + 2Bias - 1 • Set all bits of significand fraction of reciprocal • Evaluate contribution of each fraction bit in reciprocal – Clear bit i and compute X|bit i clear ´ B (for i = 22 downto 0) • If less than 1.0, set bit i for reciprocal approximation • Otherwise, leave bit i cleared Special case: Reciprocal of an integer power of 2 21 ...
View Full Document

This note was uploaded on 04/27/2010 for the course EECC 0306-381 taught by Professor Roymelton during the Spring '10 term at RIT.

Ask a homework question - tutors are online