ucb-bar / chisel2-deprecated

chisel.eecs.berkeley.edu
387 stars 89 forks source link

Negative literal problem #650

Closed ascenium closed 8 years ago

ascenium commented 8 years ago

The statement:

class foo extends Module { val io = new Bundle { val out2 = UInt( OUTPUT, 32 ) } out2 := UInt( 0xffffffff, 32 ) }

Yields the error message "UInt can't represent negative literal -1 in class foo"

Apparently, it can't represent the positive number 4,294,967,296 either.

aswaterman commented 8 years ago

This is a Scala thing. 0xffffffff is of type Int, which is a 32-bit signed type and so has value -1.

There are many workarounds including using BigInt, e.g. by writing UInt((BigInt(1) << 32) - 1).

On Mon, Feb 8, 2016 at 3:46 PM, Robert Mykland notifications@github.com wrote:

The statement:

class foo extends Module { val io = new Bundle { val out2 = UInt( OUTPUT, 32 ) } out2 := UInt( 0xffffffff, 32 ) }

Yields the error message "UInt can't represent negative literal -1 in class foo"

Apparently, it can't represent the positive number 4,294,967,296 either

— Reply to this email directly or view it on GitHub https://github.com/ucb-bar/chisel/issues/650.

ascenium commented 8 years ago

Thanks for the workaround. I guess this is why a language like scala (via Java) with no fundamental unsigned types might not be the ideal basis for a language that represents hardware.

ccelio commented 8 years ago

I guess this is why a language like scala (via Java) with no fundamental unsigned types might not be the ideal basis for a language that represents hardware.

You're just punting the problem a little further down the field - what are you suppose to do if you want to represent 33-bits? Or 65-bits? No matter what language is used as the base, you're going to hit edge cases when you rely on the native integer format types to specify literals.

Chisel's UInt type accepts Scala Ints, BigInts, and Strings. UInt( "hffffffff", 32 ) should work just fine for your needs. And if you find yourself needing to represent 33-bit literals, UInt("h1ffffffff",33) follows naturally.

ascenium commented 8 years ago

Thanks for the much more readable and maintainable approach to specifying unsigned numbers in Chisel! My hostility towards scala in my previous post was undeserved, and probably has more to do with past episodes of hair-ripping frustration at porting various bit manipulations from C++ to Java. Like with learning any language, there is the process (that I'm in the middle of) where you're learning to be articulate enough in the new language to not paint yourself into ghastly corners.

ccelio commented 8 years ago

No worries! =)