Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion core/src/main/scala/chisel3/internal/firrtl/IR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ private[chisel3] object ir {
val unsigned = if (n < 0) (BigInt(1) << width.get) + n else n
s"asSInt(${ULit(unsigned, width).name})"
}
def minWidth: Int = (if (w.known) 0 else 1) + n.bitLength

// Special case for 0 which can be specified to zero-width (but defaults to 1 bit).
def minWidth: Int = if (n == 0 && w.known) 0 else 1 + n.bitLength

def cloneWithWidth(newWidth: Width): this.type = {
SLit(n, newWidth).asInstanceOf[this.type]
Expand Down
40 changes: 40 additions & 0 deletions src/test/scala/chiselTests/SIntOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,44 @@ class SIntOpsSpec extends ChiselPropSpec with Utils {
-5.S(8.W).pad(16).litValue should be(-5)
-5.S(8.W).pad(16).getWidth should be(16)
}
<<<<<<< HEAD:src/test/scala/chiselTests/SIntOps.scala
||||||| parent of db931617 (Fix SInt literals to reject too small of widths (#4786)):src/test/scala-2/chiselTests/SIntOps.scala

property("Casting a SInt literal to a Bundle should maintain the literal value") {
class SimpleBundle extends Bundle {
val x = UInt(4.W)
val y = UInt(4.W)
}
val blit = -23.S.asTypeOf(new SimpleBundle)
blit.litOption should be(Some(0x29))
blit.x.litOption should be(Some(2))
blit.y.litOption should be(Some(9))
}
=======

property("Casting a SInt literal to a Bundle should maintain the literal value") {
class SimpleBundle extends Bundle {
val x = UInt(4.W)
val y = UInt(4.W)
}
val blit = -23.S.asTypeOf(new SimpleBundle)
blit.litOption should be(Some(0x29))
blit.x.litOption should be(Some(2))
blit.y.litOption should be(Some(9))
}

property("SInt literals with too small of a width should be rejected") {
// Sanity checks.
0.S.getWidth should be(1)
0.S(0.W).getWidth should be(0)
-1.S.getWidth should be(1)
1.S.getWidth should be(2)
// The real check.
-2.S.getWidth should be(2)
an[IllegalArgumentException] shouldBe thrownBy(-2.S(1.W))
0xde.S.getWidth should be(9)
an[IllegalArgumentException] shouldBe thrownBy(0xde.S(8.W))
an[IllegalArgumentException] shouldBe thrownBy(0xde.S(4.W))
}
>>>>>>> db931617 (Fix SInt literals to reject too small of widths (#4786)):src/test/scala-2/chiselTests/SIntOps.scala
}
Loading