Expand description
The majority candidate of a slice: the value filling more than half of its valid positions, if there is one.
A Boyer–Moore majority vote finds such a value with comparisons alone. It keeps one candidate and a counter, increments the counter on a match, decrements it otherwise, and replaces the candidate when the counter is zero. A value filling more than half of the input survives as the candidate, because every decrement pairs off two different values and each pair holds at most one copy of it.
The vote is a mergeable summary: votes over disjoint parts of the input combine into a vote over all of it, with the same guarantee. Sixteen independent votes therefore run over interleaved elements, which removes the loop-carried dependency of a single vote and lets the compiler keep the lanes in vector registers. Without a majority the candidate is an arbitrary valid value, so callers that need to tell the cases apart count it afterwards.
fearless_simd compiles the loops once per instruction set level, such as AVX2 or NEON,
and selects one for the current CPU at runtime. LLVM vectorizes each of them.
Traits§
- Majority
Key - A fixed-width value that a majority vote compares for equality.
Functions§
- majority_
candidate - Runs a Boyer–Moore vote over the valid
valuesand returns its candidate.