Docsity

Arithmetic for Computers: Arithmetic Operations on Integers and Floating-Point Numbers, Exams of Computer Architecture and Organization

Partial preview of the text

Download Arithmetic for Computers: Arithmetic Operations on Integers and Floating-Point Numbers and more Exams Computer Architecture and Organization in PDF only on Docsity!

Chapter 3 Arithmetic for Computers

Chapter 3 — Arithmetic for Computers — 2

Arithmetic for Computers „

Operations on integers

„

Addition and subtraction

„

Multiplication and division

„

Dealing with overflow

„

Floating-point real numbers

„

Representation and operations

§3.1 Introduction

Chapter 3 — Arithmetic for Computers — 4

Integer Subtraction „

Add negation of second operand

„

Example: 7 – 6 = 7 + (–6)

„

Overflow if result out of range

„ Subtracting two +ve or two –ve operands, no overflow „ Subtracting +ve1 from –ve2 operand, e.g., -ve2 – ve „ Overflow if result sign is 0 „ Subtracting –ve1 from +ve2 operand, e.g., ve2 – (- ve1) „ Overflow if result sign is 1

Overflow conditions „

Overflow conditions for addition andsubtraction

Chapter 3 — Arithmetic for Computers — 5

Chapter 3 — Arithmetic for Computers — 7

Arithmetic for Multimedia „

Graphics and media processing operateson 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 representablevalue

c.f. 2s-complement modulo arithmetic

„

E.g., clipping in audio, saturation in video

Detecting Overflow „

For signed addition „ For unsigned addition

Chapter 3 — Arithmetic for Computers — 8

**addu $t $t $t

$t0 = $t1+$t2, but don’t trap**

xor $t $t $t

check if sings differ, $t3 < 0 if signs differ

slt $t $t $zero $ t3=1 if signs differ bne $t $zero No_overflow

$t1, $t2 signs differ, and so no overflow

xor $t $t $t

sign of sum match too? # $t3 < 0 if sum sign differ

slt $t $t $zero

$t3=1 if sum sign differ

bne $t $zero Overflow

all 3 signs differ, go to overflow

**addu $t $t $t

$t0 = $t1+$t**

nor $t $t $zero

t3=NOT $t1 (2’s comp – 1 : 2^32-$t1-1)

sltu $t $t $t $ (2^32 - $t1 – 1) < $$t2 => 2^32-1 < $t1 + $t bne $t $zero Overflow

if (2^32-1<$t1+$t2) goto overflow

$t1 0..… $t2 1..… $t3 1…..

Chapter 3 — Arithmetic for Computers — 10

Multiplication „

Start with long-multiplication approach

1000 × 10011000 0000 0000 10001001000 Length of product =length ofmultiplicand + length+ multiplier, if weignore the sign bits multiplicand multiplier product §3.3 Multiplication Multiplication hardware is simplyshifts and adds

Chapter 3 — Arithmetic for Computers — 11

Multiplication Hardware

Initially 0 1000 × 10011000 0000 0000 10001001000 Takes about 100 cycles if each step takes 1 cycle

Chapter 3 — Arithmetic for Computers — 13

Faster Multiplier „

Uses multiple adders for the 32 additions

Cost/performance tradeoff

„

Can be pipelined

Several multiplication performed in parallel

Chapter 3 — Arithmetic for Computers — 14

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

(pseudo

instruction)

„ Least-significant 32 bits of product –> rd

Chapter 3 — Arithmetic for Computers — 16

Division Hardware

Initially dividend Initially divisor in left half

Chapter 3 — Arithmetic for Computers — 17

Optimized Divider „

One cycle per partial-remainder subtraction

Remainder is shifted left

Reminder in the HI, quotient on the LO

Looks a lot like a multiplier!

„ Same hardware can be used for both

Chapter 3 — Arithmetic for Computers — 19

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 — 20

Floating Point „

Representation for non-integral numbers

„

Including very small and very large numbers

„

Like scientific notation

„

–2.34 × 10

56 „

+0.002 × 10

  • „

+987.02 × 10

9 „

In binary

„

xxxxxxx

2

× 2

yyyy „

Types

float

and

double

in C

normalized not normalized §3.5 Floating Point