Carry-save addition trees and logarithmic-depth bit counting #
A 3-to-2 carry-save adder (csa3to2 w) reduces three w-bit words x, y, z to a bitwise
exclusive-or word csaSum x y z and a shifted carry word csaCarry x y z in constant depth (≤ 6)
and linear size (≤ 20 w), preserving the sum of their unsigned values modulo 2 ^ w
(val_csaSum_add_val_csaCarry_mod). Two carry-save layers form a 4-to-2 reducer (csa4to2 w) that
combines two redundant carry-save pairs (a₁, b₁) and (a₂, b₂) into one pair (s, c) in depth
at most 12 and size at most 40 w.
Combining k one-bit inputs along a balanced binary tree (Circuit.balanced) of 4-to-2 reducers
produces a carry-save pair whose sum modulo 2 ^ w is the Hamming weight BitString.weight x, in
depth O(log k) and size O(k w). Iterating csa3to2 w with a zero third word w times
(finishCircuit w w) clears a further carry bit at every step
(sndBits_eval_finishCircuit_eq_false), collapsing the carry-save pair to a single w-bit binary
numeral in depth O(w) and size O(w²).
Taking w = ⌈log₂ (k + 1)⌉ gives a De Morgan circuit (weightCircuit k) of depth O(log k) and
size O(k log k) that outputs the exact binary representation of BitString.weight x
(val_eval_weightCircuit) Chandra, Stockmeyer and Vishkin,
Vollmer, Chapter 1.
Main definitions #
Cslib.Circuits.Boolean.csa3to2,Cslib.Circuits.Boolean.csa4to2: 3-to-2 and 4-to-2 carry-save reduction circuits onw-bit words.Cslib.Circuits.Boolean.finishCircuit: carry-clearing iteration reducing a carry-save pair to a singlew-bit word.Cslib.Circuits.Boolean.weightCircuit: De Morgan circuit computing the binary numeral of the Hamming weight ofkinput bits.
Main results #
Cslib.Circuits.Boolean.val_csaSum_add_val_csaCarry_mod: 3-to-2 carry-save reduction preserves the sum modulo2 ^ w.Cslib.Circuits.Boolean.val_eval_weightCircuit:weightCircuit kcomputes the exact binary numeral ofBitString.weight xin⌈log₂ (k + 1)⌉bits.Cslib.Circuits.Boolean.size_weightCircuit_le,Cslib.Circuits.Boolean.depth_weightCircuit_le: sizeO(k log k)and depthO(log k).
References #
- [A. K. Chandra, L. Stockmeyer, U. Vishkin, Constant depth reducibility] [CSV84]
- H. Vollmer, Introduction to Circuit Complexity
Unsigned word values and carry-save arithmetic #
The carry of a 1-bit full adder: whether at least two of the three bits are true.
Instances For
The bitwise exclusive-or word of a 3-to-2 carry-save adder on w-bit words.
Instances For
The shifted carry word of a 3-to-2 carry-save adder on w-bit words: bit 0 is false and
bit i + 1 is the full-adder carry from bit i.
Instances For
Carry-save 3-to-2 invariant: the sum and shifted carry words have the same sum of unsigned
values modulo 2 ^ w as the three input words.
3-to-2 and 4-to-2 carry-save circuits #
Carry-clearing iteration #
Balanced carry-save tree for Hamming weight #
weightCircuit k computes the exact binary numeral of BitString.weight x.