Chapter IV Computer Arithmetic
Chapter IV Computer Arithmetic
Jehan-Franois Pris
[email protected]
Chapter Organization
A warning
Binary addition, subtraction, multiplication and
division are very easy
General concept
Decimal addition
(carry) 1_
19
+ 7
26
Binary addition
( carry) 111_
10011
+ 111
11010
16+8+2 = 26
Realization
Simplest solution is a battery of full adders
o s3
x3 y3
s2
x2 y2
s1
x1 y1
s0
x0 y0
Observations
Adder add four-bit values
Output o indicates if there is an overflow
A result that cannot be represented using 4
bits
Happens when x + y > 15
Operation is slowed down by carry propagation
Faster solutions (not discussed here)
Signed addition in
4-bit arithmetic
( carry) 11_
1011
+ 0011
1110
-5 + 3 = -2
Signed addition in
4-bit arithmetic
( carry) 1__
0110
+ 0011
1001
6 + 3 -7
(false)
Example
Four-bit arithmetic:
Sixteen possible values
Positive overflow happens when result > 7
Negative overflow happens when result < -8
Eight-bit arithmetic:
256 possible values
Positive overflow happens when result > 127
Negative overflow happens when result < -128
Why?
To keep the CPU as simple and regular as
possible
An interesting consequence
Most C compilers ignore overflows
C compilers must use unsigned arithmetic for
their integer operations
Fortran compilers expect overflow conditions to
be detected
Fortran compilers must use signed arithmetic
for their integer operations
Subtraction
Can be implementing by
Specific hardware
Negating the subtrahend
Negating a number
Toggle all bits then add one
0
1
2
3
4
5
6
7
1111
1110
1101
1100
1011
1010
1001
1000
+1 = 0000 0
+1 = 1111 -1
+1 = 1110 -2
+1 = 1101 -3
+1 = 1100 -4
+1 = 1011 -5
+1 = 1010 -6
+1 = 1001 -7
-8
-7
-6
-5
-4
-3
-2
-1
0111
0110
0101
0100
0011
0010
0001
0000
+1 =1000
+1 = 0111
+1 = 0110
+1 = 0101
+1 = 0100
+1 = 0011
+1 = 0010
+1 = 0001
?
7
6
5
4
3
2
1
MULTIPLICATION
Decimal multiplication
(carry) 1_
37
x 12
74
370
444
Binary multiplication
(carry)
111 _
1101
x 101
1101
00
110100
1000001
Algorithm
Clear contents of 64-bit product register
For (i = 0; i <32; i++) {
If (LSB of multiplier_register ==1)
Add contents of multiplicand register to product
register
Shift right one position multiplier register
Shift left one position multiplicand register
} / / for loop
64-bit
ALU
Product (64 bits)
Shift Right
Multiplier
Control
Shift Right
Multiplier
To get next bit
( LSB to MSB)
Control
Explanations
Multiplicand register must be 64-bit wide
because 32-bit multiplicand will be shifted 32
times to the left
Requires a 64-bit ALU
Product register must be 64-bit wide to
accommodate the result
Contents of multiplier register is shifted 32 times
to the right so that each bit successively
becomes its least significant bit (LSB)
Example (I)
Multiply 0011 by 0011
Start
Multiplicand
Multiplier
0011
0011
Product
0000
First addition
Multiplicand
Multiplier
0011
0011
Product
0011
Example (II)
Shift right and left
Multiplicand
Multiplier
0110
0001
Second addition
Multiplicand
Multiplier
0110
0001
0110 + 011 = 1001
Product
0011
Product
1001
Example (III)
Shift right and left
Multiplicand
Multiplier
1100
0000
Product
1001
First Optimization
Must have a 64-bit ALU
More complex than a 32-bit ALU
Solution is not to shift the multiplicand
After each cycle, the LSB being added
remains unchanged
Will save that bit elsewhere and shift the
product register one position to the left after
each iteration
Binary multiplication
1101
x 101
1101
00
110100
1000101
Algorithm
Clear contents of 64-bit product register
For (i = 0; i <32; i++) {
If (LSB of multiplier_register ==1)
Add contents of multiplicand register to
product register
Save LSB of product register
Shift right one position both multiplier
register and product register
} / / for loop
32-bit
ALU
Product (64 bits)
Multiplier
Control
+ Test
Shift Right and Save
Product
--
Result
--
First digit
Multiplicand
27
Product
54
Result
--
Multiplier
12
Result
4
Second digit
Multiplicand
Multiplier
27
1
Result
4
Product
32
Result
24
Example (I)
Multiply 0011 by 0011
Start
Multiplicand
Multiplier
0011
0011
Product
--
Result
--
First bit
Multiplicand
0011
Product
0011
Result
--
Multiplier
0011
Example (II)
Shift right multiplier and product
Multiplicand
Multiplier Product
0011
0001
0001
Result
1-
Second bit
Multiplicand
Multiplier Product
Result
0011
0001
0100
1Product register contains 0011 + 001 = 0100
Example (III)
Shift right multiplier and product
Multiplicand
Multiplier Product
0011
0000
010
Result
01-
Second Optimization
Both multiplier and product must be shifted to
one position to the right after each iteration
Both are now 32-bit quantities
Can store both quantities in the product register
32-bit
ALU
Multiplier + Product
Control
+ Test
Third Optimization
Multiplication requires 32 additions and 32 shift
operations
Can have two or more partial multiplications
One using bits 0-15 of multiplier
A second using bits 16-31
then add together the partial results
DIVISION
Division
Implemented by successive subtractions
Result must verify the equality
Dividend = Multiplier Quotient + Remainder
303
2126
-210
26
-21
5
Binary division
11
011
1011
-11
X
1011
>>-11
Observations
Binary division is actually simpler
We start with a left-shifted version of divisor
We try to subtract it from dividend
No need to find out which multiple to subtract
We mark 1 for success, 0 for failure
We shift divisor one position left after every
attempt
How we proceed (I) After each step we shift the quotient to the right
one position at a time
Divisor
Divisor
Div isor
Divisor
Division Algorithm
For i in range(0,33) :
# from 0 to 32
Subtract contents of divisor register from
remainder register
If remainder 0 :
Shift quotient register to the left
Set new rightmost bit to 1
Else :
Undo subtraction
Shift quotient register to the left
Set new rightmost bit to 0
Shift right one position contents of divisor register
A simple divider
Shift Right
Divisor (64 bits)
64-bit
ALU
Remainder (64 bits)
Shift Left
Quotient
Control
+ Test
Signed division
Easiest solution is to remember the sign of the
operands and adjust the sign of the quotient and
remainder accordingly
A little problem:
5 2 = 2 and the remainder is 1
-5 2 = -2 and the remainder is -1
The sign of the remainder must match the sign
of the quotient
TRANSITION SLIDE
Here end the materials that were on the first fall
2012 midterm
Here start the materials that will be on the fall
2012 midterm
To be moved to
the right place
Representation
Sign + exponent + coefficient
SExp
Coefficient
The coefficient
Also known as fraction or significand
Most significant bit is always one
Implicit and not represented
0011000000000000000000000000
Biased exponent is 127ten
True coefficient is implicit one followed by all
zeroes
First example
0 011 00000000000000000000000000000
Sign bit is zero:
Number is positive
Biased exponent is 127
Power of two is zero
Normalized binary value is
1.0000000
Number is 120 = 1
Second example
0 100 10000000000000000000000000000
Sign bit is zero:
Number is positive
Biased exponent is 128
Power of two is 1
Normalized binary value is
1.1000000
Number is 1.121 = 11 = 3ten
Third example
1 011011000000000000000000000000000
Sign bit is 1:
Number is negative
Biased exponent is 126
Power of two is 1
Normalized binary value is
1.1100000
Number is 1.1121 = 0.111 = 7/8ten
Can we do it now?
0
129ten
10100000000000000000000000000
Sign bit is 0:
Number is ___________
Biased exponent is 129
Power of two is _______
Normalized binary value is
1.__________
Number is _________________________
First example
Represent 7:
Convert to binary: 111
Normalize: 1.1122
Sign bit is 0
Biased exponent is 127 + 2 = 10000001two
Coefficient is
11000
0 1001 11000000000000000000000000000
Second example
Represent 1/2
Convert to binary: 0.1
Normalize: 1.02-1
Sign bit is 0
Biased exponent is 127 1 = 01111110two
Coefficient is 000
0 011000000000000000000000000000000
Third example
Represent 2
Convert to binary: 10
Normalize: 1.021
Sign bit is 1
Biased exponent is 127 + 1 = 10000000two
Coefficient is 000
1 100000000000000000000000000000000
Fourth example
Represent 9/4
Convert to binary: 100122
Normalize: 1.00121
Sign bit is 0
Biased exponent is 127 + 1 = 10000000two
Coefficient is 00100
1 100000100000000000000000000000000
Can we do it now?
Represent 6.25:
Convert to binary: ________
Normalize: 1.______2_______
Sign bit is _____
Biased exponent is 127 + ___ = ______ten
Coefficient is_________
Range
Can represent numbers between
1.0002126 and 1.1112127
Say between 2126 and 2128
Observing that 210 103
we divide the exponents by 10 and multiply them
by 3 to obtain the interval expressed in powers of
10
Approximate range is 1038 to 1038
Accuracy
We have 24 significant bits
Theoretical precision of 1/224, that is, roughly
1/107
Cannot add correctly billions or trillions
Actual situation is worse if we do too many
computations
1,000,000 999,999.4875 = ???
Guard bits
Do all arithmetic operations with two additional
bits to reduce rounding errors
FP division
Very tricky
One good solution is to multiply the dividend by
the inverse of the divisor
A trap
Addition does not necessarily commute:
91037 + 91037 + 410-37
Observe that
(91037 + 91037) + 410-37 = 410-37
while
91037 + (91037+ 410-37) = 0
due to the limited accuracy of FP numbers
IMPLEMENTATIONS
Overview
FPU offers a very familiar user interface
Eight general purpose FP registers
Distinct from the integer registers
Two-operand instructions in both RR and RX
formats
Includes single-precision and double-precision
versions or addition, subtraction, multiplication
and division
Examples of RR instructions
AFR f1, f2
add contents of floating-point
register f2 into f1
ADR f1,f2
add contents of double-precision
register f2 into f1
LFR f1, f2
load contents of floating-point
register f2 into f1
Also had load positive, load negative, load
complement instructions for floating-point and
double-precision operands
Examples of RX instructions
AF r1, d(r2)
AD r1,d(r2)
MIPS FP INSTRUCTIONS
Overview
Thirty-two specialized single-precision registers:
$f0, $f1, $f31
Each pair of single-precision registers forms a
double-precision register
*.s instructions apply to single precision format
*.d instructions apply to double precision format
Most instructions are in the R format
bclf a
x86 FP INSTRUCTIONS
Overview
Original x86 FP coprocessor had a stack
architecture
Stack registers were 80-bit wide as well as all
internal registers
Better accuracy
Provided single and double precision operations
Example
a=b+c
Load b on top of stack
Load c on top of stack
Add c to b
Store result into a
b
b
----c
b
b
----b+c
-----
REVIEW QUESTIONS
Review questions
How would you represent 0.5 in double precision?
How would you convert this double-precision
value into a single precision format?
When doing accounting, we could do all the
computations in cents using integer arithmetic.
What would we win? What would we lose?
Solutions
How would you represent 0.5 in double precision?
Normalized representation: 1.0 2-1
Sign: 0
Biased exponent: 1023 1 = 1022
Coefficient: All zeroes
Because the 1 is implicit
Solutions
How would you convert this double-precision value
into a single precision format?
Same normalized representation: 1.0 2-1
Same sign: 0
New biased exponent: 127 1 = 126
Same coefficient: All zeroes
Because the 1 is implicit
Solutions
When doing accounting, we could do all the
computations in cents using integer arithmetic. What
would we win? What would we lose?
Big plus:
The results would be exact
Big minus:
Could not handle numbers bigger than
$20,000,000 in 32-bit signed arithmetic
Why $20,000,000?
32-bit unsigned arithmetic can represent
numbers from 0 to 232 1
32-bit unsigned arithmetic can represent
numbers from -231 to 231 1
Roughly from -2000,000,000 to 2,000,000,000
Must divide by 100 as we were using cents!
TRANSITION SLIDE
Here end the materials that were on the first fall
2012 midterm