1
0

util: Majority on sets of bools

This commit is contained in:
Henry Cook 2017-02-27 20:18:28 -08:00
parent 6958f05a85
commit 9bfcb40cb4

View File

@ -160,3 +160,15 @@ object Random
private def partition(value: UInt, slices: Int) =
Seq.tabulate(slices)(i => value < UInt(round((i << value.getWidth).toDouble / slices)))
}
object Majority {
def apply(in: Set[Bool]): Bool = {
val n = (in.size >> 1) + 1
val clauses = in.subsets(n).map(_.reduce(_ && _))
clauses.reduce(_ || _)
}
def apply(in: Seq[Bool]): Bool = apply(in.toSet)
def apply(in: UInt): Bool = apply(in.toBools.toSet)
}