ECE260: Fundamentals of Computer Engineering

Similar documents
Chapter 3: Arithmetic for Computers

Thomas Polzer Institut für Technische Informatik

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

Computer Architecture Set Four. Arithmetic

Chapter Three. Arithmetic

Arithmetic for Computers. Hwansoo Han

Chapter 3. Arithmetic Text: P&H rev

Module 2: Computer Arithmetic

Math in MIPS. Subtracting a binary number from another binary number also bears an uncanny resemblance to the way it s done in decimal.

NUMBER OPERATIONS. Mahdi Nazm Bojnordi. CS/ECE 3810: Computer Organization. Assistant Professor School of Computing University of Utah

Chapter 3 Arithmetic for Computers (Part 2)

Divide: Paper & Pencil

Integer Arithmetic. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Homework 3. Assigned on 02/15 Due time: midnight on 02/21 (1 WEEK only!) B.2 B.11 B.14 (hint: use multiplexors) CSCI 402: Computer Architectures

MIPS Integer ALU Requirements

CPS 104 Computer Organization and Programming

COMPUTER ORGANIZATION AND DESIGN

Lecture 8: Addition, Multiplication & Division

COMP 303 Computer Architecture Lecture 6

ECE232: Hardware Organization and Design

Signed Multiplication Multiply the positives Negate result if signs of operand are different

EE260: Logic Design, Spring n Integer multiplication. n Booth s algorithm. n Integer division. n Restoring, non-restoring

CS/COE0447: Computer Organization

CS/COE0447: Computer Organization

COMPUTER ARITHMETIC (Part 1)

More complicated than addition. Let's look at 3 versions based on grade school algorithm (multiplicand) More time and more area

Fast Arithmetic. Philipp Koehn. 19 October 2016

Chapter 5 : Computer Arithmetic

Chapter 3 Arithmetic for Computers

Integer Multiplication and Division

Number Systems and Computer Arithmetic

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Review: MIPS Organization

Chapter 3 Arithmetic for Computers. ELEC 5200/ From P-H slides

Computer Arithmetic Ch 8

Computer Arithmetic Ch 8

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Organisasi Sistem Komputer

361 div.1. Computer Architecture EECS 361 Lecture 7: ALU Design : Division

Computer Architecture. Chapter 3: Arithmetic for Computers

Arithmetic for Computers

Outline. EEL-4713 Computer Architecture Multipliers and shifters. Deriving requirements of ALU. MIPS arithmetic instructions

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

ECE331: Hardware Organization and Design

COMPUTER ORGANIZATION AND. Edition. The Hardware/Software Interface. Chapter 3. Arithmetic for Computers

Chapter 10 - Computer Arithmetic

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

TDT4255 Computer Design. Lecture 4. Magnus Jahre

Integer Multiplication. Back to Arithmetic. Integer Multiplication. Example (Fig 4.25)

carry in carry 1101 carry carry

Chapter 3. Arithmetic for Computers

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

CO212 Lecture 10: Arithmetic & Logical Unit

Tailoring the 32-Bit ALU to MIPS

COMP2611: Computer Organization. Data Representation

5DV118 Computer Organization and Architecture Umeå University Department of Computing Science Stephen J. Hegner. Topic 3: Arithmetic

Computer Organisation CS303

COMPUTER ARCHITECTURE AND ORGANIZATION. Operation Add Magnitudes Subtract Magnitudes (+A) + ( B) + (A B) (B A) + (A B)

Chapter 3 Arithmetic for Computers

Lecture 13: (Integer Multiplication and Division) FLOATING POINT NUMBERS

Chapter 3. Arithmetic for Computers

Chapter 4 Section 2 Operations on Decimals

CSE 141 Computer Architecture Summer Session Lecture 3 ALU Part 2 Single Cycle CPU Part 1. Pramod V. Argade

By, Ajinkya Karande Adarsh Yoga

Chapter Two MIPS Arithmetic

Internal Data Representation

Arithmetic. Chapter 3 Computer Organization and Design

ECE260: Fundamentals of Computer Engineering

EEC 483 Computer Organization

Timing for Ripple Carry Adder

CS/COE 0447 Example Problems for Exam 2 Spring 2011

