Part III The Arithmetic/Logic Unit Slide About This Presentation This presentation is intended to support the use of the textbook Computer Architecture: From Microprocessors to Supercomputers, Oxford University Press, 2005, ISBN 0-19-515455-X It is updated regularly by the author as part of his teaching of the upperdivision course ECE 154, Introduction to Computer Architecture, at the University of California, Santa Barbara Instructors can use these slides freely in classroom teaching and for other educational purposes Any other use is strictly prohibited © Behrooz Parhami Edition Released Revised Revised Revised Revised First July 2003 July 2004 July 2005 Mar 2006 Jan 2007 Slide III The Arithmetic/Logic Unit Overview of computer arithmetic and ALU design: • Review representation methods for signed integers • Discuss algorithms & hardware for arithmetic ops • Consider floating-point representation & arithmetic Topics in This Part Chapter Number Representation Chapter 10 Adders and Simple ALUs Chapter 11 Multipliers and Dividers Chapter 12 Floating-Point Arithmetic Slide Computer Arithmetic as a Topic of Study Brief overview article – Encyclopedia of Info Systems, Academic Press, 2002, Vol 3, pp 317-333 Our textbook’s treatment of the topic falls between the two extremes (4 chap.) Graduate course ECE 252B – Text: Computer Arithmetic, Oxford U Press, 2000 Slide Number Representation Arguably the most important topic in computer arithmetic: • Affects system compatibility and ease of arithmetic • Two’s complement, flp, and unconventional methods Topics in This Chapter 9.1 Positional Number Systems 9.2 Digit Sets and Encodings 9.3 Number-Radix Conversion 9.4 Signed Integers 9.5 Fixed-Point Numbers 9.6 Floating-Point Numbers Slide 9.1 Positional Number Systems Representations of natural numbers {0, 1, 2, 3, …} ||||| ||||| ||||| ||||| ||||| || 27 11011 XXVII sticks or unary code radix-10 or decimal code radix-2 or binary code Roman numerals Fixed-radix positional representation with k digits k–1 Value of a number: x = (xk–1xk–2 x1x0)r = xi r i i=0 For example: 27 = (11011)two = (124) + (123) + (022) + (121) + (120) Number of digits for [0, P]: k = logr (P + 1) = logr P + Slide Unsigned Binary Integers 0000 1111 1110 15 0001 14 0010 1101 0011 13 1100 12 1011 Turn x notches counterclockwise to add x Inside: Natural number Outside: 4-bit encoding 11 10 1010 0100 0101 12 11 10 15 6 1001 14 13 1000 0110 0111 Turn y notches clockwise to subtract y Figure 9.1 Schematic representation of 4-bit code for integers in [0, 15] Slide Representation Range and Overflow Overflow region max max Numbers smaller than max Overflow region Numbers larger than max Finite set of representable numbers Figure 9.2 Overflow regions in finite number representation systems For unsigned representations covered in this section, max – = Example 9.2, Part d Discuss if overflow will occur when computing 317 – 316 in a number system with k = digits in radix r = 10 Solution The result 86 093 442 is representable in the number system which has a range [0, 99 999 999]; however, if 317 is computed en route to the final result, overflow will occur Slide 9.2 Digit Sets and Encodings Conventional and unconventional digit sets Decimal digits in [0, 9]; 4-bit BCD, 8-bit ASCII Hexadecimal, or hex for short: digits 0-9 & a-f Conventional ternary digit set in [0, 2] Conventional digit set for radix r is [0, r – 1] Symmetric ternary digit set in [–1, 1] Conventional binary digit set in [0, 1] Redundant digit set [0, 2], encoded in bits ( 1 )two and ( 1 )two represent 22 Slide Carry-Save Numbers Radix-2 numbers using the digits 0, 1, and Example: (1 1)two = (123) + (022) + (221) + (120) = 13 Possible encodings (a) Binary (b) Unary 1 MSB LSB 00 01 10 11 (Unused) 0 = 0 = 00 01 (First alternate) 10 (Second alternate) 11 First bit Second bit 0 1 = 1 = 10 Slide 10 Exceptions Undefined results lead to NaN (not a number) (0) / (0) = NaN (+) + (–) = NaN (0) () = NaN () / () = NaN Arithmetic operations and comparisons with NaNs NaN + x = NaN NaN + NaN = NaN NaN = NaN NaN NaN = NaN NaN < false NaN = Nan false NaN (+) true NaN NaN true Examples of invalid-operation exceptions Addition: Multiplication: Division: Square-root: (+) + (–) 0 or Operand < Slide 77 12.3 Floating-Point Addition (2e1s1 + (2e1(s2 / 2e1–e2)) = 2e1(s1 s2 / 2e1–e2) )(2e2s2 )Numbers to be added: 5 Operand with x = 2 1.00101101 1 smaller exponent y = 2 1.11101101 to be preshifted Operands after alignment shift: 5 x = 2 1.00101101 5 y = 2 0.000111101101 Result of addition: 5 s = 2 1.010010111101 5 s = 2 1.01001100 Figure 12.4 Extra bits to be rounded off Rounded sum Alignment shift and rounding in floating-point addition Slide 78 Input Hardware for Floating-Point Addition Input Unpack Signs Exponents Significands AddSub Mu x Sub Possible swap & complement Align significands Control & sign logic Add Normalize & round Figure 12.5 Simplified schematic of a floating-point adder Sign Exponent Significand Pack Output Slide 79 12.4 Other Floating-Point Operations Overflow (underflow) possible Floating-point multiplication (2e1s1) (2e2s2) = 2e1+ e2(s1 s2) Product of significands in [1, 4) If product is in [2, 4), halve to normalize (increment exponent) Overflow (underflow) possible Floating-point division (2e1s1) / (2e2s2) = 2e1– e2(s1 / s2) Ratio of significands in (1/2, 2) If ratio is in (1/2, 1), double to normalize (decrement exponent) Floating-point square-rooting (2es)1/2 = 2e/2(s)1/2 = 2(e–1)2(2s)1/2 Normalization not needed when e is even when e is odd Slide 80 Hardware for Floating-Point Multiplication and Division Input Input Unpack Signs Exponents Significands MulDiv Multiply or divide Control & sign logic Normalize & round Figure 12.6 Simplified schematic of a floatingpoint multiply/divide unit Sign Exponent Significand Pack Output Slide 81 12.5 Floating-Point Instructions Floating-point arithmetic instructions for MiniMIPS: F 31 add.s $f0,$f8,$f10 # set $f0 to ($f8) +fp ($f10) sub.d $f0,$f8,$f10 # set $f0 to ($f8) –fp ($f10) mul.d $f0,$f8,$f10 # set $f0 to ($f8) fp ($f10) div.s $f0,$f8,$f10 # set $f0 to ($f8) /fp ($f10) neg.s op $f0,$f8 ex # set $f0 to –($f8) fs fd 25 20 ft 15 10 fn 0 0 0 0 x 1 0 0 0 0 0 0 x x x Floating-point instruction s=0 d=1 Source register Source register Destination register add.* = sub.* = mul.* = div.* = neg.* = Figure 12.7 The common floating-point instruction format for MiniMIPS and components for arithmetic instructions The extension (ex) field distinguishes single (* = s) from double (* = d) operands Slide 82 The Floating-Point Unit in MiniMIPS Loc Loc Loc B / location m 32 Memory up to 30 words Loc Loc m 8 m 4 Coprocessor EIU Execution & integer unit $0 $1 $2 (Main proc.) $31 Integer mul/div ALU Hi Chapter 10 Chapter 11 Figure 5.1 Lo Chapter 12 FPU (Coproc 1) FP arith $0 $1 $2 Floatingpoint unit $31 Pairs of registers, beginning with an even-numbered one, are used for double operands TMU BadVaddr Trap & (Coproc 0) Status memory Cause unit EPC Memory and processing subsystems for MiniMIPS Slide 83 Floating-Point Format Conversions MiniMIPS instructions for number format conversion: cvt.s.w cvt.d.w cvt.d.s cvt.s.d cvt.w.s cvt.w.d F 31 op $f0,$f8 $f0,$f8 $f0,$f8 $f0,$f8 $f0,$f8 $f0,$f8 25 ex # # # # # # 20 set set set set set set ft $f0 $f0 $f0 $f0 $f0 $f0 15 to to to to to to fs single(integer $f8) double(integer $f8) double($f8) single($f8,$f9) integer($f8) integer($f8,$f9) 10 fd fn 0 0 0 0 x 0 0 0 0 0 0 0 0 x x x Floating-point instruction Figure 12.8 *.w = w.s = w.d = *.* = Unused Source register Destination register To format: s = 32 d = 33 w = 36 Floating-point instructions for format conversion in MiniMIPS Slide 84 Floating-Point Data Transfers MiniMIPS instructions for floating-point load, store, and move: lwc1 swc1 mov.s mov.d mfc1 mtc1 F 31 $f8,40($s3) $f8,A($s3) $f0,$f8 $f0,$f8 $t0,$f12 $f8,$t4 op 25 20 ft load mem[40+($s3)] into $f8 store ($f8) into mem[A+($s3)] load $f0 with ($f8) load $f0,$f1 with ($f8,$f9) load $t0 with ($f12) load $f8 with ($t4) 15 fs 10 fd fn 0 0 0 0 x 0 0 0 0 0 0 0 0 1 Floating-point instruction R ex # # # # # # 31 op s=0 d=1 25 rs Unused 20 rt Source register 15 rd Destination register 10 sh mov.* = fn 0 0 0 x 0 1 0 0 0 0 0 0 0 0 Floating-point instruction Figure 12.9 mfc1 = mtc1 = Source register Destination register Unused Unused Instructions for floating-point data movement in MiniMIPS Slide 85 Floating-Point Branches and Comparisons MiniMIPS instructions for floating-point load, store, and move: bc1t bc1f c.eq.* c.lt.* c.le.* I 31 L L $f0,$f8 $f0,$f8 $f0,$f8 op 25 20 branch on fp flag true branch on fp flag false if ($f0)=($f8), set flag to “true” if ($f0)