C753 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C7 · Lesson 53 · Arithmetic lab

Lookahead carries, Booth multiplication, division, BCD and saturation, in Python

Lessons 16 to 20 built adders, multipliers and number formats as circuits. Each algorithm a circuit implements can be written first as a few lines of code that work bit by bit, exactly as the hardware does, and checked against ordinary arithmetic on every input. In this lab you'll compute carries by lookahead, multiply signed numbers with Booth's algorithm, divide by restoring subtraction, add decimal digits in BCD, and add with saturation, testing each exhaustively over small widths, which is how hardware arithmetic is verified.

Play

  • The curve shows how many add or subtract operations Booth's algorithm needs, on average, against the number of bits, compared with plain shift-and-add, which adds once per 1 bit in the multiplier.
  • Booth adds only at the boundaries of runs of 1s. For random multipliers that's about half the bits either way; for multipliers with long runs of 1s, far fewer.
  • The readout gives the worst case for each: Booth's is a multiplier of alternating 1s and 0s.

Predict

Booth's algorithm multiplies by 0111 1110 (126). How many additions or subtractions does it do, against shift-and-add's one per 1 bit?

Name it

Lookahead carries

With gi=aibi and pi=ai⊕bi, every carry can be written directly from the inputs:

ci+1=gi+pigi−1+pipi−1gi−2+⋯+pipi−1⋯p0c0.

Computing each carry this way, with no carry waiting on another, is what a lookahead adder does in parallel.

Booth multiplication

Scan the multiplier's bits from the bottom, each with the bit below it (a 0 below the lowest). At a pair 10 (a run of 1s starts), subtract the multiplicand; at 01 (a run ends), add it; otherwise do nothing. Shift the accumulator and multiplier right together, arithmetically, after each bit. After n steps the 2n-bit result is the signed product.

Restoring division

For each dividend bit from the top: shift it into a partial remainder; if the remainder is at least the divisor, subtract and set that quotient bit to 1. After n steps, the quotient and remainder are complete.

BCD addition and saturation

Add BCD digit by digit; any digit sum above 9 gets 6 added and carries into the next digit. A saturating n-bit adder clamps results to −2n−1 and 2n−1−1 instead of wrapping.

Worked example

Multiply −7 by 3 with 4-bit Booth's algorithm.

Show the solution
  1. Write the multiplier. The multiplier 3 is 0011.
  2. Bit 0. With a 0 below it's 10: subtract −7, so the accumulator gains 7, then shift.
  3. Bit 1. With 1 below it's 11: nothing.
  4. Bit 2. With 1 below it's 01: add −7.
  5. Bit 3. With 0 below: nothing.
  6. Read the result. Two operations, and the 8-bit result reads −21.
  7. Why it works. Booth treated 0011 as 4−1: −7×4−(−7)×1=−21

Answer −21, from two operations.

Worked example

Divide 143 by 11 by restoring division with 8 bits.

Show the solution
  1. Shift in until it fits. Shifting in 143 = 1000 1111 bit by bit, the partial remainder first reaches 11 or more at the fifth bit: 1000 1 = 17, subtract 11 → 6.
  2. Shift in the rest. 6 → 13 (subtract → 2), 2 → 5, 5 → 11 (subtract → 0).
  3. Read the result. The quotient bits form 0000 1101 = 13 and the remainder is 0: 143=13×11
  4. The cost. Each step is a shift and a compare-subtract, so an n-bit divider takes n steps, far more than an adder.

Answer the quotient is 13 and the remainder 0.

Worked example

Add the BCD numbers 59 and 27, and 999 and 1.

Show the solution
  1. Units. 9 + 7 = 16, above 9, so add 6: 16+6=22=101102 digit 6, carry 1.
  2. Tens. 5+2+1=8 so the result is 86.
  3. 999 + 1. Each digit overflows in turn: 10 → 0 carry 1, three times, giving 1000.
  4. In hardware. The correction ripples like a carry, and BCD hardware is a binary adder with a correction stage per digit.

Answer 59 + 27 = 86, and 999 + 1 = 1000.

Check

Step 1. Write cla_carries(a, b, cin, n) returning the list of carries c0 (= cin) to cn, each computed directly from the g and p terms by the lookahead formula, not from the carry before it.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 2. Write booth(a, b, n) returning (product, ops): the signed product of the n-bit two's-complement numbers a and b by radix-2 Booth, and the number of additions plus subtractions it performed.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 3. Write restoring_divide(N, D, n) returning (quotient, remainder) for unsigned n-bit N and D > 0.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 4. Write bcd_add(x, y) adding two BCD numbers (each a Python integer whose hex digits are the decimal digits, like 0x59 for 59) and returning the BCD sum.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 5. Write saturating_add(a, b, n) returning the n-bit signed saturating sum of a and b.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

A multiplier's only 1s are a single run of 6 consecutive bits starting at bit 3. How many additions or subtractions does Booth's algorithm perform?

Explain why

Why are hardware arithmetic algorithms usually verified by exhaustive testing at small widths?

Practice

Keep practicing

2 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.