Chapter 3. Arithmetic for Computers

Similar documents
Chapter 3 Arithmetic for Computers

Chapter 3. Arithmetic Text: P&H rev

Thomas Polzer Institut für Technische Informatik

Chapter 3. Arithmetic for Computers

Floating Point Arithmetic

TDT4255 Computer Design. Lecture 4. Magnus Jahre

Chapter 3. Arithmetic for Computers

Chapter 3. Arithmetic for Computers

Computer Architecture and IC Design Lab. Chapter 3 Part 2 Arithmetic for Computers Floating Point

Arithmetic for Computers. Hwansoo Han

Floating Point Arithmetic. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

COMPUTER ORGANIZATION AND DESIGN

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

CSCI 402: Computer Architectures. Arithmetic for Computers (4) Fengguang Song Department of Computer & Information Science IUPUI.

Integer Arithmetic. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Chapter 3 Arithmetic for Computers (Part 2)

Written Homework 3. Floating-Point Example (1/2)

September, 2003 Saeid Nooshabadi

Review: MIPS Organization

Computer Architecture. Chapter 3: Arithmetic for Computers

Module 2: Computer Arithmetic

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

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

CSCI 402: Computer Architectures. Arithmetic for Computers (3) Fengguang Song Department of Computer & Information Science IUPUI.

Chapter 3. Floating Point. Floating Point Standard. Morgan Kaufmann Publishers 13 November, Chapter 3 Arithmetic for Computers 1

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

Divide: Paper & Pencil

ECE260: Fundamentals of Computer Engineering

ECE232: Hardware Organization and Design

September, Saeid Nooshabadi. Overview IEEE 754 Standard COMP Microprocessors and Embedded Systems

ECE 154A Introduction to. Fall 2012

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

COMP2611: Computer Organization. Data Representation

Floating Point COE 308. Computer Architecture Prof. Muhamed Mudawar. Computer Engineering Department King Fahd University of Petroleum and Minerals

Arithmetic for Computers. Integer Addition. Chapter 3

Instruction Set extensions to X86. Floating Point SIMD instructions

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

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

MIPS Integer ALU Requirements

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

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

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

Integer Subtraction. Chapter 3. Overflow conditions. Arithmetic for Computers. Signed Addition. Integer Addition. Arithmetic for Computers

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

ECE331: Hardware Organization and Design

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

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

CO212 Lecture 10: Arithmetic & Logical Unit

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

Floating Point Arithmetic

Chapter 3 Arithmetic for Computers

3.5 Floating Point: Overview

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

Chapter Three. Arithmetic

Arithmetic. Chapter 3 Computer Organization and Design

Chapter 3: Arithmetic for Computers

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

Number Systems and Computer Arithmetic

ECE331: Hardware Organization and Design

Computer Architecture Set Four. Arithmetic

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

Floating Point Numbers

Numeric Encodings Prof. James L. Frankel Harvard University

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

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

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

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

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Floating-Point Arithmetic

September, Saeid Nooshabadi. Review: Floating Point Representation COMP Single Precision and Double Precision

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS Arithmetic (a) The four possible cases Carry (b) Truth table x y

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

Organisasi Sistem Komputer

CS 101: Computer Programming and Utilization

CPE300: Digital System Architecture and Design

Foundations of Computer Systems

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

M1 Computers and Data

Floating Point Numbers. Lecture 9 CAP

xx.yyyy Lecture #11 Floating Point II Summary (single precision): Precision and Accuracy Fractional Powers of 2 Representation of Fractions

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

Computer Arithmetic Ch 8

Computer Arithmetic Ch 8

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Floating Point Arithmetic

IEEE Standard 754 for Binary Floating-Point Arithmetic.

By, Ajinkya Karande Adarsh Yoga

Floating Point January 24, 2008

Chapter 2 Data Representations

IEEE Standard for Floating-Point Arithmetic: 754

UNIT-III COMPUTER ARTHIMETIC

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

CS61C : Machine Structures

CS61C Floating Point Operations & Multiply/Divide. Lecture 9. February 17, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)

Divide: Paper & Pencil CS152. Computer Architecture and Engineering Lecture 7. Divide, Floating Point, Pentium Bug. DIVIDE HARDWARE Version 1

Review 1/2 Big Idea: Instructions determine meaning of data; nothing inherent inside the data Characters: ASCII takes one byte

The Sign consists of a single bit. If this bit is '1', then the number is negative. If this bit is '0', then the number is positive.

CSCI 402: Computer Architectures

Chapter 5 : Computer Arithmetic

Transcription:

Chapter 3 Arithmetic for Computers

