L04 Arithmetic I
L04 Arithmetic I
Instructor: K. O. Boateng
TA:
Semester 1, 2022/23
No inherent meaning
– f(b31…b0) => integer value
– f(b31…b0) => control signals
Background
32-bit types include
– Unsigned integers
– Signed integers
– Single-precision floating point
– MIPS instructions (book inside cover)
Unsigned Integers
f(b …b ) = b x 2 31
+ … + b x 2 1
+ b x 2 0
31 0 31 1 0
Treat as normal binary number
E.g. 011010101
= 1 x 27 + 1 x 2 6 + 0 x 2 5 + 1 x 2 4 + 0 x 2 3 + 1 x 2 2 + 0 x 2 1 + 1 x 2 0
= 128 + 64 + 16 + 4 + 1 = 213
Max f(111…11) = 232 – 1 = 4,294,967,295
Min f(000…00) = 0
Range [0,232-1] => # values (232-1) – 0 + 1 = 232
Signed Integers
2’s complement
f(b31…b0) = -b31 x 231 + … b1 x 21 + b0 x 20
Max f(0111…11) = 231 – 1 = 2147483647
Min f(100…00) = -231 = -2147483648
(asymmetric)
Range[-231,231-1] => # values(231-1 - -231)+1 = 232
Invert bits and add one: e.g. –6
– 000…0110 => 111…1001 + 1 => 111…1010
Why 2’s Complement
Why not use sign-magnitude?
2’s complement makes hardware simpler
Just like humans don’t work with Roman
numerals
Representation affects ease of calculation, not
correctness of answer
000 000
111 0 001 111 0 001
-1 1 -3 1
110 -2 2 010 110 -2 2 010
-3 3 -1 3
101 -4 011 101 -0 011
100 100
Addition and Subtraction
4-bit unsigned example
0 0 1 1 3
1 0 1 0 10
1 1 0 1 13
4-bit 2’s complement – ignoring overflow
0 0 1 1 3
1 0 1 0 -6
1 1 0 1 -3
Subtraction
A – B = A + 2’s complement of B
E.g. 3 – 2
0 0 1 1 3
1 1 1 0 -2
0 0 0 1 1
Full Adder
Full adder (a,b,cin) => (cout, s)
c = two or more of (a, b, c ) are 1s
out in
s = exactly one or three of (a,b,c ) are 1s
in
a b cin cout s
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
The Full Adder
Arithmetic-logic units (ALUs)
ci+1 = ai * bi + ai * ci + bi * ci
Ripple-carry Subtractor
Arithmetic-logic units 18
The multi-purpose adder
• One adder performing two separate functions
• “Sub” acts like a function select input
– determines whether the circuit performs addition or subtraction
• Circuit-wise, all “Sub” does is modify the adder’s inputs A
and CI
Arithmetic-logic units 19
Modifying the adder inputs
• By the same approach, we can use an adder to compute
other functions as well
• Figure out which functions are needed, and then put the
right circuitry into the “Input Logic” box
Arithmetic-logic units 20
Some more possible functions
• Adder inputs A, B and CI can be set to compute either X + Y
or X - Y
• How can we produce the increment function G = X + 1?
One way: Set A = 0000, B = X, and CI = 1
• How about decrement: G = X - 1?
A = 1111 (-1), B = X, CI = 0
A = 0000, B = X, CI = 0
Arithmetic-logic units 22
Table of arithmetic functions
• Here are some of the different possible arithmetic
operations
• We’ll need some way to specify which function we’re
interested in, so we’ve randomly assigned a selection code
S2 S1 S0
to each operation Arithmetic operation
0 0 0 X (transf er)
0 0 1 X +1 (increment)
0 1 0 X +Y (add)
0 1 1 X +Y +1
1 0 0 X +Y’ (1C subtraction)
1 0 1 X +Y’ +1 (2C subtraction)
1 1 0 X –1 (decrement)
1 1 1 X (transf er)
Arithmetic-logic units 23
Mapping the table to an adder
• This second table shows what the adder’s inputs should be
for each of our eight desired arithmetic operations
– Adder input CI is always the same as selection code bit S 0.
– B is always set to X.
– A depends only on S2 and S1.
• These equations depend on both the desired operations
and the assignment of selection codes
Selection code Desired arithmetic operation Required adder inputs
S2 S1 S0 G (A +B +CI ) A B CI
0 0 0 X (transf er) 0000 X 0
0 0 1 X +1 (increment) 0000 X 1
0 1 0 X +Y (add) Y X 0
0 1 1 X +Y +1 Y X 1
1 0 0 X +Y’ (1C subtraction) Y’ X 0
1 0 1 X +Y’ +1 (2C subtraction) Y’ X 1
1 1 0 X –1 (decrement) 1111 X 0
1 1 1 X (transf er) 1111 X 1
Arithmetic-logic units 24
Building the input logic
• Compute adder input A, given input Y and the function select
code S (actually just S2 and S 1—why?)
• Here is an abbreviated truth table:
S2 S1 A
0 0 0000
0 1 Y
1 0 Y’
1 1 1111
Arithmetic-logic units 25
Primitive gate-based input logic
0 0 0000 0 1 0 0
0 1 Y 0 1 1 1
1 0 Y’ 1 0 0 1
1 1 1111 1 0 1 0
1 1 0 1
1 1 1 1
Arithmetic-logic units 26
Primitive gate implementation
• From the truth table, we can
find an MSP (minimal SoP):
S1
0 0 1 0
S2 1 0 1 1
Yi
Ai = S 2 Y i ’ + S 1 Y i
Arithmetic-logic units 27
Bitwise operations
• Most computers also support logical operations like AND,
OR and NOT, but extended to multi-bit words
• To apply a logical operation to two words X and Y, apply the
operation on each pair of bits Xi and Yi:
1011 1011 1011
AND 1 1 1 0 OR 1110 XOR 1 1 1 0
1010 1111 0101
Arithmetic-logic units 28
Bitwise operations in programming
• Languages like C, C++ and Java provide bitwise logical
operations:
• Not the same as the operators &&, || and !, which treat each
integer as a single logical value (0 is false, everything else is
true):
Arithmetic-logic units 29
Bitwise operations in networking
• IP addresses are actually 32-bit binary numbers, and
bitwise operations can be used to find network information
• For example, you can bitwise-AND an address
192.168.10.43 with a “subnet mask” to find the “network
address,” or which network the machine is connected to
192.168. 10. 43 = 11000000.10101000.00001010.00101011
& 255.255.255.224 = 11111111.11111111.11111111.11100000
192.168. 10. 32 = 11000000.10101000.00001010.00100000
Arithmetic-logic units 30
Defining a logic unit
• A logic unit supports different logical
functions on two multi-bit inputs X and
Y, producing an output G
• This abbreviated table shows four
possible functions and assigns a
selection code S to each
S1 S0 Output
0 0 Gi =X iYi
0 1 Gi =X i +Yi
1 0 Gi =X i Yi
1 1 Gi =X i’
Arithmetic-logic units 31
Our simple logic unit
• Inputs:
– X (4 bits)
– Y (4 bits)
– S (2 bits)
• Outputs:
– G (4 bits)
Arithmetic-logic units 32
Combining the arithmetic and logic units
Arithmetic-logic units 33
Our ALU function table
• This table shows a sample
function table for an ALU S3 S2 S1 S0 Operation
• All of the arithmetic 0 0 0 0 G =X
operations have S3=0, and 0 0 0 1 G =X +1
all of the logical operations 0 0 1 0 G =X +Y
have S3=1 0 0 1 1 G =X +Y +1
0 1 0 0 G =X +Y’
• These are the same 0 1 0 1 G =X +Y’ +1
functions we saw when we 0 1 1 0 G =X –1
built our arithmetic and 0 1 1 1 G =X
logic units a few minutes 1 x 0 0 G =X and Y
ago 1 x 0 1 G =X or Y
• Since our ALU only has 4 1 x 1 0 G =X Y
logical operations, we don’t 1 x 1 1 G =X’
need S2—The operation
done by the logic unit
depends only on S1 and S0
Arithmetic-logic units 34
A complete ALU circuit
4 4
Arithmetic-logic units 35
Comments on the multiplexer
Arithmetic-logic units 36
The completed ALU
4
4
4
Arithmetic-logic units 37
Arithmetic summary
• Binary representations,
signed/unsigned
• Arithmetic
– Full adder, ripple-carry, carry lookahead
– Carry-select, Carry-save
– Overflow, negative
– More (multiply/divide/FP) later
• Logical
– Shift, and, or
Carry Lookahead
The above ALU is too slow
– Gate delays for add = 32 x FA + XOR ~= 64
Theoretically, in parallel
– Sum0 = f(cin, a0, b0)
– Sumi = f(cin, ai…a0,, bi…b0)
– Sum31 = f(cin, a31…a0, b31…b0)
Any boolean function in two levels, right?
– Wrong! Too much fan-in!
Carry Lookahead
Carry Lookahead
Need compromise
– Build tree so delay is O(log2 n) for n bits
– E.g. 2 x 5 gate delays for 32 bits
We will consider basic concept with
– 4 bits
– 16 bits
Carry Lookahead
Carry Lookahead
Carry Lookahead
0101 0100
0011 0110
Recall: ci+1 = ai * bi + ai * ci + bi * ci
= ai * bi + (ai + bi) * ci
= gi + pi * ci
c 1 = g0 + p 0 * c 0
c2 = g1 + p1 * c1 = g1 + p1 * (g0 + p0 * c0)
= g1 + p 1 * g 0 + p 1 * p 0 * c 0
c3 = g2 + p2 * g1 + p2 * p1 * g0 + p2 * p1 * p0 * c0
c4 = g3 + p3*g2 + p3*p2*g1 + p3*p2*p1*g0 + p3*p2*p1*p0*c0
Uses one level to form pi and gi, two levels for
carry
But, this needs n+1 fanin at the OR and the
rightmost AND
4-bit Carry Lookahead Adder
c0
c4 Carry Lookahead Block
g3 p3 a3 b3 g2 p2 a2 b2 g1 p1 a1 b1 g0 p0 a0 b0
c3 c2 c1 c0
s3 s2 s1 s0
Hierarchical Carry Lookahead
for 16 bits
c0
c16 Carry Lookahead Block
c12 c8 c4 c0
= G0,3 + P0,3 c0
G0,3 = g3+ p3 * g2 + p3 * p2 * g1 + p3 * p2 * p1 * g0
G4,7 = g7 + p7 * g6 + p7 * p6 * g5 + p7 * p6 * p5 * g4
P4,7 = p7 * p6* p5 * p4
P8,11 = p11*p10*p9*p8
G12,15 = g15 + p15 * g14 + p15* p14 * g13 + p15 * p14 * p13 * g12
= G0,3 + P0,3 c0
where j = (i+k+1)/2
Thus
60
© 2000 Mark Hill
Carry-Select Adder
61
© 2000 Mark Hill
Other Adders: Carry Save
Logical Operations
Bitwise AND, OR, XOR, NOR
– Implement w/ 32 gates in parallel
Shifts and rotates
– sll -> shift left logical (0->LSB)
– srl -> shift right logical (0->MSB)
– sra -> shift right arithmetic (old MSB->new MSB)
– rol => rotate left (MSB->LSB)
– ror => rotate right (LSB->MSB)
Shifter
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 S0
D7 D6 D5 D4 D3 D2 D1 D0 }S0=0
D6 D5 D4 D3 D2 D1 D0 0 }S0=1
D7 D6 D5 D4 D3 D2 D1 D0
0 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
S1
D7 D6 D5 D4 D3 D2 D1 D0 }S1=0
D5 D4 D3 D2 D1 D0 0 0 }S1=1
D7 D6 D5 D4 D3 D2 D1 D0
0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
S2
D7 D6 D5 D4 D3 D2 D1 D0 }S2=0
D3 D2 D1 D0 0 0 0 0 }S2=1
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 Shamt0
0
0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
Shamt1
0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
Shamt2
ASSIGNMENT: