Computer arithmetic
Computer arithmetic
4
Arithmetic for Computers
Arithmetic
Where we've been:
performance
abstractions
instruction set architecture
assembly language and machine language
What's up ahead:
implementing the architecture
operation
32 ALU
result
32
b
32
Numbers
Bits are just bits (no inherent meaning)
conventions define relationship between bits and numbers
Binary integers (base 2)
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001...
ambiguous zero
001 = +1 001 = +1 001 = +1
ambiguous zero
010 = +2 010 = +2 010 = +2
011 = +3 011 = +3 011 = +3
100 = 0 100 = -3 100 = -4
101 = -1 101 = -2 101 = -3
110 = -2 110 = -1 110 = -2
111 = -3 111 = 0 111 = -1
Issues:
balance – equal number of negatives and positives
ambiguous zero – whether more than one zero representation
ease of arithmetic operations
Which representation is best? Can we get both balance and non-ambiguous
zero?
Representation Formulae
Two’s complement:
= X’, if xn = 0
-2n + X’, if xn = 1
One’s complement:
xnX’ = X’, if xn = 0
-2n + 1 + X’, if xn = 1
Two's Complement Operations
Negation Shortcut: To negate any two's complement integer
(except for minint) invert all bits and add 1
note that negate and invert are different operations!
why does this work? Remember we don’t know how to add in 2’s
complement yet! Later…!
Sign Extension Shortcut: To convert an n-bit integer into an
integer with more than n bits – i.e., to make a narrow integer fill
a wider word – replicate the most significant bit (msb) of the
original number to fill the new bits to its left
Example: 4-bit 8-bit
0010 = 0000 0010
1010 = 1111 1010
why is this correct? Prove!
Two’s Complement Addition
Perform add just as in junior school (carry/borrow 1s)
Examples (4-bits):
0101 0110 1011 1001 1111
0001 0101 0111 1010 1110
Do these sums now!! Remember all registers are 4-bit including result register!
So you have to throw away the carry-out from the msb!!
Have to beware of overflow : if the fixed number of bits (4, 8, 16,
32, etc.) in a register cannot represent the result of the operation
terminology alert: overflow does not mean there was a carry-out from
the msb that we lost (though it sounds like that!) – it means simply
that the result in the fixed-sized register is incorrect
as can be seen from the above examples there are cases when the result
is correct even after losing the carry-out from the msb
Two’s Complement Addition:
Verifying Carry/Borrow method
Two (n+1)-bit integers: X = xnX’, Y = ynY’
Carry/borrow 0 X’ + Y’ 2n 2n X’ + Y’ 2n+1 –
add X + Y (no CarryIn to last bit) 1
(CarryIn to last bit)
xn = 0, yn = 0 ok not ok(overflow!)
xn = 1, yn = 0 ok ok
xn = 0, yn = 1 ok ok
xn = 1, yn = 1 not ok(overflow!) ok
A + B 0 0 0
A + B 0 0 0
A – B 0 0 0
A – B 0 0 0
a
0
1
Result
b 0 2
CarryOut
Multiply
Grade school shift-add method:
Multiplicand 1000
Multiplier x 1001
1000
0000
0000
1000
Product 01001000
m bits x n bits = m+n bit product
Binary makes it easy:
multiplier bit 1 => copy multiplicand (1 x multiplicand)
multiplier bit 0 => place 0 (0 x multiplicand)
3 versions of multiply hardware & algorithm:
Shift-add Multiplier Version 1
Start
Multiplier
64-bit ALU Shift right
2. Shift the Multiplicand register left 1 bit
32 bits
Product
Control test
Write 3. Shift the Multiplier register right 1 bit
64 bits
Done Algorithm
Shift-add Multiplier Version1
Start
Yes: 32 repetitions
Done
Algorithm
Observations on Multiply
Version 1
1 step per clock cycle nearly 100 clock cycles to multiply two
32-bit numbers
Half the bits in the multiplicand register always 0
64-bit adder is wasted
0’s inserted to right as multiplicand is shifted left
least significant bits of product never
change once formed
Multiplier
32-bit ALU Shift right
32 bits 2. Shift the Product register right 1 bit
Shift right
Product Control test
Write
3. Shift the Multiplier register right 1 bit
64 bits
Multiplier0 = 1 1. Test
Multiplier0
Multiplier0 = 0
Example: 0010 * 0011:
Yes: 32 repetitions
Done
Algorithm
Observations on Multiply
Version 2
Each step the product register wastes space that exactly matches
the current size of the multiplier
Multiplicand
32-bit ALU
Yes: 32 repetitions
Done
Algorithm
Observations on Multiply
Version 3
2 steps per bit because multiplier & product combined
What about signed multiplication?
easiest solution is to make both positive and remember whether to
negate product when done, i.e., leave out the sign bit, run for 31 steps,
then negate if multiplier and multiplicand have opposite signs
Junior school method: see how big a multiple of the divisor can be
subtracted, creating quotient digit at each step
Binary makes it easy first, try 1 * divisor; if too big, 0 * divisor
Dividend = (Quotient * Divisor) + Remainder
3 versions of divide hardware & algorithm:
Start
2a. Shift the Quotient register to the left, 2b. Restore the original value by adding
Quotient setting the new rightmost bit to 1 the Divisor register to the Remainder
64-bit ALU Shift left register and place the sum in the
32 bits Remainder register. Also shift the
Quotient register to the left, setting the
new least significant bit to 0
Remainder Control
Write test
64 bits
Yes: 33 repetitions
Done Algorithm
Observations on Divide Version 1
Half the bits in divisor always 0
1/2 of 64-bit adder is wasted
Divisor Remainder register is initialized 2. Subtract the Divisor register from the
left half of the Remainder register and
with the dividend at right place the result in the left half of the
32 bits
Remainder register
Quotient
32-bit ALU Shift left
Remainder >
– 0 Remainder < 0
32 bits Test Remainder
Yes: 32 repetitions
Each step the remainder register wastes space that exactly matches
the current size of the quotient
Divisor
Remainder >
– 0 Remainder < 0
32 bits Test Remainder
32-bit ALU
3a. Shift the Remainder register to the 3b. Restore the original value by adding
left, setting the new rightmost bit to 1 the Divisor register to the left half of the
Remainder register and place the sum
Shift right in the left half of the Remainder register.
Remainder Control Also shift the Remainder register to the
Shift left
test left, setting the new rightmost bit to 0
Write
64 bits
Yes: 32 repetitions
Signed divide:
make both divisor and dividend positive and perform division
negate the quotient if divisor and dividend were of opposite signs
make the sign of the remainder match that of the dividend
this ensures always
dividend = (quotient * divisor) + remainder
–quotient (x/y) = quotient (–x/y) (e.g. 7 = 3*2 + 1 & –7 = –3*2 – 1)
MIPS Notes
div (signed), divu (unsigned), with two 32-bit register
operands, divide the contents of the operands and put
remainder in Hi register and quotient in Lo; overflow is ignored
in both cases
31 bits 30 to 20 bits 19 to 0
bits 31 to 0
Addition
Shift the smaller number to the right until its
exponent would match the larger exponent
Overflow or Yes
underflow?
No
Exception
No
Still normalized?
Yes
Done
Floating Point
Addition Sign Exponent Significand Sign Exponent Significand
Compare
Small ALU exponents
Hardware:
Exponent
difference
0 1 0 1 0 1
Shift smaller
Control Shift right
number right
Add
Big ALU
0 1 0 1
Increment or
decrement Shift left or right Normalize
Multpication
to get the new biased exponent
Overflow or Yes
underflow?
No
Exception
No
Still normalized?
Yes
Done
Floating Point Complexities
In addition to overflow we can have underflow (number too
small)
Accuracy is the problem with both overflow and underflow
because we have only a finite number of bits to represent
numbers that may actually require arbitrarily many bits
limited precision rounding rounding error
IEEE 754 keeps two extra bits, guard and round
four rounding modes
positive divided by zero yields infinity
zero divide by zero yields not a number
other complexities
Implementing the standard can be tricky
Not implementing the standard can be even worse
see text for discussion of Pentium bug!
MIPS Floating Point
MIPS has a floating point coprocessor (numbered 1, SPIM) with
thirty-two 32-bit registers $f0 - $f31. Two of these are required
to hold doubles. Floating point instructions must use only even-
numbered registers (including those operating on single floats).
SPIM simulates MIPS floating point.
Other instructions…
Summary
Computer arithmetic is constrained by limited precision
Bit patterns have no inherent meaning but standards do exist:
two’s complement
IEEE 754 floating point
Computer instructions determine meaning of the bit patterns.
Performance and accuracy are important so there are many
complexities in real machines (i.e., algorithms and
implementation)