Floating Point Arithmetic

ECE260: Fundamentals of Computer Engineering

Boolean Algebra. Chapter 3. Boolean Algebra. Chapter 3 Arithmetic for Computers 1. Fundamental Boolean Operations. Arithmetic for Computers

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

COMPUTER ORGANIZATION AND DESIGN. 5 th Edition. The Hardware/Software Interface. Chapter 3. Arithmetic for Computers

9 Multiplication and Division

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

Arithmetic Logic Unit

ECE331: Hardware Organization and Design

Numeric Encodings Prof. James L. Frankel Harvard University

Arithmetic and Bitwise Operations on Binary Data

EECS150 - Digital Design Lecture 13 - Combinational Logic & Arithmetic Circuits Part 3

Number Systems and Their Representations

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Chapter 3. Arithmetic for Computers

Chapter 2 Data Representations

Chapter 3. Arithmetic for Computers

CPE300: Digital System Architecture and Design

Data Representations & Arithmetic Operations

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

UNIVERSITY OF MASSACHUSETTS Dept. of Electrical & Computer Engineering. Digital Computer Arithmetic ECE 666

CS101 Lecture 04: Binary Arithmetic

Chapter 5: Computer Arithmetic. In this chapter you will learn about:

Number Systems. Both numbers are positive

CHW 261: Logic Design

(+A) + ( B) + (A B) (B A) + (A B) ( A) + (+ B) (A B) + (B A) + (A B) (+ A) (+ B) + (A - B) (B A) + (A B) ( A) ( B) (A B) + (B A) + (A B)

Signed umbers. Sign/Magnitude otation

UNIT-III COMPUTER ARTHIMETIC

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: The MIPS ISA (P&H ) Consulting hours. Milestone #1 (due 1/26)

Transcription:

Arithmetic for Computers James Moscola Dept. of Engineering & Computer Science York College of Pennsylvania Based on Computer Organization and Design, 5th Edition by Patterson & Hennessy

Arithmetic for Computers Operations on integers Addition and subtraction Multiplication and division Dealing with overflow Floating-point real numbers Representation and operations 2

Binary Integer Addition Benefit of 2 s complement integer representation: Example: 7 ten + 6 ten Same binary addition procedure will work for adding both signed and unsigned numbers Grade school style! If result is out of range, overflow occurs Adding positive and negative operands Example expanded to show carries inline No overflow will occur Adding two positive operands Overflow occurred if sign bit of result is 1 Adding two negative operands Overflow occurred if sign bit of result is 0 3

Binary Integer Subtraction Two options: Subtract numbers directly grade school style Example: 7 ten - 6 ten Grade school style Negate 2 nd operand and perform an addition If result is out of range, overflow occurs Subtracting two positive or two negative operands No overflow will occur Negate 2 nd operand and add Subtracting positive from negative operand Overflow occurred if sign bit of result is 0 Subtracting negative from positive operand Overflow occurred if sign bit of result is 1 4

Dealing with Overflow Some languages (e.g., C) ignore overflow Up to the programmer to address potential overflow issues The following table summarizes the results that indicate overflow occurred Other languages (e.g., Fortran, Ada) will cause an exception if overflow occurs Exception notifies programmer so that overflow can be handled In MIPS, overflow behavior is as follows: Signed instructions raise exceptions (e.g. add, addi, sub) Unsigned instructions do not raise exceptions (e.g. addu, addiu, subu) Examples: Result = Op A + Op B IF (Op A 0 and Op B 0 and Result < 0) THEN overflow occurred Result = Op A - Op B IF (Op A 0 and Op B < 0 and Result < 0) THEN overflow occurred 5

