Skip to content

Commit c0a0989

Browse files
committed
formalization
1 parent 97d0ff7 commit c0a0989

2 files changed

Lines changed: 187 additions & 78 deletions

File tree

paper.tex

Lines changed: 178 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@
123123
\newacronym{hls}{HLS}{High-level Synthesis}
124124
\newacronym{asic}{ASIC}{application-specific integrated circuits}
125125

126+
\newcommand{\sem}[1]{\llbracket #1 \rrbracket}
127+
\newcommand{\Circ}{\mathit{Circuit}}
128+
\newcommand{\Col}{\mathit{Column}}
129+
\newcommand{\Heap}{\mathit{BitHeap}}
130+
\newcommand{\Arith}{\mathit{Arith}}
131+
\newcommand{\Step}{\mathit{Step}}
132+
\newcommand{\Chain}{\mathit{Chain}}
133+
\newcommand{\var}{\mathsf{var}}
134+
\newcommand{\HA}{\mathsf{HA}}
135+
\newcommand{\FA}{\mathsf{FA}}
136+
\newcommand{\maj}{\mathsf{maj}}
137+
\newcommand{\zext}{\mathsf{zext}}
138+
\newcommand{\trunc}{\mathsf{trunc}}
139+
\newcommand{\flatten}[1]{\lfloor #1 \rfloor}
140+
\newcommand{\lift}[1]{\lceil #1 \rceil}
141+
\newcommand{\compile}[1]{\langle\!\langle #1 \rangle\!\rangle}
142+
126143
\graphicspath{{./images/}}
127144

128145
% Define macros that are used in this paper
@@ -332,9 +349,10 @@ \section{Background}
332349
where the term $x_i \cdot y_j$ is the logical AND of the bits, and the weight of the resulting bit is $2^{i+j}$.
333350
Therefore, a multiplication can be implemented as a summation of weighted bits, where the bits are generated by logical AND of the bits of the two operands.
334351

335-
A bit heap is a data structure that effectively represents the summation of weighted bits \cite{arithmetic_core_generation_bitheaps}. Bits in a bit heap are organized into columns based on their weights, and the height of each column corresponds to the number of bit values that need to be summed for that particular weight.
352+
A bit heap is a data structure that effectively represents the summation of weighted bits \cite{arithmetic_core_generation_bitheaps}. Bits in a bit heap are organized into columns based on their weights, and the height of each column corresponds to the number of bit values that need to be summed for that particular weight.
336353

337354
\subsection{Compression Algorithms}
355+
\label{comp_algo}
338356
In arithmetic circuits, a main contributor to delay is carry propagation. In cases where multiple summands must be added, the naive approach chains carry-propagation adders, growing the delay linearly with the number of bits and summands. For example, multiplication is usually implemented as addition of multiple summands.
339357

340358
Compressor trees are built to address this issue by reducing multiple summands to two using carry-save arithmetic,
@@ -347,7 +365,7 @@ \subsubsection{Wallace Tree}
347365

348366
\subsubsection{Dadda's Algorithm}
349367
Dadda's algorithm is a method for reducing the partial products of an $n$-bit multiplier to a two-row representation
350-
using \acrfull{fa} and \acrfull{ha}, after which a single \acrfull{cpa} produces the final result \cite{application_specific_arithmetic}.
368+
using \acrfull{fa} and \acrfull{ha}, after which a single \acrfull{cpa} produces the final result \cite{application_specific_arithmetic} \cite{dadda_tree}.
351369

352370
\begin{figure}[h]
353371
\centering
@@ -388,18 +406,55 @@ \subsubsection{Dadda's Algorithm}
388406
\end{tcolorbox}
389407

390408
\section{Formalization}
391-
In this section, we present the formalization of the bit heap data structure and compression algorithms in Lean 4.
409+
In this section, we present the formalization of the bit heap data structure as well as the underlying hardware-level constructs and compression algorithms in Lean 4.
392410
We also discuss the correctness of the composition of adders and how we relate bit heaps to bit-vector arithmetic.
393411

412+
\subsection{Circuit}
413+
The building block of the bit heap data structure is a single bit.
414+
In our formalization, the Circuit data structure represent a boolean expression, that is named by a natural-number index.
415+
416+
\[
417+
\begin{array}{r@{\;}c@{\;}l@{\qquad}l}
418+
\multicolumn{4}{l}{%
419+
\text{indices } \mathbb{N} \ni n,k \qquad
420+
\text{width } \mathbb{N}^{+} \ni w \qquad
421+
\circledast \in \{\oplus,\wedge,\vee,\barwedge\}}\\[6pt]
422+
\Circ \ni c &::=& b_n \mid 0 \mid 1 \mid c \circledast c &
423+
\end{array}
424+
\]
425+
426+
A \texttt{Circuit} describes a logical expression, that can be a binary variable, constant, or any binary operation over composition Circuit objects.
427+
The bits represented by \texttt{Circuit} are symbolic and do not hold any value.
428+
A Circuit is assigned a value under a bit environment $\sigma$,
429+
which maps the \texttt{Circuit} to Boolean values.
430+
431+
\[
432+
\begin{array}{r@{\;}c@{\;}l}
433+
\multicolumn{3}{@{}l@{}}{%
434+
\sigma \;=\; \mathbb{N} \to \mathbb{B}} \\[4pt]
435+
\mathcal{C}\sem{b_n}\sigma &=& \sigma(n)\\
436+
\mathcal{C}\sem{0}\sigma &=& \mathit{false}\\
437+
\mathcal{C}\sem{1}\sigma &=& \mathit{true}\\
438+
\mathcal{C}\sem{c_1 \circledast c_2}\sigma
439+
&=& \mathcal{C}\sem{c_0}\sigma \circledast \mathcal{C}\sem{c_1}\sigma
440+
\end{array}
441+
\]
442+
443+
We write $\lift{\cdot} : \mathbb{B} \to \{0,1\}$ for the conversion of a
444+
Boolean into the natural number, so that $\lift{\mathcal{C}\sem{c}\sigma}$ is
445+
the numeric contribution of circuit $c$.
446+
394447
\subsection{Bit Heap Data Structure}
395-
A bit heap of width $w$ is a vector of $w$ columns, where the column at index $i$ holds the bits of weight $2^i$.
396-
Bits are implemented as an inductive type that we call \texttt{Circuit}.
397-
A \texttt{Circuit} describes a logical expression, that can be any binary operation, constant, or a binary variable.
398-
The bits represented by \texttt{Circuit} is merely symbolic and does not hold any value, it hold how a bit is computed.
399-
A value is obtained only under an environment, which assigns Boolean values to the input variables.
400-
We use $\bit{c}{\mathit{env}}$ for the value of the \texttt{Circuit} $c$ under the environment $\mathit{env}$.
401-
A \texttt{Column} is a set of such \texttt{Circuit} elements.
402-
The fact that a \texttt{Column} is a set and not a multiset opens up an optimization path that we discuss below.
448+
A bit heap of width $w$ is a vector of $w$ columns, where the column at index $k$ holds the bits of weight $2^k$.
449+
A \texttt{Column} is a set of \texttt{Circuit} elements.
450+
451+
\[
452+
\begin{array}{r@{\;}c@{\;}l@{\qquad}l}
453+
\Col \ni \gamma &::=& \{c_0,\dots,c_n\} \\[2pt]
454+
\Heap_w \ni h &::=& \{ \gamma_0,\dots,\gamma_{w-1} \} &
455+
\end{array}
456+
\]
457+
403458

404459
\begin{definition}[Bit heap]
405460
\label{def:bitheap}
@@ -408,119 +463,164 @@ \subsection{Bit Heap Data Structure}
408463
\[
409464
\mathtt{BitHeap}\;w \;\triangleq\; \mathtt{Vector}\;\mathtt{Column}\;w
410465
\]
411-
where the component at index $i$ holds the bits of weight $2^{i}$.
412466
\end{definition}
413467

468+
To compute the value of the bit heap, we compute the weighted sum of the value held by columns, which is the sum of the values of the bits under a certain environment.
469+
The value of the bit heap is computed in modular arithmetic. Therefore, all correctness statements below are stated as modulo $2^w$.
470+
471+
\[
472+
\begin{array}{r@{\;}c@{\;}l@{\qquad}l}
473+
\mathcal{G}\sem{\gamma}\sigma
474+
&=& \textstyle\sum_{c \in \gamma} \lift{\mathcal{C}\sem{c}\sigma}
475+
& \mathcal{G}\sem{\cdot}\sigma : \Col \to \mathbb{N}\\[6pt]
476+
\mathcal{H}\sem{h}\sigma
477+
&=& \bigl(\textstyle\sum_{k<w} 2^{k}\cdot\mathcal{G}\sem{h_k}\sigma\bigr)
478+
\bmod 2^{w}
479+
& \mathcal{H}\sem{\cdot}\sigma : \Heap_w \to \mathbb{Z}
480+
\end{array}
481+
\]
482+
414483
The value of a bit heap is obtained by summing each column and combining the columns with Horner's method:
415484
\begin{equation}
416-
\sum_{i=0}^{n} a_i x^i = a_0 + x \left( a_1 + x \left( a_2 + \cdots + x \, a_n \right) \right)
485+
\begin{split}
486+
\Bigl( \sum_{k=0}^{w-1} &\mathcal{G}\sem{h_k}\sigma \cdot x^{k} \Bigr) \bmod 2^{w}\\
487+
&= \Bigl( \mathcal{G}\sem{h_0}\sigma + x \bigl( \mathcal{G}\sem{h_1}\sigma
488+
+ x ( \mathcal{G}\sem{h_2}\sigma + \cdots\\
489+
&\qquad + x \, \mathcal{G}\sem{h_{w-1}}\sigma ) \bigr) \Bigr) \bmod 2^{w}
490+
\end{split}
417491
\end{equation}
418492

419-
By setting $x = 2$, and having $a_i$'s as the value of column at index $i$, we obtain the value of the bit heap.
420-
The Horner's method has a useful property that it can be implemented as a recursive function:
421-
422-
\begin{lean4}
423-
def HornersMethod (env : BitEnv) : List Column -> Nat
424-
| [] => 0
425-
| c :: rest => (c.eval env) + 2 * HornersMethod env rest
426-
\end{lean4}
493+
By setting $x = 2$, so that each column at index $k$ contributes its value $\mathcal{G}\sem{h_k}\sigma$ weighted by $2^{k}$, and reducing modulo $2^{w}$, we obtain the value of the bit heap.
494+
Horner's method is structurally recursive on the list of columns, which makes it convenient for induction in Lean.
427495

428-
This recursive structure makes this definition convenient for our proofs in Lean 4 ITP.
496+
\begin{table*}[t]
497+
\centering
498+
\caption{Compression steps. $\maj$ denotes the majority function
499+
$(i \wedge j) \vee (i \wedge l) \vee (j \wedge l)$.}
500+
\label{tab:steps}
501+
\begin{tabular}{@{}lll@{}}
502+
\toprule
503+
Step & Applicability & Effect \\
504+
\midrule
505+
$\HA(k,i,j)$
506+
& $i,j \in \gamma_k$, $i \neq j$
507+
& remove $i,j$ from $\gamma_k$;\;
508+
add $i \oplus j$ at $\gamma_k$;\;
509+
add $i \wedge j$ at $\gamma_{k{+}1}$ \\[2pt]
510+
$\FA(k,i,j,l)$
511+
& $i,j,l \in \gamma_k$, pairwise distinct
512+
& remove $i,j,l$ from $\gamma_k$;\;
513+
add $i \oplus j \oplus l$ at $\gamma_k$;\;
514+
add $\maj(i,j,l)$ at $\gamma_{k{+}1}$ \\
515+
\bottomrule
516+
\end{tabular}
517+
\end{table*}
429518

430519
Our framework is based on modular arithmetic, so correctness statements for operations on a bit heap hold only modulo $2^w$.
431-
The reason lies in the fixed nature of hardware, the number of bits is fixed and cannot be changed.
432520
That means when a carry is produced on the last column, it is dropped.
433521

434-
Adding a bit to the bit heap requires care for the case where the bit to-be-added is already in the bit heap, since \texttt{Column}s are defined as sets and sets do not allow duplicates of the same values.
435-
If the bit is already in the column $i$, we remove the existing copy and insert the bit into the next column, which is correct
436-
because $x + x = 2x$, and repeat if the next column also contains it.
437-
Effectively this results in building the bit heap in an optimized way by construction.
438-
Another important operation is the bit removal operation.
439-
Introduction of the removal operation forces us to use \texttt{Int} instead of \texttt{Nat}, since in bit removal the value of the bit heap can go to negative temporarily.
440-
441-
Two theorems describe the effect of adding and removing bits from the bit heap.
442-
Adding a bit $c$ to column $k$ increases the value by $2^k \cdot c$ (Theorem~\ref{thm:addBit}), and removing a bit $c$ from column $k$, provided $c$ is
443-
actually in that column, decreases it by $2^k \cdot c$ (Theorem~\ref{thm:removeBit}), both modulo $2^w$.
444-
The \texttt{evalMod} function is implemented as evaluation using Horner's method modulo $2^w$
522+
Two theorems describe the primitive operations. Adding a bit $c$ at
523+
column $k$ increases the value by $2^{k}\lift{\mathcal{C}\sem{c}\sigma}$
524+
(\ref{thm:addBit}). Removing a bit $c$ from column $k$, provided
525+
$c$ is present in that column, decreases it by the same amount
526+
(\ref{thm:removeBit}).
445527

446528
\begin{theorem}[\texttt{evalMod\_heap\_addBit}]
447529
\label{thm:addBit}
448-
For all $h : \mathtt{BitHeap}\;w$, $k : \mathbb{N}$, $c : \mathtt{Circuit}$
449-
and $\mathit{env} : \mathtt{BitEnv}$,
530+
For all $h : \Heap_w$, $k : \mathbb{N}$, $c : \Circ$ and
531+
$\sigma : \mathbb{N} \to \mathbb{B}$,
450532
\[
451-
\mathtt{evalMod}\big(\mathtt{addBit}(k, c, h),\, \mathit{env}\big)
533+
\mathcal{H}\sem{\mathtt{addBit}(k, c, h)}\sigma
452534
\;=\;
453-
\Big(\mathtt{evalMod}(h, \mathit{env}) + 2^{k}\cdot\bit{c}{\mathit{env}}\Big)
454-
\bmod 2^{w}
535+
\bigl(\mathcal{H}\sem{h}\sigma
536+
+ 2^{k}\cdot\lift{\mathcal{C}\sem{c}\sigma}\bigr) \bmod 2^{w}.
455537
\]
456538
\end{theorem}
457539

458540
\begin{theorem}[\texttt{evalMod\_heap\_removeBit}]
459541
\label{thm:removeBit}
460-
For all $h : \mathtt{BitHeap}\;w$, $k : \mathbb{N}$, $c : \mathtt{Circuit}$
461-
and $\mathit{env} : \mathtt{BitEnv}$ with $c \in h.\mathtt{get}(k)$,
542+
For all $h : \Heap_w$, $k : \mathbb{N}$, $c : \Circ$ and
543+
$\sigma : \mathbb{N} \to \mathbb{B}$ with $c \in h_k$,
462544
\[
463-
\mathtt{evalMod}\big(\mathtt{removeBit}(k, c, h),\, \mathit{env}\big)
545+
\mathcal{H}\sem{\mathtt{removeBit}(k, c, h)}\sigma
464546
\;=\;
465-
\Big(\mathtt{evalMod}(h, \mathit{env}) - 2^{k}\cdot\bit{c}{\mathit{env}}\Big)
466-
\bmod 2^{w}
547+
\bigl(\mathcal{H}\sem{h}\sigma
548+
- 2^{k}\cdot\lift{\mathcal{C}\sem{c}\sigma}\bigr) \bmod 2^{w}.
467549
\]
468550
\end{theorem}
469551

470-
On top of these theorems, we prove that applying a half adder and a full adder preserves the value of the bit heap.
471-
These two adders are the building blocks of our compression algorithms (Section~\ref{sec:compression})
552+
Since a \texttt{Column} is defined as a set, it cannot contain duplicates.
553+
Therefore, if the same bit is added to a \texttt{Column} at index k, we propagete the bit to the next column, since $2^{k}c + 2^{k}c = 2^{k+1}c$, and the propagation repeats if the next column already contains the circuit as well.
554+
Effectively this results in building the bit heap in an optimized way during construction.
555+
\autoref{thm:addBit} is proved by functional induction on \texttt{addBit}, following the carry propagation described above.
556+
557+
558+
\subsection{Compression Algorithms}
559+
\label{sec:compression}
560+
Compression reduces the height of a bit heap to two, after which a
561+
single \acrshort{cpa} produces the result \cite{application_specific_arithmetic}.
562+
The reduction is carried out by compressors, half adders ($2{:}2$),
563+
full adders ($3{:}2$), or in general any $N{:}M$ compressor with
564+
$N \ge M$. We formalize half and full adders, and build compressor trees
565+
from them. However, our framework can easily be extended for any compressor.
472566

473-
\begin{theorem}[\texttt{halfAdder\_correct\_mod}]
474-
\label{thm:halfAdder-correct}
475-
For all $h : \mathtt{BitHeap}\;w$, $k : \mathbb{N}$, $i, j : \mathtt{Circuit}$
476-
with $i, j \in h.\mathtt{get}(k)$ and $i \neq j$,
477567
\[
478-
\forall\, \mathit{env} : \mathtt{BitEnv},
479-
\mathtt{evalMod}\big(\mathtt{halfAdder}(k, i, j, h).\mathtt{heap},\, \mathit{env}\big)
480-
\;=\;
481-
\mathtt{evalMod}(h, \mathit{env})
568+
\begin{array}{r@{\;}c@{\;}l@{\qquad}l}
569+
\Step \ni s &::=& \HA(k,c,c) \mid \FA(k,c,c,c) &\\[2pt]
570+
\Chain \ni S &::=& [\,] \mid s :: S &
571+
\end{array}
482572
\]
483-
\end{theorem}
484573

485-
\begin{theorem}[\texttt{fullAdder\_correct\_mod}]
486-
\label{thm:fullAdder-correct}
487-
For all $h : \mathtt{BitHeap}\;w$, $k : \mathbb{N}$, $i, j, l : \mathtt{Circuit}$
488-
with $i, j, l \in h.\mathtt{get}(k)$ pairwise distinct,
574+
Each compression step records the type of adder, the column it acts on, and the bits it consumes.
575+
The effects of a single step on the heap with the applicability conditions are shown in Table~\ref{tab:steps}.
576+
Both applicability conditions are forced by Theorem~\ref{thm:removeBit}, which requires bits to be present and that they are pairwise distinct.
577+
Writing $s(h)$ for the heap obtained by applying step $s$ to $h$, we prove that each step preserves the value of the heap.
578+
579+
\begin{theorem}[\texttt{halfAdder\_correct\_mod}, \texttt{fullAdder\_correct\_mod}]
580+
\label{thm:step-correct}
581+
For all $h : \Heap_w$, $s : \Step$ and
582+
$\sigma : \mathbb{N} \to \mathbb{B}$, if $s$ is applicable to $h$ by obeying the conditions stated in \ref{tab:steps}, then
489583
\[
490-
\forall\, \mathit{env} : \mathtt{BitEnv},
491-
\mathtt{evalMod}\big(\mathtt{fullAdder}(k, i, j, l, h).\mathtt{heap},\, \mathit{env}\big)
492-
\;=\;
493-
\mathtt{evalMod}(h, \mathit{env})
584+
\mathcal{H}\sem{h}\sigma \;=\; \mathcal{H}\sem{s(h)}\sigma.
494585
\]
495586
\end{theorem}
496587

497-
A half adder on column $c$ with bits $i$ and $j$ removes both from the column, inserts $i \oplus j$ into column $c$ and $i \wedge j$ into column $c+1$.
498-
The correctness theorem for the half adder states that the resulting bit heap has the same value modulo $2^w$, under the hypothesis that $i$ and $j$ belong to column $c$ and that they are distinct.
499-
The membership hypothesis is required by the remove operation, and the uniqueness is needed to ensure we do not remove the same bit twice from the bit heap.
500-
We know the latter is already satisfied by our implementation of the bit insertion function, and the way we satisfy the former will be explained in \autoref{sec:compression}.
501-
To prove Theorem~\ref{thm:halfAdder-correct}), we rewrite insertion and removal theorems and the goal becomes an arithmetic identity on boolean values of $i$ and $j$, after which a case split on the boolean values closes the goal.
502-
The full adder is proved the same way (Theorem~\ref{thm:fullAdder-correct}), with an extra carry-in bit $k$, the sum becomes $i \oplus j \oplus k$, and a carry $(i \wedge j) \vee (i \wedge k) \vee (j \wedge k)$.
503-
These few definitions and theorems form the basis of our formalization and they provide all the machinery the compression algorithms require.
588+
With proofs being similar for both half and full adder cases, the proofs rewrite the Theorems \ref{thm:addBit} and \ref{thm:removeBit} until the
589+
goal becomes an arithmetic identity over the Boolean values of bits, after which a case split on the boolean values closes the goal.
504590

591+
A compression algorithm produces a chain of compressors.
592+
Applicability is a condition on the heap the step is applied to, so it must be checked against the intermediate bit heap, not the original.
505593

506-
\subsection{Compression Algorithms}
507-
\label{sec:compression}
508-
We implement Wallace and Dadda trees.
509-
594+
\begin{definition}[Well-formed chain]
595+
\label{def:wellformed-chain}
596+
A chain $s :: S$ is well-formed for $h$ if $s$ is applicable to $h$ and $S$ is well-formed for $s(h)$.
597+
\end{definition}
510598

511-
\subsubsection{Correctness of Composition of Adders}
599+
\begin{theorem}[\texttt{applyChain\_correct\_mod}]
600+
\label{thm:chain-correct}
601+
For all $h : \Heap_w$ and $S: \Chain$ is well-formed for $h$,
602+
\[
603+
\forall \sigma,\quad
604+
\mathcal{H}\sem{h}\sigma \;=\; \mathcal{H}\sem{S(h)}\sigma.
605+
\]
606+
\end{theorem}
512607

608+
\autoref{thm:chain-correct} is proven by induction on the chain, using Theorem~\ref{thm:step-correct} for the base case and the induction hypothesis for the tail of the chain.
513609

514-
Adder:
610+
In our framework, we separate the correctness of the compression from the generation of the chain.
611+
Theorem~\ref{thm:chain-correct} proves that any well-formed chain preserves the value of the heap.
612+
Therefore, any algorithm producing a well-formed chain is proven correct.
613+
That means adding a new algorithm does not require any new proof.
614+
We use this to implement Wallace and Dadda trees against a single correctness theorem.
515615

516-
ChainPreconditions:
517-
- applyChainSafe:
616+
For chains produced by a compression algorithm, we provide a checked application \texttt{applyChainSafe}, which tests applicability at each step and returns $\mathtt{none}$ on failure.
518617

519618
\subsection{From Bit Heaps to Bit-Vector Arithmetic}
520619
At the core of our formalization lies the correctness of Bit heap operations with respect to the semantics of bit-vector arithmetic.
521620

522621
\texttt{ArithCircuit}, \texttt{toBitHeap}, \texttt{denote}, \texttt{toBitHeap\_correct}, \texttt{compressed\_toBitHeap\_correct}
523622

623+
524624
\section{Integration}
525625

526626
\subsection{Datapath Synthesis Engine}

references.bib

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ @ARTICLE{wallace_tree
2626
keywords={Adders;Acceleration;Arithmetic;Hardware;Investments;Computer peripherals;Circuits;Power generation economics;Contracts;Physics computing},
2727
doi={10.1109/PGEC.1964.263830}}
2828

29+
@article{dadda_tree,
30+
author={Dadda, Luigi},
31+
journal={Alta Frequenza},
32+
title={Some Schemes for Parallel Multipliers},
33+
year={1965},
34+
volume={34},
35+
number={5},
36+
pages={349-356}}
37+
2938
@article{sca,
3039
author = {Konrad, Alexander and Scholl, Christoph},
3140
title = {Symbolic computer algebra for multipliers revisited - demonstrating the significance of order and phase optimization},

0 commit comments

Comments
 (0)