RE: [sv-bc] RE: clog2 problem

From: Brad Pierce <Brad.Pierce_at_.....>
Date: Wed Jan 23 2008 - 14:16:21 PST
Isn't asking "How many bits does it take to store this value?" somewhat
like asking "Is 323 a random number?"

Doesn't the answer depend at least on the set of possible values and on
which bit-encodings of that set are allowed?

-- Brad  

-----Original Message-----
From: owner-sv-bc@eda.org [mailto:owner-sv-bc@eda.org] On Behalf Of
Arturo Salz
Sent: Wednesday, January 23, 2008 1:41 PM
To: Greg Jaxon
Cc: sv-bc@eda.org
Subject: RE: [sv-bc] RE: clog2 problem

Actually, I stand corrected by both Greg and Shalom.

As pointed by Greg, using N |1'b1 is safer to avoid context and sign
extensions.
Shalom pointed out that if N==1 $clog(N) returns 0, which is not the
expected result.

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) And that doesn't
deal with negative numbers.

	Arturo

-----Original Message-----
From: owner-sv-bc@eda.org [mailto:owner-sv-bc@eda.org] On Behalf Of Greg
Jaxon
Sent: Wednesday, January 23, 2008 11:42 AM
To: Arturo Salz
Cc: Bresticker, Shalom; sv-bc@eda.org
Subject: Re: [sv-bc] RE: clog2 problem

Arturo is right, but thanks to the madness of context-sensitive
expression width rules, even N|1 is dangerous when N is an expression.
     (expr) | signed'(1'b1)
may work better in a macro or other type-independent setting.
Note that 1'sb1 would be sign-extended to match width (bad!), whereas
signed'(1'b1) will 0-extend the literal before cloaking it with
signedness (so as to not poison expr with unsignedness).

Greg

Arturo Salz wrote:
> Shalom,
> 
>  
> 
> No. I meant "N | 1" (the bitwise or operator). I didn't use + because 
> addition could cause a wrap-around to 0 and distort the result.
> 
> Using "N | 1" works for all positive numbers, even 0. (btw, I changed 
> the subject line)
> 
>  
> 
>             Arturo
> 
>  
> 
>
------------------------------------------------------------------------
> 
> *From:* Bresticker, Shalom [mailto:shalom.bresticker@intel.com]
> *Sent:* Tuesday, January 22, 2008 2:05 AM
> *To:* Arturo Salz; sv-bc@eda.org
> *Subject:* RE: [sv-bc] Agenda: January 21, 2008 SV-BC Meeting (NOTE:
NEW
> CALL LOGISTICS)
> 
>  
> 
> Arturo,
> 
>  
> 
> Did you mean $clog2(N+1) ?
> 
>  
> 
> Thanks,
> 
> Shalom
> 
>      
> 
>      
> 
>     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 general expression needed to determine the minimum number of
>     bits would be *$clog2(N | 1) *not  *$clog2(N)*.
> 
> 
> --
> This message has been scanned for viruses and dangerous content by 
> *MailScanner* <http://www.mailscanner.info/>, and
is
> believed to be clean.


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



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

This archive was generated by hypermail 2.1.8 : Wed Jan 23 2008 - 14:17:21 PST