# VHDL "compile time" math?

I have a generic VHDL entity and I have this code:

--- in testbench
const bits : integer := 13;          -- number of bits
const pow: integer := 8192;          -- 2^bits
const squared : integer := 67108864; -- pow^2


In where I pass those 3 constants to the entity. What I would really like is something like:

--- in testbench
const bits: integer := 13;

--- in architecture (generic bits:integer)
const pow : integer := 2^bits;
const squared : integer := pow^2;


Since bits is known at synthesis time is there any way to do it without using hardware?

• Do you want the final circuit to be able to do these calculations, or do you just want to define constant values (constant during operation of the circuit) inside the description ? Commented Mar 2, 2016 at 10:23
• @MaximGi constant values (edited the answer) Commented Mar 2, 2016 at 10:23
• This is not VHDL code .... Commented Mar 2, 2016 at 10:30
• @rhunes Ok. It is important you precise this because it will make a huge difference in the final layout, since doing these calculations would implement multipliers all over the place which are fairly complex components Commented Mar 2, 2016 at 10:36

The VHDL power operator is **.

So your code should look like this:

-- in testbench
constant bits    : integer := 13;

-- in architecture
constant pow     : integer := 2**bits;
constant squared : integer := pow**2;

• Is that supposed to generate hardware to calculate that or because is marked as const the math will be performed before synthesation? Any chance you could do the same with math functions (e.g round) ? Commented Mar 2, 2016 at 10:35
• All constants are evaluated at elaboration time (synthesis time). In most cases no "extra hardware" is generated. Special cases are for example: constants, which are mapped to ROMs. You can even do floating point calculations and rounding as long as it results in a constant. Commented Mar 2, 2016 at 10:44
• You can even do time-based calculations involving clock periods and baud rates, then round to a natural constant. Much better than magic numbers in counters, timers and UARTs,
– user16324
Commented Mar 2, 2016 at 14:23