RE: [sv-bc] RE: clog2 problem

From: Arturo Salz <Arturo.Salz_at_.....>
Date: Wed Jan 23 2008 - 15:09:14 PST
Steve,

But, your last statement is precisely the point that originated this
discussion. Users are looking for a constant function that can be used
to declare a bit vector large enough to store a particular value. If
this functionality was built into the language then they would just be
able to use it - like most other auxiliary functions such as $bits,
$left. etc....

The problem is that such a function does not exist in the language, The
closest thing is $clog2, which almost does what they need, but not
exactly. When the users misread the LRM thinking that $clog2 actually
returns what they expected - which it didn't - they filed a bug report
asking to change the semantics of $clog2 to do what they need. I know
that the LRM is correct, strictly speaking, but, it is not hard to see
how an average user might misinterpret the LRM and stumble into this.
The fix would be simple, first add a clarification that $clog2 does not
return the width needed to store a number. This could be followed by an
enhancement to add a function that computes this value.

	Arturo

-----Original Message-----
From: owner-sv-bc@eda.org [mailto:owner-sv-bc@eda.org] On Behalf Of
Steven Sharp
Sent: Wednesday, January 23, 2008 2:52 PM
To: Greg.Jaxon@synopsys.COM; Arturo.Salz@synopsys.COM
Cc: sv-bc@eda.org
Subject: RE: [sv-bc] RE: clog2 problem


>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.


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Wed Jan 23 15:09:43 2008

This archive was generated by hypermail 2.1.8 : Wed Jan 23 2008 - 15:10:02 PST