Integer Multiplication Here s the classic grade school Times Table At some point you probably memorized this 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 0 1 0 1 2 3 4 5 6 7 8 9 2 0 2 4 6 8 10 12 14 16 18 3 0 3 6 9 12 15 18 21 24 27 4 0 4 8 12 16 20 24 28 32 36 5 0 5 10 15 20 25 30 35 40 45 6 0 6 12 18 24 30 36 42 48 54 7 0 7 14 21 28 35 42 49 56 63 8 0 8 16 24 32 40 48 56 64 72 9 0 9 18 27 36 45 54 63 72 81 Multiplying two numbers together looks something like this: Multiplicand Multiplier + A 3 B 3 Product Note: multiplying N-digit number by M-digit A 2 number gives (N+M)-digit result x A 1 A 0 A 3 B 0 B 3 B 2 B 1 A 3 B 1 A 3 B 2 A 3 B 0 A 2 B 0 A 1 B 0 A 2 B 1 A 1 B 1 A 0 B 1 A 2 B 2 A 1 B 2 A 0 B 2 A 2 B 3 A 1 B 3 A 0 B 3 A0B0 RESULT 6

Binary Integer Multiplication Once again, it s the same as grade school multiplication, only easier The Times Table is significantly smaller 0 1 0 0 0 1 0 1 but the process is exactly the same! Example of multiplying two numbers together: Multiplicand Multiplier 1 0 0 0two 1 0 0 1two 1 0 0 0two 0 0 0 0twoxx 0 0 0 0twoxxxx + 1 0 0 0twoxxxxxx Product 1 0 0 1 0 0 0two Note: multiplying two 4-bit numbers together produces an 8-bit result 7

Multiplication Hardware & Algorithm Basic hardware for 32-bit architecture 64-bit registers for multiplicand and product 32-bit register for multiplier 64-bit ALU to perform repeated additions 8

Multiplication Example Multiplication example using basic hardware and 4-bit inputs 4-bit example requires only 4 iterations, not 32 Initialize Product register to 0 Example: 2 ten 3 ten 4 th 4 4 9

Optimized (for size) Multiplication Hardware Reduced hardware requirements Start Multiplicand register and ALU now 32-bit Multiplier no longer has dedicated register Product0 = 1 1. Test Product0 Product0 = 0 Right half of Product register is initialized with multiplier, left half initialized to zero 1a. Add multiplicand to the left half of the product and place the result in the left half of the Product register 2. Shift the Product register right 1 bit 32nd repetition? No: < 32 repetitions Yes: 32 repetitions Done 10

Multiplication Example #2 Be sure to try out the previous multiplication example using the optimized hardware! Example: 2 ten 3 ten 11

A Faster Multiplier Uses multiple adders in a tree structure Requires more silicon but can be pipelined to perform much faster Cost/performance tradeoff 12

Signed Multiplication Recall from grade school arithmetic that the Product is negative if the signs of the Multiplicand and the Multiplier differ positive positive = positive negative negative = positive positive negative = negative Thus, in hardware: Perform the multiplication algorithm for 31 iterations (not 32) (this ignores the sign bit) If the original sign bits differed, then negate the result Be sure to do sign extension during computation when shifting multiplier to the right 13

Binary Integer Division The grade school long-division algorithm works for binary integer division First, ensure that the divisor is not 0 IF divisor dividend THEN place a 1 in the quotient and subtract the divisor from the dividend ELSE place a 0 in the quotient and expand the dividend to include the next bit Example of long-division on binary numbers: Quotient Dividend 1001twox 1000two 1001010twox Divisor -1000twoxxxx 10twoxxx 101twoxx 1010twox When dividend is exhausted, whatever is left over is the remainder Remainder -1000twox 10twox Note: dividing n-bit operands yields an n-bit quotient and an n-bit remainder 14

Binary Integer Division Several different approaches to perform long-division in hardware Restoring division subtracts the divisor from the dividend without comparing them If the result of the subtraction is < 0, then the dividend was smaller than the divisor Insert a 0 into the quotient Restore the dividend to its previous state by adding the divisor back in If the result of the subtraction is 0, then the dividend was larger than or equal to the divisor Insert a 1 into the quotient 15

Division Hardware & Algorithm Basic hardware for 32-bit architecture 64-bit registers for divisor and remainder 32-bit register for quotient 64-bit ALU to perform repeated sub/add ops 16

Division Example Division example using basic hardware and 4-bit inputs 4-bit example requires 5 iterations (one more than word size) Initialize Quotient register to 0 / Remainder register to dividend / and place divisor in top half of Divisor register Example: 7 ten 2 ten 5 th 5 5 17

