standard function for calculating the number of bits of a natural number?

Discussion in 'VHDL' started by Frank Buss, Jul 29, 2006.

  1. Frank Buss

    Frank Buss Guest

    I'm using this function for calculating the number of bits needed to encode
    a natural number:

    function num_bits(n: natural) return natural is
    begin
    if n > 0 then
    return 1 + num_bits(n / 2);
    else
    return 1;
    end if;
    end num_bits;

    I wonder if there is already such a function in the IEEE.* libraries or
    some special VHDL operator.

    --
    Frank Buss,
    http://www.frank-buss.de, http://www.it4-systems.de
    Frank Buss, Jul 29, 2006
    #1
    1. Advertising

  2. Frank Buss

    David Bishop Guest

    Re: standard function for calculating the number of bits of a naturalnumber?

    Frank Buss wrote:
    > I'm using this function for calculating the number of bits needed to encode
    > a natural number:
    >
    > function num_bits(n: natural) return natural is
    > begin
    > if n > 0 then
    > return 1 + num_bits(n / 2);
    > else
    > return 1;
    > end if;
    > end num_bits;
    >
    > I wonder if there is already such a function in the IEEE.* libraries or
    > some special VHDL operator.


    Try this one:

    function log2 (A : NATURAL) return NATURAL is
    begin
    for I in 1 to 30 loop -- Works for up to 32 bits
    if (2**I > A) then return(I-1);
    end if;
    end loop;
    return(30);
    end function log2;
    David Bishop, Jul 30, 2006
    #2
    1. Advertising

  3. Frank Buss

    Frank Buss Guest

    David Bishop wrote:

    > Try this one:
    >
    > function log2 (A : NATURAL) return NATURAL is
    > begin
    > for I in 1 to 30 loop -- Works for up to 32 bits
    > if (2**I > A) then return(I-1);
    > end if;
    > end loop;
    > return(30);
    > end function log2;


    This is just another self-written function, which could be written with a
    "while" loop for >32 bits. But I've found a solution:

    use IEEE.MATH_REAL.ALL;

    integer(trunc(log2(real(value)))) + 1

    But this could cause problems, depending on the mantissa size of floating
    point numbers in the VHDL implementation and when "value" is near 2**n
    (with n=1,2,3...), e.g. if 2**32=4294967296 is represented as 4.294e9,
    because then the result is 32, but you'll need 33 bits for storing the
    number 2**32. Would be nice to have a log2 and looks like something like
    this is defined in some Xilinx VHDL library files, but not in a standard
    VHDL IEEE library.

    Looks like I have to create my own package with such utility functions.

    --
    Frank Buss,
    http://www.frank-buss.de, http://www.it4-systems.de
    Frank Buss, Jul 31, 2006
    #3
  4. Re: standard function for calculating the number of bits of a naturalnumber?

    Frank Buss wrote:

    > Looks like I have to create my own package with such utility functions.


    I have collected some of my vector length functions here:

    http://home.comcast.net/~mike_treseler/min_vec_len.vhd

    -- Mike Treseler
    Mike Treseler, Jul 31, 2006
    #4
    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. GGG
    Replies:
    10
    Views:
    12,510
    Donar
    Jul 6, 2006
  2. sarmin kho
    Replies:
    2
    Views:
    816
    A. Lloyd Flanagan
    Jun 15, 2004
  3. Miki Tebeka
    Replies:
    1
    Views:
    432
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  4. Dale Dellutri
    Replies:
    9
    Views:
    1,065
    dfighter
    Apr 4, 2009
  5. Erik the Red
    Replies:
    4
    Views:
    167
    Chris Pine
    Jul 29, 2005
Loading...

Share This Page