log_2 command in vhdl?

C

Carson

Hi,

Is there a command log_2 in vhdl?

For example, would be nice that by specifying the depth of the
memory module, it can figure out the required bit-width for the address
lines.

Thanks,

Carson
 
E

Egbert Molenkamp

There is no log2 function. In a similar situation I use the following
function.
Notice that the 'while loop' is more convenient but I know from at least one
synthesis tool that it does not support the while, therefor for loop is
used.
In the latter solution you must know the upper bound, e.g. 10 in thsi
example (for i in 0 to 10).

Egbert Molenkamp

function log2 (N : positive) return positive is -- N should be power of
two.
variable tmp, i_res : integer;
begin
tmp:=1 ; i_res:=0;
-- while tmp < N loop
-- tmp := tmp*2;
-- i:=i+1;
-- end loop;
for i in 0 to 10 loop
if tmp<N then
tmp:=tmp*2;
else
i_res:=i;
exit;
end if;
end loop;

assert 2**i_res=N report "N is not a power of 2" severity note;
return i_res;
end log2;
 
O

Olaf Petzold

Carson said:
Hi,

Is there a command log_2 in vhdl?

For example, would be nice that by specifying the depth of the
memory module, it can figure out the required bit-width for the address
lines.

Thanks,

Carson

Hi, here is my approach used for an generic mux. Rename the package
name maybe to math e.g.

library ieee;
use ieee.std_logic_1164.all;

package mux_g_pkg is
function log2_f(n : integer) return integer;
end package;

package body mux_g_pkg is

-- very simple log2 function
function log2_f(n : in integer) return integer is
variable i : integer := 0;
begin
while (2**i <= n) loop
i := i + 1;
end loop;
return i-1;
end log2_f;

end mux_g_pkg;

Regards,
Olaf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top