Docsity

CS429: Computer Organization and Architecture - Floating Point, Summaries of Computer Architecture and Organization

Toggle sidebar
bg1
CS429: Computer Organization and Architecture
Floating Point
Dr. Bill Young
Department of Computer Science
University of Texas at Austin
Last updated: February 5, 2020 at 15:08
CS429 Slideset 4: 1 Floating Point
Topics of this Slideset
IEEE Floating Point Standard
Rounding
Floating point operations
Mathematical properties
CS429 Slideset 4: 2 Floating Point
Floating Point Puzzles
i n t x = . . . ;
f l o a t f = . . . ;
d o u b l e d = . . . ;
For each of the following, either:
argue that it is true for all argument values, or
explain why it is not true.
Assume neither d nor f is NaN.
x == (int)(float) x
x == (int)(double) x
f == (float)(double) f
d == (float) d
f == -(-f)
2/3 == 2/3.0
d < 0.0 →((d*2) < 0.0)
d > f →-f > -d
d*d >= 0.0
(d+f)-d == f
CS429 Slideset 4: 3 Floating Point
IEEE Floating Point Standard
IEEE Standard 754
Established in 1985 as a uniform standard for floating point
arithmetic
It is supported by all major CPUs.
Before 1985 there were many idiosyncratic formats.
Driven by Numerical Concerns
Nice standards for rounding, overflow, underflow
Hard to make go fast: numerical analysts predominated over
hardware types in defining the standard
Now all (add, subtract, multiply) operations are fast except
divide.
CS429 Slideset 4: 4 Floating Point
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS429: Computer Organization and Architecture - Floating Point and more Summaries Computer Architecture and Organization in PDF only on Docsity!

CS429: Computer Organization and Architecture

Floating Point Dr. Bill YoungDepartment of Computer ScienceUniversity of Texas at Austin Last updated: February 5, 2020 at 15:08^ CS429 Slideset 4: 1

Floating Point

Topics of this Slideset^ IEEE Floating Point Standard^ Rounding^ Floating point operations^ Mathematical properties

CS429 Slideset 4: 2

Floating Point

Floating Point Puzzles^ i n t^ x =

... ; f l o a t f =^... ; d o u b l e d =^... ; For each of the following, either: argue that it is true for all argument values, or explain why it is not true.Assume neither d nor f is NaN. x == (int)(float) xx == (int)(double) xf == (float)(double) fd == (float) df == -(-f)2/3 == 2/3.0d < 0.^

→^ ((d*2) < 0.0) d > f^

→^ -f > -d d*d >= 0.0(d+f)-d == f

CS429 Slideset 4: 3

Floating Point

IEEE Floating Point Standard^ IEEE Standard 754^ Established in 1985 as a uniform standard for floating pointarithmetic^ It is supported by all major CPUs.^ Before 1985 there were many idiosyncratic formats.^ Driven by Numerical Concerns^ Nice standards for rounding, overflow, underflow^ Hard to make go fast: numerical analysts predominated overhardware types in defining the standard^ Now all (add, subtract, multiply) operations are fast exceptdivide.

CS429 Slideset 4: 4

Floating Point

Fractional Binary Numbers: Examples^ The binary number

bbbbi^ i −^12

...^ b.b 1 0 − bb... 1 −^2 −^3

b represents− j^

a particular (positive) sum. Each digit is multiplied by a power oftwo according to the following chart:^ Bit:^

bb.. .i^ i −^1

bb^2

b ·^ b^0 −

bb 1 −^2

...^ b − 3 − j

i Weight: 2 i −^1 ...

^4

1 ·^1 /

2 1 /^4

− / 8_..._ 2 j

Representation:^ Bits to the right of the

binary point

represent fractional

powers of 2. This represents the rational number:

i ∑ k^ b ×^2 k^ k =− j

The sign is treated separately.

CS429 Slideset 4: 5

Floating Point

Fractional Binary Numbers: Example

CS429 Slideset 4: 6

Floating Point

Fractional Binary Numbers: Examples

Value^

Representation 5 + 3 /^4

101.^112

2 + 7 /^8

10.^1112

63 /^64

0.^1111112

Observations^ Divide by 2 by shifting right^ Multiply by 2 by shifting left^ Numbers of the form 0

.^11111...

are just below 1.0 2 1 / 2 + 1 / 4 + 1

/ 8 +^...^ + 1

i^ / 2 →^1_._^0 We use the notation 1

.^0 −^ ǫ. CS429 Slideset 4: 7^ Floating Point

