smp: Use bitwise operation to count cpu number

Previously, we iterated over all CPUs using CPU_FOREACH and checked
individual bits to count valid CPUs. Refactor this to use a bitwise AND
and popcount to count the number of enabled bits directly.

Approved by:    markj (mentor)
MFC after:      2 weeks
Differential Revision: https://reviews.freebsd.org/D54474
This commit is contained in:
ShengYi Hung
2026-01-04 00:32:50 +08:00
parent efcfba9b31
commit e387d9438b
+3 -5
View File
@@ -588,7 +588,7 @@ smp_rendezvous_cpus(cpuset_t map,
void (* teardown_func)(void *),
void *arg)
{
int curcpumap, i, ncpus = 0;
int curcpumap, ncpus = 0;
/* See comments in the !SMP case. */
if (!smp_started) {
@@ -609,10 +609,8 @@ smp_rendezvous_cpus(cpuset_t map,
*/
MPASS(curthread->td_md.md_spinlock_count == 0);
CPU_FOREACH(i) {
if (CPU_ISSET(i, &map))
ncpus++;
}
CPU_AND(&map, &map, &all_cpus);
ncpus = CPU_COUNT(&map);
if (ncpus == 0)
panic("ncpus is 0 with non-zero map");