Morgan Kaufmann Publishers Arithmetic for Computers

Slides:



Advertisements
Similar presentations
Morgan Kaufmann Publishers Arithmetic for Computers
Advertisements

Spring 2013 Advising Starts this week! CS2710 Computer Organization1.
Chapter 3 Arithmetic for Computers. Chapter 3 — Arithmetic for Computers — 2 Arithmetic for Computers Operations on integers Addition and subtraction.
Computer Organization CS224 Fall 2012 Lesson 19. Floating-Point Example  What number is represented by the single-precision float …00 
CS/ECE 3330 Computer Architecture Chapter 3 Arithmetic.
Chapter 3 Arithmetic for Computers. Exam 1 CSCE
Lecture 16: Computer Arithmetic Today’s topic –Floating point numbers –IEEE 754 representations –FP arithmetic Reminder –HW 4 due Monday 1.
Lecture 15: Computer Arithmetic Today’s topic –Division 1.
CMPT 334 Computer Organization Chapter 3 Arithmetic for Computers [Adapted from Computer Organization and Design 5 th Edition, Patterson & Hennessy, ©
Lecture Objectives: 1)Perform binary division of two numbers. 2)Define dividend, divisor, quotient, and remainder. 3)Explain how division is accomplished.
Chapter 3 Arithmetic for Computers. Multiplication More complicated than addition accomplished via shifting and addition More time and more area Let's.
CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (2)
Lecture 9 Sept 28 Chapter 3 Arithmetic for Computers.
1  2004 Morgan Kaufmann Publishers Chapter Three.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
CPSC 321 Computer Architecture ALU Design – Integer Addition, Multiplication & Division Copyright 2002 David H. Albonesi and the University of Rochester.
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
Computer Arithmetic.
Computing Systems Basic arithmetic for computers.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /14/2013 Lecture 16: Floating Point Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
CSF 2009 Arithmetic for Computers Chapter 3. Arithmetic for Computers Operations on integers Addition and subtraction Multiplication and division Dealing.
Floating Point Representation for non-integral numbers – Including very small and very large numbers Like scientific notation – –2.34 × –
Lecture 12: Integer Arithmetic and Floating Point CS 2011 Fall 2014, Dr. Rozier.
Chapter 3 Arithmetic for Computers (Integers). Florida A & M University - Department of Computer and Information Sciences Arithmetic for Computers Operations.
Conversion to Larger Number of Bits Ex: Immediate Field (signed 16 bit) to 32 bit Positive numbers have implied 0’s to the left. So, put 16 bit number.
Chapter 3 Arithmetic for Computers. Chapter 3 — Arithmetic for Computers — 2 Arithmetic for Computers Operations on integers Addition and subtraction.
Floating Point Numbers Representation, Operations, and Accuracy CS223 Digital Design.
C OMPUTER O RGANIZATION AND D ESIGN The Hardware/Software Interface 5 th Edition Chapter 3 Arithmetic for Computers.
Division Check for 0 divisor Long division approach – If divisor ≤ dividend bits 1 bit in quotient, subtract – Otherwise 0 bit in quotient, bring down.
Arithmetic for Computers Chapter 3 1. Arithmetic for Computers  Operations on integers  Addition and subtraction  Multiplication and division  Dealing.
Chapter 9 Computer Arithmetic
William Stallings Computer Organization and Architecture 8th Edition
/ Computer Architecture and Design
Morgan Kaufmann Publishers Arithmetic for Computers
Computer Architecture & Operations I
ALU Architecture and ISA Extensions
Morgan Kaufmann Publishers Arithmetic for Computers
Computer Architecture & Operations I
Morgan Kaufmann Publishers Arithmetic for Computers
Integer Multiplication and Division
Computer Architecture & Operations I
CS/COE0447 Computer Organization & Assembly Language
Morgan Kaufmann Publishers Arithmetic for Computers
Morgan Kaufmann Publishers
/ Computer Architecture and Design
Morgan Kaufmann Publishers
Ch 3: Computer Arithmetic
CDA 3101 Summer 2007 Introduction to Computer Organization
William Stallings Computer Organization and Architecture 7th Edition
Arithmetic for Computers
CS352H: Computer Systems Architecture
CSCE 350 Computer Architecture
Computer Architecture Computer Science & Engineering
Arithmetic Logical Unit
How to represent real numbers
Computer Arithmetic Multiplication, Floating Point
ECEG-3202 Computer Architecture and Organization
Computer Architecture
Chapter 8 Computer Arithmetic
Morgan Kaufmann Publishers Arithmetic for Computers
Number Representation
Presentation transcript:

