S3 · Lesson 15 · Arithmetic lab
Prefix adders, Booth recoding, Dadda trees and Newton–Raphson, bit-exact in Python
Arithmetic hardware is easiest to trust when it has been modeled bit for bit and compared against plain integer arithmetic on thousands of inputs. This lab builds the models that hardware designers keep beside their RTL: a Kogge–Stone adder that counts its own levels, a radix-4 Booth recoder, a Dadda-tree stage counter, and a Newton–Raphson reciprocal whose correct bits double each step. Each one checks a formula from lessons 8 to 11 against the thing itself.
- The stems are the correct bits of a Newton–Raphson reciprocal after k iterations, starting from a seed accurate to the slider's number of bits.
- Each iteration squares the error, so the correct bits double: an 8-bit table seed reaches 64 bits in three steps.
- The dashed line marks the 53 bits of a double's significand.
Predict
Radix-4 Booth recoding turns each pair of multiplier bits, with the bit below them, into a digit . What digits does the 8-bit multiplier 0x5D (93) give, least significant first?
0x5D is 01011101. With : digit 0 is ; digit 1 is ; digit 2 is ; digit 3 is . Check: . Four digits for eight bits: half the partial products, each an easy multiple of the multiplicand.
Name it
What the lab builds
- Kogge–Stone: bitwise , ; then levels of the prefix operator at spans 1, 2, 4, …; the carry into bit i is the group generate , and .
- Booth radix 4: digits with for two's complement y.
- Dadda: the heights 2, 3, 4, 6, 9, 13, 19, … (each the last); h rows need as many stages as there are heights below h.
- Newton–Raphson: , with .
Check
Write ks_add(a, b, n): add two n-bit unsigned numbers (n a power of two) with a Kogge–Stone prefix network, returning (sum mod , number of prefix levels). Build it from g, p and the prefix operator; don't use a + b.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write booth4(y, n): the radix-4 Booth digits of the n-bit two's complement number y (n even), least significant first.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write dadda_stages(h): the number of reduction stages a Dadda tree needs to bring h rows down to 2.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write nr_bits(d, x0, k): run k Newton–Raphson steps for 1/d from x0 and return the number of correct bits, . Then check that the bits double.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why is radix-4 Booth recoding worth its extra logic in a multiplier?
A plain multiplier makes one partial product per multiplier bit, so a 32-bit multiply sums 32 rows, and the reduction tree's size and depth grow with the number of rows. Booth recoding reads the multiplier two bits at a time, with one bit of overlap, and turns each pair into a digit from −2 to 2. Every digit's partial product is easy to form: zero, the multiplicand, twice it by a shift, or their negations. That halves the rows to about 17, saving roughly half the adders in the tree and two of its stages, and it handles two's complement operands without correction terms. The recoder and the multiplexers it drives are small compared with the adders they remove.