Skip to content

Commit

Permalink
foldLeft instead of /: (#2354)
Browse files Browse the repository at this point in the history
  • Loading branch information
ingallsj authored Mar 27, 2020
1 parent bb10dbe commit 6ac7132
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/rocket/PMP.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class PMP(implicit p: Parameters) extends PMPReg {

class PMPHomogeneityChecker(pmps: Seq[PMP])(implicit p: Parameters) {
def apply(addr: UInt, pgLevel: UInt): Bool = {
((true.B, 0.U.asTypeOf(new PMP)) /: pmps) { case ((h, prev), pmp) =>
pmps.foldLeft((true.B, 0.U.asTypeOf(new PMP))) { case ((h, prev), pmp) =>
(h && pmp.homogeneous(addr, pgLevel, prev), pmp)
}._1
}
Expand All @@ -160,7 +160,7 @@ class PMPChecker(lgMaxSize: Int)(implicit val p: Parameters) extends Module
pmp0.cfg.w := default
pmp0.cfg.x := default

val res = (pmp0 /: (io.pmp zip (pmp0 +: io.pmp)).reverse) { case (prev, (pmp, prevPMP)) =>
val res = (io.pmp zip (pmp0 +: io.pmp)).reverse.foldLeft(pmp0) { case (prev, (pmp, prevPMP)) =>
val hit = pmp.hit(io.addr, io.size, lgMaxSize, prevPMP)
val ignore = default && !pmp.cfg.l
val aligned = pmp.aligned(io.addr, io.size, lgMaxSize, prevPMP)
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/util/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package object util {
val truncIdx =
if (idx.isWidthKnown && idx.getWidth <= log2Ceil(x.size)) idx
else (idx | UInt(0, log2Ceil(x.size)))(log2Ceil(x.size)-1, 0)
(x.head /: x.zipWithIndex.tail) { case (prev, (cur, i)) => Mux(truncIdx === i.U, cur, prev) }
x.zipWithIndex.tail.foldLeft(x.head) { case (prev, (cur, i)) => Mux(truncIdx === i.U, cur, prev) }
}
}

Expand All @@ -40,15 +40,15 @@ package object util {
def rotate(n: UInt): Seq[T] = {
require(isPow2(x.size))
val amt = n.padTo(log2Ceil(x.size))
(x /: (0 until log2Ceil(x.size)))((r, i) => (r.rotate(1 << i) zip r).map { case (s, a) => Mux(amt(i), s, a) })
(0 until log2Ceil(x.size)).foldLeft(x)((r, i) => (r.rotate(1 << i) zip r).map { case (s, a) => Mux(amt(i), s, a) })
}

def rotateRight(n: Int): Seq[T] = x.takeRight(n) ++ x.dropRight(n)

def rotateRight(n: UInt): Seq[T] = {
require(isPow2(x.size))
val amt = n.padTo(log2Ceil(x.size))
(x /: (0 until log2Ceil(x.size)))((r, i) => (r.rotateRight(1 << i) zip r).map { case (s, a) => Mux(amt(i), s, a) })
(0 until log2Ceil(x.size)).foldLeft(x)((r, i) => (r.rotateRight(1 << i) zip r).map { case (s, a) => Mux(amt(i), s, a) })
}
}

Expand Down Expand Up @@ -134,14 +134,14 @@ package object util {

def rotateRight(n: UInt): UInt = {
val amt = n.padTo(log2Ceil(x.getWidth))
(x /: (0 until log2Ceil(x.getWidth)))((r, i) => Mux(amt(i), r.rotateRight(1 << i), r))
(0 until log2Ceil(x.getWidth)).foldLeft(x)((r, i) => Mux(amt(i), r.rotateRight(1 << i), r))
}

def rotateLeft(n: Int): UInt = if (n == 0) x else Cat(x(x.getWidth-1-n,0), x(x.getWidth-1,x.getWidth-n))

def rotateLeft(n: UInt): UInt = {
val amt = n.padTo(log2Ceil(x.getWidth))
(x /: (0 until log2Ceil(x.getWidth)))((r, i) => Mux(amt(i), r.rotateLeft(1 << i), r))
(0 until log2Ceil(x.getWidth)).foldLeft(x)((r, i) => Mux(amt(i), r.rotateLeft(1 << i), r))
}

// compute (this + y) % n, given (this < n) and (y < n)
Expand Down

0 comments on commit 6ac7132

Please sign in to comment.