aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/constraint/IsNeg.scala
blob: e2e590aef7ec9077c6481e99226582ff195fd611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// SPDX-License-Identifier: Apache-2.0

package firrtl.constraint

object IsNeg {
  def apply(child: Constraint): Constraint = new IsNeg(child, 0).reduce()
}

// Dummy arg is to get around weird Scala issue that can't differentiate between a
//   private constructor and public apply that share the same arguments
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
    // -[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) })
    case x: IsVar => this
    case _ => this
  }

  lazy val children = Vector(child)

  override def map(f: Constraint => Constraint): Constraint = IsNeg(f(child))

  override def serialize: String = "(-" + child.serialize + ")"
}