Optimized (for size) Division Hardware Reduced hardware requirements Divisor register and ALU now 32-bit Quotient no longer has dedicated register Right half of Remainder register is initialized with dividend, and left half to 0 When division operation is complete: Left half of Remainder register contains the 32-bit remainder value Right half of the Remainder register contains the 32-bit quotient value Only the shift left operation is used for the division operation Well, then why is there a right shift input?!??!??!?! 18

Combined Hardware for Multiplication/Division Same hardware can be used for both multiplication and division algorithms Earlier, reduced hardware requirement for each unit when they were optimized for size Now, reusing same hardware for both MUL and DIV further reduces hardware requirements 19

A Faster Divider Multiplication can be parallelized Additional hardware resources can be used to perform multiplication faster Sacrifice size and cost for better performance Division cannot be parallelized like multiplication Dividers are slow Only produce a single bit for the quotient on each iteration Other, faster division algorithms do exist, but we won t cover them here 20

Signed Division Recall from grade school arithmetic that the Quotient is negative if the signs of the Dividend and the Divisor differ positive positive = positive negative negative = positive positive negative = negative Thus, in hardware: Perform the division algorithm If the sign bits of the dividend and the divisor differ, then negate the quotient Set the sign of the remainder to be the same as the dividend 21

Floating-Point Numbers Floating point numbers are numbers that have floating decimal points Used to represent non-integer numbers, including: Real numbers Examples: 99.2 3.14159265359 Very small numbers such as fractions Examples: 0.00187-0.1211 Very large numbers that cannot be represented using using the provided word size Examples: 987.02 10 9-0.002 10 4 In many programming languages, declare floating point numbers using float or double keyword Two different floating-point representations: single-precision and double-precision 22

Single-Precision Floating-Point Representation Represented using a single 32-bit word Sign bit (s) specifies sign of the floating-point value 0 indicates a positive / 1 indicates a negative Includes 8-bit exponent and 23-bit fraction Value of floating-point number is computed as: with a bias of 127 for single-precision values x = (-1) s (1 + fraction) 2 (exponent - bias) index 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 s exponent fraction 1 bit 8 bits 23 bits 23

Double-Precision Floating-Point Representation Represented using a TWO 32-bit words (totaling 64-bits) Sign bit (s) specifies sign of the floating-point value 0 indicates a positive / 1 indicates a negative Includes 11-bit exponent and 52-bit fraction Value of floating-point number is computed as: with a bias of 1023 for double-precision values x = (-1) s (1 + fraction) 2 (exponent - bias) index 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 s exponent fraction 1 bit 11 bits 20 bits fraction (continued) 32 bits 24

Bias? What the heck is that? Floating-point numbers can be negative AND also have a negative exponent Example: -0.002 10 4 The sign bit of a floating-point number, (s), indicates the sign of the entire value, not the exponent How about embedding a second sign bit in the exponent (bit 30) and storing as 2 s complement? Meh.. it would work, but it would make comparing floating-point values difficult Direct comparison of binary floating-point values would not be possible since negative numbers would appear larger than positive numbers Instead, bias the exponent value by the largest positive value and adjust exponent when interpreting value of floating-point number Enables DIRECT comparison of binary floating-point numbers 25

Single-Precision Range Exponents 0000_0000 two and 1111_1111 two are reserved Exponent 0 ten with a fraction of 0 ten indicates the value zero Exponent 255 ten with a fraction of 0 ten indicates the value Exponent 255 ten with any nonzero fraction indicates the value NaN (Not a Number) Smallest value in single-precision range: Binary exponent value: 0000_0001 two Actual exponent after biasing: 1 ten 127 ten = 126 ten Binary fraction value: 000_0000_0000_0000_0000_0000 two Significand: 1 ten + 0 ten = 1 ten Final value: ± significand 2 actual_exponent ± 1.0 2 126 ± 1.2 10 38 26

Single-Precision Range (continued) Largest value in single-precision range: Binary exponent value: 1111_1110 two Actual exponent after biasing: 254 ten 127 ten = +127 ten Binary fraction value: 111_1111_1111_1111_1111_1111 two Fraction value: 1 ten Significand: 1 ten + 1 ten 2 ten Final value: ± significand 2 actual_exponent ± 2.0 2 +127 ± 3.4 10 +38 27

