Discussion:
how to do log2(n) in verilog?
(too old to reply)
goannae
2006-07-05 13:07:49 UTC
Permalink
Hi,

I would like to parameterize a counter to count an n bit binary input. Thus
the size of the count is at lease log2(n) bits. I have difficulty to
calculate log2(n) in verilog. I am wondering if log2(n) can be done in
verilog as:
parameter InputLength = 8;
parameter CounterSize = log2(InputLength);
are not acceptable.

Thank you in advance,

Goanna
d***@gmail.com
2006-07-05 14:52:38 UTC
Permalink
Post by goannae
Hi,
I would like to parameterize a counter to count an n bit binary input. Thus
the size of the count is at lease log2(n) bits. I have difficulty to
calculate log2(n) in verilog. I am wondering if log2(n) can be done in
parameter InputLength = 8;
parameter CounterSize = log2(InputLength);
are not acceptable.
Thank you in advance,
Goanna
I think the following will work:

parameter InputLength = 8;
parameter CounterSize = 1<<InputLength - 1;

Correct me if I'm wrong or I misunderstood your question.
goannae
2006-07-06 02:55:55 UTC
Permalink
Thank you for your help. I did not put my question correctly. Please excuse
my bad English.
It would be clearer if I had asked how many bits are required to represent
an integer (eg, an integer 13 would require 4 bits).
The answer would be at least log2(n) bits where n is the integer. Then the
question was how to realise log2(n) in verilog.

Thanks again,

Goanna
Post by goannae
Post by goannae
Hi,
I would like to parameterize a counter to count an n bit binary input. Thus
the size of the count is at lease log2(n) bits. I have difficulty to
calculate log2(n) in verilog. I am wondering if log2(n) can be done in
parameter InputLength = 8;
parameter CounterSize = log2(InputLength);
are not acceptable.
Thank you in advance,
Goanna
parameter InputLength = 8;
parameter CounterSize = 1<<InputLength - 1;
Correct me if I'm wrong or I misunderstood your question.
mk
2006-07-05 18:21:57 UTC
Permalink
Post by goannae
Hi,
I would like to parameterize a counter to count an n bit binary input. Thus
the size of the count is at lease log2(n) bits. I have difficulty to
calculate log2(n) in verilog. I am wondering if log2(n) can be done in
parameter InputLength = 8;
parameter CounterSize = log2(InputLength);
are not acceptable.
They are if you define a log2 like this:

function integer log2;
input [31:0] value;
for (log2=0; value>0; log2=log2+1)
value = value>>1;
endfunction

You can also parameterize the size of the input value.
goannae
2006-07-06 02:57:17 UTC
Permalink
Thank you very much for your help, mate. It works fine. Much appreciated.

Goanna
Post by mk
Post by goannae
Hi,
I would like to parameterize a counter to count an n bit binary input. Thus
the size of the count is at lease log2(n) bits. I have difficulty to
calculate log2(n) in verilog. I am wondering if log2(n) can be done in
parameter InputLength = 8;
parameter CounterSize = log2(InputLength);
are not acceptable.
function integer log2;
input [31:0] value;
for (log2=0; value>0; log2=log2+1)
value = value>>1;
endfunction
You can also parameterize the size of the input value.
s***@cadence.com
2006-07-10 19:12:26 UTC
Permalink
Post by goannae
Hi,
I would like to parameterize a counter to count an n bit binary input. Thus
the size of the count is at lease log2(n) bits. I have difficulty to
calculate log2(n) in verilog.
In Verilog-2001, you can write your own functions that can be used in
constant expressions, as long as they abide by the restrictions imposed
for such "constant functions." Then you have to write the function
correctly.
This is clearly non-trivial, since the version originally published as
an
example in the Verilog-2001 standard was wrong.

In Verilog-2005, there is a built-in $clog2 system function that
computes
what you want and can be used in constant expressions. However, I
doubt
that anyone has implemented it yet.

Loading...