• Keine Ergebnisse gefunden

Developing a Specific Design Flow

7. SyReC Building Blocks

7.3. Expression Operations

7.3.2. Arithmetic Operations

Arithmetic operations tend to be the most complicated operations, especially regarding the extension to arbitrary bit widths of the input variables. Many realizations of adders, multipliers, etc. in reversible logic have been proposed, but most of them are limited to certain bit widths (see e.g. [GW15, ME13, GK14]). The building blocks proposed here are far from optimal regarding gate cost, but automatically expandable to any bit width.

Addition and Subtraction

To calculate the expression a + b, a set of new lines z with constant input value 0 is added to the circuit. Then, the addition can be done by “copying” the value of ato the new lines and increasing it by b, which is equivalent to executing the statements z ^=

a; z += b. To realize this statements, the building blocks for the assignments described in Sec. 7.2 can be used. As a result, the building block for the addition is composed of the building blocks for XOR assignment and increase, as shown in Fig. 7.9a.

The subtraction is implemented in a similar fashion, as shown in Fig. 7.9b. Fora -b, the value ofais first “copied” and then decreased byb.

a0 a0

a1 a1

... ...

an−1 an−1

0 (a + b)0

0 (a + b)1

... ...

0 (a + b)n−1

...

+=

+=... +=

b0 b0

b1 b1

... ...

bn−1 bn−1

. ..

. ..

(a)a + b

a0 a0

a1 a1

... ...

an−1 an−1

0 (a - b)0

0 (a - b)1

... ...

0 (a - b)n−1

...

-=

-=... -=

b0 b0

b1 b1

... ...

bn−1 bn−1

. ..

. ..

(b)a - b

Figure 7.9.: Building Blocks for Addition and Subtraction

Multiplication

The multiplication is realized using partial products. For example, the expression a * b, withaand b having a bit width of three, is calculated as follows.

( b2b1b0)·a0 + ( b2b1b0) ·a1

+ (b2b1b0) ·a2 z2z1z0

Note that the bit width of the result is the same as the inputs’, so only the lower bits of the arithmetic result are provided. For calculating the upper bits of the multiplication, the expressiona *> bis used, as described in the next section.

The building block implementing the multiplication is shown in Fig. 7.10. The first block is a controlled XOR assignment, i.e. b is assigned to z iff a0 = 1. Corresponding to the first line in the example calculation, this is equal to z = a0 ·b. The next block implements the next step by calculating zn−1zn−2. . . z1 += a1 ·bn−2. . . b1b0, which is equal toz += a1·(b <<1). Continuing this scheme, all partial products are added up to the final multiplication result.

7.3. Expression Operations

a0 a0

a1 a1

... ...

an−2 an−2

an−1 an−1

z0= 0 (a * b)0

z1= 0 (a * b)1

... ...

zn−2= 0 (a * b)n−2

zn−1= 0 (a * b)n−1

ˆ=

ˆ=...

ˆ=

ˆ=

...

+=...

+=

+=

...

. ..

. ..

. ..

+=

+= +=

b0 b0

b1 b1

... ...

bn−2 bn−2

bn−1 bn−1

Figure 7.10.: Building Block fora * b

Upper Bits of Multiplication

To compute the upper bits of a multiplication, the whole product is calculated, i.e. if both input variables have a bit width of n, a set of 2n new lines is used to compute the result. Like in the previous section, partial products are applied, as shown in the following for a bit width of three.

( b2b1b0)·a0

+ ( b2 b1b0) ·a1 + (b2 b1 b0) ·a2

z5z4z3z2z1z0

Although the complete setz ist needed for the calculation, the actual result of a *>

bis found on the “upper half”z5z4z3.

Fig. 7.11 shows the implementation of the full multiplication. Again, each block calculates one partial product and adds it to the result. But since no bits are cut off here, all blocks (except the first) have the same size. For example, when computing only the lower bits of the multiplication, z += a1·(b << 1) is equal to zn−1zn−2. . . z1 +=

a1·bn−2. . . b1b0, where bn−1 is cut off. When computing the full multiplication, z +=

a1·(b <<1) is equal tozn+1znzn−1. . . z1 += a1·bn−1bn−2. . . b1b0. As can be seen, one more bit of band two more bits of z are involved. The bit zn+1 needs to be included in theincrease block in case a carry is produced by adding the respective partial product.

An increase block as described in Sec. 7.2.2 does not compute this carry value, so a slightly modified version is applied. Fig. 7.12 shows this building block, where a line and two gates (highlighted in gray) are added to calculate cn−1.

a0 a0

a1 a1

... ...

an−2 an−2

an−1 an−1

z0 = 0 (a * b)0

z1 = 0 (a * b)1

... ...

zn−1 = 0 (a * b)n−1

zn = 0 (a *> b)0

zn+1 = 0 (a *> b)1

... ...

z2n−2 = 0 (a *> b)n−2

z2n−1 = 0 (a *> b)n−1

ˆ=

...

ˆ=

ˆ=

...

...

+=

+=

+=

... +=

. ..

. . .

. ..

. ..

...

... +=

+=

+=

+=

...

+=

+=

... +=

+=

b0 b0

b1 b1

... ...

bn−2 bn−2

bn−1 bn−1

Figure 7.11.: Building Block fora *> b

. ..

. ..

a0 a0

a1 a1

a2 a2

... ...

an−1 an−1

b0 b0

b1 b1

b2 b2

... ...

bn−1 bn−1

0 cn−1

. ..

. .. bn−2 . ..b3 b3 . ..

. .. bn−2

an−2

. ..

. ..

. .. calculateci

Figure 7.12.: Building Block fora += bwith Carry Out

7.3. Expression Operations

q0= 0 (a / b)0

... ...

qn−2= 0 (a / b)n−2

qn−1= 0 >= (a / b)n−1

>=

>=

...

...

a0 (a % b)0

... ...

an−2 (a % b)n−2

an−1 = = (a % b)n−1

=

...

...

=

=

=

b0 b0

b1 b1

b2 b2

... ...

bn−1 bn−1

. .. . ..

. ..

. ..

(a) Calculating Quotient and Remainder

q0= 0 (a / b)0

... ...

qn−2= 0 (a / b)n−2

qn−1= 0 (a / b)n−1

a0 a0

... ...

an−2 an−2

an−1 an−1

...

... ...

%

%

%

/

/ /

...

... + = + = + = . ..

. ..

. .. + = + =

+ =

b0 b0

b1 b1

b2 b2

... ...

bn−1 bn−1

(b)a / b

0 a % b

0 a / b

ˆ= %

/

a a

b b

(c)a % b

Figure 7.13.: Building Blocks for Division and Modulo

Division and Modulo

The result of a division (a / b) is the quotient, while the result of a modulo operation (a % b) is the remainder of the division. Naturally, both operations can be realized with the same building block. The underlying idea is the standard algorithm for long division, as shown in the following example fora= 111 andb= 010.

1 1 1 / 010 = 011

−1 0 0 1 1

−0 1 0 0 0 1

First,a2 is compared tob. Obviously,a2= 1 is smaller thanb= 10, sobdoes not “go into”a2. As a result, the first bit of the quotient is 0. Next,a2a1 is compared tob. Since a2a1 = 11 is not smaller thanb, the second bit of the quotient is 1, and b is subtracted from a2a1, resulting in a2a1. In the last step, a2a1a0 is compared to b. As b goes into

011, the third bit of the quotient is 1, andb is subtracted froma2a1a0, resulting in the remainder 001.

Fig. 7.13a shows the building block implementing this algorithm. A set of new lines q with constant input value 0 is used to calculate the quotient, while the remainder is computed on the lines ofa. The first step is comparingan−1 tob. Naturally, an−1 can only be greater than or equal tob, if bn−1bn−2. . . b2b1 = 0. For this reason, NOT gates are added to the lines ofbn−1bn−2. . . b2b1 and used as control lines for the comparison of an−1 and b0. So iff bn−1bn−2. . . b2b1 = 0 and an−1 >=b0, i.e. b goes into an−1, the value of qn−1 is set to 1 and b0 is subtracted from an−1 (which is equal to subtracting b). Otherwise,qn−1 remains 0. A NOT gate is added to restore the original value ofb1 for the next step. Then, iffbn−1bn−2. . . b2 = 0 andan−1an−2 >=b1b0, the value ofqn−2 is set to 1 andb1b0 (equal to b) is subtracted froman−1an−2. This scheme is continued untilbis compared to and possibly subtracted from an−1an−2. . . a1a0, which completes the calculation.

While this building block computes both the quotient and the remainder, it does not preserve the input value of a. Thus, additional effort is necessary for the desired implementation of the division and modulo operations. Fig. 7.13b shows the building block used to compute a / b. Here, the original value of ais restored by adding b to the remainder where it was subtracted before, i.e. reversing the subtractions.

The implementation of a % b is shown in Fig. 7.13c. Since the new value of ais the result here, the original value is preserved by “copying” it to another set of new lines before the calculation is done.