-
Notifications
You must be signed in to change notification settings - Fork 597
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide require message for negative widths (#4008)
(cherry picked from commit 751a4cf) # Conflicts: # core/src/main/scala/chisel3/Width.scala
- Loading branch information
1 parent
daede76
commit dfe9e68
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package chisel3 | ||
|
||
object Width { | ||
def apply(x: Int): Width = KnownWidth(x) | ||
def apply(): Width = UnknownWidth() | ||
} | ||
|
||
sealed abstract class Width { | ||
type W = Int | ||
def min(that: Width): Width = this.op(that, _ min _) | ||
def max(that: Width): Width = this.op(that, _ max _) | ||
def +(that: Width): Width = this.op(that, _ + _) | ||
def +(that: Int): Width = this.op(this, (a, b) => a + that) | ||
@deprecated( | ||
"The width of shift-right now differs by type, use unsignedShiftRight and signedShiftRight", | ||
"Chisel 7.0.0" | ||
) | ||
def shiftRight(that: Int): Width = this.op(this, (a, b) => 0.max(a - that)) | ||
def unsignedShiftRight(that: Int): Width = this.op(this, (a, b) => 0.max(a - that)) | ||
def signedShiftRight(that: Int): Width = this.op(this, (a, b) => 1.max(a - that)) | ||
def dynamicShiftLeft(that: Width): Width = | ||
this.op(that, (a, b) => a + (1 << b) - 1) | ||
|
||
def known: Boolean | ||
def get: W | ||
protected def op(that: Width, f: (W, W) => W): Width | ||
} | ||
|
||
sealed case class UnknownWidth() extends Width { | ||
def known: Boolean = false | ||
def get: Int = None.get | ||
def op(that: Width, f: (W, W) => W): Width = this | ||
override def toString: String = "" | ||
} | ||
|
||
sealed case class KnownWidth(value: Int) extends Width { | ||
require(value >= 0, s"Widths must be non-negative, got $value") | ||
def known: Boolean = true | ||
def get: Int = value | ||
def op(that: Width, f: (W, W) => W): Width = that match { | ||
case KnownWidth(x) => KnownWidth(f(value, x)) | ||
case _ => that | ||
} | ||
override def toString: String = s"<${value.toString}>" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters