Skip to content

Commit b7334a3

Browse files
committed
Add source info to floor, ceil, round methods
1 parent d2f429b commit b7334a3

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

src/main/scala/fixedpoint/BinaryPoint.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// - UnknownBinaryPoint
77
// - HasBinaryPoint
88
//
9-
// HasBinaryPoint uses Bundle as a self-type as opposed to Bits used in Chisel's code.
9+
// HasBinaryPoint uses Record as a self-type as opposed to Bits used in Chisel's code.
1010

1111
package fixedpoint
1212

src/main/scala/fixedpoint/FixedPoint.scala

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
// This file contains the definitions of FixedPoint class and companion object. Much of Chisel's original code
4-
// is reused, but Bundle is inherited from instead of Bits. Relevant methods from Bits and Chisel's FixedPoint
4+
// is reused, but Record is inherited from instead of Bits. Relevant methods from Bits and Chisel's FixedPoint
55
// have also been implemented in order to maximally replicate the original FixedPoint interface.
66

77
// Notes:
@@ -195,8 +195,6 @@ sealed class FixedPoint private[fixedpoint] (width: Width, private var _inferred
195195

196196
def binaryPoint: BinaryPoint = _inferredBinaryPoint
197197

198-
override def litValue: BigInt = data.litValue
199-
200198
private def requireKnownBP(message: => Any = "Unknown binary point is not supported in this operation"): Unit = {
201199
require(_inferredBinaryPoint.isInstanceOf[KnownBinaryPoint], message)
202200
}
@@ -287,6 +285,27 @@ sealed class FixedPoint private[fixedpoint] (width: Width, private var _inferred
287285
override def do_abs(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
288286
FixedPoint.fromData(_inferredBinaryPoint, data.abs)
289287

288+
def do_floor(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = {
289+
requireKnownBP()
290+
// Set the fractional part to zeroes
291+
val floored = Cat(data >> binaryPoint.get, 0.U(binaryPoint.get.W)).asSInt
292+
FixedPoint.fromData(binaryPoint, floored)
293+
}
294+
295+
def do_ceil(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = {
296+
requireKnownBP()
297+
// Get a number with the fractional part set to ones
298+
val almostOne = ((1 << binaryPoint.get) - 1).U(width)
299+
// Add it to the number and floor it
300+
(this + FixedPoint.fromData(binaryPoint, almostOne.asSInt)).floor
301+
}
302+
303+
def do_round(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = {
304+
requireKnownBP()
305+
// Add 0.5 to the number and then floor it
306+
(this + 0.5.F(binaryPoint)).floor
307+
}
308+
290309
def do_===(that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool =
291310
comparativeOp(that, _ === _)
292311

@@ -326,6 +345,12 @@ sealed class FixedPoint private[fixedpoint] (width: Width, private var _inferred
326345

327346
def unary_-% : FixedPoint = macro SourceInfoTransform.noArg
328347

348+
def floor: FixedPoint = macro SourceInfoTransform.noArg
349+
350+
def ceil: FixedPoint = macro SourceInfoTransform.noArg
351+
352+
def round: FixedPoint = macro SourceInfoTransform.noArg
353+
329354
def ===(that: FixedPoint): Bool = macro SourceInfoTransform.thatArg
330355

331356
def =/=(that: FixedPoint): Bool = macro SourceInfoTransform.thatArg
@@ -417,6 +442,8 @@ sealed class FixedPoint private[fixedpoint] (width: Width, private var _inferred
417442

418443
override def litOption: Option[BigInt] = data.litOption
419444

445+
override def litValue: BigInt = data.litValue
446+
420447
override def toString: String = {
421448
litToDoubleOption match {
422449
case Some(value) => s"FixedPoint$width$binaryPoint($value)"
@@ -429,25 +456,4 @@ sealed class FixedPoint private[fixedpoint] (width: Width, private var _inferred
429456
s"FixedPoint$width$binaryPoint$suffix"
430457
}
431458
}
432-
433-
def floor: FixedPoint = {
434-
requireKnownBP()
435-
// Set the fractional part to zeroes
436-
val floored = Cat(data >> binaryPoint.get, 0.U(binaryPoint.get.W)).asSInt
437-
FixedPoint.fromData(binaryPoint, floored)
438-
}
439-
440-
def ceil: FixedPoint = {
441-
requireKnownBP()
442-
// Get a number with the fractional part set to ones
443-
val almostOne = ((1 << binaryPoint.get) - 1).U(width)
444-
// Add it to the number and floor it
445-
(this + FixedPoint.fromData(binaryPoint, almostOne.asSInt)).floor
446-
}
447-
448-
def round: FixedPoint = {
449-
requireKnownBP()
450-
// Add 0.5 to the number and then floor it
451-
(this + 0.5.F(binaryPoint)).floor
452-
}
453459
}

0 commit comments

Comments
 (0)