Docsity

MIPS Floating Point Arithmetic - Lecture Slides | CS 232, Study notes of Computer Architecture and Organization

Partial preview of the text

Download MIPS Floating Point Arithmetic - Lecture Slides | CS 232 and more Study notes Computer Architecture and Organization in PDF only on Docsity!

MIPS floating-point arithmetic

Question

ƒ^ Which of the following are represented by thehexadecimal number 0x39383700 ?— the integer

— the string

— the float

— the instruction

xor

$24, $9, 0x

MIPS floating-point arithmetic

Answer

ƒ^ Which of the following are represented by thehexadecimal number 0x39383700 ?Answer: All of them. They are just differentinterpretations of the same bit patterns. ƒ^ Then how does the machine know which interpretation youwant?You have to explicitly tell the machine which interpretation you want.— Use an

integer load

(lw) to interpret them as an

int

— Use a

floating point load

(l.s) to interpret them as a

float

— Use a

branch

or a^

jump

(bne or j) to interpret them as an

instruction

MIPS floating-point arithmetic

Floating-point representation

ƒ^ IEEE numbers are stored using a kind of scientific notation.

±^ mantissa * 2

exponent

ƒ^ We can represent floating-point numbers with three binary fields: a signbit s, an exponent field e, and a fraction field f. ƒ^ The IEEE 754 standard defines several different precisions.— Single precision numbers include an 8-bit exponent field and a 23-bit

fraction, for a total of 32 bits.— Double precision numbers have an 11-bit exponent field and a 52-bitfraction, for a total of 64 bits.

f

e s

MIPS floating-point arithmetic

Sign

ƒ^ The sign bit is 0 for positive numbers and 1 for negative numbers. ƒ^ But unlike integers, IEEE values are stored in signed magnitude format.

f

e s

MIPS floating-point arithmetic

Exponent

ƒ^ The e field represents the exponent as a biased number.— It contains the actual exponent

plus^

127 for single precision, or the

actual exponent

plus^

1023 in double precision.

— This converts all single-precision exponents from

  • 127 to +127 into

unsigned numbers from 0 to 255, and all double-precision exponentsfrom^

  • 1024 to +1023 into unsigned numbers from 0 to 2047. ƒ^ Two examples with single-precision numbers are shown below.— If the exponent is 4, the e field will be 4 + 127 = 131 (

— If e contains 01011101 (

), the actual exponent is 93 10

-^ 127 =

ƒ^ Storing a biased exponent

before

a normalized mantissa means we can

compare IEEE values as if they were signed integers.

f

e s

MIPS floating-point arithmetic

Converting an IEEE 754 number to decimal

ƒ^ The decimal value of an IEEE number is given by the formula:

(1^ -^ 2s) * (1 + f) * 2

e-bias

ƒ^ Here, the s, f and e fields are assumed to be in decimal.— (

-^ 2s) is 1 or

  • 1, depending on whether the sign bit is 0 or 1.

— We add an implicit 1 to the fraction field f, as mentioned earlier.— Again, the bias is either 127 or 1023, for single or double precision.

f

e s

MIPS floating-point arithmetic

Example IEEE-decimal conversion

ƒ^ Let’s find the decimal value of the following IEEE number.

ƒ^ First convert each individual field to decimal.— The sign bit s is 1.— The e field contains 01111100 = 124

— The mantissa is 0.11000… = 0.

.^10

ƒ^ Then just plug these decimal values of s, e and f into our formula.

(1^ -^ 2s) * (1 + f) * 2

e-bias

ƒ^ This gives us (

-^ 2) * (1 + 0.75) * 2

124-

=^ (-

-3^ )^ =^

MIPS floating-point arithmetic

Converting a decimal number to IEEE 754

ƒ^ What is the single-precision representation of 347.625?1.

First convert the number to binary: 347.625 = 101011011.

.^2

2.^ Normalize the number by shifting the binary point until there is asingle 1 to the left:101011011.101 x 2

0 =^ 1.01011011101 x 2

8

3.^ The bits to the right of the binary point comprise the fractional fieldf. 4.^ The number of times you shifted gives the exponent. The field eshould contain: exponent + 127.5.^ Sign bit: 0 if positive, 1 if negative.

MIPS floating-point arithmetic

Steps 1-2: the actual addition

  1. Equalize the exponents.The operand with the smaller exponent should be rewritten by increasingits exponent and shifting the point leftwards.

-1^ =

With four significant digits, this gets rounded to:This can result in a loss of least significant digits—the rightmost 1 in thiscase. But rewriting the number with the larger exponent could result inloss of the

most

significant digits, which is much worse.

  1. Add the mantissas.

MIPS floating-point arithmetic

Steps 3-5: representing the result

  1. Normalize the result if necessary.

This step may cause the point to shift either left or right, and theexponent to either increase or decrease.4. Round the number if needed.

2 gets rounded to

  1. Repeat Step 3 if the result is no longer normalized.We don’t need this in our example, but it’s possible for rounding to adddigits—for example, rounding 9.9995 yields 10.000.Our result is

, or^

. The correct answer is 100.151, so we have

the right answer to four significant digits, but there’s a small error already.

MIPS floating-point arithmetic

FPU like co-processor on chip

MIPS floating-point arithmetic

Summary

ƒ^ The IEEE 754 standard defines number representations and operations forfloating-point arithmetic. ƒ^ Having a finite number of bits means we can’t represent all possible realnumbers, and errors will occur from approximations.