Morgan Kaufmann Publishers Arithmetic for Computers 12 September, 2018 Arithmetic for Computers Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Floating Point Representation for non-integral numbers Including very small and very large numbers Like scientific notation –2.34 × 1056 +0.002 × 10–4 +987.02 × 109 In binary ±1.xxxxxxx2 × 2yyyy Types float and double in C normalized not normalized Chapter 3 — Arithmetic for Computers

Floating Point Standard Morgan Kaufmann Publishers 12 September, 2018 Floating Point Standard Defined by IEEE Std 754-1985 Developed in response to divergence of representations Portability issues for scientific code Now almost universally adopted Two representations Single precision (32-bit) Double precision (64-bit) Chapter 3 — Arithmetic for Computers

IEEE Floating-Point Format Morgan Kaufmann Publishers 12 September, 2018 IEEE Floating-Point Format single: 8 bits double: 11 bits single: 23 bits double: 52 bits S Exponent Fraction S: sign bit (0  non-negative, 1  negative) Normalize significand: 1.0 ≤ |significand| < 2.0 Always has a leading pre-binary-point 1 bit, so no need to represent it explicitly (hidden bit) Significand is Fraction with the “1.” restored Exponent: excess representation: actual exponent + Bias Ensures exponent is unsigned Single: Bias = 127; Double: Bias = 1203 Chapter 3 — Arithmetic for Computers

Single-Precision Range Morgan Kaufmann Publishers 12 September, 2018 Single-Precision Range Exponents 00000000 and 11111111 reserved Smallest value Exponent: 00000001  actual exponent = 1 – 127 = –126 Fraction: 000…00  significand = 1.0 ±1.0 × 2–126 ≈ ±1.2 × 10–38 Largest value exponent: 11111110  actual exponent = 254 – 127 = +127 Fraction: 111…11  significand ≈ 2.0 ±2.0 × 2+127 ≈ ±3.4 × 10+38 Chapter 3 — Arithmetic for Computers

Double-Precision Range Morgan Kaufmann Publishers 12 September, 2018 Double-Precision Range Exponents 0000…00 and 1111…11 reserved Smallest value Exponent: 00000000001  actual exponent = 1 – 1023 = –1022 Fraction: 000…00  significand = 1.0 ±1.0 × 2–1022 ≈ ±2.2 × 10–308 Largest value Exponent: 11111111110  actual exponent = 2046 – 1023 = +1023 Fraction: 111…11  significand ≈ 2.0 ±2.0 × 2+1023 ≈ ±1.8 × 10+308 Chapter 3 — Arithmetic for Computers

Floating-Point Precision Morgan Kaufmann Publishers 12 September, 2018 Floating-Point Precision Relative precision all fraction bits are significant Single: approx 2–23 Equivalent to 23 × log102 ≈ 23 × 0.3 ≈ 6 decimal digits of precision Double: approx 2–52 Equivalent to 52 × log102 ≈ 52 × 0.3 ≈ 16 decimal digits of precision Chapter 3 — Arithmetic for Computers

Floating-Point Example Morgan Kaufmann Publishers 12 September, 2018 Floating-Point Example Represent –0.75 –0.75 = (–1)1 × 1.12 × 2–1 S = 1 Fraction = 1000…002 Exponent = –1 + Bias Single: –1 + 127 = 126 = 011111102 Double: –1 + 1023 = 1022 = 011111111102 Single: 1011111101000…00 Double: 1011111111101000…00 Chapter 3 — Arithmetic for Computers

Floating-Point Example Morgan Kaufmann Publishers 12 September, 2018 Floating-Point Example What number is represented by the single-precision float 11000000101000…00 S = 1 Fraction = 01000…002 Exponent = 100000012 = 129 x = (–1)1 × (1 + 012) × 2(129 – 127) = (–1) × 1.25 × 22 = –5.0 Chapter 3 — Arithmetic for Computers

Arithmetic for Computers Morgan Kaufmann Publishers 12 September, 2018 Arithmetic for Computers Operations on integers Addition and subtraction Multiplication and division Dealing with overflow Floating-point real numbers Representation and operations Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Integer Addition Example: 7 + 6 Overflow if result out of range Adding +ve and –ve operands, no overflow Adding two +ve operands Overflow if result sign is 1 Adding two –ve operands Overflow if result sign is 0 Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Integer Subtraction Add negation of second operand 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 Subtracting –ve from +ve operand Overflow if result sign is 1 Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Dealing with Overflow Some languages (e.g., C) ignore overflow Use MIPS addu, addui, subu instructions Other languages (e.g., Ada, Fortran) require raising an exception Use MIPS add, addi, sub instructions On overflow, invoke exception handler Save PC in exception program counter (EPC) register Jump to predefined handler address mfc0 (move from coprocessor reg) instruction can retrieve EPC value, to return after corrective action Chapter 3 — Arithmetic for Computers

Arithmetic for Multimedia Morgan Kaufmann Publishers 12 September, 2018 Arithmetic for Multimedia Graphics and media processing operates 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 SIMD (single-instruction, multiple-data) Saturating operations On overflow, result is largest representable value c.f. 2s-complement modulo arithmetic E.g., clipping in audio, saturation in video Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Multiplication Start with long-multiplication approach multiplicand 1000 × 1001 0000 1001000 multiplier product Length of product is the sum of operand lengths Chapter 3 — Arithmetic for Computers

Multiplication Hardware Morgan Kaufmann Publishers Multiplication Hardware 12 September, 2018 Initially 0 Chapter 3 — Arithmetic for Computers

Multiplication Example (0010 * 0011) Morgan Kaufmann Publishers Multiplication Example (0010 * 0011) 12 September, 2018 Each step in an iteration handles one bit and takes 1 clock cycle  each iteration = 3 clock cycles for 1 bit 32-bit multiplication = 32*3 ~ 100 clock cycles +/- ops are 5 to 100 times more popular than * Improvement: 1 clock cycle per iteration (bit) Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Optimized Multiplier Perform steps in parallel: add/shift, i.e., (shift mcand & mplier) in parallel with (mcand + mplier) Multiplier placed in right half of Product register Multiplicand in left half One cycle per partial-product addition That’s ok, if frequency of multiplications is low Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers Example 12 September, 2018 Product register at the beginning: 1001 (9) 0000 0101 × 0101 (5) Add mcand to left half? shift 1001 1001 0101 0100 1010 0000 0100 1010 0010 0101 1001 1011 0101 0101 1010 0000 0101 1010 0010 1101 0101101 (45) Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Example: 2 * 3 = 0010 * 0011  Multiplicand = 0010 Xplier = 0011 Product register: 0000 0011  have to add mcand to left half of Prod register = 0010 0011  right shift entire Product register to get = 0001 0001 (one (+ half) clock cycle) Next clock cycle to handle next bit: 0001 0001  add mcand to left half of Prod register to get 0011 0001  right shift entire Product register to get 0001 1000 (one (+ half) clock cycle); since last two bits Since last two bits = 0: 0000 0110 right shift two bits. Done. Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Booth’s Algorithm 26 25 24 23 22 21 20 0 1 1 0 1 1 0 = 54 Beg. of run 1’s; End of run of 1’s How do you know where the beg/end of a run is? -- Look at the previous bit Take 26 – 24 = 64 – 16 = 48 and 23 – 21 = 8 – 2 = 6  48 + 6 = 54 Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers Start from the right, for each multiplier bit, also examine bit to its right: 00: middle of a run of 0’s, do nothing 10: begininning of a run of 1’s, subtract multiplicand 01: end of a run of 1’s, add multiplicand 11: midlle of run of 1’s, do nothing 43 = 00000101011 (multiplicand) *12 = 00000001100 (multiplier) 0 = 00000000000 //multiplier bits 0_ (implicit at beginning) 0 = 00000000000 //multiplier bits 00 (shift 1, if needed) -172 = 11101010100 //multiplier bits 10 (shift 2) 0 = 00000000000 //multiplier bits 11 (shift 3, if needed) +688 = 01010110000 //multiplier bits 01 (shift 4) and add to sum 516 = 01000000100 -172 = convert multiplicand to 2’s complement and shift bit corresponding to position of multiplier bit 2’s comp of 43 = 11111010101 Shifted left 2 bits = 11101010100 Add to current sum. 12 September, 2018 Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers Faster Multiplier 12 September, 2018 Uses multiple adders Cost/performance tradeoff Can be pipelined Several multiplication performed in parallel Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 MIPS Multiplication Two 32-bit registers for product HI: most-significant 32 bits LO: least-significant 32-bits Instructions mult rs, rt / multu rs, rt 64-bit product in HI/LO mfhi rd / mflo rd Move from HI/LO to rd Can test HI value to see if product overflows 32 bits mul rd, rs, rt Least-significant 32 bits of product –> rd Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Division Check for 0 divisor Long division approach If divisor ≤ dividend bits 1 bit in quotient, subtract Otherwise 0 bit in quotient, bring down next dividend bit Restoring division Do the subtract, and if remainder goes < 0, add divisor back Signed division Divide using absolute values Adjust sign of quotient and remainder as required quotient dividend 1001 1000 1001010 -1000 10 101 1010 divisor remainder n-bit operands yield n-bit quotient and remainder Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers Division 12 September, 2018 Quotient: left shift & add 1 if reminder >=0 Divisor: in left half; shift right 1 bit after subtraction Remainder: holds current dividend after subtract Loop for all 32 bits: { Remainder -= Divisor if Remainder < 0 Remainder += Divisor; # restore Quotient << 1 and replace LSB = 0 else Quotient << 1 and replace LSB = 1 Divisor >> 1; } Quotient Remainder 1001 1000 1001010 -1000000 0001010 100000 001010 -10000 -1000 10 Divisor Remainder Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Initially divisor in left half Initially dividend Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Optimized Divider One cycle per partial-remainder subtraction Looks a lot like a multiplier! Same hardware can be used for both Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Faster Division Can’t use parallel hardware as in multiplier Subtraction is conditional on sign of remainder Faster dividers e.g., Sweeney, Robertson, and Tocher (SRT) division determines quotient (lookup table) based on divisor and dividend. Still require multiple steps Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 MIPS Division Use HI/LO registers for result HI: 32-bit remainder LO: 32-bit quotient Instructions div rs, rt / divu rs, rt No overflow or divide-by-0 checking Software must perform checks if required Use mfhi, mflo to access result Chapter 3 — Arithmetic for Computers