Arithmetic for Computers Unsigned vs. Signed Integers Operations on integers Addition and subtraction Multiplication and division Dealing with overflow Floating-point real numbers Representation and operations 3.1 Int roduction Chapter 3 Arithmetic for Computers 2

Unsigned Binary Integers Given an n-bit number x = x + n 1 n 2 1 0 n 12 + xn 22 + L+ x12 x02 Range: 0 to +2 n 1 Example 2.4 Sig gned and Unsigne ed Numb bers 0000 0000 0000 0000 0000 0000 0000 1011 2 =0+ +1 2 3 +0 2 2 +1 2 1 +1 2 0 = 0 + + 8 + 0 + 2 + 1 = 11 10 Using 32 bits 0 to +4,294,967,295 Chapter 3 Arithmetic for Computers 3

Review: 2 s Complement Binary Representation Negate 1011 and add a 1 1010 complement all the bits Note: negate and invert are different! -2 3 = -(2 3-1) = 2 3-1 = 2 sc binary decimal 1000-8 1001-7 1010-6 1011-5 1100-4 1101-3 1110-2 1111-1 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 Chapter 3 Arithmetic for Computers 4

2s-Complement Signed Integers Given an n-bit number x = x + n 1 n 2 1 0 n 12 + xn 22 + L+ x12 x02 Range: 2 n 1 to +2 n 1 1 Example 1111 1111 1111 1111 1111 1111 1111 1100 2 = 1 2 31 +1 2 30 + + 1 2 2 +0 2 1 +0 2 0 = 2,147,483,648 + 2,147,483,644 = 4 10 Using 32 bits 2,147,483,648 to +2,147,483,647 Chapter 3 Arithmetic for Computers 5

2s-Complement Signed Integers Bit 31 is sign bit 1f for negative numbers 0 for non-negative numbers ( 2 n 1 ) can t be represented Non-negative numbers have the same unsigned and 2s-complement representation Some specific numbers 0: 0000 0000 0000 1: 1111 1111 1111 Most-negative: 1000 0000 0000 Most-positive: 0111 1111 1111 Chapter 3 Arithmetic for Computers 6

MIPS Number Representations 32-bit signed numbers (2 s complement): 0000 0000 0000 0000 0000 0000 0000 0000 two = 0 ten 0000 0000 0000 0000 0000 0000 0000 0001 two = + 1 ten... maxint MSB 0111 1111 1111 1111 1111 1111 1111 1110 two = + 2,147,483,646,, ten 0111 1111 1111 1111 1111 1111 1111 1111 two = + 2,147,483,647 ten 1000 0000 0000 0000 0000 0000 0000 0000 two = 2,147,483,648 ten 1000 0000 0000 0000 0000 0000 0000 0001 two = 2,147,483,647 ten... 1111 1111 1111 1111 1111 1111 1111 1110 two = 2 minint ten 1111 1111 1111 1111 1111 1111 1111 1111 two = 1 ten LSB Converting <32-bit values into 32-bit values copy the most significant bit (the sign bit) into the empty bits 0010 -> 0000 0010 1010 -> 1111 1010 sign extend versus zero extend (lb vs. lbu) Chapter 3 Arithmetic for Computers 7

Signed Negation Complement and add 1 Complement means 1 0, 0 1 x + x = 1111...1112 = 1 x + 1= x Example: negate +2 +2 = 0000 0000 0010 2 2 = 1111 1111 1101 2 + 1 = 1111 1111 1110 2 Chapter 3 Arithmetic for Computers 8

Sign Extension Representing a number using more bits Preserve the numeric value In ARM instruction set LDRSB,LDRSH: extend loaded byte/halfword Replicate the sign bit to the left c.f. unsigned values: extend with 0s Examples: 8-bit to 16-bit +2: 0000 0010 => 0000 0000 0000 0010 2: 1111 1110 => 1111 1111 1111 1110 Chapter 3 Arithmetic for Computers 9

Integer Addition Example: 7 + 6 3.2 Ad dition and Subtraction 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 10

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 11

Dealing with Overflow Some languages (e.g., C) ignore overflow Use ARM ADD,SUB instructions Other languages (e.g., Ada, Fortran) require raising an exception Use ARM ADDS,SUBS instructions On overflow, invoke exception handler Chapter 3 Arithmetic for Computers 12

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 13

Multiplication Start with long-multiplication approach 3.3 Mu ultiplicatio on multiplicand 1000 multiplier 1001 1000 0000 0000 1000 product 1001000 Length of product is the sum of operand lengths Chapter 3 Arithmetic for Computers 14

