diff options
| author | chick | 2020-08-14 19:47:53 -0700 |
|---|---|---|
| committer | Jack Koenig | 2020-08-14 19:47:53 -0700 |
| commit | 6fc742bfaf5ee508a34189400a1a7dbffe3f1cac (patch) | |
| tree | 2ed103ee80b0fba613c88a66af854ae9952610ce /src/main/scala/firrtl/constraint | |
| parent | b516293f703c4de86397862fee1897aded2ae140 (diff) | |
All of src/ formatted with scalafmt
Diffstat (limited to 'src/main/scala/firrtl/constraint')
| -rw-r--r-- | src/main/scala/firrtl/constraint/Constraint.scala | 2 | ||||
| -rw-r--r-- | src/main/scala/firrtl/constraint/ConstraintSolver.scala | 120 | ||||
| -rw-r--r-- | src/main/scala/firrtl/constraint/Inequality.scala | 6 | ||||
| -rw-r--r-- | src/main/scala/firrtl/constraint/IsAdd.scala | 37 | ||||
| -rw-r--r-- | src/main/scala/firrtl/constraint/IsFloor.scala | 16 | ||||
| -rw-r--r-- | src/main/scala/firrtl/constraint/IsKnown.scala | 4 | ||||
| -rw-r--r-- | src/main/scala/firrtl/constraint/IsMax.scala | 30 | ||||
| -rw-r--r-- | src/main/scala/firrtl/constraint/IsMin.scala | 37 | ||||
| -rw-r--r-- | src/main/scala/firrtl/constraint/IsMul.scala | 25 | ||||
| -rw-r--r-- | src/main/scala/firrtl/constraint/IsNeg.scala | 12 | ||||
| -rw-r--r-- | src/main/scala/firrtl/constraint/IsPow.scala | 10 | ||||
| -rw-r--r-- | src/main/scala/firrtl/constraint/IsVar.scala | 3 |
12 files changed, 148 insertions, 154 deletions
diff --git a/src/main/scala/firrtl/constraint/Constraint.scala b/src/main/scala/firrtl/constraint/Constraint.scala index 247593ee..1a3bc21a 100644 --- a/src/main/scala/firrtl/constraint/Constraint.scala +++ b/src/main/scala/firrtl/constraint/Constraint.scala @@ -12,7 +12,7 @@ trait Constraint { /** Trait for constraints with more than one argument */ trait MultiAry extends Constraint { - def op(a: IsKnown, b: IsKnown): IsKnown + def op(a: IsKnown, b: IsKnown): IsKnown def merge(b1: Option[IsKnown], b2: Option[IsKnown]): Option[IsKnown] = (b1, b2) match { case (Some(x), Some(y)) => Some(op(x, y)) case (_, y: Some[_]) => y diff --git a/src/main/scala/firrtl/constraint/ConstraintSolver.scala b/src/main/scala/firrtl/constraint/ConstraintSolver.scala index a421ae17..64271ae1 100644 --- a/src/main/scala/firrtl/constraint/ConstraintSolver.scala +++ b/src/main/scala/firrtl/constraint/ConstraintSolver.scala @@ -24,7 +24,6 @@ class ConstraintSolver { type ConstraintMap = mutable.HashMap[String, (Constraint, Boolean)] private val solvedConstraintMap = new ConstraintMap() - /** Clear all previously recorded/solved constraints */ def clear(): Unit = { constraints.clear() @@ -78,7 +77,7 @@ class ConstraintSolver { def get(b: Constraint): Option[IsKnown] = { val name = b match { case IsVar(name) => name - case x => "" + case x => "" } solvedConstraintMap.get(name) match { case None => None @@ -94,7 +93,7 @@ class ConstraintSolver { def get(b: Width): Option[IsKnown] = { val name = b match { case IsVar(name) => name - case x => "" + case x => "" } solvedConstraintMap.get(name) match { case None => None @@ -103,10 +102,8 @@ class ConstraintSolver { } } - private def add(c: Inequality) = constraints += c - /** Creates an Inequality given a variable name, constraint, and whether its >= or <= * @param left * @param right @@ -114,7 +111,7 @@ class ConstraintSolver { * @return */ private def genConst(left: String, right: Constraint, geq: Boolean): Inequality = geq match { - case true => GreaterOrEqual(left, right) + case true => GreaterOrEqual(left, right) case false => LesserOrEqual(left, right) } @@ -122,14 +119,13 @@ class ConstraintSolver { def serializeConstraints: String = constraints.mkString("\n") /** For debugging, can serialize the solved constraints */ - def serializeSolutions: String = solvedConstraintMap.map{ + def serializeSolutions: String = solvedConstraintMap.map { case (k, (v, true)) => s"$k >= ${v.serialize}" case (k, (v, false)) => s"$k <= ${v.serialize}" }.mkString("\n") - - - /************* Constraint Solver Engine ****************/ + /** *********** Constraint Solver Engine *************** + */ /** Merges constraints on the same variable * @@ -148,17 +144,16 @@ class ConstraintSolver { private def mergeConstraints(constraints: Seq[Inequality]): Seq[Inequality] = { val mergedMap = mutable.HashMap[String, Inequality]() constraints.foreach { - case c if c.geq && mergedMap.contains(c.left) => - mergedMap(c.left) = genConst(c.left, IsMax(mergedMap(c.left).right, c.right), true) - case c if !c.geq && mergedMap.contains(c.left) => - mergedMap(c.left) = genConst(c.left, IsMin(mergedMap(c.left).right, c.right), false) - case c => - mergedMap(c.left) = c + case c if c.geq && mergedMap.contains(c.left) => + mergedMap(c.left) = genConst(c.left, IsMax(mergedMap(c.left).right, c.right), true) + case c if !c.geq && mergedMap.contains(c.left) => + mergedMap(c.left) = genConst(c.left, IsMin(mergedMap(c.left).right, c.right), false) + case c => + mergedMap(c.left) = c } mergedMap.values.toList } - /** Attempts to substitute variables with their corresponding forward-solved constraints * If no corresponding constraint has been visited yet, keep variable as is * @@ -167,15 +162,16 @@ class ConstraintSolver { * @return Forward solved constraint */ private def forwardSubstitution(forwardSolved: ConstraintMap)(constraint: Constraint): Constraint = { - val x = constraint map forwardSubstitution(forwardSolved) + val x = constraint.map(forwardSubstitution(forwardSolved)) x match { - case isVar: IsVar => forwardSolved get isVar.name match { - case None => isVar.asInstanceOf[Constraint] - case Some((p, geq)) => - val newT = forwardSubstitution(forwardSolved)(p) - forwardSolved(isVar.name) = (newT, geq) - newT - } + case isVar: IsVar => + forwardSolved.get(isVar.name) match { + case None => isVar.asInstanceOf[Constraint] + case Some((p, geq)) => + val newT = forwardSubstitution(forwardSolved)(p) + forwardSolved(isVar.name) = (newT, geq) + newT + } case other => other } } @@ -190,11 +186,12 @@ class ConstraintSolver { */ private def backwardSubstitution(backwardSolved: ConstraintMap)(constraint: Constraint): Constraint = { constraint match { - case isVar: IsVar => backwardSolved.get(isVar.name) match { - case Some((p, geq)) => p - case _ => isVar - } - case other => other map backwardSubstitution(backwardSolved) + case isVar: IsVar => + backwardSolved.get(isVar.name) match { + case Some((p, geq)) => p + case _ => isVar + } + case other => other.map(backwardSubstitution(backwardSolved)) } } @@ -211,7 +208,7 @@ class ConstraintSolver { * @return */ private def removeCycle(name: String, geq: Boolean)(constraint: Constraint): Constraint = - if(geq) removeGeqCycle(name)(constraint) else removeLeqCycle(name)(constraint) + if (geq) removeGeqCycle(name)(constraint) else removeLeqCycle(name)(constraint) /** Removes solvable cycles of <= inequalities * @param name Name of the variable on left side of inequality @@ -220,7 +217,7 @@ class ConstraintSolver { */ private def removeLeqCycle(name: String)(constraint: Constraint): Constraint = constraint match { case x if greaterEqThan(name)(x) => VarCon(name) - case isMin: IsMin => IsMin(isMin.children.filter{ c => !greaterEqThan(name)(c)}) + case isMin: IsMin => IsMin(isMin.children.filter { c => !greaterEqThan(name)(c) }) case x => x } @@ -231,43 +228,48 @@ class ConstraintSolver { */ private def removeGeqCycle(name: String)(constraint: Constraint): Constraint = constraint match { case x if lessEqThan(name)(x) => VarCon(name) - case isMax: IsMax => IsMax(isMax.children.filter{c => !lessEqThan(name)(c)}) + case isMax: IsMax => IsMax(isMax.children.filter { c => !lessEqThan(name)(c) }) case x => x } private def greaterEqThan(name: String)(constraint: Constraint): Boolean = constraint match { case isMin: IsMin => isMin.children.map(greaterEqThan(name)).reduce(_ && _) - case isAdd: IsAdd => isAdd.children match { - case Seq(isVar: IsVar, isVal: IsKnown) if (isVar.name == name) && (isVal.value >= 0) => true - case Seq(isVal: IsKnown, isVar: IsVar) if (isVar.name == name) && (isVal.value >= 0) => true - case _ => false - } - case isMul: IsMul => isMul.children match { - case Seq(isVar: IsVar, isVal: IsKnown) if (isVar.name == name) && (isVal.value >= 0) => true - case Seq(isVal: IsKnown, isVar: IsVar) if (isVar.name == name) && (isVal.value >= 0) => true - case _ => false - } + case isAdd: IsAdd => + isAdd.children match { + case Seq(isVar: IsVar, isVal: IsKnown) if (isVar.name == name) && (isVal.value >= 0) => true + case Seq(isVal: IsKnown, isVar: IsVar) if (isVar.name == name) && (isVal.value >= 0) => true + case _ => false + } + case isMul: IsMul => + isMul.children match { + case Seq(isVar: IsVar, isVal: IsKnown) if (isVar.name == name) && (isVal.value >= 0) => true + case Seq(isVal: IsKnown, isVar: IsVar) if (isVar.name == name) && (isVal.value >= 0) => true + case _ => false + } case isVar: IsVar if isVar.name == name => true case _ => false } private def lessEqThan(name: String)(constraint: Constraint): Boolean = constraint match { case isMax: IsMax => isMax.children.map(lessEqThan(name)).reduce(_ && _) - case isAdd: IsAdd => isAdd.children match { - case Seq(isVar: IsVar, isVal: IsKnown) if (isVar.name == name) && (isVal.value <= 0) => true - case Seq(isVal: IsKnown, isVar: IsVar) if (isVar.name == name) && (isVal.value <= 0) => true - case _ => false - } - case isMul: IsMul => isMul.children match { - case Seq(isVar: IsVar, isVal: IsKnown) if (isVar.name == name) && (isVal.value <= 0) => true - case Seq(isVal: IsKnown, isVar: IsVar) if (isVar.name == name) && (isVal.value <= 0) => true - case _ => false - } + case isAdd: IsAdd => + isAdd.children match { + case Seq(isVar: IsVar, isVal: IsKnown) if (isVar.name == name) && (isVal.value <= 0) => true + case Seq(isVal: IsKnown, isVar: IsVar) if (isVar.name == name) && (isVal.value <= 0) => true + case _ => false + } + case isMul: IsMul => + isMul.children match { + case Seq(isVar: IsVar, isVal: IsKnown) if (isVar.name == name) && (isVal.value <= 0) => true + case Seq(isVal: IsKnown, isVar: IsVar) if (isVar.name == name) && (isVal.value <= 0) => true + case _ => false + } case isVar: IsVar if isVar.name == name => true - case isNeg: IsNeg => isNeg.child match { - case isVar: IsVar if isVar.name == name => true - case _ => false - } + case isNeg: IsNeg => + isNeg.child match { + case isVar: IsVar if isVar.name == name => true + case _ => false + } case _ => false } @@ -283,7 +285,7 @@ class ConstraintSolver { case isVar: IsVar if isVar.name == name => has = true case _ => } - constraint map rec + constraint.map(rec) } rec(constraint) has @@ -300,7 +302,7 @@ class ConstraintSolver { checkMap(c.left) = c seq ++ Nil case Some(x) if x.geq != c.geq => seq ++ Seq(x, c) - case Some(x) => seq ++ Nil + case Some(x) => seq ++ Nil } } } diff --git a/src/main/scala/firrtl/constraint/Inequality.scala b/src/main/scala/firrtl/constraint/Inequality.scala index 0fa1d2eb..a01b7c85 100644 --- a/src/main/scala/firrtl/constraint/Inequality.scala +++ b/src/main/scala/firrtl/constraint/Inequality.scala @@ -6,9 +6,9 @@ package firrtl.constraint * Is passed to the constraint solver to resolve */ trait Inequality { - def left: String + def left: String def right: Constraint - def geq: Boolean + def geq: Boolean } case class GreaterOrEqual(left: String, right: Constraint) extends Inequality { @@ -20,5 +20,3 @@ case class LesserOrEqual(left: String, right: Constraint) extends Inequality { val geq = false override def toString: String = s"$left <= ${right.serialize}" } - - diff --git a/src/main/scala/firrtl/constraint/IsAdd.scala b/src/main/scala/firrtl/constraint/IsAdd.scala index e177a8b9..9305db89 100644 --- a/src/main/scala/firrtl/constraint/IsAdd.scala +++ b/src/main/scala/firrtl/constraint/IsAdd.scala @@ -1,39 +1,38 @@ // See LICENSE for license details. - package firrtl.constraint // Is case class because writing tests is easier due to equality is not object equality -case class IsAdd private (known: Option[IsKnown], - maxs: Vector[IsMax], - mins: Vector[IsMin], - others: Vector[Constraint]) extends Constraint with MultiAry { +case class IsAdd private (known: Option[IsKnown], maxs: Vector[IsMax], mins: Vector[IsMin], others: Vector[Constraint]) + extends Constraint + with MultiAry { def op(b1: IsKnown, b2: IsKnown): IsKnown = b1 + b2 lazy val children: Vector[Constraint] = { - if(known.nonEmpty) known.get +: (maxs ++ mins ++ others) else maxs ++ mins ++ others + if (known.nonEmpty) known.get +: (maxs ++ mins ++ others) else maxs ++ mins ++ others } def addChild(x: Constraint): IsAdd = x match { - case k: IsKnown => new IsAdd(merge(Some(k), known), maxs, mins, others) - case add: IsAdd => new IsAdd(merge(known, add.known), maxs ++ add.maxs, mins ++ add.mins, others ++ add.others) - case max: IsMax => new IsAdd(known, maxs :+ max, mins, others) - case min: IsMin => new IsAdd(known, maxs, mins :+ min, others) - case other => new IsAdd(known, maxs, mins, others :+ other) + case k: IsKnown => new IsAdd(merge(Some(k), known), maxs, mins, others) + case add: IsAdd => new IsAdd(merge(known, add.known), maxs ++ add.maxs, mins ++ add.mins, others ++ add.others) + case max: IsMax => new IsAdd(known, maxs :+ max, mins, others) + case min: IsMin => new IsAdd(known, maxs, mins :+ min, others) + case other => new IsAdd(known, maxs, mins, others :+ other) } override def serialize: String = "(" + children.map(_.serialize).mkString(" + ") + ")" - override def map(f: Constraint=>Constraint): Constraint = IsAdd(children.map(f)) + override def map(f: Constraint => Constraint): Constraint = IsAdd(children.map(f)) def reduce(): Constraint = { - if(children.size == 1) children.head else { + if (children.size == 1) children.head + else { (known, maxs, mins, others) match { case (Some(k), _, _, _) if k.value == 0 => new IsAdd(None, maxs, mins, others).reduce() case (Some(k), Vector(max), Vector(), Vector()) => max.map { o => IsAdd(k, o) }.reduce() case (Some(k), Vector(), Vector(min), Vector()) => min.map { o => IsAdd(k, o) }.reduce() - case _ => this + case _ => this } } } @@ -45,8 +44,10 @@ object IsAdd { case _ => apply(Seq(left, right)) } def apply(children: Seq[Constraint]): Constraint = { - children.foldLeft(new IsAdd(None, Vector(), Vector(), Vector())) { (add, c) => - add.addChild(c) - }.reduce() + children + .foldLeft(new IsAdd(None, Vector(), Vector(), Vector())) { (add, c) => + add.addChild(c) + } + .reduce() } -}
\ No newline at end of file +} diff --git a/src/main/scala/firrtl/constraint/IsFloor.scala b/src/main/scala/firrtl/constraint/IsFloor.scala index 5de4697e..60f049bb 100644 --- a/src/main/scala/firrtl/constraint/IsFloor.scala +++ b/src/main/scala/firrtl/constraint/IsFloor.scala @@ -10,13 +10,13 @@ case class IsFloor private (child: Constraint, dummyArg: Int) extends Constraint override def reduce(): Constraint = child match { case k: IsKnown => k.floor - case x: IsAdd => this - case x: IsMul => this - case x: IsNeg => this - case x: IsPow => this + case x: IsAdd => this + case x: IsMul => this + case x: IsNeg => this + case x: IsPow => this // floor(max(a, b)) -> max(floor(a), floor(b)) - case x: IsMax => IsMax(x.children.map {b => IsFloor(b)}) - case x: IsMin => IsMin(x.children.map {b => IsFloor(b)}) + case x: IsMax => IsMax(x.children.map { b => IsFloor(b) }) + case x: IsMin => IsMin(x.children.map { b => IsFloor(b) }) case x: IsVar => this // floor(floor(x)) -> floor(x) case x: IsFloor => x @@ -24,9 +24,7 @@ case class IsFloor private (child: Constraint, dummyArg: Int) extends Constraint } val children = Vector(child) - override def map(f: Constraint=>Constraint): Constraint = IsFloor(f(child)) + override def map(f: Constraint => Constraint): Constraint = IsFloor(f(child)) override def serialize: String = "floor(" + child.serialize + ")" } - - diff --git a/src/main/scala/firrtl/constraint/IsKnown.scala b/src/main/scala/firrtl/constraint/IsKnown.scala index 5bd25f92..07e0531c 100644 --- a/src/main/scala/firrtl/constraint/IsKnown.scala +++ b/src/main/scala/firrtl/constraint/IsKnown.scala @@ -34,11 +34,9 @@ trait IsKnown extends Constraint { /** Floor */ def floor: IsKnown - override def map(f: Constraint=>Constraint): Constraint = this + override def map(f: Constraint => Constraint): Constraint = this val children: Vector[Constraint] = Vector.empty[Constraint] def reduce(): IsKnown = this } - - diff --git a/src/main/scala/firrtl/constraint/IsMax.scala b/src/main/scala/firrtl/constraint/IsMax.scala index 3f24b7c0..0ba20c08 100644 --- a/src/main/scala/firrtl/constraint/IsMax.scala +++ b/src/main/scala/firrtl/constraint/IsMax.scala @@ -4,7 +4,7 @@ package firrtl.constraint object IsMax { def apply(left: Constraint, right: Constraint): Constraint = (left, right) match { - case (l: IsKnown, r: IsKnown) => l max r + case (l: IsKnown, r: IsKnown) => l.max(r) case _ => apply(Seq(left, right)) } def apply(children: Seq[Constraint]): Constraint = { @@ -15,33 +15,32 @@ object IsMax { } } -case class IsMax private[constraint](known: Option[IsKnown], - mins: Vector[IsMin], - others: Vector[Constraint] - ) extends MultiAry { +case class IsMax private[constraint] (known: Option[IsKnown], mins: Vector[IsMin], others: Vector[Constraint]) + extends MultiAry { - def op(b1: IsKnown, b2: IsKnown): IsKnown = b1 max b2 + def op(b1: IsKnown, b2: IsKnown): IsKnown = b1.max(b2) override def serialize: String = "max(" + children.map(_.serialize).mkString(", ") + ")" - override def map(f: Constraint=>Constraint): Constraint = IsMax(children.map(f)) + override def map(f: Constraint => Constraint): Constraint = IsMax(children.map(f)) lazy val children: Vector[Constraint] = { - if(known.nonEmpty) known.get +: (mins ++ others) else mins ++ others + if (known.nonEmpty) known.get +: (mins ++ others) else mins ++ others } def reduce(): Constraint = { - if(children.size == 1) children.head else { + if (children.size == 1) children.head + else { (known, mins, others) match { case (Some(IsKnown(a)), _, _) => // Eliminate minimums who have a known minimum value which is smaller than known maximum value val filteredMins = mins.filter { case IsMin(Some(IsKnown(i)), _, _) if i <= a => false - case other => true + case other => true } // If a successful filter, rerun reduce val newMax = new IsMax(known, filteredMins, others) - if(filteredMins.size != mins.size) { + if (filteredMins.size != mins.size) { newMax.reduce() } else newMax case _ => this @@ -50,10 +49,9 @@ case class IsMax private[constraint](known: Option[IsKnown], } def addChild(x: Constraint): IsMax = x match { - case k: IsKnown => new IsMax(known = merge(Some(k), known), mins, others) - case max: IsMax => new IsMax(known = merge(known, max.known), max.mins ++ mins, others ++ max.others) - case min: IsMin => new IsMax(known, mins :+ min, others) - case other => new IsMax(known, mins, others :+ other) + case k: IsKnown => new IsMax(known = merge(Some(k), known), mins, others) + case max: IsMax => new IsMax(known = merge(known, max.known), max.mins ++ mins, others ++ max.others) + case min: IsMin => new IsMax(known, mins :+ min, others) + case other => new IsMax(known, mins, others :+ other) } } - diff --git a/src/main/scala/firrtl/constraint/IsMin.scala b/src/main/scala/firrtl/constraint/IsMin.scala index ee97e298..2c5db14d 100644 --- a/src/main/scala/firrtl/constraint/IsMin.scala +++ b/src/main/scala/firrtl/constraint/IsMin.scala @@ -4,43 +4,44 @@ package firrtl.constraint object IsMin { def apply(left: Constraint, right: Constraint): Constraint = (left, right) match { - case (l: IsKnown, r: IsKnown) => l min r + case (l: IsKnown, r: IsKnown) => l.min(r) case _ => apply(Seq(left, right)) } def apply(children: Seq[Constraint]): Constraint = { - children.foldLeft(new IsMin(None, Vector(), Vector())) { (add, c) => - add.addChild(c) - }.reduce() + children + .foldLeft(new IsMin(None, Vector(), Vector())) { (add, c) => + add.addChild(c) + } + .reduce() } } -case class IsMin private[constraint](known: Option[IsKnown], - maxs: Vector[IsMax], - others: Vector[Constraint] - ) extends MultiAry { +case class IsMin private[constraint] (known: Option[IsKnown], maxs: Vector[IsMax], others: Vector[Constraint]) + extends MultiAry { - def op(b1: IsKnown, b2: IsKnown): IsKnown = b1 min b2 + def op(b1: IsKnown, b2: IsKnown): IsKnown = b1.min(b2) override def serialize: String = "min(" + children.map(_.serialize).mkString(", ") + ")" - override def map(f: Constraint=>Constraint): Constraint = IsMin(children.map(f)) + override def map(f: Constraint => Constraint): Constraint = IsMin(children.map(f)) lazy val children: Vector[Constraint] = { - if(known.nonEmpty) known.get +: (maxs ++ others) else maxs ++ others + if (known.nonEmpty) known.get +: (maxs ++ others) else maxs ++ others } def reduce(): Constraint = { - if(children.size == 1) children.head else { + if (children.size == 1) children.head + else { (known, maxs, others) match { case (Some(IsKnown(i)), _, _) => // Eliminate maximums who have a known maximum value which is larger than known minimum value val filteredMaxs = maxs.filter { case IsMax(Some(IsKnown(a)), _, _) if a >= i => false - case other => true + case other => true } // If a successful filter, rerun reduce val newMin = new IsMin(known, filteredMaxs, others) - if(filteredMaxs.size != maxs.size) { + if (filteredMaxs.size != maxs.size) { newMin.reduce() } else newMin case _ => this @@ -49,9 +50,9 @@ case class IsMin private[constraint](known: Option[IsKnown], } def addChild(x: Constraint): IsMin = x match { - case k: IsKnown => new IsMin(merge(Some(k), known), maxs, others) - case max: IsMax => new IsMin(known, maxs :+ max, others) - case min: IsMin => new IsMin(merge(min.known, known), maxs ++ min.maxs, others ++ min.others) - case other => new IsMin(known, maxs, others :+ other) + case k: IsKnown => new IsMin(merge(Some(k), known), maxs, others) + case max: IsMax => new IsMin(known, maxs :+ max, others) + case min: IsMin => new IsMin(merge(min.known, known), maxs ++ min.maxs, others ++ min.others) + case other => new IsMin(known, maxs, others :+ other) } } diff --git a/src/main/scala/firrtl/constraint/IsMul.scala b/src/main/scala/firrtl/constraint/IsMul.scala index 3f637d75..a4acd74c 100644 --- a/src/main/scala/firrtl/constraint/IsMul.scala +++ b/src/main/scala/firrtl/constraint/IsMul.scala @@ -10,9 +10,11 @@ object IsMul { case _ => apply(Seq(left, right)) } def apply(children: Seq[Constraint]): Constraint = { - children.foldLeft(new IsMul(None, Vector())) { (add, c) => - add.addChild(c) - }.reduce() + children + .foldLeft(new IsMul(None, Vector())) { (add, c) => + add.addChild(c) + } + .reduce() } } @@ -20,19 +22,20 @@ case class IsMul private (known: Option[IsKnown], others: Vector[Constraint]) ex def op(b1: IsKnown, b2: IsKnown): IsKnown = b1 * b2 - lazy val children: Vector[Constraint] = if(known.nonEmpty) known.get +: others else others + lazy val children: Vector[Constraint] = if (known.nonEmpty) known.get +: others else others def addChild(x: Constraint): IsMul = x match { - case k: IsKnown => new IsMul(known = merge(Some(k), known), others) - case mul: IsMul => new IsMul(merge(known, mul.known), others ++ mul.others) - case other => new IsMul(known, others :+ other) + case k: IsKnown => new IsMul(known = merge(Some(k), known), others) + case mul: IsMul => new IsMul(merge(known, mul.known), others ++ mul.others) + case other => new IsMul(known, others :+ other) } override def reduce(): Constraint = { - if(children.size == 1) children.head else { + if (children.size == 1) children.head + else { (known, others) match { - case (Some(Closed(x)), _) if x == BigDecimal(1) => new IsMul(None, others).reduce() - case (Some(Closed(x)), _) if x == BigDecimal(0) => Closed(0) + case (Some(Closed(x)), _) if x == BigDecimal(1) => new IsMul(None, others).reduce() + case (Some(Closed(x)), _) if x == BigDecimal(0) => Closed(0) case (Some(Closed(x)), Vector(m: IsMax)) if x > 0 => IsMax(m.children.map { c => IsMul(Closed(x), c) }) case (Some(Closed(x)), Vector(m: IsMax)) if x < 0 => @@ -46,7 +49,7 @@ case class IsMul private (known: Option[IsKnown], others: Vector[Constraint]) ex } } - override def map(f: Constraint=>Constraint): Constraint = IsMul(children.map(f)) + override def map(f: Constraint => Constraint): Constraint = IsMul(children.map(f)) override def serialize: String = "(" + children.map(_.serialize).mkString(" * ") + ")" } diff --git a/src/main/scala/firrtl/constraint/IsNeg.scala b/src/main/scala/firrtl/constraint/IsNeg.scala index 46f739c6..574cfd47 100644 --- a/src/main/scala/firrtl/constraint/IsNeg.scala +++ b/src/main/scala/firrtl/constraint/IsNeg.scala @@ -11,10 +11,10 @@ object IsNeg { case class IsNeg private (child: Constraint, dummyArg: Int) extends Constraint { override def reduce(): Constraint = child match { case k: IsKnown => k.neg - case x: IsAdd => IsAdd(x.children.map { b => IsNeg(b) }) - case x: IsMul => IsMul(Seq(IsNeg(x.children.head)) ++ x.children.tail) - case x: IsNeg => x.child - case x: IsPow => this + case x: IsAdd => IsAdd(x.children.map { b => IsNeg(b) }) + case x: IsMul => IsMul(Seq(IsNeg(x.children.head)) ++ x.children.tail) + case x: IsNeg => x.child + case x: IsPow => this // -[max(a, b)] -> min[-a, -b] case x: IsMax => IsMin(x.children.map { b => IsNeg(b) }) case x: IsMin => IsMax(x.children.map { b => IsNeg(b) }) @@ -24,9 +24,7 @@ case class IsNeg private (child: Constraint, dummyArg: Int) extends Constraint { lazy val children = Vector(child) - override def map(f: Constraint=>Constraint): Constraint = IsNeg(f(child)) + override def map(f: Constraint => Constraint): Constraint = IsNeg(f(child)) override def serialize: String = "(-" + child.serialize + ")" } - - diff --git a/src/main/scala/firrtl/constraint/IsPow.scala b/src/main/scala/firrtl/constraint/IsPow.scala index 54a06bf8..2a1fb14a 100644 --- a/src/main/scala/firrtl/constraint/IsPow.scala +++ b/src/main/scala/firrtl/constraint/IsPow.scala @@ -12,22 +12,20 @@ case class IsPow private (child: Constraint, dummyArg: Int) extends Constraint { override def reduce(): Constraint = child match { case k: IsKnown => k.pow // 2^(a + b) -> 2^a * 2^b - case x: IsAdd => IsMul(x.children.map { b => IsPow(b)}) + case x: IsAdd => IsMul(x.children.map { b => IsPow(b) }) case x: IsMul => this case x: IsNeg => this case x: IsPow => this // 2^(max(a, b)) -> max(2^a, 2^b) since two is always positive, so a, b control magnitude - case x: IsMax => IsMax(x.children.map {b => IsPow(b)}) - case x: IsMin => IsMin(x.children.map {b => IsPow(b)}) + case x: IsMax => IsMax(x.children.map { b => IsPow(b) }) + case x: IsMin => IsMin(x.children.map { b => IsPow(b) }) case x: IsVar => this case _ => this } val children = Vector(child) - override def map(f: Constraint=>Constraint): Constraint = IsPow(f(child)) + override def map(f: Constraint => Constraint): Constraint = IsPow(f(child)) override def serialize: String = "(2^" + child.serialize + ")" } - - diff --git a/src/main/scala/firrtl/constraint/IsVar.scala b/src/main/scala/firrtl/constraint/IsVar.scala index 98396fa0..18fb53b2 100644 --- a/src/main/scala/firrtl/constraint/IsVar.scala +++ b/src/main/scala/firrtl/constraint/IsVar.scala @@ -16,7 +16,7 @@ trait IsVar extends Constraint { override def serialize: String = name - override def map(f: Constraint=>Constraint): Constraint = this + override def map(f: Constraint => Constraint): Constraint = this override def reduce() = this @@ -24,4 +24,3 @@ trait IsVar extends Constraint { } case class VarCon(name: String) extends IsVar - |
