Sub topic 12
Hardware multiplication and division
This chapter assumes you know two's complement arithmetic, how a ripple-carry adder works, and the register-transfer notation from the datapath chapter. Multiplication and division carry only a small weight in GATE. The questions are usually about counting iterations, sizing registers, or tracing a few steps of Booth's algorithm.
Why the ALU does not multiply in one step
A combinational multiplier for n-bit operands needs about n² gates and a long delay. Simple processors instead reuse the adder over several clock cycles, controlled by the control unit. The cost is time: an n-bit multiply takes n cycles, not one.
Every algorithm in this chapter uses the same hardware pieces:
- An n-bit adder/subtractor.
- Registers A, Q and M, each n bits wide.
- A one-bit register C, which holds the carry out of the adder.
- A shifter that moves registers A and Q one position at a time.
- A counter that tracks how many iterations remain.
Unsigned shift-and-add multiplication
M holds the multiplicand, Q holds the multiplier, and A starts at 0. A and Q together form a 2n-bit product register.
Repeat n times:
- If the least significant bit of Q (Q0) is 1, add M to A. The carry goes into C.
- Shift C, A and Q right by one bit as one chain. C moves into the top of A, the low bit of A moves into the top of Q, and Q0 is discarded.
After n iterations, A:Q holds the 2n-bit product.
Shifting Q right frees its top bit for the low bits of A. Because of this, the product never needs more than 2n bits of register space.
def shift_add_multiply(multiplicand, multiplier, n):
"""Unsigned n-bit multiply; returns the 2n-bit product."""
mask = (1 << n) - 1
A, Q, M = 0, multiplier & mask, multiplicand & mask
for _ in range(n):
if Q & 1:
A += M # A may now use n+1 bits (carry in C)
combined = (A << n) | Q # chain C, A, Q
combined >>= 1 # shift the whole chain right
A = combined >> n
Q = combined & mask
return (A << n) | Q
print(shift_add_multiply(13, 11, 4)) # 143Booth's algorithm for signed numbers
Plain shift-and-add fails for negative multipliers in two's complement. Booth's algorithm handles signed operands directly. It treats a run of 1s in the multiplier as one subtraction at the start of the run and one addition just after its end. For example, 0111100 is 2⁶ − 2² (subtract 2², add 2⁶).
The hardware adds a one-bit register Q₋₁, initially 0. At each step it examines the pair (Q0, Q₋₁):
| Q0 | Q₋₁ | Action before shifting |
|---|---|---|
| 0 | 0 | None |
| 1 | 1 | None |
| 1 | 0 | A = A − M (start of a run of 1s) |
| 0 | 1 | A = A + M (end of a run of 1s) |
Then it performs an arithmetic right shift of A, Q and Q₋₁ together. Arithmetic means the sign bit of A is copied into the vacated top position. The loop runs n times.
def booth_multiply(m, q, n):
"""Signed n-bit Booth multiply; returns the signed 2n-bit product."""
mask = (1 << n) - 1
A, Q, M, q_1 = 0, q & mask, m & mask, 0
for _ in range(n):
pair = (Q & 1, q_1)
if pair == (0, 1):
A = (A + M) & mask
elif pair == (1, 0):
A = (A - M) & mask
q_1 = Q & 1 # arithmetic shift right
Q = (Q >> 1) | ((A & 1) << (n - 1))
sign = A >> (n - 1)
A = (A >> 1) | (sign << (n - 1))
product = (A << n) | Q
if product >> (2 * n - 1): # reinterpret as signed
product -= 1 << (2 * n)
return product
print(booth_multiply(-3, 7, 4)) # -21
print(booth_multiply(5, -2, 4)) # -10Booth's algorithm does not always reduce the work. A multiplier such as 01010101 has a transition at almost every bit, so it needs about n add or subtract operations. A multiplier such as 00111100 needs only two. The number of operations depends on the multiplier's bit pattern, but the number of iterations is always n.
Restoring division
Division is the reverse process. Q holds the dividend, M holds the divisor, and A starts at 0. After n iterations, Q holds the quotient and A holds the remainder.
Repeat n times:
- Shift A:Q left by one bit. The top bit of Q moves into the bottom of A.
- Compute A = A − M.
- If A is negative, the subtraction was too large. Restore A by adding M back, and set Q0 = 0.
- Otherwise keep the result and set Q0 = 1.
def restoring_divide(dividend, divisor, n):
"""Unsigned n-bit divide; returns (quotient, remainder)."""
mask = (1 << n) - 1
A, Q, M = 0, dividend & mask, divisor
for _ in range(n):
A = (A << 1) | ((Q >> (n - 1)) & 1) # shift A:Q left
Q = (Q << 1) & mask
A -= M
if A < 0:
A += M # restore
else:
Q |= 1
return Q, A
print(restoring_divide(13, 3, 4)) # (4, 1)The restore step costs an extra addition on every failed subtraction. In the worst case an iteration performs a subtract and an add.
Non-restoring division
Non-restoring division removes the restore step. If A is negative after a subtraction, the next iteration shifts left and then adds M instead of subtracting it. Shifting a negative remainder left doubles the error, and adding M next time cancels it exactly: 2(A − M) + M = 2A − M.
- If A ≥ 0 after the previous step, shift left and compute A = A − M.
- If A < 0 after the previous step, shift left and compute A = A + M.
- Set Q0 = 1 if the new A is non-negative, otherwise 0.
- After the last iteration, if A is negative, add M once to correct the remainder.
Each iteration needs exactly one add or subtract, so the delay is more predictable.
Comparison of the algorithms
| Algorithm | Iterations for n bits | Add/subtract per iteration | Handles signed numbers |
|---|---|---|---|
| Shift-and-add multiply | n | 0 or 1 add | Unsigned only |
| Booth multiply | n | 0 or 1 add or subtract | Yes, two's complement |
| Restoring divide | n | 1 subtract, plus 1 add if restoring | Unsigned |
| Non-restoring divide | n | Exactly 1, plus at most 1 final correction | Unsigned, with extensions for signed |
GATE patterns
- Iteration count. An n-bit multiply or divide takes n iterations. Total time is roughly n × (adder delay + shift delay). With a carry-lookahead adder, each iteration is faster than with a ripple-carry adder.
- Register sizes. The product of two n-bit numbers needs 2n bits. A:Q holds the product, and the extra carry bit C is needed only during the add.
- Booth operation counts. Scan the multiplier from the right, with a 0 appended on the right. Each 10 pair is a subtraction. Each 01 pair is an addition. All other pairs do nothing.
- Remainder sign. Non-restoring division may leave a negative remainder before the final correction. A question that asks for the register contents after the last iteration, before correction, expects the negative value.
Exercise
An 8-bit Booth multiplier is 00111100. How many additions and subtractions does Booth's algorithm perform, and how many iterations does it run? How many additions would plain shift-and-add perform for the same multiplier?
Show answer
Append Q₋₁ = 0 and scan the pairs (Qi, Qi−1) from the right. The pair at bit 2 is (1, 0), so there is one subtraction. The pair at bit 6 is (0, 1), so there is one addition. Booth performs 2 operations (1 subtraction and 1 addition) in 8 iterations. Plain shift-and-add adds M once for each 1 bit in the multiplier. 00111100 has four 1s, so it performs 4 additions in 8 iterations.
Exercise
In restoring division of a 4-bit dividend by a 4-bit divisor, how many iterations are performed, and what does register A hold at the end?
Show answer
The algorithm performs 4 iterations, one per dividend bit. Register Q holds the quotient and register A holds the remainder.