## Arithmetic Tips & Tricks #1

August 1, 2007Every single one of us had sometime or another to design a block utilizing some arithmetic operations. Usually we use the necessary operator and forget about it, but since we are “hardware men” (should be said with pride and a full chest) we know there is much more going under the hood. I intend to have a series of posts dealing specifically with arithmetic implementation tips and tricks. There are plenty of them, I don’t know all, probably not even half. So if you got some interesting ones please send them to me and I will post them with credits.

Let’s start. This post will explain 2 of the most obvious and simple ones.

**Multiplying by a constant**

Multipliers are extremely area hungry and thus when possible should be eliminated. One of the classic examples is when multiplying by a constant.

Assume you need to multiply the result of register A by a factor, say 5. Instead of instantiating a multiplier, you could “shift and add”. 5 in binary is 101, just add A to A00 (2 trailing zeros, have the effect of multiplying by 4) and you have the equivalent of multiplying by 5, since what you basically did was 4A+A = 5A.

This is of course very simplistic, but when you write your code, make sure the constant is not passed on as an argument to a function. It might be that the synthesis tool knows how to handle it, but why take the risk.

**Adding a bounded value**

Sometimes (or even often), we need to add two values where one is much smaller than the other and *bounded*. For example adding a 3 bit value to a 32 bit register. The idea here is not to be neat and pad the 3 bit value by leading zeros and create by force a 32 bit register from it. Why? adding two 32 bit values instantiates full adder logic on all the 32 bits, while adding 3 bits to 32 will infer a full adder logic on the 3 LSBs and an increment logic (which is much faster and cheaper) on the rest of the bits. I am quite positive that today’s synthesis tools know how to handle this, but again, it is good practice to always check the synthesis result and see what came up. If you didn’t get what you wanted it is easy enough to force it by coding it in such a way.

For multiplication by a constant, shift-and-subtract is also useful. 8A – A = 7A requires fewer operations than 4A + 2A + A = 7A.

The first integer you can’t use a single add/subtract to effect a multiplication by is 11. Sequence A129523 in the Online Encyclopedia of Integer Sequences has a list of all of the numbers requiring no more than 1 add/subtract, although it includes the trivial cases of straight power-of-two multiplications as well.

by Phil R August 1, 2007 at 10:28 pmThat is correct, sometimes it is more efficient to subtract rather than add.

As for the second point, even 2 additions (which should instantiate a carry-save adder) are cheaper than a multiplication.

thanks for the input and reference!!!

by Nir Dahan August 2, 2007 at 5:42 amPlease google Reduce adder graph RAG (algorithm) .It descibes about using adders to implment multiplication with constant

Manan

by Manan October 15, 2007 at 10:15 pm“……Assume you need to multiply the result of register A by a factor, say 5. Instead of instantiating a multiplier, you could “shift and add”. 5 in binary is 101, just add A to A00 (2 trailing zeros, have the effect of multiplying by 4) and you have the equivalent of multiplying by 5, since what you basically did was 4A+A = 5A……..”

Could you please tell me how can we make it with full and half adders? Thanx!

by wiza January 11, 2010 at 7:44 pm