Go Back   Velocity Reviews > Newsgroups > VHDL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

VHDL - log2(N)

 
Thread Tools Search this Thread
Old 08-27-2004, 11:24 AM   #1
Default log2(N)


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
  Reply With Quote
Old 08-27-2004, 11:48 AM   #2
Jonathan Bromley
 
Posts: n/a
Default Re: log2(N)
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
  Reply With Quote
Old 08-29-2004, 08:34 PM   #3
David Bishop
 
Posts: n/a
Default Re: log2(N)
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
  Reply With Quote
Old 09-01-2004, 01:39 AM   #4
Michael
 
Posts: n/a
Default Re: log2(N)
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
  Reply With Quote
Old 09-17-2009, 06:59 PM   #5
texvnirap
Junior Member
 
Join Date: Sep 2009
Posts: 1
Default 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
texvnirap is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46