The Sklansky adder computes the sum full
This proof shows: correctness, termination.
Let a, b, c0, gi, pi and the carries ci+1=gi∨(pi∧ci) be as on the page of the Kogge-Stone adder, whose proof shows three facts that we use here.
- For a block k..i of positions there is a pair (Gi:k,Pi:k) such that the carry out of position i is Gi:k∨(Pi:k∧x) when the carry into position k is x, and (Gk:k,Pk:k)=(gk,pk).
- For adjacent blocks, (Gi:k,Pi:k)=(Gi:j+1,Pi:j+1)∘(Gj:k,Pj:k) for k≤j<i.
- After line L2 replaces g0 by g0∨(p0∧c0), the G part of the block 0..i equals ci+1, and a+b+c0=∑i<n(pi⊕ci)2i+cn2n.
Lemma. Let 0≤lo<hi≤n, and suppose that before the call PREFIX(lo,hi), (G[i],P[i])=(gi,pi) for every i∈[lo,hi). Then the call terminates, it changes no position outside [lo,hi), and afterwards (G[i],P[i])=(Gi:lo,Pi:lo) for every i∈[lo,hi). The depth of the recursion below the call is ⌈log2(hi−lo)⌉.
Proof. By strong induction on ℓ=hi−lo≥1.
If ℓ=1, line L7 returns at once, and (G[lo],P[lo])=(glo,plo)=(Glo:lo,Plo:lo). The recursion depth is 0=⌈log21⌉.
If ℓ≥2, line L8 sets mid=lo+⌈ℓ/2⌉, so lo<mid<hi, and both ranges [lo,mid) and [mid,hi) are shorter than ℓ and disjoint. By the induction hypothesis the two calls of line L9 terminate, each changes only its own range, and afterwards position i holds (Gi:lo,Pi:lo) for lo≤i<mid and (Gi:mid,Pi:mid) for mid≤i<hi. Since the ranges are disjoint, the order in which the two calls run does not matter, so they may run in parallel.
Line L10 reads the pair at mid−1, which is (Gmid−1:lo,Pmid−1:lo) and is not changed by line L10, since mid−1<mid. For each i∈[mid,hi) it sets the pair at i to
(Gi:mid,Pi:mid)∘(Gmid−1:lo,Pmid−1:lo)=(Gi:lo,Pi:lo) by fact 2 with j=mid−1. So every position of [lo,hi) holds the block starting at lo, and nothing outside [lo,hi) changed.
The two halves have lengths ⌈ℓ/2⌉ and ⌊ℓ/2⌋, and ⌈log2⌈ℓ/2⌉⌉=⌈log2ℓ⌉−1 for ℓ≥2, so the recursion depth below the call is 1+(⌈log2ℓ⌉−1)=⌈log2ℓ⌉. □
Theorem. SKLANSKY-ADD terminates and returns the n+1 bits of a+b+c0.
Proof. For n=0 the result is the single bit c0. For n≥1, line L3 sets (G[i],P[i])=(gi,pi) for all i, with g0 already updated by line L2, so the lemma applies to PREFIX(0,n): it terminates, with recursion depth ⌈log2n⌉, and leaves (Gi:0,Pi:0) at every position. By fact 3, G[i]=ci+1, so line L4 sets the true carries, and line L5 computes si=pi⊕ci. By fact 3 again, ∑i<nsi2i+cn2n=a+b+c0. ■