log2(N)

Discussion in 'VHDL' started by ALuPin, Aug 27, 2004.

  1. ALuPin

    ALuPin Guest

    What library do I need to declare the following signal:

    constant L: integer:=log2(N); --ceiling log2(N)


    Thank you for your help.

    Kind regards
     
    ALuPin, Aug 27, 2004
    #1
    1. Advertising

  2. On 27 Aug 2004 03:24:25 -0700, (ALuPin) wrote:

    >What library do I need to declare the following signal:
    >
    >constant L: integer:=log2(N); --ceiling log2(N)


    Sadly, that one is missing... but this will work
    in both synthesis and simulation:


    package usefuls is
    --- find minimum number of bits required to
    --- represent N as an unsigned binary number
    ---
    function log2_ceil(N: natural) return positive;
    end;

    package body usefuls is
    --- find minimum number of bits required to
    --- represent N as an unsigned binary number
    ---
    function log2_ceil(N: natural) return positive is
    begin
    if N < 2 then
    return 1;
    else
    return 1 + log2_ceil(N/2);
    end if;
    end;
    end;

    Converting my tail-recursive function into
    an iterative implementation is left as an exercise
    for the student :)
    --
    Jonathan Bromley, Consultant

    DOULOS - Developing Design Know-how
    VHDL, Verilog, SystemC, Perl, Tcl/Tk, Verification, Project Services

    Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, BH24 1AW, UK
    Tel: +44 (0)1425 471223 mail:
    Fax: +44 (0)1425 471573 Web: http://www.doulos.com

    The contents of this message may contain personal views which
    are not the views of Doulos Ltd., unless specifically stated.
     
    Jonathan Bromley, Aug 27, 2004
    #2
    1. Advertising

  3. ALuPin

    David Bishop Guest

    This code is from the proposed floating point packages.

    -- Integer version of the "log2" command
    -- Synthisable
    function log2(A : natural) return natural is
    begin
    for I in 1 to 30 loop -- Works for up to 32 bit integers
    if(2**I > A) then return(I-1);
    end if;
    end loop;
    return(30);
    end function log2;

    ALuPin wrote:

    > What library do I need to declare the following signal:
    >
    > constant L: integer:=log2(N); --ceiling log2(N)
    >
    >
    > Thank you for your help.
    >
    > Kind regards


    --
    NAME: David W. Bishop INTERNET:
     
    David Bishop, Aug 29, 2004
    #3
  4. ALuPin

    Michael Guest

    Dave,

    When do you think the VHDL-200X effort will be finalized? Are we going
    to have a VHDL 2004 standard? 2005?

    Just curious

    David Bishop wrote:
    > This code is from the proposed floating point packages.
    >
    > -- Integer version of the "log2" command
    > -- Synthisable
    > function log2(A : natural) return natural is
    > begin
    > for I in 1 to 30 loop -- Works for up to 32 bit integers
    > if(2**I > A) then return(I-1);
    > end if;
    > end loop;
    > return(30);
    > end function log2;
    >
    > ALuPin wrote:
    >
    >> What library do I need to declare the following signal:
    >>
    >> constant L: integer:=log2(N); --ceiling log2(N)
    >>
    >>
    >> Thank you for your help.
    >>
    >> Kind regards

    >
    >
     
    Michael, Sep 1, 2004
    #4
  5. ALuPin

    texvnirap

    Joined:
    Sep 17, 2009
    Messages:
    1
    Correction

    function log2_ceil(N: natural) return positive is
    begin
    if N < 2 then
    return 1;
    else
    return 1 + log2_ceil(N/2);
    end if;
    end;

    The above function is not correct (try N=2).

    I think this works:


    function log2_ceil(N : integer) return integer is

    begin

    if (N <= 2) then
    return 1;​
    else
    if (N mod 2 = 0) then
    return 1 + log2_ceil(N/2);​
    else
    return 1 + log2_ceil((N+1)/2);​
    end if;​
    end if;​
    end function log2_ceil;
     
    texvnirap, Sep 17, 2009
    #5
    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. Brian Schroeder

    Math.log2 ?

    Brian Schroeder, Sep 2, 2004, in forum: Ruby
    Replies:
    4
    Views:
    420
    Phil Tomson
    Sep 3, 2004
  2. Phrogz
    Replies:
    8
    Views:
    283
    Morton Goldberg
    Feb 8, 2007
Loading...

Share This Page