Re: [PATCH] Make for_each_cpu_mask a bit smaller

From: Matthew Wilcox
Date: Mon May 12 2008 - 07:57:07 EST


On Mon, May 12, 2008 at 01:04:54PM +0200, Alexander van Heukelum wrote:
> > #define for_each_cpu_mask(cpu, mask) \
> > for ((cpu) = -1; \
> > (cpu) < NR_CPUS; \
> > (cpu) = find_next_cpu_mask((cpu), &(mask)))
> >
> > int find_next_cpu_mask(int n, const cpumask_t *srcp)
> > {
> > return find_next_bit(srcp->bits, NR_CPUS, ++n);
> > }
> >
> > That actually behaves the way I'd expect a function called
> > 'find_next_cpu_mask' to work. It also abuses the 'for' condtion
> > less and might take a little less text space.
>
> But it does not work.

You're right, of course. Either of these should work:

#define for_each_cpu_mask(cpu, mask) \
for ((cpu) = find_next_cpu_mask(-1, &(mask)); \
(cpu) < NR_CPUS; \
(cpu) = find_next_cpu_mask((cpu), &(mask)))

#define for_each_cpu_mask(cpu, mask) \
for ((cpu) = -1; \
(cpu) = find_next_cpu_mask((cpu), &(mask)), (cpu) < NR_CPUS; \
)

int find_next_cpu_mask(int n, const cpumask_t *srcp)
{
return find_next_bit(srcp->bits, NR_CPUS, ++n);
}

> I think of find_next_cpu_mask(cpu, mask) as: "find next
> cpu-index in mask, starting at index cpu". And similar
> with find_next_bit.

If you're determined to stick to your original formulation, renaming it
to find_valid_cpu_mask() would work better.

> As for the text-space argument, I think you might be right.
> Just not on i386/x86_64 where initialising a register to -1
> can be done in three bytes, initialising to 0 in two bytes
> and an increment in one byte :-).

While the initialisation may take one extra byte, the increment is
done in the function, and so takes at least one byte out of the loop.
Of course on other instruction sets, that's probably 4 bytes out of the
loop and they can initialise to -1 as cheaply as init to 0, so it's a
win on non-x86 and neutral on x86.

--
Intel are signing my paycheques ... these opinions are still mine
"Bill, look, we understand that you're interested in selling us this
operating system, but compare it to ours. We can't possibly take such
a retrograde step."
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/