---
title: Adders and the carry problem
heading: Ripple, lookahead, prefix and carry-select adders, and overflow
eyebrow: S3 · Lesson 8 · Adders and the carry problem
xp: 50
---

::: hook
Addition is the most common operation in a datapath, and its difficulty is one bit: the carry. In a ripple-carry adder the carry walks from the least significant bit to the most, one stage at a time, so a 64-bit add takes 64 gate delays. Every fast adder is a trick for computing carries in parallel. Lookahead and prefix adders compute every carry in logarithmic time with a tree of generate and propagate signals; carry-select adders compute both possible answers early and pick one. Each trades area and wiring for delay, and choosing among them is a classic piece of datapath design.
:::

::: play
- Worst-case delay, in gate delays, against adder width for three designs: ripple carry (one delay per bit), carry-select with equal blocks of $\sqrt N$ bits, and a parallel-prefix (Kogge–Stone) adder.
- Ripple grows linearly, carry-select as $2\sqrt N$, prefix as $\log_2N$ plus two levels for the generate/propagate inputs and the sum XOR.
- At 8 bits they're close; at 64 bits the prefix adder is about eight times faster than ripple.
:::
:::

```sim plot
view: graph
x: N = 4..128 log
y: d = 0..130
plot: N | label: ripple carry
plot: 2*sqrt(N) | color: ch2 | label: carry-select, √N-bit blocks
plot: log(N)/log(2) + 2 | color: ch3 | label: parallel prefix (Kogge–Stone)
```

::: predict
## A 64-bit ripple-carry adder takes 1.9 ns. Roughly how fast is a 64-bit parallel-prefix adder in the same gates?

- [ ] About the same: it does the same arithmetic
- [ ] About twice as fast
- [x] About eight times as fast
- [ ] About 64 times as fast

::: reveal
About eight times: $\log_264$ = 6 prefix levels plus about 2 more for the bit-level generate/propagate signals and the final sum, 8 gate delays against 64. The price is area and wiring: a Kogge–Stone adder has $N\log_2N - N + 1$ = 321 prefix cells for 64 bits, many with long wires. Brent–Kung adders use far fewer cells (120) at nearly twice the depth (11 levels).
:::
:::

::: nameit
## Generate and propagate

For bit i, $g_i = a_ib_i$ (this bit creates a carry) and $p_i = a_i \oplus b_i$ (this bit passes an incoming carry along). Then $c_{i+1} = g_i + p_ic_i$ and $s_i = p_i \oplus c_i$. The ripple adder evaluates that recurrence one bit at a time: delay proportional to N.

## Prefix adders

