Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Log2(x) for vhdl?

7,643 views
Skip to first unread message

Allan Herriman

unread,
Nov 9, 2001, 6:24:32 AM11/9/01
to
On Fri, 09 Nov 2001 22:01:41 +1100, Russell Shaw
<rjs...@iprimus.com.au> wrote:

>Hi all,
>
>There's an exponent operator (**), but no log-base-2 (from what
>i could see). Such a function (with integer result) would be useful
>would it not?:
>
>constant MAXADDR: natural:=1000;
>.
>.
>.
>signal addrcntr:unsigned(LOG2(MAXADDR) downto 0);
>
>LOG2 should round upwards.
>
>Could a function be written to do it?

You mean like the ones in this thread?
http://groups.google.com/groups?threadm=iWOC4.143%249o.293380%40news.magma.ca

Regards,
Allan.

Ray Andraka

unread,
Nov 9, 2001, 9:54:54 AM11/9/01
to
THis is one I wrote a few years ago. I keep it in my common library so
that it is always available.

function Log2( input:integer ) return integer is
variable temp,log:integer;
begin
temp:=input;
log:=0;
while (temp /= 0) loop
temp:=temp/2;
log:=log+1;
end loop;
return log;
end function log2;


Russell Shaw wrote:

> Hi all,
>
> There's an exponent operator (**), but no log-base-2 (from what
> i could see). Such a function (with integer result) would be useful
> would it not?:
>
> constant MAXADDR: natural:=1000;
> .
> .
> .
> signal addrcntr:unsigned(LOG2(MAXADDR) downto 0);
>
> LOG2 should round upwards.
>
> Could a function be written to do it?

--
--Ray Andraka, P.E.
President, the Andraka Consulting Group, Inc.
401/884-7930 Fax 401/884-7950
email r...@andraka.com
http://www.andraka.com

"They that give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety."
-Benjamin Franklin, 1759


Russell Shaw

unread,
Nov 9, 2001, 7:27:01 PM11/9/01
to
Thanks RA, that works good. I thought of using a loop, but
thought a synthesis tool wouldn't like things like 'while'...

Ray Andraka

unread,
Nov 9, 2001, 8:13:31 PM11/9/01
to
This function is not intended to synthesize directly into hardware, rather it
is used to generate constants for synthesized hardware. Typically for sizing
a bus to a maximum value or setting the number of layers in a tree
structure. A hardware Log2 is a first '1' detect and encoder, or cna be done
with a normalizing shifter.

Russell Shaw wrote:

--

Phoenix Ch

unread,
Nov 11, 2001, 9:23:03 PM11/11/01
to
Hi,
When I try to connect the nntp: news.magma.ca,
I get the error like this:
502 Permission Denied - Denied (Typhoon v1.2.3)
what should I do?

Allan Herriman

unread,
Nov 11, 2001, 10:48:42 PM11/11/01
to
On Mon, 12 Nov 2001 10:23:03 +0800, Phoenix Ch <mp...@sohu.com> wrote:

>Hi,
>When I try to connect the nntp: news.magma.ca,
>I get the error like this:
>502 Permission Denied - Denied (Typhoon v1.2.3)
>what should I do?

Trying to connect to someone else's (private) news server is not
likely to be successful.
I suggest using Google, or some other public news archive.
The URL I posted
http://groups.google.com/groups?threadm=iWOC4.143%249o.293380%40news.magma.ca
works. (I just tested it.)

Regards,
Allan.

Ian Smith

unread,
Nov 12, 2001, 6:09:39 AM11/12/01
to
You could extend the idea with a log floor and ceiling functions; or, put
another way, the floor is the log rounded down and the ceiling the log
rounded up.


-- purpose: computes ceil(log2(n))
function log2ceil (n : integer) return integer is

variable m, p : integer;
begin
m := 0;
p := 1;
for i in 0 to n loop
if p < n then
m := m + 1;
p := p * 2;
end if;
end loop;
return m;

end log2ceil;

-- purpose: computes floor(log2(n))
function log2floor (n : integer) return integer is

variable m, p : integer;
begin
m := -1;
p := 1;
for i in 0 to n loop
if p <= n then
m := m + 1;
p := p * 2;
end if;
end loop;
return m;

end log2floor;


0 new messages