Re: [sv-bc] Query for sysytem function $clog2

From: Steven Sharp <sharp_at_.....>
Date: Wed Feb 20 2008 - 12:23:31 PST
>From: Sumay Guin <sumay@cal.interrasystems.com>

>Though $clog2 is not a SV specific function, but I am not sure where 
>should I ask my quaries. So I am using SV-BC
>alias. If it is not proper body to discuss then please let me know.

Yes, it is the proper place.

>verilog LRM 1364-2005 section 17.11.1 says  that " This ( $clog2 ) 
>system function can be used to compute the minimum
>address width necessary to address a memory of a given size or the 
>minimum vector width necessary to represent
>a given number of states".

It also defines it as the ceiling of the log base 2 (i.e. the log
base 2 rounded up to the nearest integer).
 
>Consider the scenario,
>      
>       module Top;
>        localparam b = $clog2(-1); // is the result 32  ?

Yes, since the argument is treated as unsigned.  Thus the argument
is (2**32)-1, for which the log2 is slightly less than 32.

>        localparam c = $clog2(1);  //  is the result 1  ?

No, the result should be 0.  The log2 of 1 is 0 (since 2**0=1).

>        localparam d = $clog2(100); // is the result 7 ?

Yes, since the log2 of 100 is between 6 and 7.

>        localparam e = $clog2(32); //  is the result 5  ? though no. of 
>bits to represent 32 is 6

The result is 5, since the log2 of 32 is 5 (because 2**5=32).

Also note that it takes 5 address bits for a memory with 32 locations,
and it takes 5 bits to represent 32 distinct states (such as the values
in the range 0 to 31).  Because 0 is an extra state that has to be
represented in binary, the number of bits required to represent N in
binary is $clog2(N+1).

>Stanandard simulator behaviour is conflictiing and confusing for the 
>case. For e.g. some of them
>giving 6 for $clog2(32). What is the corret result?

The correct result is 5, so some of them are wrong.

Steven Sharp
sharp@cadence.com


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Wed Feb 20 12:25:00 2008

This archive was generated by hypermail 2.1.8 : Wed Feb 20 2008 - 12:27:43 PST