You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reducing integer multiplication into bitwise operations is a well known approach in order to accelerate things
like matrix multiplication, see xy for instance.
One of the examples in xy is the following: Given a matrix $X \in \{ 0,1,2 \}^{m \times p}$, we want to calculate
$X X^\top$ as fast as possible. Since matrix multiplication is basically just a couple of scalar products, we only
consider the latter for the ease of clarity. On the other hand, the scalar product only consists of two operations:
integer multiplication and integer addition. The former is computationally expensive, so we'd like to
implement the integer multiplication $a \cdot b$ with $a, b \in \{0,1,2\}$ through bitwise operations.
One reasonable choice is using the two bitwise operations (which are cheap to compute) and two simple lookup tables:
Instead of doing it by hand, we could formulate this task as an integer optimization problem.
Let's assume a set of numbers $I$ is given (think of $I = \{ 0, 1, 2\}$) with $N := \max I$. First, we notice that
$a\, \text{AND} \,b \leq \max\{a,b\} =: N_1$ for all $a,b \in I$
$a\, \text{OR} \,b \leq \max\{a,b\} =: N_2$ for all $a,b \in I$
$a\, \text{XOR} \,b \leq \max\{a,b\} =: N_3$ for all $a,b \in I$.
Now we introduce three integer variables $l_1 \in [-N, N^2]^{N_1}$, $l_2 \in [-N, N^2]^{N_2}$, $l_3 \in [-N, N^2]^{N_3}$
representing our desired lookup tables. In detail, the integer variable $l_{i,k}$ represents the value of the $i$-th lookup
table at index $k$. In order to choice between the lookup tables, we further introduce three integer variables $s_1,s_2,s_3 \in \{-1, 0, 1\}$.
Next we define the three sets $B_1 = \{ a \, \text{AND}\, b \mid a, b \in I \}$, $B_2 = \{ a \, \text{XOR}\, b \mid a, b \in I \}$
and $B_3 = \{ a \, \text{OR}\, b \mid a, b \in I \}$.
We want to use as few lookup tables as possible and the as much as possible values inside these tables should be zero.
Hence, we want to solve the following optimization problem:
$$
\min \sum s_j^2 + \sum l_{j,k}^2
$$
$$
a \cdot b = \sum_{k \in B_1} s_1 \cdot l_{i,k} + \sum_{k \in B_2} s_2 \cdot l_{2,k} + \sum_{k \in B_3} s_3 \cdot l_{3,k}
\quad \forall a, b \in I
$$
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters