Function popCount [src]
r = @popCount(a) with 2s-complement semantics.
r and a may be aliases.
Assets the result fits in r. Upper bound on the number of limbs needed by
r is calcTwosCompLimbCount(bit_count).
Prototype
pub fn popCount(r: *Mutable, a: Const, bit_count: usize) void
Parameters
r: *Mutable
a: Const
bit_count: usize
Source
pub fn popCount(r: *Mutable, a: Const, bit_count: usize) void {
r.copy(a);
if (!a.positive) {
r.positive = true; // Negate.
r.bitNotWrap(r.toConst(), .unsigned, bit_count); // Bitwise NOT.
r.addScalar(r.toConst(), 1); // Add one.
}
var sum: Limb = 0;
for (r.limbs[0..r.len]) |limb| {
sum += @popCount(limb);
}
r.set(sum);
}