Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
CSE 575 Computer Arithmetic Spring 2005 Mary Jane Irwin (www.cse.psu.edu/~mji) Computer Arithmetic CSE575 Introduction.1 © MJIrwin, PSU, 2005 Administrivia Mary Jane Irwin » 348C IST Bldg » Office hours: by appointment » [email protected]; www.cse.psu.edu/~mji Optional: Digital Arithmetic, Ercogovac & Lang, Morgan Kaufmann, 2004 Prerequisite: CSE 477 and CSE 431 Lectures are scheduled from 2:30 to 4:30 (but will often be over by 4:00, especially if a colloquium is scheduled) Computer Arithmetic CSE575 Introduction.2 © MJIrwin, PSU, 2005 Course Grading Homeworks (2 in total) Course project 150 pts 350 pts TOTAL 500 pts Computer Arithmetic CSE575 Introduction.3 © MJIrwin, PSU, 2005 Remaining Lecture Schedule Mar 15 Introduction, number repr Dr. Irwin Mar 17 Local project design review Theo T. Mar 22 Global project review Dr. Vijay Mar 24 Global project review Dr. Vijay Mar 29 Addition Dr. Irwin Apr 1 Redundant repr & its uses Dr. Irwin Apr 5 Multiplication Dr. Irwin Apr 7 Local/Global project review Dr. Vijay Apr 12 Division Dr. Irwin Chp 5 Apr 14 Flt point repr & operation Dr. Irwin Chp 8 Apr 19 Function evaluation Dr. Irwin Chp 10, 11 Apr 21 Final global project review Dr. Vijay Apr 26 Other # systems Dr. Irwin Apr 28 Final global project review Dr. Vijay Computer Arithmetic CSE575 Introduction.4 Chp 1 Chp 2 Chp 4 © MJIrwin, PSU, 2005 Computer Arithmetic “ is a subfield of digital computer organization … deals with the hardware realization of arithmetic functions … a major thrust <of which> is the design of hardware algorithms and circuits to enhance the speed of numeric operations.” Parhami “ encompasses the study of number representation, algorithms for operations on numbers, implementations of arithmetic units in hardware, and their use …” Ercegovac & Lang Computer Arithmetic CSE575 Introduction.5 © MJIrwin, PSU, 2005 What Is Computer Arithmetic? Hardware – Design of efficient circuits for arithmetic operations (+, -, x, /, sqrt, log, sine, …) Issues » Algorithm » Accuracy » Speed/power/area/ reliability trade-offs » Test/verification Computer Arithmetic Software – Numerical methods for solving systems of equations (CSE 45x and 55x) Issues » Algorithm » Error analysis » Computational complexity » Test/verification CSE575 Introduction.6 © MJIrwin, PSU, 2005 Applications General purpose systems » Fast primitive operations for processor datapaths Special purpose systems » Signal and image processing » Real-time 3D graphics » HDTV, image compression » Network processors (data compression, encryption/decryption) »… Computer Arithmetic CSE575 Introduction.7 © MJIrwin, PSU, 2005 Design Axes Architectural innovations » Systolic arrays, wave pipelining, MMX, razor pipelines Technological advancements » 3D, multivalued gates, nanotechnology, … Representation (encoding) systems » Redundant, residue, … Arithmetic algorithms » SD arithmetic, serial arithmetic (lsb and msb first), saturating arithmetic, MACs, parallel multipliers, … Computer Arithmetic CSE575 Introduction.8 © MJIrwin, PSU, 2005 Representing Numbers infinite (real numbers) finite (machine representation) Computer Arithmetic CSE575 Introduction.9 © MJIrwin, PSU, 2005 Number Representations “Understanding different ways of representing numbers, including their relative cost-performance benefits and conversions between various representations, is an important prerequisite for designing efficient arithmetic algorithms or circuits.” Parhami Computer Arithmetic CSE575 Introduction.10 © MJIrwin, PSU, 2005 Machine Representation Characteristics 1492 Positional - value of each symbol depends on its relative position (1492 1942) Fixed radix, r - unit of each position is a constant multiple of the radix (base); usually a positive integer Consecutive symbol (or digit) set, d, with at least r symbols, e.g., d [0, 1, 2, …, 8, 9] Computer Arithmetic CSE575 Introduction.11 © MJIrwin, PSU, 2005 Machine Repr. Characteristics, con’t 1492 Scale factor, s - implied radix point (integer, fraction, or mixed number) Precision, n = k + l - granularity of representation (k: # integer digits, l: # of fractional digits), ulp = r0 = 1 for integers, ulp = r-n for fractions Range – number of distinct values from largest to smallest; depends on n, s, & r Computer Arithmetic CSE575 Introduction.12 © MJIrwin, PSU, 2005 Aside: Some Notation X - (capital letters) full precision operand consisting of multiple bits (base 2) or digits (base 2 and higher) X = xiri = (xk-1 xk-2 … x1 x0. x-1 x-2 … x-l+1 x-l )r xi - (small letters) single bit or digit values (i negative then a fractional bit/digit, i positive then an integer bit/digit) Xi - (capital letters) full precision result from the ith iteration of a computation Computer Arithmetic CSE575 Introduction.13 © MJIrwin, PSU, 2005 Binary Encoding Examples Digit vector of length n = k + l ( xk-1 xk-2 … x1 x0. x-1 x-2 … x-l+1 x-l )r= xiri -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 n=4 Unsigned integer Sign magnitude integer 2+2 floating point Sx2E, S (0,1,2,3) E (-2,-1,0,1) Computer Arithmetic CSE575 Introduction.14 © MJIrwin, PSU, 2005 Fixed-radix Positional # Systems 0110 Decimal Equilavent? Binary: r=2, d[0,1] 6 Balanced ternary: r=3, d[-1,0,1] 12 Negabinary: r=-2, d[0,1] 2 Redundant (signed digit): r=2, d[-1,0,1] 6 Fractional radix: r=1/2, d[0,1] ¾ Irrational radix: r=sqrt(2), d[0,1] Complex-radix: r=2j, d[0,1,2,3] Computer Arithmetic CSE575 Introduction.16 2 + √2 © MJIrwin, PSU, 2005 Real Number Representations Floating point (approximation) Slash (rational) representations » Keep numbers as num/denom pairs – Exact arithmetic (e.g., 2/3) – Division is multiplication Logarithmic representation » Keep numbers as log2 |A| – Easy multiply and divide – Large dynamic range, but low precision Computer Arithmetic CSE575 Introduction.17 © MJIrwin, PSU, 2005 Number Radix Conversion Assume that you are familiar with techniques for » converting to/from radix 2, 4, 8, 10, 16, … » doing arithmetic in the old radix » doing arithmetic in the new radix » using Horner’s rule shortcut to convert between radices Computer Arithmetic CSE575 Introduction.18 © MJIrwin, PSU, 2005 Choosing a “Best” Radix Conversion » I/O is in decimal, so r=10 (BCD) would require no conversion, but wastes storage space and arithetic unit area and delay Compatibility » CMOS circuitry supports two states (on, off) only, so r=2 (or some power of 2) Reliability » circuitry supports binary logic only (as opposed to multivalued logic) Computer Arithmetic CSE575 Introduction.19 © MJIrwin, PSU, 2005 Choosing a “Best” Radix con’t Efficiency The # of different representations is M=rn Assuming the component cost for repr is proportional to r (with binary logic it’s really prop. to log2r for r a power of 2), then the cost of the repr is $ = K1 n r $ = K1 (ln M)/(ln r) r = K2 r/(ln r) To minimize $, take derivative at zero wrt r d$/dr = (K2 (ln r -1))/ (ln r2) 0 So, ln r = 1 (r = e = 2.71828…) is the most efficient radix! Cost of r=2 and r=4 are about equal. Computer Arithmetic CSE575 Introduction.20 © MJIrwin, PSU, 2005 Binary Full Adder (FA) Review 2ci+1 + si = xi + yi + ci addend augend xi y i ci+1 carry-out FA carry-in ci si sum si = xi yi ci y ci ci+1 s 0 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 1 1 1 1 (odd parity function) ci+1 = xi&yi | xi&ci | yi&ci Computer Arithmetic x (majority function) CSE575 Introduction.21 © MJIrwin, PSU, 2005 Ripple Carry Adder (RCA) cout=c4 x3 y 3 x2 y2 x1 y1 x0 y0 FA FA FA FA s3 s2 s1 s0 sign bits c0=cin mod(2k) adder Can we use it to do both addition and subtraction? How about negation? How do we determine overflow, zero result, …? How do we make it fast, power efficient, reliable, …? Computer Arithmetic CSE575 Introduction.22 © MJIrwin, PSU, 2005 Representing Signed Numbers Signed magnitude Complement » Two’s complement » One’s complement Biased (excess) sign indicated by most significant digit Choice depends on area, delay, power, reliability of arithmetic hardware (adder) implementation Computer Arithmetic CSE575 Introduction.23 © MJIrwin, PSU, 2005 Sign Magnitude (SM) most significant digit is the sign indicator, remaining digits are the magnitude 0000 1111 0001 1110 0010 -7 0 +1 -6 +2 1101 -5 +3 0011 1100 -4 SM +4 0100 1011 -3 +5 0101 -2 +6 1010 0110 -1 -0 +7 1001 0111 1000 increment X = xk-1 xk-2 … x1 x0 k-2 X = (-1)xk-12ixi i=0 Computer Arithmetic CSE575 Introduction.24 © MJIrwin, PSU, 2005 Characteristics of Binary SM k-bit symmetric range of [-2k-1+ulp, 2k-1-ulp] two representations for zero simple negation (complement sign bit) addition of unlike sign numbers must be handled differently than addition of like sign numbers (SM not a modular system) symmetric shift – left: shift in zeros; right: shift zeros into magnitude - while maintaining sign bit extend - left (sign): pad with zeros; right: pad with zeros – while maintaining sign bit Computer Arithmetic CSE575 Introduction.25 © MJIrwin, PSU, 2005 SM Adder/Subtractor sign of X sign of Y X Y Selective compl X complement Xcompl add/sub Control cout sign compl S sign of S Computer Arithmetic cin Binary adder Selective complement S =X ± Y CSE575 Introduction.26 © MJIrwin, PSU, 2005 Complement Representations negation constant, M, so that negative representation of Y is M-Y 0 M-1 M-2 M-3 -2 -3 M-N Computer Arithmetic -1 0 +1 unsigned representations 1 2 +2 +3 3 signed +4 4 values increment -N +P P MN+P+1 CSE575 Introduction.27 © MJIrwin, PSU, 2005 Addition Rules Desired Operation Computation to be performed mod M Correct result with no overflow Overflow condition (+X) + (+Y) (+X) – (-Y) X+Y X+Y X+Y>P (+X) + (-Y) (+X) – (+Y) X + (M-Y) X-Y if Y <= X M-(Y-X) if Y > X NA (-X) + (+Y) (-X) – (-Y) (M-X) + Y Y-X if X <=Y M-(X-Y) if X > Y NA (-X) + (-Y) (-X) – (+Y) (M-X) + (M-Y) M – (X + Y) X+Y>N with a selection negation unit available all the operations are the same! Computer Arithmetic CSE575 Introduction.28 © MJIrwin, PSU, 2005 Complement Rep Options Two auxiliary operations required » negation (computing M-Y) » computation of residues mod M Select M so these two operations are simple Radix complement M= rk » negation – » mod M – Diminished radix complement M = rk – ulp » negation – » mod M Computer Arithmetic CSE575 Introduction.29 © MJIrwin, PSU, 2005 Two’s Complement (2’sc) M = 2k (for k=4, l=0, M = 24 =16) 0000 1111 0001 1110 0010 -1 0 +1 -2 +2 1101 -3 +3 0011 1100 -4 2s’c +4 0100 1011 -5 +5 0101 -6 +6 1010 0110 -7 -8 +7 1001 0111 1000 increment a mod(2k) system k-2 X = -2k-1xk-1 + 2ixi i=0 Computer Arithmetic CSE575 Introduction.30 © MJIrwin, PSU, 2005 Characteristics of 2’sc asymmetric range of [-2k-1, 2k-1 - ulp] » so negation can lead to overflow! one representation for zero (all zeros) negation – complement all the bits and add 1 2s’c(Y) = 2k – Y = ((2k – ulp) – Y) + ulp = Ycompl + ulp mod - simply drop the high order carry-out RCA with negation logic can handle both add and subtract asymmetric shift – left: shift in zeros; right: shift in sign extend - left (sign): replicate sign bit; right: pad with zeros Computer Arithmetic CSE575 Introduction.31 © MJIrwin, PSU, 2005 2’sc Negation Example RC(Y) = rk – Y rK = -Y = 1 - 0 yk-1 0 yk-2 ... ... 0 0 y1 y0 0 r-1 r-1 ... r-1 -yk-1 -yk-2 ... -y1 -y0 r complement the digit Computer Arithmetic CSE575 Introduction.33 © MJIrwin, PSU, 2005 2s’c Adder/Subtractor X overflow: ck-1 ck cout = ck Y Selective complement Y or Ycompl Binary adder add/sub 0 for addition 1 for subtraction cin X±Y mitigates disadvantage of negation removes disadvantage of asymmetric range » if operand to be negated is -2k-1, it is represented in two parts: Ycompl (which is 2k-1 -1) and cin Computer Arithmetic CSE575 Introduction.34 © MJIrwin, PSU, 2005 Overflow Looking at the just the sign bit results no overflow here ever! overflow = ck-1 ck xk-1 yk-1 ck-1 ck sk-1 0 0 0 0 0 0 0 0 1 0 1 +2k-1 negative? - overflow 0 1 0 0 1 -2k-1 negative? - okay 0 1 1 1 0 0 positive? - okay 1 0 0 0 1 -2k-1 negative? - okay 1 0 1 1 0 0 positive? - okay 1 1 0 1 0 -2k positive? - overflow 1 1 1 1 1 -2k-1 negative? - okay +2k -2k-1 -2k-1 -2k-1 +2k-1 Computer Arithmetic CSE575 Introduction.35 positive? - okay bit position weights © MJIrwin, PSU, 2005 One’s Complement (1’sc) M = 2k – 1 (for k=4, l=0, M = 24 – 1 = 15) 0000 1111 0001 1110 0010 -0 0 +1 -1 +2 1101 -2 +3 0011 1100 -3 1s’c +4 0100 1011 -4 +5 0101 -5 +6 1010 0110 -6 -7 +7 1001 0111 1000 increment a mod(2k-1) system k-2 X = -(2k-1-1)xk-1 + 2ixi i=0 Computer Arithmetic CSE575 Introduction.36 © MJIrwin, PSU, 2005 Characteristics of 1’sc symmetric range of [-2k-1 + ulp, 2k-1 - ulp] two representations for zero negation – complement all the bits 1s’c(Y) = 2k – 1 - Y = (2k – ulp) – Y = Ycompl mod - end around carry » reduces magnitude by 2k - ulp if cout = 1 RCA with negation logic and end around carry can handle both add and subtract symmetric shift – left: shift in sign; right: shift in sign extend - left (sign): replicate sign bit; right: replicate sign bit Computer Arithmetic CSE575 Introduction.37 © MJIrwin, PSU, 2005 End Around Carry When adding two 1’sc [mod(2k – ulp)] numbers that results in a high order carry out, result will be off by 2k - ulp End-around carry » Drop a high order carry-out of 1 and simultaneously insert a carry-in of 1 into position l » Since the dropped carry is worth 2k units and the inserted carry is worth ulp, the combined effect is to reduce the magnitude of the result by 2k – ulp Overflow – ? for you to consider Computer Arithmetic CSE575 Introduction.38 © MJIrwin, PSU, 2005 1s’c Adder/Subtractor X overflow: ck-1 ck cout = ck Y Selective complement Y or Ycompl Binary adder cin add/sub 0 for addition 1 for subtraction end around carry X±Y Does the end around carry terminate? Assuming so, how long do we have to wait? Computer Arithmetic CSE575 Introduction.39 © MJIrwin, PSU, 2005 Complement Rep Options Revisited Two auxiliary operations required » negation (computing M-Y) » computation of residues mod M Select M so these two operations are simple Radix complement M= rk » negation – replace each digit by its radix complement [(r-1) - xi] and add a unit in the ls digit » mod M – ignore the high order carry out Diminished radix complement M = rk – ulp » negation – replace each nonzero digit by its radix complement » mod M - do end around carry Computer Arithmetic CSE575 Introduction.40 © MJIrwin, PSU, 2005 Biased (Excess) most significant digit is the sign indicator, remaining digits are the magnitude 0000 1111 0001 1110 0010 +7 -8 -7 +6 -6 1101 0011 +5 -5 1100 +4 XC8 -4 0100 1011 +3 -3 0101 +2 -2 1010 0110 +1 0 -1 1001 0111 1000 Computer Arithmetic CSE575 Introduction.41 increment a mod(2k) system © MJIrwin, PSU, 2005 Characteristics of Biased similar to 2s’c with sign bit inverted (0 -> negative; 1 -> positive) bias is always in the sign position addition requires subtraction of bias X + Y + bias = (X + bias) + (Y + bias) - bias » with a bias of 2k-1 just complement the msb used to represent the exponent in floating point notation » simplifies comparison of floating point numbers (with leading excess exponent, integer comparison logic suffices) Computer Arithmetic CSE575 Introduction.42 © MJIrwin, PSU, 2005 Indirect Signed Arithmetic When hardware uses unsigned operands » some multiplication and division schemes » floating point mantissa units Sign removal Sign logic Unsigned operation Adjustment Other examples of pre/post operand processing » dealing with unacceptable or inconvenient operands – infinity, NAN – sin(2 + X) = sin( - X) = sin X » more efficient division when divisor can be scaled (along with the dividend) to be in a certain range Computer Arithmetic CSE575 Introduction.43 © MJIrwin, PSU, 2005 Key References Biannual Proc. Of the Computer Arithmetic Symposia, 1975 – 2003. Cavanagh, Digital Computer Arithmetic Design and Implementation, McGraw Hill, 1984. Ercegovac & Lang, Digital Arithmetic, Morgan Kaufmann, 2004. Flynn & Oberman, Advanced Computer Arithmetic Design, Wiley, 2001. Gosling, Design of Arithmetic Units for Digital Computers, Macmillan, 1980. Koren, Computer Arithmetic Algorithms, Prentice-Hall, 1993. Omondi, Computer Arithmetic Systems: Algorithms, Architectures, and Implementation, Prentice-Hall, 1994. Parhami, Computer Arithmetic, Oxford Press, 1999. Scott, Computer Number Systems and Arithmetic, Prentice-Hall, 1985. Spaniol, Computer Arithmetic Logic and Design, Wiley, 1981. Swartzlander, Ed., Computer Arithmetic, Dowden, 1980. Swartzlander, Ed., Computer Arithmetic II, IEEE Press, 1990. Waser & Flynn, Introduction to Arithmetic for Digital Systems Designers, Holt, 1982. Computer Arithmetic CSE575 Introduction.44 © MJIrwin, PSU, 2005