问题
I have recently started work in scala, and am required to create an implementation of MD5. It is my understanding that MD5 requires unsigned types, which scala does not come with. As I will soon begin Chisel, which does have unsigned types, I decided to implement its library. Everything appears good so far, except when doing the below bitwise operations, my F value becomes -271733879, which causes an error "Caused by: java.lang.IllegalArgumentException: requirement failed: UInt literal -271733879 is negative" as UInts can't be negative.
if(i<16){
F = ((B & C) | ((~B) & D))
g = i
}
There is more to the error message, but it is just the trace list of different libraries and classes that had an error because of this error, and thus I did not post it because I didn't think it was important. If it was, I can edit this and post it all.
My B, C, and D values are equal to the lower case equivalents listed below, and it is the first time through the for loop so they have not yet updated.
var a0 : UInt = UInt(0x67452301)
var b0 : UInt = UInt(0xefcdab89)
var c0 : UInt = UInt(0x98badcfe)
var d0 : UInt = UInt(0x10325476)
Any Help would be greatly appreciated.
回答1:
For the sake of my answer, I am using the Chisel 3 preferred 123.U
style for specifying literals rather than the Chisel 2 UInt(123)
style, but this answer works for either.
There are several ways you could do this:
- Use Scala Long (put
L
at end of literal)val myUInt = 0x98badcfeL.U
- This obviously won't work for larger than 64-bit
- Use Scala BigInt
val myUInt = BigInt("98badcfe", 16).U
- Use Chisel's shorthand for constructing BigInts from Strings
val myUInt = "x98badcfe".U
- hex = x | h, dec = d, oct = o, bin = b
来源:https://stackoverflow.com/questions/44507807/chisel-uint-negative-value-error