From 9bfcb40cb4f1b7fd1adf6014f30cb55f040242f0 Mon Sep 17 00:00:00 2001 From: Henry Cook Date: Mon, 27 Feb 2017 20:18:28 -0800 Subject: [PATCH] util: Majority on sets of bools --- src/main/scala/util/Misc.scala | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/scala/util/Misc.scala b/src/main/scala/util/Misc.scala index 006aa696..55dc1ed0 100644 --- a/src/main/scala/util/Misc.scala +++ b/src/main/scala/util/Misc.scala @@ -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) +}