Skip to content

Commit

Permalink
Fixed point width inference was wrong when binary points didn't align.
Browse files Browse the repository at this point in the history
  • Loading branch information
shunshou authored and edwardcwang committed Jul 25, 2017
1 parent 48c0322 commit cc69a72
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions chiselFrontend/src/main/scala/chisel3/core/Bits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -825,12 +825,36 @@ sealed class FixedPoint private (width: Width, val binaryPoint: BinaryPoint, lit
/** subtract (no growth) operator */
final def -% (that: FixedPoint): FixedPoint = macro SourceInfoTransform.thatArg

def do_+& (that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
binop(sourceInfo, FixedPoint((this.width max that.width) + 1, this.binaryPoint max that.binaryPoint), AddOp, that)
def do_+& (that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = {
(this.width, that.width, this.binaryPoint, that.binaryPoint) match {
case (KnownWidth(thisWidth), KnownWidth(thatWidth), KnownBinaryPoint(thisBP), KnownBinaryPoint(thatBP)) =>
val thisIntWidth = thisWidth - thisBP
val thatIntWidth = thatWidth - thatBP
val newBinaryPoint = thisBP max thatBP
val newWidth = (thisIntWidth max thatIntWidth) + newBinaryPoint + 1
binop(sourceInfo, FixedPoint(newWidth.W, newBinaryPoint.BP), AddOp, that)
case _ =>
val newBinaryPoint = this.binaryPoint max that.binaryPoint
binop(sourceInfo, FixedPoint(UnknownWidth(), newBinaryPoint), AddOp, that)
}
}

def do_+% (that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
(this +& that).tail(1).asFixedPoint(this.binaryPoint max that.binaryPoint)
def do_-& (that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
binop(sourceInfo, FixedPoint((this.width max that.width) + 1, this.binaryPoint max that.binaryPoint), SubOp, that)
def do_-& (that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = {
(this.width, that.width, this.binaryPoint, that.binaryPoint) match {
case (KnownWidth(thisWidth), KnownWidth(thatWidth), KnownBinaryPoint(thisBP), KnownBinaryPoint(thatBP)) =>
val thisIntWidth = thisWidth - thisBP
val thatIntWidth = thatWidth - thatBP
val newBinaryPoint = thisBP max thatBP
val newWidth = (thisIntWidth max thatIntWidth) + newBinaryPoint + 1
binop(sourceInfo, FixedPoint(newWidth.W, newBinaryPoint.BP), SubOp, that)
case _ =>
val newBinaryPoint = this.binaryPoint max that.binaryPoint
binop(sourceInfo, FixedPoint(UnknownWidth(), newBinaryPoint), SubOp, that)
}
}

def do_-% (that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
(this -& that).tail(1).asFixedPoint(this.binaryPoint max that.binaryPoint)

Expand Down

0 comments on commit cc69a72

Please sign in to comment.