Multiplication Hardware Initially 0 Chapter 3 Arithmetic for Computers 15

Optimized Multiplier Perform steps in parallel: add/shift One cycle per partial-product addition That s ok, if frequency of multiplications is low Chapter 3 Arithmetic for Computers 16

Faster Multiplier Uses multiple adders Cost/performance tradeoff Can be pipelined Several multiplication performed in parallel Chapter 3 Arithmetic for Computers 17

ARM Multiplication The MUL instruction is used to multiply signed or unsigned variables to produce a 32-bit result Instruction MUL Rd,Rm,Rs ; Rd = Rm * Rs (32 bits) Because the MUL instruction produces only the lower 32 bits of the 64-bit product, MUL gives the same answer for both signed and unsigned numbers Chapter 3 Arithmetic for Computers 18

Division divisor quotient dividend remainder 1001 1000 1001010-1000 10 101 1010-1000 10 n-bit operands yield n-bit quotient and remainder 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 3.4 Div vision Chapter 3 Arithmetic for Computers 19

Division Hardware Initially divisor in left half Initially dividend Chapter 3 Arithmetic for Computers 20

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 21

Faster Division Can t use parallel hardware as in multiplier Subtraction is conditional on sign of remainder Faster dividers (e.g. SRT devision) generate multiple quotient bits per step Still require multiple steps Chapter 3 Arithmetic for Computers 22

ARM Division The classic ARM instruction set does not include divide instruction ARMv7R and ARMv7M include Signed integer divide - SDIV Unsigned integer divide id - UDIV Chapter 3 Arithmetic for Computers 23

Floating Point Representation for non-integral numbers Including very small and very large numbers 3.5 Flo oating Po oint Like scientific notation 2.34 10 56 +0.002 10 4 +987.02 10 9 normalized not normalized In binary ±1.xxxxxxx 2 2 yyyy Types float and double in C Chapter 3 Arithmetic for Computers 24

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 i (64-bit) Chapter 3 Arithmetic for Computers 25

IEEE Floating-Point Format single: 8 bits double: 11 bits S Exponent single: 23 bits double: 52 bits Fraction x = ( 1) S (1+ Fraction) 2 (Exponent Bias) 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 26

IEEE 754 FP Standard Encoding Most (all?) computers these days conform to the IEEE 754 floating point standard (-1) sign x (1+F) x 2 E-bias Formats for both single and double precision F is stored in normalized form where the msb in the fraction is 1 (so there is no need to store it!) called the hidden bit To simplify sorting FP numbers, E comes before F in the word and E is represented in excess (biased) notation Single Precision Double Precision Object Represented E (8) F (23) E (11) F (52) 0 0 0 0 true zero (0) 0 nonzero 0 nonzero ± denormalized number ± 1-254 anything ± 1-2046 anything ± floating point number ± 255 0 ± 2047 0 ± infinity 255 nonzero 2047 nonzero not a number (NaN) Chapter 3 Arithmetic for Computers 27

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 28

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 29

Floating-Point Precision Relative precision all fraction bits are significant Single: approx 2 23 Equivalent to 23 log 10 2 23 0.3 6 decimal digits of precision Double: approx 2 52 Equivalent to 52 log 10 2 52 03 0.3 16 decimal digits of precision Chapter 3 Arithmetic for Computers 30

Floating-Point Example Represent 0.75 0.75 = ( 1) 1 1.1 2 2 1 S = 1 Fraction = 1000 00 2 Exponent = 1+Bi Bias Single: 1 + 127 = 126 = 01111110 2 Double: 1 + 1023 = 1022 = 01111111110 2 Single: 1011111101000 00 Double: 1011111111101000 00 Chapter 3 Arithmetic for Computers 31

