Skip to content

Commit c11e56e

Browse files
authored
Chained CSA (#3)
Add N:2 CSA compressor tree.
1 parent badf31b commit c11e56e

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

DatapathVerification/CSA.lean

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,19 @@ theorem mul4_correct (a b : BitVec 4) : a * b = mul4 a b := by
6666
simp only [mul4, carrySave]
6767
bv_decide
6868

69+
-- N:2 compressor implementation.
70+
-- Takes a vector of n bit-vectors and reduces them to 2 bit-vectors (sum and carry) using a tree of carry-save adders.
71+
def chain {w n : Nat} (v : Vector (BitVec w) n) : CSAResult w :=
72+
match n with
73+
| 0 => ⟨0, 0
74+
| 1 => ⟨v[0], 0
75+
| 2 => carrySave w v[0] v[1] 0
76+
| 3 => carrySave w v[0] v[1] v[2]
77+
| n + 1 =>
78+
let ⟨sum, carry⟩ := chain (v.take n) -- takes the first n elements of the vector.
79+
let ⟨s, t⟩ := carrySave w sum (carry <<< 1) (v.back) -- the chained carry is shifted left by 1 to align with the next input.
80+
⟨s, t⟩ -- return the carry without shifting, the next level handles it.
81+
82+
#eval chain (v := (⟨#[5, 2, 3, 7, 3], rfl⟩ : Vector (BitVec 32) 5))
83+
6984
end CSA

0 commit comments

Comments
 (0)