Representable Numbers^ Limitation^ You can only represent numbers of the form

i^ y + x / 2.

Other fractions (rationals) have repeating bit representations Irrationals have infinite, non-repeating representations^ Value^

Representation 1 /^3

0. 0101010101[01]

2

1 /^5

0. 001100110011[0011]

2

1 /^10

0. 0001100110011[0011]

2

CS429 Slideset 4: 8

Floating Point

Floating Point Precisions^ Encoding

s^ exp

frac

The most significant bit is the sign bit. The^ exp^ field encodes E. The^ frac^

field encodes M. Sizes^ Single precision: 8 exp bits, 23 frac bits, for 32 bits total^ Double precision: 11 exp bits, 52 frac bits, for 64 bits total^ Extended precision: 15 exp bits, 63 frac bits (onlyIntel-compatible machines)

CS429 Slideset 4: 13

Floating Point

Normalized Numeric Values^ Condition:

exp^6 = 000

...^ 0 and exp

6 = 111^...

^1

Exponent is coded as a biased value E^ =^ Exp^ −

Bias Exp : unsigned value denoted by

exp.

Bias : Bias value^ In general:

e − Bias = 2 1 −^ 1, where

e^ is the number of

exponent bits Single precision: 127 (

Exp : 1^...^ 254,

E^ :^ −^126_..._

Double precision: 1023 (

Exp : 1^...^ 2046,

E^ :^ −^1022

...^ 1023)

Significand coded with implied leading 1 M^ = 1 .xxx

...^ x^2 xxx...^ x^ : bits of

frac Minimum when 000

...^ 0 ( M^ = 1

Maximum when 111

...^ 1 ( M^ = 2

.^0 −^ ǫ )

We get the extra leading bit “for free.”

CS429 Slideset 4: 14

Floating Point

Converting Between Float and Decimal

CS429 Slideset 4: 15

Floating Point

Normalized Encoding Example^ Value:^ float F = 15213.0;^15213

= 1.^11011011011012

13 × 22

Significand M = 1_._^1101101101101

2 frac^ =^11011011011010000000000 Exponent E = 13Bias = 127Exp = 140 =

10001100 CS429 Slideset 4: 16

Floating Point

Normalized Encoding Example^ Floating Point Representation^ Hex: 466DB400Binary: 0100 0110 0110 1101 1011 0100 0000 0000140:^

15213:^

1110 1101 1011 01 CS429 Slideset 4: 17^

Floating Point

Normalized Example^ Given the bit string 0x40500000, what floating point number doesit represent?

CS429 Slideset 4: 18

Floating Point

Normalized Example^ Given the bit string 0x40500000, what floating point number doesit represent?^ Writing this as a bit string gives us:

We see that this is a positive, normalized number.

exp = 128

−^ 127 = 1

So, this number is:

1.^101 ×^22

1 = 11.^012

= 3.^25

CS429 Slideset 4: 19

Floating Point

Denormalized Values^ Condition:

exp = 000... Value^ Exponent values: E = -Bias + 1

Why this value? Floats:^ −126; Doubles:

−^1022 Significand value: M = 0

.xxx^...^ x^2 , where^ xxx

...^ x^ are the

bits of frac. Cases exp = 000

...^ 0 and frac = 000

...^^0

represents values of 0 notice that we have distinct +0 and -0 exp = 000^...^ 0 and frac

6 = 000^...

^0

These are numbers very close to 0.0 Lose precision as they get smaller Experience “gradual underflow” CS429 Slideset 4: 20

Floating Point

Tiny Floating Point Example^ 8-bit Floating Point Representation^ The sign bit is in the most significant bit.^ The next four bits are the exponent with a bias of 7.^ The last three bits are the frac.^ This has the general form of the IEEE Format^ Has both normalized and denormalized values.^ Has representations of 0, NaN, infinity.

s^ exp

frac CS429 Slideset 4: 25

Floating Point

Values Related to the Exponent

Exp^ exp

E^2

E^ comment 0 0000

-6^ 1/

(denorms) 1 0001

-6^ 1/

-5^ 1/

-4^ 1/

-3^ 1/

-2^ 1/

-1^ 1/

+1^2

+2^4

+3^8

+4^16

+5^32

+6^64

+7^128

n/a^

(inf, NaN) CS429 Slideset 4: 26

Floating Point

Dynamic Range

s^ exp^ frac

E^ Value 0 0000 000 -^

0 0 0000 001 -^

1 /^8 ×^1 / 64 = 1

/^512 closest to zero

Denormalized

0 0000 010 -

2 /^8 ×^1 /

64 = 2 /^512

numbers^

... 0 0000 110 -^

6 /^8 ×^1 / 64 = 6

/^512

0 0000 111 -^

7 /^8 ×^1 / 64 = 7

/^512 largest denorm

0 0001 000 -^

8 /^8 ×^1 / 64 = 8

/^512 smallest norm

0 0001 001 -^

9 /^8 ×^1 / 64 = 9

/^512

... 0 0110 110 -^

14 /^8 ×^1 / 2 = 14

/^16

Normalized^

0 0110 111 -^

15 /^8 ×^1 / 2 = 15

/^16 closest to 1 below

numbers^

0 0111 000 0

8 /^8 ×^ 1 = 1 0 0111 001 0

9 /^8 ×^ 1 = 9 /

8 closest to 1 above

0 0111 010 0

10 /^8 ×^ 1 = 10

/^8

... 0 1110 110 7

14 /^8 ×^ 128 = 224 0 1110 111 7

15 /^8 ×^ 128 = 240

largest norm

0 1111 000 n/a^

∞ CS429 Slideset 4: 27

Floating Point

Simple Float System^ Notice that the floating point numbers are not distributed evenlyon the number line.^ Suppose M is the largest possible exponent, m is the smallest,

1 is 8

the smallest positive number representable, and

7 the largest 4

positive number representable.

What is the format? CS429 Slideset 4: 28

Floating Point

Interesting FP Numbers^ Description

exp^ frac

Numeric value

Zero^

00_..._^^00

...^^00 0_._^0

Smallest Pos. Denorm

00_..._^^00

00_..._^^01

{−^23 , −^52 }^ ×^2

{−^126 , −^1022 }

Single^ ≈^1_._^4

−^45 × 10 Double^ ≈^4_._^9

−^324 × 10 Largest Denorm.

00_..._

^00 11_..._^^11

(1_._^0 −^ ǫ )

{−^126 , −^1022 × 2 }

Single^ ≈^1_._^18

−^38 × 10 Double^ ≈^2_._^2

−^308 × 10 Smallest Pos. Norm.

00_..._^^01

00_..._^^00

{−^126 , 1_._ 0 × 2 −^1022 }

Just larger than the largest denomalized.One

01_..._^^11

...^^00 1_._^0

Largest Norm.

11

...^^10 ...^^11 (2_._^0

{^127 , − ǫ ) × 2 1023 }

Single^ ≈^3_._^4

(^38) × 10 Double^ ≈^1_._^8

(^308) × 10 CS429 Slideset 4: 29

Floating Point

Special Properties of Encoding^ FP Zero is the Same as Integer Zero

: All bits are 0.

Can (Almost) Use Unsigned Integer Comparison^ Must first compare sign bits.^ Must consider

NaNs are problematic:^ Will be greater than any other values.^ What should the comparison yield? Otherwise, it’s OK.^ Denorm vs. normalized works.^ Normalized vs. infinity works.

CS429 Slideset 4: 30

Floating Point

Floating Point Operations^ Conceptual View^ First compute the exact result.^ Make it fit into the desired precision.

Possibly overflows if exponent is too large. Possibly round to fit into frac. Rounding Modes (illustrated with $ rounding)

$1.40^ $1.

$1.^

$2.50^ -$1.

Toward Zero

$1^ $

$^

$2^ -$

Round down (

−∞)^

$1^ $

$^

$2^ -$

Round up (+

∞)^

$2^ $

$^

$3^ -$

Nearest even (default)

$^

$2^ $

$^

1 Round down: rounded result is close to but no greater thantrue result. 2 Round up: rounded result is close to but no less than trueresult.

CS429 Slideset 4: 31

Floating Point

Closer Look at Round to Even^ Default Rounding Mode^ Hard to get any other kind without dropping into assembly.^ All others are statistically biased; the sum of a large set ofvalues will consistently be under- or over-estimated.^ Applying to Other Decimal Places / Bit Positions^ When exactly halfway between two possible values, round so thatthe least significant digit is even.E.g., round to the nearest hundredth:

1.23^ Less than half way

1.24^ Greater than half way

1.24^ Half way, round up

1.24^ Half way, round down CS429 Slideset 4: 32^

Floating Point

Multiplication Warning^ Binary Example^ (−^1

2. 01 × 2 )(

4. 1 × 2 )

=^ −(1.^01

2 × 1. 1)(

4 × 2 )

=^ −^1.^111

6 × 2

=^ −^10.^0 ×

round to even

=^ −^1_._^0 ×

adjust exponent

Be careful if you try to do this in the floating point format, ratherthan in scientific notation. Since the exponents are biased in FPformat, adding them would give you:

(2 + bias) + (4 + bias) = 6 + 2*bias To adjust you have to subtract the bias.

CS429 Slideset 4: 37

Floating Point

FP Addition^ Operands:

S^1 (−1)×

E^1 M × 2 , 1

S^2 (−1)×^

E^2 M × 22

Assume^ E

>^ E 1 2

Exact Result:

S^ (−1)×^

EM × 2

Sign S, Significand M; result of signed align and add. Exponent E:

E^1

Fixing^ If^ M^ ≥^ 2, shift M right, increment E If M <^ 1, shift M left k positions, decrement E by k if E is out of range, overflow Round M to fit frac precision If you try to do this in the FP form, recall that both exponents arebiased.

CS429 Slideset 4: 38

Floating Point

Addition Examples^ Decimal Example^ (−^3

2. 4 × 10 ) + (

4. 2 × 10 )

= (−^3.^4 ×

2. 0 × 10 )^

align exponents

= (−^3_._ 4 + 520

2. 0) × 10

= 516.^6 ×

= 5.^166 ×

fix exponent

= 5_._^17 ×^10

4

round

Binary Example^ (−^1_._^01

2 × 2 ) + (

4. 1 × 2 )

= (−^1.^01

2 × 2 ) + (

2. 0 × 2 )^

align exponents

= (−^1_._ 01 + 110

2. 0) × 2

= 100.^11 ×

= 1.^0011 ×

fix exponent

= 1_._^01 ×^2

4

round CS429 Slideset 4: 39

Floating Point

Mathematical Properties of FP Add^ Compare to those of Abelian Group^ Closed under addition? Yes, but may generate infinity or NaN.^ Commutative? Yes.^ Associative? No, because of overflow and inexactness ofrounding.^ O is additive identity? Yes.^ Every element has additive inverse? Almost, except forinfinities and NaNs.^ Monotonicity^ a^

≥^ b^ =⇒^ a

+^ c^ ≥^ b^ +

c? Almost, except for infinities and NaNs.

CS429 Slideset 4: 40

Floating Point

Mathematical Properties of FP Mult^ Compare to those of Commutative Ring^ Closed under multiplication? Yes, but may generate infinity orNaN.^ Multiplication Commutative? Yes.^ Multiplication is Associative? No, because of possible overflowand inexactness of rounding.^ 1 is multiplicative identity? Yes.^ Multiplication distributes over addition? No, because ofpossible overflow and inexactness of rounding.^ Monotonicity^ a^

≥^ b^ &^ c^ ≥

0 =⇒^ a^ ×

c^ ≥^ b^ ×^ c

? Almost, except for

infinities and NaNs.

CS429 Slideset 4: 41

Floating Point

Floating Point in C^ C guarantees two levels^ float: single precision^ double: double precision^ Conversions^ Casting among int, float, and double changes numeric values^ Double or float to int:

truncates fractional part like rounding toward zero not defined when out of range: generally saturates to TMin orTMax int to double: exact conversion as long as int has

≤^ 53-bit

word size int to float: will round according to rounding mode.

CS429 Slideset 4: 42

Floating Point

Answers to FP Puzzles^ i n t^ x =

... ; f l o a t f =^... ; d o u b l e d =^... ; Assume neither d nor f is NaN. x == (int)(float) x

No: 24 bit significand

x == (int)(double) x

Yes: 53 bit significand

f == (float)(double) f

Yes: increases precision

d == (float) d

No: loses precision

f == -(-f)^

Yes: just change sign bit

2/3 == 2/3.

No: 2/3 == 0

d < 0.^

→^ ((d*2) < 0.0)

Yes

d > f^

→^ -f > -d^

Yes

d*d >= 0.^

Yes

(d+f)-d == f

No: not associative CS429 Slideset 4: 43

Floating Point

Ariane 5^ On June 4, 1996 an unmanned Ariane 5rocket launched by the European SpaceAgency exploded just forty seconds afterits lift-off from Kourou, French Guiana.The rocket was on its first voyage, aftera decade of development costing $7billion.^ The destroyed rocket and its cargo were valued at $500 million.

CS429 Slideset 4: 44

Floating Point