Floating-Point Example What number is represented by the single- precision i float 11000000101000 00 S = 1 Fraction = 01000 00 2 Exponent = 10000001 2 = 129 x=( ( 1) 1 (1 +.01 2 ) 2 = ( 1) 1.25 2 2 = 5.0 (129 127) Chapter 3 Arithmetic for Computers 32

Denormal Numbers Exponent = 000...0 hidden bit is 0 x = ( 1) S (0 + Fraction) 2 Bias Smaller than normal numbers allow for gradual underflow, with diminishing precision Denormal with fraction = 000...0 x S = ( 1) (0 + 0) 2 Bias Two representations of f00! 0.0! = ±0.00 Chapter 3 Arithmetic for Computers 33

Infinities and NaNs Exponent = 111...1, Fraction = 000...0 ±Infinity Can be used in subsequent calculations, avoiding need for overflow check Exponent = 111...1, 1 Fraction 000...0 0 Not-a-Number (NaN) Indicates illegal or undefined result e.g., 0.0 / 0.0 Can be used in subsequent calculations Chapter 3 Arithmetic for Computers 34

Floating-Point Addition Consider a 4-digit decimal example 9.999999 10 1 + 1.610 10 1 1. Align decimal points Shift number with smaller exponent 9.999 10 1 + 0.016 10 1 2. Add significands ifi 9.999 10 1 + 0.016 10 1 = 10.015 10 1 3. Normalize result & check for over/underflow 1.0015 10 2 4. Round and renormalize if necessary 1.002 10 2 Chapter 3 Arithmetic for Computers 35

Floating-Point Addition Now consider a 4-digit binary example 1.000 110 2 2 1 + 1.110 2 2 2 (0.5 + 0.4375) 1. Align binary points Shift number with smaller exponent 1.000 2 2 1 + 0.111 2 2 1 2. Add significands ifi 1.000 2 2 1 + 0.111 2 2 1 = 0.001 2 2 1 3. Normalize result & check for over/underflow 1.000 2 2 4, with no over/underflow 4. Round and renormalize if necessary 1.000 2 2 4 (no change) = 0.0625 Chapter 3 Arithmetic for Computers 36

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 37

FP Adder Hardware Step 1 Step 2 Step 3 Step 4 Chapter 3 Arithmetic for Computers 38

Floating-Point Multiplication Consider a 4-digit decimal example 1.110110 10 10 9.200 10 5 1. Add exponents For biased exponents, subtract bias from sum New exponent = 10 + 5 = 5 2. Multiply significands 1.110110 9.200 = 10.212 10.212 10 5 3. Normalize result & check for over/underflow 1.0212 10 6 4. Round and renormalize if necessary 1.021 10 6 5. Determine sign of result from signs of operands +1.021 10 6 Chapter 3 Arithmetic for Computers 39

Floating-Point Multiplication Now consider a 4-digit binary example 1.000 1 110 2 2 2 1.110 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.000 110 1102 110 2 1.110 2 = 1.1102 1.110 2 2 3 3. Normalize result & check for over/underflow 1.110110 3 2 2 (no change) with no over/underflow 4. Round and renormalize if necessary 1.110 2 2 3 (no change) 5. Determine sign: +ve ve ve 1.110 2 2 3 = 0.21875 Chapter 3 Arithmetic for Computers 40

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 41

FP Instructions in ARM FP hardware is coprocessor 1 Adjunct processor that extends the ISA Separate FP registers 32 single-precision: s0, s1, s31 Paired for double-precision: s0/s1, s2/s3, to give 16 double precision registers : d0,d1..d15 FP instructions operate only on FP registers Programs generally don t do integer ops on FP data, or vice versa More registers with minimal code-size impact FP load and store instructions FLDS,FSTS,FLDD,FSTD e.g., FLDS s1, [r1,#100] ; s1 = mem [r1+400] Chapter 3 Arithmetic for Computers 42

FP Instructions in ARM Single-precision arithmetic FADDS,FSUBS,FMULS,FDIVSFSUBS FMULS FDIVS e.g., FADDS s2,s4,s6 ; s2 = s4 + s6 Double-precision arithmetic FADDD,FSUBD,FMULD,FDIVD e.g., FSUBD d2,d4,d6 ; d2 = d4 d6 Single- and double-precision comparison FCMPS, FCMPD e.g. FCMPS s2,s4 ; if (s2 s4) Branch on FP condition flag FMSTAT ; cond.flags = FP cond. flags Copy FP condition flags to integer condition flag Chapter 3 Arithmetic for Computers 43

Summary of ARM FP instructions discussed thus far Chapter 3 Arithmetic for Computers 44

FP Example: F to C C code: float f2c (float fahr) { return ((5.0/9.0)*(fahr - 32.0)); } fahr in s12, result in s0, literals in global memory space Compiled ARM code: f2c: FLDS s16, [r12, const5] ;s16 = 5.0 FLDS s18, [r12, const9] ;s18 = 9.0 FDIVS s16,s16,s18 ; s16 = 5.0/9.0 FLDS s18, [r12,const32] ; s18 = 32.0 FSUBS s18,s12,s18 s12 ; s18 = fahr 32 FMULS s0,s16,s18 ; s0 = (5/9)* (fahr 32) MOV pc,lr ; return Chapter 3 Arithmetic for Computers 45

FP Example: Array Multiplication X = X + Y Z All 32 32 matrices, 64-bit double-precision i elements C code: void mm (double x[][], double y[][], double z[][]) { int i, j, k; for (i = 0; i! = 32; i = i + 1) for (j = 0; j! = 32; j = j + 1) for (k = 0; k! = 32; k = k + 1) x[i][j] = x[i][j] + y[i][k] * z[k][j]; } Chapter 3 Arithmetic for Computers 46

FP Example: Array Multiplication ARM code: Addresses of variable x, y, z in r0, r1, r2, and i,j, j k in r3, r4, r5. The address of x[ i ][ j ] is r6 and address of y[ i ][ j ] or z[ i ][ j ] is r12. Mm: SUB sp,sp,#12 ; make room on stack for 3 regs STR r4,[sp,#8] ; save r4 on stack STR r5,[sp,#4] ; save r5 on stack STR r6,[sp,#0] ; save r6 on stack MOV i, 0 ; i = 0 initialize 1 st for loop L1: MOV j, 0 ; j = 0 restart 2 nd for loop L2: MOV k,0 ; k = 0 restart 3 rd for loop ADD xijaddr,j,i, LSL #5 ; xijaddr = i*size(row) + j ADD xijaddr,x,xijaddr,lsl #3 ; xijaddr = byte address of x[i][j] Chapter 3 Arithmetic for Computers 47

FP Example: Array Multiplication FLDD s4, [xijaddr,#0] ; s4 = 8 bytes of x[i][j] L3: ADD tempaddr, j,k,lsl #5 ; tempaddr = k *size(row) + j ADD tempaddr,z,tempaddr,lsl #3 ; tempaddr= byte ;address of z[k][j] FLDD s16,[tempaddr,#0] ; s16 = 8 bytes of z[k][j] ADD tempaddr, k,i,lsl #5 ; tempaddr = i *size(row) + k ADD tempaddr,y,tempaddr,lsl #3 ; tempaddr= byte ;address of y[i][k] FLDD s18,[tempaddr,#0] ; s18 = 8 bytes of y[i][k] FMULD s16,s18,s16 s18 ; s16 = y[i][k] * z[k][j] FADDD s4,s4,s16 ; s4 =x[i][j] + ; y[i][k]*z[k][j] Chapter 3 Arithmetic for Computers 48

FP Example: Array Multiplication ADD k,k,#1 ; k = k+1 CMP k, #32 BLT L3 ; if (k<32) go to L3 FSTD s4, [xijaddr,#0] ; x[i][j] = s4 ADD j,j, #1 ; j = j+1 CMP j #32 BLT ls ; if ( j<32) go to L2 ADD i,i, #1 ; i = i + 1 CMP i, #32 BLT L1 ; if ( i<32) go to L1 Chapter 3 Arithmetic for Computers 49

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 g languages g and FP libraries just use defaults Trade-off between hardware complexity, performance, and market requirements Chapter 3 Arithmetic for Computers 50

Interpretation of Data The BIG Picture Bits have no inherent meaning Interpretation depends on the instructions applied Computer representations of numbers Finite range and precision Need to account for this in programs Chapter 3 Arithmetic for Computers 51

Associativity Parallel programs may interleave operations in unexpected orders Assumptions of associativity may fail (x+y)+z x+(y+z) x -1.50E+38-1.50E+38 y 1.50E+38 0.00E+00 z 10 1.0 10 1.0 1.50E+38 1.00E+00 0.00E+00 Need to validate parallel l programs under varying degrees of parallelism 3.6 Pa arallelism and Com mputer Arithmetic c: Associa ativity Chapter 3 Arithmetic for Computers 52

Right Shift and Division Left shift by i places multiplies an integer by 2 i Right shift divides by 2 i? Only for unsigned integers For signed integers Arithmetic right shift: replicate the sign bit eg e.g., 5/4 11111011 2 >> 2 = 11111110 2 = 2 Rounds toward c.f. 11111011 2 >>> 2 = 00111110 2 = +62 3.8 Fa llacies an nd Pitfalls Chapter 3 Arithmetic for Computers 53

Who Cares About FP Accuracy? Important for scientific code But for everyday consumer use? My bank balance is out by 0.0002! The Intel Pentium FDIV bug The market expects accuracy See Colwell, The Pentium Chronicles Chapter 3 Arithmetic for Computers 54

Concluding Remarks ISAs support arithmetic Signed and unsigned integers Floating-point approximation to reals Bounded range and precision Operations can overflow and underflow ARM core instructions dominate integer SPEC2006 execution and integer and arithmetic core dominate SPEC2006 floating point execution. 3.9 Co oncluding Remark ks Chapter 3 Arithmetic for Computers 55