CSI 312 Lecture 5, Fall 2011
American University of Science and Technology · Computer Architecture · 23 pages · Dr. Abbas Tarhini
Showing the first 10 of 23 pages. Download the PDF to see all of it.
More CSI 312 files
Show the text of this lab
Computer Architecture
Lecture 9 - Floating Point
2011
Reading: 3.6-3.9
Homework: 3.30, 3.35, 3.37, 3.38, 3.40, 3.44
Why did the Ariane 5 Explode?
(image source: java.sun.com)
2
Outline - Floating Point
Motivation and Key Ideas
IEEE 754 Floating Point Format
Floating Point Arithmetic
MIPS Floating Point Instructions
Summary
3
Floating Point - Motivation
Review: n-bit integer representations
Unsigned:
0 to 2n-1
Signed Two’s Complement:
- 2n-1 to 2n-1-1
Biased (excess-b):
-b to 2n-b
Problem: how do we represent:
Very large numbers
9,345,524,282,135,672,
2354
Very small numbers
0.00000000000000005216,
2-100
Rational numbers
2/3
Irrational numbers
sqrt(2)
Transcendental numbers
e, π
4
Fixed Point Representation
Idea: fixed-point numbers with fractions
Decimal point (binary point) marks start of fraction
Decimal: 1.2503 = 1 X 100 + 2 X 10-1 + 5 X 10-2 + 3 X 10-4
Binary: 1.0100001 = 1 X 20 + 1 X 2-2 + 1 X 2-7
Problems
Limited locations for “decimal point” (binary point”)
Won’t work for very small or very larger numbers
5
Another Approach: Scientific Notation
Represent a number as a combination of
Mantissa (significand): Normalized number
AND
Exponent (base 10)
Example:
6.02 X 1023
Significand
(mantissa)
Radix
(base)
Exponent
6
Floating Point
Key idea: adapt scientific notation to binary
Fixed-width binary number for significand
Fixed-width binary number for exponent (base 2)
Idea: represent a number as
1.xxxxxxxtwo X 2yyyy
Significand
(mantissa)
Radix
(2)
Exponent
Leading ‘1’
(Implicit)
Important Points:
This is a tradeoff between precision and range
Arithmetic is approximate - error is inevitable!
7
Outline - Floating Point
Motivation and Key Ideas
IEEE 754 Floating Point Format
Floating Point Arithmetic
MIPS Floating Point Instructions
Summary
8
IEEE 754 Floating Point
Single precision (C/C++/Java float type)
Value N = (-1)S X 1.F X 2E-127
Double precision (C/C++/Java double type)
Value N = (-1)S X 1.F X 2E-1023
S
E Exponent
F Significand
1 bit
8 bits
23 bits
S
E Exponent
F Significand
F Significand (continued - 52 bits total)
1 bit
11 bits
20 bits
32 bits
Bias
Bias
The exponent represents both positive and negative values. To do this, a bias is added to the actual
exponent in order to get the stored exponent. For IEEE SP , this value is 127 to represent 0, thus, a
stored value of 200 indicated an exponent of (200 – 127) = 73. an exponent of 3 is stored as 130. an
exponent of -2 is stored as (-2 +127) = 125. For DP bias = 1024.
9
Floating Point Examples
8.75ten = 1 X 23 + 1 X 2-1 + 1 X 2-2 = 1.00011 X 23
Single Precision:
• Significand: 1.00011000…. (note leading 1 is implied)
• Exponent: 3 + 127 = 130 = 10000010two
Double Precision:
• Significand: 1.00011000…
• Exponent: 3 + 1023 = 1026 = 10000000010two
0
E Exponent
F Significand
S
1 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
S
E Exponent
F Significand
F Significand (continued - 52 bits total)
0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10
Floating Point Examples
-0.375ten = 1 X 2-2 + 1 X 2-3 = 1. 1 X 2-2
Single Precision:
• Significand: 1.1000….
• Exponent: -2 + 127 = 125 = 01111101two
Double Precision:
• Significand: 1.1000…
• Exponent: -2 + 1023 = 1021 = 01111111101two
1
E Exponent
F Significand
S
0 1 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
S
E Exponent
F Significand
F Significand (continued - 52 bits total)
1 0 1 1 1 1 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
11
Floating Point Examples
Q: What is the value of the following single-
precision word?
Significand = 1 + 2-1 + 2-4 + 2-8 + 2-10 + 2-12
Exponent = 8 - 127 = -119
Final Result = (1 + 2-1 + 2-4 + 2-8 + 2-10 + 2-12) X 2-119
= 2.36 X 10-36
0
E Exponent
F Significand
S
0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0
12
Outline - Floating Point
Motivation and Key Ideas
IEEE 754 Floating Point Format
Floating Point Arithmetic
MIPS Floating Point Instructions
Rounding & Errors
Summary
Floating Point Addition
13
Add: 9.999 x 101 to 1.610 x 10-1
Step 1: Normalize the smaller number to fit with the
larger:
0.0161 x 101
If we can represent only 4 digit number so it becomes:
0.016 x 101
Step 2:
9.999
0.016
10.015 (not in normalized scientific notation
Step 3: 10.015 x 101 = 1.0015 x 102
14
Floating Point Addition (Fig. 3.16)
1. Align binary point to number with larger exponent
2. Add significands
3. Normalize result and adjust exponent
4. If overflow/underflow throw exception
5. Round result (go to 3 if normalization needed again)
A
1.11 X 20
1.11 X 20
1.75
+ B
+ 1.00 X 2-2
+ 0.01 X 20
0.25
10.00 X 20
(Normalize)
1.00 X 21
2.00
15
Floating Point Multiplication (Fig. 3.18)
1. Add 2 exponents together to get new exponent
(subtract 127 to get proper biased value)
2. Multiply significands
3. Normalize result if necessary (shift right) & adjust
exponent
4. If overflow/underflow throw exception
5. Round result (go to 3 if normalization needed again)
6. Set sign of result using sign of X, Y
16
Outline - Floating Point
Motivation and Key Ideas
IEEE 754 Floating Point Format
Floating Point Arithmetic
MIPS Floating Point Instructions
Summary
17
MIPS Floating Point Instructions
Organized as a coprocessor
Separate registers $f0-$f31
Separate operations
Separate data transfer (to same memory)
Basic operations
add.s - single
add.d - double
sub.s - single
sub.d - double
mul.s - single
mul.d - double
div.s - single
div.d - double
18
MIPS Floating Point Instructions (cont’d)
Data transfer
lwc1, swcl (l.s, s.s) - load/store float
to fp reg
l.d, s.d - load/store double to fp reg pair
Testing / branching
c.lt.s, c.lt.d, c.eq.s, c.eq.d, …
compare and set condition bit if true
bclt - branch if condition true
bclf - branch if condition false
19
Outline - Floating Point
Motivation and Key Ideas
IEEE 754 Floating Point Format
Floating Point Arithmetic
MIPS Floating Point Instructions
Summary
20
Addendum -
Why Did the Ariane 5 Explode?
In 1996 Ariane 5 Flight 501 exploded after launch.
Estimated cost of accident: $500 million
21
Addendum -
Why Did the Ariane 5 Explode?
The cause was traced to the Inertial reference
system (SRI).
Both the main and backup SRI failed.
Both units failed due to an out-of-range conversion
Input: double precision floating point
Output: 16-bit integer for “horizontal bias” (BH)
Careful analysis during software design had
indicated that BH would “fit” in 16 bits
So, why didn’t it fit?
22
Addendum -
Why did the Ariane 5 Explode?
Careful analysis during software design had
indicated that BH would “fit” in 16 bits
BUT, all analysis had been done for the Ariane 4,
the predecessor of Ariane 5 - software was reused
Since Ariane 5 was a larger rocket, the values for
BH were higher than anticipated
AND, there was no handler to deal with the
exception!
For more information:
http://www.ima.umn.edu/~arnold/disasters/ariane.html
Or, Google “Ariane 5 Flight 501”
23
Summary - Chapter 3
Important Topics
Signed & Unsigned Numbers (3.2)
Addition and Subtraction (3.3)
Constructing an ALU (B.5)
Multiplication and Division (3.4, 4.5)
Floating Point (3.6)
Coming Up:
Performance (Chapter 4)