Double-Precision Range Exponents 000_0000_0000 two and 111_1111_1111 two are reserved Exponent 0 ten with a fraction of 0 ten indicates the value zero Exponent 2047 ten with a fraction of 0 ten indicates the value Exponent 2047 ten with any nonzero fraction indicates the value NaN (Not a Number) Smallest value in double-precision range: Binary exponent value: 000_0000_0001 two Actual exponent after biasing: 1 ten 1023 ten = 1022 ten Binary fraction value: Significand: 000_0000_0000 0000_0000_0000 two (52 bits of zeros) 1 ten + 0 ten = 1 ten Final value: ± significand 2 actual_exponent ± 1.0 2 1022 ± 2.2 10 308 28

Double-Precision Range (continued) Largest value in double-precision range: Binary exponent value: 111_1111_1110 two Actual exponent after biasing: 2046 ten 1023 ten = +1023 ten Binary fraction value: 111_1111_1111 1111_1111_1111 two (52 bits of ones) Fraction value: 1 ten Significand: 1 ten + 1 ten 2 ten Final value: ± significand 2 actual_exponent ± 2.0 2 +1023 ± 1.8 10 +308 29

Floating-Point Precision Single-precision Approximately 2 23 Can represent a value x and (x + 2-23 ), but not the numbers in between Double-precision Approximately 2 52 Can represent a value x and (x + 2-52 ), but not the numbers in between 30

Floating-Point Example (bin -> float) What number is represented by the single-precision floating-point value? index 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 bit 8 bits 23 bits Sign bit: 1 (negative number) x = (-1) s (1 + fraction) 2 (exponent - bias) Exponent: 1000_0001 two = 129 ten Fraction: 010_0000_0000_0000_0000_0000 two (0 2-1 ) + (1 2-2 ) + + (0 2-23 ) = 0 + (1 ¼) + + 0 = ¼ =.25 x = (-1) s (1 + fraction) 2 (exponent - bias) = (-1) 1 (1 +.25) 2 (129-127) = -1 1.25 2 2 = -5.0 31

Floating-Point Example (float -> bin) What is the binary representation of the decimal number 29.28125 in single-precision float format? Step #1 Rewrite whole number portion in binary: 29 ten = 11101 two Step #2 Rewrite fractional portion in binary: 0.28125 ten = 010_0100_0000_0000_0000_0000 two set bit -1? // 2-1 = 0.5 // 0.5 > 0.28125 (no, too big) // 0xx_xxxx_xxxx_xxxx_xxxx_xxxx set bit -2? // 2-2 = 0.25 // 0.25 0.28125 (yes, it fits) // 01x_xxxx_xxxx_xxxx_xxxx_xxxx 0.28125-0.25 = 0.03125 remains set bit -3? // 2-3 = 0.125 // 0.125 > 0.03125 (no, too big) // 010_xxxx_xxxx_xxxx_xxxx_xxxx set bit -4? // 2-4 = 0.0625 // 0.0625 > 0.03125 (no, too big) // 010_0xxx_xxxx_xxxx_xxxx_xxxx set bit -5? // 2-5 = 0.03125 // 0.03125 0.03125 (yes, it fits) // 010_01xx_xxxx_xxxx_xxxx_xxxx 0.03125-0.03125 = 0 remains (DONE) set bit -6 through bit -23 to 0 // 010_0100_0000_0000_0000_0000 32

Floating-Point Example (float -> bin) (continued) What is the binary representation of the decimal number 29.28125 in single-precision float format? Step #3 Combine the rewritten components: 11101. 01001000000000000000000 two Step #4 Normalize the value (shift decimal): 1.11010100100000000000000 two 2 4 Normalized value represents the (1 + fraction), drop the leading 1 from the normalized value Step #5 Determine sign bit and exponent bits: Original value, 29.28125, was positive so: s = 0 Exponent from normalized value = 4, add to bias to determine exponent bits exponent = 4 + bias = 4 + 127 = 131 ten = 1000_0011 two Step #6 Put it all together: s _ exponent _ fraction = 0 _ 1000_0011 _ 110_1010_0100_0000_0000_0000 two = 0100_0001_1110_1010_0100_0000_0000_0000 two = 41EA4000 hex 33