Floating point arithmetic operations

advertisement
Floating-Point Arithmetic Operations
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
1
Floating-Point Hardware
•
Older computer systems included hardware
support for integer arithmetic operations only.
– For floating-point operations, they relied on
software libraries for simulating the floating-point
operations using the hardware’s integer units.
•
Including floating-point hardware is quite
expensive. However, it is much more efficient
than the software method.
•
This is why almost all modern computers have
hardware that is specifically designed for
performing floating-point arithmetic operations.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
2
Range of Representation
•
Floating-point representation increases the range
of values that can be stored in a given register.
•
Consider a 48-bit register:
– The range of signed integers that can be stored is:
-247  247–1
• 247 because one bit must be reserved for the sign.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
3
Range of Representation
•
Assuming 36 bits for a fractional mantissa and 12
bits for the exponent, calculate the range of
floating-point numbers that can be stored:
– One bit for the mantissa sign  35 bits for the
mantissa.
– Fractional mantissa  1 – 2-35
– One bit for the exponent sign  11 bits for the
exponent.
– Integer exponent  22047
±(1 – 2-35) x 22047
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
4
Alignment
•
Consider the following operation:
123 X 100 + 456 X 10-2
– How can we add these two numbers?
•
Before floating-point numbers can be added or
subtracted, their exponents must be made equal.
This process is called alignment.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
5
Aligning the Mantissas
•
To align floating-point numbers:
– Determine which has the smaller exponent.
– Shift the mantissa of the number with the smaller
exponent (the smaller number) to the right and
increment its exponent by 1.
– Repeat until the two exponents become equal.
•
Shifting the smaller number to the right will
discard lesser significant bits.
– If the shifting results in a 0 mantissa, stop.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
6
Biased Exponent
•
A “biased” exponent is an exponent to which a
value has been added to make it always positive.
– This makes it easier to compare exponents and
determine which is larger.
• Very useful during alignment.
•
Using 8 bits for the exponent we can represent
values from –128 up to 127.
– If we add 128 to these values, we would end up
with exponents in the range of 0 to 255.
– Deciding that 129 is larger than 126 is much
easier than deciding whether 1 is larger than –2.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
7
Addition and Subtraction
•
There are 4 main steps to adding or subtracting
floating-point numbers:
– Check for zeros.
– Align the mantissas.
– Add or subtract the mantissas.
• Since the exponents are now equal, the mantissas can
be added/subtracted following the regular rules of
addition/subtraction.
– Normalize the result.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
8
Examples
•
Represent 0.5 and 0.25 using the following
floating point representation:
 6 bit normalized signed 2’s complement fraction
mantissa, including the sign.
• 1 bit for the sign, 5 bits for the mantissa.
 3 bit biased exponent.
• Range of representation is -4 to 3.
 Add 4 to bias it.
•
Follow the steps of floating point addition to find
the sum of the two numbers.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
9
Examples (Contd.)
 0.5  0.1 X 20
• 0 10000 100 (Already normalized).
 0.25  0.01 X 20
• 0 01000 100
• 0 10000 011 (After normalization).
0 10000 100
+ 0 10000 011
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
10
Examples (Contd.)
1.
Check for Zero.
–
2.
None of the numbers is 0.
Align Mantissas
0 10000 100
+ 0 10000 011
3.
Add
0 10000
+ 0 01000
0 11000
4.
0 10000 100
+ 0 01000 100
The result is
0 11000 100
Normalize
–
Already normalized.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
11
Examples (Contd.)
•
Add 0.5 to –0.25
0.5
0.1 X 20
0 10000 100
-0.25
-0.01 X 20
- 0 01000 100
1.
Check for zeros.
•
2.
None.
Align Mantissas.
•
3.
1 11000 100
Already Aligned.
Add Mantissas.
0 10000
+ 1 11000
1 0 01000
Preliminary
Result
4.
0 01000 100
Normalize the result.
0 10000 011
Final Result
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
12
Examples (Contd.)
•
Add -0.5 to 0.25
-0.5
-0.1 X 20
- 0 10000 100
1 10000 100
0.25
0.01 X 20
0 01000 100
0 10000 011
1.
Check for zeros.
•
2.
3.
None.
Align Mantissas.
•
0 10000 011
0 01000 100
Add Mantissas.
1 10000
+ 0 01000
1 11000
Preliminary
Result
4.
1 11000 100
Normalize the result.
•
Already Normalized.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
13
Overflow in Floating-Point Operations
•
What about overflow?
– If adding the mantissas produces an overflow, we
shift the result’s mantissa to the right 1 position
and increment the exponent by 1.
– If incrementing the exponent produces and
exponent overflow, the operation is terminated
and an overflow error is reported.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
14
Examples (Contd.)
•
Add 0.5 to 0.5
0.5
0.1 X 20
0 10000 100
0.5
0.1 X 20
0 10000 100
1.
Check for zeros.
•
2.
Align Mantissas.
•
3.
None.
Already Aligned.
Add Mantissas.
0 10000
+ 0 10000
1 00000
Overflow
Correction
4.
0 10000 101
Normalize the result.
•
Already Normalized.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
15
Examples (Contd.)
•
Add 4 to 4
4
0.1 X 23
0 10000 111
4
0.1 X 23
0 10000 111
1.
Check for zeros.
•
2.
Align Mantissas.
•
3.
•
None.
Already Aligned.
Add Mantissas.
0 10000
+ 0 10000
1 00000
Overflow
Attempting to correct the overflow will cause the
exponent to exceed its limit.
–
Cannot correct this overflow.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
16
Multiplication of Floating-Point Numbers
•
Multiplication of floating-point numbers is a four
step process:
–
–
–
–
Check for zeros.
Add the exponents.
Multiply the mantissas.
Normalize the product.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
17
Division of Floating-Point Numbers
•
Division is done by:
–
–
–
–
•
Check for zeros.
Align the dividend.
Subtract the exponents.
Divide the mantissas.
All of the operations on floating-point numbers
have been simplified so that the same hardware
can be used for floating-point numbers and
integers.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU
18
Download