Skip to content

Commit 35dda16

Browse files
committed
Make round method work properly with 0.BP
* Add checks with 0.BP to tests
1 parent b7334a3 commit 35dda16

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/main/scala/fixedpoint/FixedPoint.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ sealed class FixedPoint private[fixedpoint] (width: Width, private var _inferred
303303
def do_round(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = {
304304
requireKnownBP()
305305
// Add 0.5 to the number and then floor it
306-
(this + 0.5.F(binaryPoint)).floor
306+
(this + 0.5.F(1.BP)).floor.setBinaryPoint(binaryPoint.get)
307307
}
308308

309309
def do_===(that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool =
@@ -417,11 +417,9 @@ sealed class FixedPoint private[fixedpoint] (width: Width, private var _inferred
417417
val diff = that - current
418418
FixedPoint.fromData(
419419
that.BP,
420-
(if (diff >= 0) {
421-
data << diff
422-
} else {
423-
data >> -diff
424-
}).asSInt,
420+
(if (diff > 0) data << diff
421+
else if (diff < 0) data >> -diff
422+
else data).asSInt,
425423
Some(width + diff)
426424
)
427425
case UnknownBinaryPoint =>

src/test/scala/FixedPointSpec.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class FixedPointFloorTester extends BasicTester {
158158
assert(55.5.F(8.W, 2.BP).floor === 55.0.F(8.W, 2.BP))
159159
assert(-4.0.F(2.BP).floor === -4.0.F(2.BP))
160160
assert(0.125.F(8.W, 4.BP).floor === 0.0.F(8.W, 4.BP))
161+
assert(3.0.F(0.BP).floor === 3.0.F(0.BP))
161162
stop()
162163
}
163164

@@ -166,6 +167,7 @@ class FixedPointCeilTester extends BasicTester {
166167
assert(55.5.F(8.W, 2.BP).ceil === 56.0.F(8.W, 2.BP))
167168
assert(-4.0.F(2.BP).ceil === -4.0.F(2.BP))
168169
assert(0.125.F(8.W, 4.BP).ceil === 1.0.F(8.W, 4.BP))
170+
assert(3.0.F(0.BP).ceil === 3.0.F(0.BP))
169171
stop()
170172
}
171173

@@ -174,6 +176,7 @@ class FixedPointRoundTester extends BasicTester {
174176
assert(25.5.F(8.W, 2.BP).round === 26.0.F(8.W, 2.BP))
175177
assert(-4.0.F(2.BP).round === -4.0.F(2.BP))
176178
assert(0.125.F(8.W, 3.BP).round === 0.0.F(8.W, 3.BP))
179+
assert(3.0.F(0.BP).round === 3.0.F(0.BP))
177180
stop()
178181
}
179182

0 commit comments

Comments
 (0)