For better performance, use gmpy2.popcount if available.