Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed point width inference was wrong when binary points didn't align. #590

Merged
merged 1 commit into from
Jul 25, 2017
Merged
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
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