2009 dce KIẾN TRÚC MÁY TÍNH CS2009 BK TP.HCM Khoa Khoa học và Kỹ thuật Máy tính BM Kỹ thuật Máy tính Võ Tấn Phương http://www.cse.hcmut.edu.vn/ ~ vtphuong/KTMT http://www.cse.hcmut.edu.vn/ vtphuong/KTMT 2009 dce Chapter 3 Arithmetic for Computers Adapted from Computer Organization and Adapted from Computer Organization and Design, 4 th Edition, Patterson & Hennessy, © 2008 ©2009, CE Department 2 10/18/2009 2009 dce The Five classic Components of a Computer ©2009, CE Department 3 10/18/2009 2009 dce Arithmetic for Computers • Operations on integers Addition and subtraction – Addition and subtraction – Multiplication and division Dealing with overflow – Dealing with overflow • Floating-point real numbers – Representation and operations 10/18/2009 Chapter 3 — Arithmetic for Computer — 4©2009, CE Department 2009 dce Integer Addition • Example: 7 + 6 • Overflow if result out of range • Overflow if result out of range – Adding +ve and –ve operands, no overflow – Adding two + ve operands Adding two ve operands • Overflow if result sign is 1 – Adding two –ve operands • Overflow if result sign is 0 10/18/2009 Chapter 3 — Arithmetic for Computer — 5©2009, CE Department 2009 dce Integer Subtraction • Add negation of second operand • Example: 7 – 6 = 7+( – 6) Example: 7 6 7 + ( 6) +7: 0000 0000 … 0000 0111 –6: 1111 1111 … 1111 1010 +1: 0000 0000 … 0000 0001 • Overflow if result out of range – Subtracting two +ve or two – ve operands, no overflow – Subtracting +ve from –ve operand • Overflow if result sign is 0 • Overflow if result sign is 0 – Subtracting –ve from +ve operand • Overflow if result sign is 1 10/18/2009 Chapter 3 — Arithmetic for Computer — 6©2009, CE Department 2009 dce Dealing with Overflow • Some languages (e.g., C) ignore overflow – Use MIPS addu , addui , subu instructions Use MIPS addu , addui , subu instructions • Other languages (e.g., Ada, Fortran) require raising an exception require raising an exception – Use MIPS add, addi, sub instructions – O n o v e rfl o w , inv o k e e x cep ti o n h a n d l er Ooeo, oeecepo ade • Save PC in exception program counter (EPC) register Jtdfidhdldd • J ump t o pre d e fi ne d h an dl er a dd ress • mfc0 (move from coprocessor reg) instruction can retrieve EPC value, to return after corrective action 10/18/2009 Chapter 3 — Arithmetic for Computer — 7©2009, CE Department 2009 dce Arithmetic for Multimedia • Graphics and media processing operates on vectors of 8 - bit and 16 - bit data on vectors of 8 - bit and 16 - bit data – Use 64-bit adder, with partitioned carry chain • Operate on 8 × 8 - bit 4 × 16 - bit or 2 × 32 - bit vectors • Operate on 8 × 8 - bit , 4 × 16 - bit , or 2 × 32 - bit vectors – SIMD (single-instruction, multiple-data) • Saturating operations • Saturating operations – On overflow, result is largest representable value value • c.f. 2s-complement modulo arithmetic E g clipping in audio saturation in video – E . g ., clipping in audio , saturation in video 10/18/2009 Chapter 3 — Arithmetic for Computer — 8©2009, CE Department 2009 dce Multiplication • Start with long-multiplication approach 1000 × 1001 multiplicand multiplier × 1001 1000 0000 0000 1000 1001000 product Length of product is the sum of operand lengths 10/18/2009 Chapter 3 — Arithmetic for Computer — 9©2009, CE Department 2009 dce Multiplication Hardware Initiall y 0 y 10/18/2009 Chapter 3 — Arithmetic for Computer — 10©2009, CE Department