number representation part 2 floating point representations rounding representation of the galois...

Download Number Representation Part 2 Floating Point Representations Rounding Representation of the Galois Field elements ECE 645: Lecture 5

If you can't read please download the document

Upload: adela-strickland

Post on 18-Jan-2018

229 views

Category:

Documents


0 download

DESCRIPTION

Floating Point Representations

TRANSCRIPT

Number Representation Part 2 Floating Point Representations Rounding Representation of the Galois Field elements ECE 645: Lecture 5 Required Reading Behrooz Parhami, Computer Arithmetic: Algorithms and Hardware Design Chapter 17, Floating-Point Representations Chapter 17.5, Rounding schemes Rounding Algorithms 101 Floating Point Representations The ANSI/IEEE standard floating- point number representation formats Originally IEEE Superseded by IEEE Standard. - - Table 17.1 Some features of the ANSI/IEEE standard floatingpoint number representation formats 00017FFEFF7E 1+1 Decimal code Hex code Exponent value f = 0: Representation of 0 f 0: Representation of denormals, 0.f 2 126 f = 0: Representation of f 0: Representation of NaNs Exponent encoding in 8 bits for the single/short (32-bit) ANSI/IEEE format 1.f 2 e Exponent Encoding Fig Denormals in the IEEE single-precision format. New IEEE Standard Basic Formats New IEEE Standard Binary Interchange Formats Requirements for Arithmetic Results of the 4 basic arithmetic operations (+, , , ) as well as square- rooting must match those obtained if all intermediate computations were infinitely precise That is, a floating-point arithmetic operation should introduce no more imprecision than the error attributable to the final rounding of a result that has no exact representation (this is the best possible) Example: (1 + 2 1 ) (1 + 2 23 ) Rounded result1 + 2 22 Error = ulp Exact result1 + 2 24 Rounding 101 The IEEE standard includes five rounding modes: Default: Round to nearest, ties to even (rtne) Optional: Round to nearest, ties away from 0 (rtna) Round toward zero (inward) Round toward + (upward) Round toward (downward) Rounding Modes 22 Rounding Rounding occurs when we want to approximate a more precise number (i.e. more fractional bits L) with a less precise number (i.e. fewer fractional bits L') Example 1: old: (K=6, L=8) new: (K'=6, L'=2) Example 2: old: (K=6, L=8) new: (K'=6, L'=0) The following pages show rounding from L>0 fractional bits to L'=0 bits, but the mathematics hold true for any L' < L Usually, keep the number of integral bits the same K'=K 23 Rounding Equation y = round(x) Fractional partWhole part x k1 x k2... x 1 x 0. x 1 x 2... x l y k1 y k2... y 1 y 0 Round 24 Rounding Techniques There are different rounding techniques: 1) truncation results in round towards zero in signed magnitude results in round towards - in two's complement 2) round to nearest number 3) round to nearest even number (or odd number) 4) round towards + Other rounding techniques 5) jamming or von Neumann 6) ROM rounding Each of these techniques will differ in their error depending on representation of numbers i.e. signed magnitude versus two's complement Error = round(x) x 25 1) Truncation Truncation in signed-magnitude results in a number chop(x) that is always of smaller magnitude than x. This is called round towards zero or inward rounding (3.5) 10 011 (3) 10 Error = (-3.5) 10 111 (-3) 10 Error = +0.5 Truncation in two's complement results in a number chop(x) that is always smaller than x. This is called round towards - or downward-directed rounding (3.5) 10 011 (3) 10 Error = (-3.5) 10 100 (-4) 10 Error = -0.5 The simplest possible rounding scheme: chopping or truncation x k1 x k2... x 1 x 0. x 1 x 2... x l x k1 x k2... x 1 x 0 trunc ulp 26 Truncation Function Graph: chop(x) Fig Truncation or chopping of a signed-magnitude number (same as round toward 0). Fig Truncation or chopping of a 2s-complement number (same as round to -). chop( x ) 4 3 2 1 x 4 3 2 x ) 4 3 2 1 x 4 3 2 27 Bias in two's complement truncation X (binary) X (decimal) chop(x) (binary) chop(x) (decimal) Error (decimal) Assuming all combinations of positive and negative values of x equally possible, average error is In general, average error = -(2 -L' -2 -L )/2, where L' = new number of fractional bits 28 Implementation truncation in hardware Easy, just ignore (i.e. truncate) the fractional digits from L to L'+1 x k-1 x k-2.. x 1 x 0. x -1 x -2.. x -L =y k-1 y k-2.. y 1 y 0. ignore (i.e. truncate the rest) 29 2) Round to nearest number Rounding to nearest number what we normally think of when say round (2.25) 10 010 (2) 10 Error = (2.75) 10 011 (3) 10 Error = (2.00) 10 010 (2) 10 Error = (2.5) 10 011 (3) 10 Error = +0.5 [round-half-up (arithmetic rounding)] (2.5) 10 010 (2) 10 Error = -0.5 [round-half-down] 30 Round-half-up: dealing with negative numbers Rounding to nearest number what we normally think of when say round (-2.25) 10 110 (-2) 10 Error = (-2.75) 10 101 (-3) 10 Error = (-2.00) 10 110 (-2) 10 Error = (-2.5) 10 110 (-2) 10 Error = +0.5 [asymmetric implementation] (-2.5) 10 101 (-3) 10 Error = -0.5 [symmetric implementation] 31 Round to Nearest Function Graph: rtn(x) Round-half-up version Asymmetric implementation Symmetric implementation 32 Bias in two's complement round to nearest Round-half-up asymmetric implementation X (binary) X (decimal) rtn(x) (binary) rtn(x) (decimal) Error (decimal) Assuming all combinations of positive and negative values of x equally possible, average error is Smaller average error than truncation, but still not symmetric error We have a problem with the midway value, i.e. exactly at 2.5 or -2.5 leads to positive error bias always Also have the problem that you can get overflow if only allocate K' = K integral bits Example: rtn(011.10) overflow This overflow only occurs on positive numbers near the maximum positive value, not on negative numbers 33 Implementing round to nearest (rtn) in hardware Round-half-up asymmetric implementation Two methods Method 1: Add '1' in position one digit right of new LSB (i.e. digit L'+1) and keep only L' fractional bits x k-1 x k-2.. x 1 x 0. x -1 x -2.. x -L + 1 =y k-1 y k-2.. y 1 y 0. y -1 Method 2: Add the value of the digit one position to right of new LSB (i.e. digit L'+1) into the new LSB digit (i.e. digit L) and keep only L' fractional bits x k-1 x k-2.. x 1 x 0. x -1 x -2.. x -L + x -1 y k-1 y k-2.. y 1 y 0. ignore (i.e. truncate the rest) ignore (i.e truncate the rest) 34 Round to Nearest Even Function Graph: rtne(x) To solve the problem with the midway value we implement round to nearest-even number (or can round to nearest odd number) Fig Rounding to the nearest even number. Fig R* rounding or rounding to the nearest odd number. 35 Bias in two's complement round to nearest even (rtne) average error is now 0 (ignoring the overflow) cost: more hardware X (binary) X (decimal ) rtne(x) (binary) rtne(x) (decimal) Error (decimal) (overfl) 36 4) Rounding towards infinity We may need computation errors to be in a known direction Example: in computing upper bounds, larger results are acceptable, but results that are smaller than correct values could invalidate upper bound Use upward-directed rounding (round toward +) up(x) always larger than or equal to x Similarly for lower bounds, use downward-directed rounding (round toward -) down(x) always smaller than or equal to x We have already seen that round toward - in two's complement can be implemented by truncation 37 Rounding Toward Infinity Function Graph: up(x) and down(x) up(x)down(x) down(x) can be implemented by chop(x) in two's complement 38 Two's Complement Round to Zero Two's complement round to zero (inward rounding) also exists inward( x ) 4 3 2 1 x 4 3 2 39 Other Methods Note that in two's complement round to nearest (rtn) involves an addition which may have a carry propagation from LSB to MSB Rounding may take as long as an adder takes Can break the adder chain using the following two techniques: Jamming or von Neumann ROM-based 40 5) Jamming or von Neumann Chop and force the LSB of the result to 1 Simplicity of chopping, with the near-symmetry or ordinary rounding Max error is comparable to chopping (double that of rounding) 41 6) ROM Rounding Fig ROM rounding with an 8 2 table. Example: Rounding with a 32 4 table Rounding result is the same as that of the round to nearest scheme in 31 of the 32 possible cases, but a larger error is introduced when x 3 = x 2 = x 1 = x 0 = x 1 = 1 x k1... x 4 x 3 x 2 x 1 x 0. x 1 x 2... x l x k1... x 4 y 3 y 2 y 1 y 0 ROM ROM dataROM address Representation of the Galois Field elements Evariste Galois ( ) Studied the problem of finding algebraic solutions for the general equations of the degree 5, e.g., f(x) = a 5 x 5 + a 4 x 4 + a 3 x 3 + a 2 x 2 + a 1 x+ a 0 = 0 Answered definitely the question which specific equations of a given degree have algebraic solutions. On the way, he developed group theory, one of the most important branches of modern mathematics. Evariste Galois ( ) 1829 Galois submits his results for the first time to the French Academy of Sciences Reviewer 1 Augustin-Luis Cauchy forgot or lost the communication. 1830Galois submits the revised version of his manuscript, hoping to enter the competition for the Grand Prize in mathematics Reviewer 2 Joseph Fourier died shortly after receiving the manuscript. 1831Third submission to the French Academy of Sciences Reviewer 3 Simeon-Denis Poisson did not understand the manuscript and rejected it. Evariste Galois ( ) May 1832 Galois provoked into a duel The night before the duel he wrote a letter to his friend containing the summary of his discoveries. The letter ended with a plea: Eventually there will be, I hope, some people who will find it profitable to decipher this mess. May 30, 1832 Galois was grievously wounded in the duel and died in the hospital the following day. 1843Galois manuscript rediscovered by Joseph Liouville 1846 Galois manuscript published for the first time in a mathematical journal. Field Set F, and two operations typically denoted by (but not necessarily equivalent to) + and * Set F, and definitions of these two operations must fulfill special conditions. { set Zp={0, 1, 2, , p-1}, + (mod p): addition modulo p, * (mod p): multiplication modulo p } Examples of fields Infinite fields Finite fields { R= set of real numbers, + addition of real numbers * multiplication of real numbers } Finite Fields = Galois Fields GF(p) GF(2 m ) Polynomial basis representation Normal basis representation Fast in hardware Arithmetic operations present in many libraries Fast squaring GF(p m ) p prime p m number of elements in the field Most significant special cases Quotient and remainder Given integers a and n, n>0 ! q, r Z such that a = q n + r and 0 r < n q quotient r remainder (of a divided by n) q = a n = a div n r = a - q n = a a n n = = a mod n 32 mod 5 = -32 mod 5 = Integers coungruent modulo n Two integers a and b are congruent modulo n (equivalent modulo n) written a b iff a mod n = b mod n or a = b + kn, k Z or n | a - b Laws of modular arithmetic Rules of addition, subtraction and multiplication modulo n a + b mod n = ((a mod n) + (b mod n)) mod n a - b mod n = ((a mod n) - (b mod n)) mod n a b mod n = ((a mod n) (b mod n)) mod n 9 13 mod 5 = 25 25 mod 26 = Laws of modular arithmetic Modular addition Modular multiplication Regular addition Regular multiplication a+b = a+c iff b=c a+b a+c (mod n) iff b c (mod n) If a b = a c and a 0 then b = c If a b a c (mod n) and gcd (a, n) = 1 then b c (mod n) Modular Multiplication: Example 18 42 (mod 8) 6 3 6 7 (mod 8) 3 7 (mod 8) x 6 x mod x 5 x mod Finite Fields = Galois Fields GF(p) GF(2 m ) Polynomial basis representation Normal basis representation Fast in hardware Arithmetic operations present in many libraries Fast squaring GF(p m ) p prime p m number of elements in the field Most significant special cases Elements of the Galois Field GF(2 m ) Binary representation (used for storing and processing in computer systems): Polynomial representation (used for the definition of basic arithmetic operations): A = (a m-1, a m-2, , a 2, a 1, a 0 ) a i {0, 1} A(x) = a i x i = a m-1 x m-1 + a m-2 x m-2 + + a 2 x 2 + a 1 x+a 0 multiplication + addition modulo 2 (XOR) i=0 m-1 Addition and Multiplication in the Galois Field GF(2 m ) Inputs A = (a m-1, a m-2, , a 2, a 1, a 0 ) B = (b m-1, b m-2, , b 2, b 1, b 0 ) a i, b i {0, 1} Output C = (c m-1, c m-2, , c 2, c 1, c 0 ) c i {0, 1} Addition A A(x) B B(x) C C(x) = A(x) + B(x) = = (a m-1 +b m-1 ) x m-1 + (a m-2 +b m-2 ) x m-2 + + + (a 2 +b 2 ) x 2 + (a 1 +b 1 ) x + (a 0 +b 0 ) = = c m-1 x m-1 + c m-2 x m-2 + + c 2 x 2 + c 1 x+c 0 Addition in the Galois Field GF(2 m ) multiplication + addition modulo 2 (XOR) c i = a i + b i = a i XOR b i C = A XOR B Multiplication A A(x) B B(x) C C(x) = A(x) B(x) mod P(X) = c m-1 x m-1 + c m-2 x m-2 + + c 2 x 2 + c 1 x+c 0 Multiplication in the Galois Field GF(2 m ) P(x) - irreducible polynomial of the degree m P(x) = p m x m + p m-1 x m-1 + + p 2 x 2 + p 1 x+p 0