log_2 command in vhdl?

Discussion in 'VHDL' started by Carson, Oct 5, 2005.

  1. Carson

    Carson Guest

    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
    Carson, Oct 5, 2005
    #1
    1. Advertising

  2. 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;


    "Carson" <> wrote in message
    news:...
    > 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
    >
    Egbert Molenkamp, Oct 5, 2005
    #2
    1. Advertising

  3. Carson

    Olaf Petzold Guest

    Carson schrieb:
    > 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
    Olaf Petzold, Oct 5, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    3
    Views:
    4,796
    walala
    Sep 18, 2003
  2. Replies:
    2
    Views:
    8,651
    Jim Lewis
    Mar 21, 2006
  3. John
    Replies:
    9
    Views:
    374
    Kanenas
    Apr 13, 2005
  4. John

    quick question: \floor(\log_2(ix))

    John, Apr 11, 2005, in forum: C Programming
    Replies:
    22
    Views:
    738
    Chris Croughton
    Apr 15, 2005
  5. afd
    Replies:
    1
    Views:
    8,293
    Colin Paul Gloster
    Mar 23, 2007
Loading...

Share This Page