Floating-Point Addition Morgan Kaufmann Publishers 12 September, 2018 Floating-Point Addition Consider a 4-digit decimal example 9.999 × 101 + 1.610 × 10–1 1. Align decimal points Shift number with smaller exponent 9.999 × 101 + 0.016 × 101 2. Add significands 9.999 × 101 + 0.016 × 101 = 10.015 × 101 3. Normalize result & check for over/underflow 1.0015 × 102 4. Round and renormalize if necessary 1.002 × 102 Chapter 3 — Arithmetic for Computers

Floating-Point Addition Morgan Kaufmann Publishers 12 September, 2018 Floating-Point Addition Now consider a 4-digit binary example 1.0002 × 2–1 + –1.1102 × 2–2 (0.5 + –0.4375) 1. Align binary points Shift number with smaller exponent 1.0002 × 2–1 + –0.1112 × 2–1 2. Add significands 1.0002 × 2–1 + –0.1112 × 2–1 = 0.0012 × 2–1 3. Normalize result & check for over/underflow 1.0002 × 2–4, with no over/underflow 4. Round and renormalize if necessary 1.0002 × 2–4 (no change) = 0.0625 Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 FP Adder Hardware Much more complex than integer adder Doing it in one clock cycle would take too long Much longer than integer operations Slower clock would penalize all instructions FP adder usually takes several cycles Can be pipelined Chapter 3 — Arithmetic for Computers

Floating-Point Multiplication Morgan Kaufmann Publishers 12 September, 2018 Floating-Point Multiplication Consider a 4-digit decimal example 1.110 × 1010 × 9.200 × 10–5 1. Add exponents For biased exponents, subtract bias from sum New exponent = 10 + –5 = 5 2. Multiply significands 1.110 × 9.200 = 10.212  10.212 × 105 3. Normalize result & check for over/underflow 1.0212 × 106 4. Round and renormalize if necessary 1.021 × 106 5. Determine sign of result from signs of operands +1.021 × 106 Chapter 3 — Arithmetic for Computers

Floating-Point Multiplication Morgan Kaufmann Publishers 12 September, 2018 Floating-Point Multiplication Now consider a 4-digit binary example 1.0002 × 2–1 × –1.1102 × 2–2 (0.5 × –0.4375) 1. Add exponents Unbiased: –1 + –2 = –3 Biased: (–1 + 127) + (–2 + 127) = –3 + 254 – 127 = –3 + 127 2. Multiply significands 1.0002 × 1.1102 = 1.1102  1.1102 × 2–3 3. Normalize result & check for over/underflow 1.1102 × 2–3 (no change) with no over/underflow 4. Round and renormalize if necessary 1.1102 × 2–3 (no change) 5. Determine sign: +ve × –ve  –ve –1.1102 × 2–3 = –0.21875 Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 FP Adder Hardware Step 1 Step 2 Step 3 Step 4 Chapter 3 — Arithmetic for Computers

FP Arithmetic Hardware Morgan Kaufmann Publishers 12 September, 2018 FP Arithmetic Hardware FP multiplier is of similar complexity to FP adder But uses a multiplier for significands instead of an adder FP arithmetic hardware usually does Addition, subtraction, multiplication, division, reciprocal, square-root FP  integer conversion Operations usually takes several cycles Can be pipelined Chapter 3 — Arithmetic for Computers

Morgan Kaufmann Publishers 12 September, 2018 Accurate Arithmetic IEEE Std 754 specifies additional rounding control Extra bits of precision (guard, round, sticky) Choice of rounding modes Allows programmer to fine-tune numerical behavior of a computation Not all FP units implement all options Most programming languages and FP libraries just use defaults Trade-off between hardware complexity, performance, and market requirements Chapter 3 — Arithmetic for Computers