Group generate and propagate signals combine associatively: a block $(G, P)$ followed by another $(G', P')$ gives $(G' + P'G, P'P)$. Any tree that computes every prefix $(G_{i:0}, P_{i:0})$ gives every carry at once.

| Adder | Levels | Prefix cells |
|---|---|---|
| ripple | N | N − 1 |
| Kogge–Stone | $\log_2N$ | $N\log_2N - N + 1$ |
| Brent–Kung | $2\log_2N - 1$ | $2N - 2 - \log_2N$ |
| Sklansky | $\log_2N$ | $\frac{N}{2}\log_2N$, with high fan-out |

Add about two levels for the bitwise g/p and the final sum.

## Carry-select and carry-skip

A **carry-select** adder splits N bits into k blocks, computes each block's sum for both carry-in values, and picks with multiplexers as the real carry arrives: delay about $\frac{N}{k} + k$, smallest at $k = \sqrt N$, giving $2\sqrt N$. A **carry-skip** adder lets a carry jump a block whose bits all propagate.

## FPGAs

FPGAs have dedicated carry chains: ripple adders whose per-bit carry delay is tens of picoseconds, so a 32-bit add is fast enough without a prefix structure.

## Overflow

In N-bit two's complement (range $-2^{N-1}$ to $2^{N-1} - 1$), an addition overflows when both operands have the same sign and the sum's sign differs, equivalently when the carry into the top bit differs from the carry out of it: $V = c_{N-1} \oplus c_N$.
:::

::: example
## Compare a 64-bit ripple adder and a Kogge–Stone adder at 30 ps per level, and count the Kogge–Stone and Brent–Kung prefix cells.

::: solution
1. **Ripple.**
   $$\begin{aligned} t &= 64 \times 30\ \text{ps} \\ &= 1.92\ \text{ns} \end{aligned}$$
2. **Kogge–Stone delay.** 6 prefix levels plus 2 make 8 levels:
   $$\begin{aligned} t &= 8 \times 30\ \text{ps} \\ &= 240\ \text{ps} \end{aligned}$$
3. **Kogge–Stone cells.**
   $$\begin{aligned} N &= 64 \times 6 - 64 + 1 \\ &= 321 \end{aligned}$$
4. **Brent–Kung.** It takes $2 \times 6 - 1 = 11$ prefix levels (13 in total, 390 ps) and
   $$\begin{aligned} N &= 128 - 2 - 6 \\ &= 120\ \text{cells} \end{aligned}$$
5. **Which to use.** In a CPU's critical path the Kogge–Stone wins; in a low-power accumulator the Brent–Kung or even ripple wins.

**Answer:** ripple takes 1.92 ns and Kogge–Stone 240 ps with 321 prefix cells; Brent–Kung takes 390 ps with 120 cells.
:::
:::

::: example
## Add 4-bit two's complement numbers 0111 (7) and 0001 (1). Does it overflow?

::: solution
1. **Add.**
   $$0111 + 0001 = 1000$$
   which reads as −8.
2. **Check the signs.** Both operands are positive but the sum's sign bit is 1, so it overflowed: the true answer 8 is outside −8 to 7.
3. **Check the carries.** The carry into bit 3 is 1 and the carry out of bit 3 is 0. They differ, so V = 1.
4. **Saturating instead.** Saturating arithmetic (lesson 10) would clamp the result to 0111.

**Answer:** yes, it overflows: the sum 1000 reads as −8, and V = 1.
:::
:::

::: example
## A 32-bit carry-select adder costs one unit of delay per bit in a block and one per multiplexer stage. What block size is best, and how does it compare with a ripple adder at 30 ps per unit?

::: solution
1. **Delay with k blocks.** It is about
   $$t \approx \frac{32}{k} + k$$
   smallest near $k = \sqrt{32} = 5.7$.
2. **Whole blocks.** Blocks of 8 ($k = 4$) or 4 ($k = 8$) both give
   $$\frac{32}{4} + 4 = 12\ \text{units}$$
   which at 30 ps is 360 ps.
3. **Against ripple.** Ripple carry takes 32 units, 960 ps.
4. **The cost.** About twice the adder area, since every block but the first is built twice.

**Answer:** blocks of 4 or 8 bits, giving 12 units (360 ps) against 960 ps for ripple, at about twice the area.
:::
:::

::: check
given: N = 16 | 32 | 64; t = 20 | 35
answer: N*t
unit: ps
tolerance: 0.5%

## An {N}-bit ripple-carry adder has {t} ps of carry delay per bit. What's its worst-case carry delay?

::: right
Right. $Nt$ = {answer} ps.
:::

::: misses
- [any] Not yet. The carry may ripple through every bit.
:::
:::

::: check
given: N = 16 | 64 | 256
answer: log(N)/log(2)
tolerance: 0.4

## How many prefix levels does an {N}-bit Kogge–Stone adder have?

::: right
Right. $\log_2N$ = {answer}.
:::

::: misses
- [2*log(N)/log(2) - 1] That's Brent–Kung.
- [any] Not yet. Each level doubles the span of every group.
:::
:::

::: check
given: N = 16 | 32 | 64
answer: N*log(N)/log(2) - N + 1
tolerance: 0.4

## How many prefix cells does an {N}-bit Kogge–Stone adder have?

::: right
Right. $N\log_2N - N + 1$ = {answer}.
:::

::: misses
- [2*N - 2 - log(N)/log(2)] That's the Brent–Kung count.
- [any] Not yet. Count cells per level and add.
:::
:::

::: check
given: N = 16 | 32 | 128
answer: 2*log(N)/log(2) - 1
tolerance: 0.4

## How many prefix levels does an {N}-bit Brent–Kung adder have?

::: right
Right. $2\log_2N - 1$ = {answer}.
:::

::: misses
- [log(N)/log(2)] That's Kogge–Stone; Brent–Kung goes up a tree and back down.
- [any] Not yet. A reduction tree plus a distribution tree.
:::
:::

::: check
given: N = 32 | 64; k = 4 | 8 | 16
answer: N/k + k
tolerance: 0.4

## An {N}-bit carry-select adder uses {k} equal blocks. In units of one bit's carry delay (and one multiplexer per block), what's its delay?

::: right
Right. $\frac{N}{k} + k$ = {answer}.
:::

::: misses
- [N/k] Add the multiplexer chain through the k blocks.
- [any] Not yet. Ripple through one block, then a mux per block.
:::
:::

::: check
given: N = 6 | 12 | 24
answer: 2^(N - 1) - 1
tolerance: 0.4

## What's the largest number an {N}-bit two's complement register holds?

::: right
Right. $2^{N-1} - 1$ = {answer}.
:::

::: misses
- [2^N - 1] That's unsigned.
- [any] Not yet. The top bit is the sign.
:::
:::

::: check
kind: boolean
vars: A, B, S
answer: A & B & ~S | ~A & ~B & S
ops: and, or, not

## A, B and S are the sign bits of two addends and their sum. Write the overflow flag.

::: right
Right. Overflow when the operands agree in sign and the sum disagrees: $AB\bar S + \bar A\bar BS$.
:::

::: misses
- [A & B & ~S] Negative overflow too: two negatives giving a positive.
- [any] Not yet. Same-sign operands, different-sign result.
:::
:::

::: check
kind: parts
given: N = 16 | 32 | 64 | 128

## Compare prefix adders for {N} bits.

::: part
answer: round(log(N)/log(2))
tolerance: 0

### How many prefix levels does Kogge–Stone take?

::: misses
- [any] $\log_2N$.
:::
:::

::: part
answer: N*round(log(N)/log(2)) - N + 1
tolerance: 0

### How many prefix cells?

::: misses
- [any] $N\log_2N - N + 1$.
:::
:::

::: part
answer: 2*round(log(N)/log(2)) - 1
tolerance: 0

### How many levels does Brent–Kung take?

::: misses
- [any] $2\log_2N - 1$.
:::
:::

::: part
answer: 2*N - 2 - round(log(N)/log(2))
tolerance: 0

### And how many cells?

::: misses
- [any] $2N - 2 - \log_2N$.
:::
:::
:::

::: check
kind: derivation
vars: N = 4..1024, k = 1..64

## A carry-select adder of N bits is split into k equal blocks; its delay is about N/k + k units.

::: step
answer: N/k + k

### What is its delay?
:::

::: step
answer: sqrt(N)

### What block count minimizes it?
:::

::: step
answer: 2*sqrt(N)

### What is the minimum delay?
:::

::: right
Right. Square-root delay sits between ripple's N and a prefix adder's $\log_2N$.
:::
:::

::: explain
## Why can a parallel-prefix adder compute all the carries in logarithmic time?

::: model
Each carry depends on every bit below it, which seems to force a chain. But the rule for combining two adjacent blocks of bits, a block's generate and propagate signals followed by another's, is associative: it doesn't matter how the bits are grouped, as long as the order is kept. Associative operations can be evaluated by a tree instead of a chain: first combine pairs of bits, then pairs of pairs, doubling the span at every level. After $\log_2N$ levels every bit position knows whether a carry reaches it, and all the carries are available at once. The cost is the extra combining cells and wires the tree needs.
:::
:::

::: practice

::: check
given: N = 24 | 48; t = 25 | 40
answer: N*t
unit: ps
tolerance: 0.5%

## Ripple delay: {N} bits × {t} ps?

::: misses
- [any] Nt.
:::
:::

::: check
given: N = 8 | 16 | 32 | 64 | 128 | 256
answer: N*log(N)/log(2) - N + 1
tolerance: 0.4

## Kogge–Stone cells for {N} bits?

::: misses
- [any] $N\log_2N - N + 1$.
:::
:::

::: check
given: N = 16 | 64; k = 2 | 8
answer: N/k + k
tolerance: 0.4

## Carry-select delay: {N} bits, {k} blocks?

::: misses
- [any] $N/k + k$.
:::
:::
:::
