[sv-bc] clog2 problem

From: Steven Sharp <sharp_at_.....>
Date: Wed Jan 23 2008 - 14:30:11 PST
>From: "Arturo Salz" <Arturo.Salz@synopsys.com>

>The verbiage of the $clog2 function is contradictory and misleading.

Whether the verbiage is misleading is a matter of opinion.  The
verbiage is not contradictory, and is completely correct.

>The second paragraph has led some users to believe that $clog2 returns
>the minimum number of bits needed to store a particular value, but, that
>is not true if $clog2 is indeed ceil( log2( N ) ).

The second paragraph does not say that it returns the minimum number of
bits needed to store a particular value.  It says that it returns the
minimum number of bits necessary to represent a given number of states.
There are N+1 different values in the range [0:N], so it takes $clog2(N+1)
bits to represent all values in the range [0:N].  Most users are aware
of this at some level, since they write ranges like [0:N-1] all the time,
knowing that there are N values in that range.

It wouldn't hurt if the paragraph said that representing the values in
the range [0:N-1] was an example of representing N values, to remind
users of this fact.  However, what the paragraph says is correct.


>For example:
>
> 
>
>integer result;
>
>result = $clog2(n);
>
> 
>
 The general
>expression needed to determine the minimum number of bits would be
>$clog2(N | 1) not  $clog2(N). The example seems to reinforce this idea.

I am not sure how the example reinforces this idea, since it just shows
how to call the function, and says nothing about what the argument
represents.  You seem to be suggesting that all uses of this function
would be of the form $clog2(n|1), so that any other use is misleading.
But if I want to know the address width required for a 1024-element
RAM, that is $clog2(1024).  Users generally pass down the size of the
memory, not the highest address that must be represented.

Also, you are incorrect that the number of bits to represent N would
be $clog2(N | 1).  Mathematically it is $clog2(N+1).  Your concern with
overflow in computer arithmetic has led to a formula that gets the wrong
answer in a smaller case.  It takes 1 bit to represent the value 1.  My
formula of $clog2(1+1) gives the correct value of 1 bit.  Your formula
of $clog2(1|1) gives an incorrect value of 0 bits.  While you might be
more likely to run into a value that overflows than a value of 1, you
can always deal with overflow by forcing the addition to be done in a
wider width.

>The second paragraph's intent seems to be informational only, that is,
>the $clog2 function can be used *as an aid* to compute the minimum
>vector width.

Again, the paragraph is correct.  The function can be used to compute
the minimum vector width for a given number of states, which may be
used as an aid to compute the minimum vector width to hold a given
value, which requires a number of states equal to the value-1.

It may be helpful to add to the explanatory text in the second paragraph,
to help users who are rusty on their math.  However, no change is required
for correctness.


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 Jan 23 14:30:37 2008

This archive was generated by hypermail 2.1.8 : Wed Jan 23 2008 - 14:30:49 PST