D
davew
Guest
Would someone please perform a sanity check on my logic regarding this
function because it seems to be widely referenced/recommended:
document:
http://www.sunburst-design.com/papers/CummingsHDLCON2001_Verilog2001.pdf
recommends the following function to calculate ceil(log2(x)).
function integer clogb2;input [31:0] value;
for (clogb2=0; value>0; clogb2=clogb2+1)
value = value>>1;
endfunction
I tried to use this in my design (using Altera Quartus as it happens)
but I get an incorrect result for a value such as 8 i.e. where log2(x)
has a purely integer result.
As I see it, this function would return the same result for x=8 or x=9
i.e. 4.
Surely, ceil(log2(8)) = 3?
and ceil(log2(9)) = 4?
I used this modified version which works for me:
function integer clogb2;
input [31:0] value;
value=value-1;
<--------------------------------------------------------------added
for (clogb2=0; value>0; clogb2=clogb2+1)
value = value>>1;
endfunction
It wouldn't make any sense to call this function with a value of 0,
since this is not mathematically a good idea. If called with a value
of 1, then I think the result would be correct also, i.e. 0.
function because it seems to be widely referenced/recommended:
document:
http://www.sunburst-design.com/papers/CummingsHDLCON2001_Verilog2001.pdf
recommends the following function to calculate ceil(log2(x)).
function integer clogb2;input [31:0] value;
for (clogb2=0; value>0; clogb2=clogb2+1)
value = value>>1;
endfunction
I tried to use this in my design (using Altera Quartus as it happens)
but I get an incorrect result for a value such as 8 i.e. where log2(x)
has a purely integer result.
As I see it, this function would return the same result for x=8 or x=9
i.e. 4.
Surely, ceil(log2(8)) = 3?
and ceil(log2(9)) = 4?
I used this modified version which works for me:
function integer clogb2;
input [31:0] value;
value=value-1;
<--------------------------------------------------------------added
for (clogb2=0; value>0; clogb2=clogb2+1)
value = value>>1;
endfunction
It wouldn't make any sense to call this function with a value of 0,
since this is not mathematically a good idea. If called with a value
of 1, then I think the result would be correct also, i.e. 0.