RE: [sv-bc] RE: clog2 problem

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

>So to avoid both corner cases N==1 or N = '1 (all ones), you'd need to
>construct a slightly more complicated macro:
>    `define BitWidth(N)  (N=='1) ? 1 : $clog( N|1'b1)

Um, (N=='1) is checking for N being all ones, not N being 1.  You would
have to change that to (N==1'b1) instead.

While that might work correctly, it is hardly intuitive.  Adding 1 is
intuitive, to add in the extra value for 0.  Users are already used to
having to subtract 1 to go the opposite direction.  To avoid overflow,
do something to increase the size of the intermediate value.


>And that doesn't deal with negative numbers.

Whose log is mathematically undefined.  The LRM says that the argument
of $clog2 is treated as unsigned, and specifically defines the result
for 0 to be 0 (since it would otherwise be undefined).

Greg raised the issue that using an unsigned 1'b1 might result in
intermediate calculations being done unsigned, when there might be
negative signed intermediate values that ultimately resulted in a
positive signed value.

This sort of complexity is a good reason for the LRM not to try to
tell the user how to compute the number of bits required to represent
a particular value in an unsigned binary representation.  It specifies
what $clog2 does, which is what is needed.  It is up to the user to
figure out how to use it.

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:52:24 2008

This archive was generated by hypermail 2.1.8 : Wed Jan 23 2008 - 14:52:35 PST