Re: [PATCH] x86/hweight: Don't clobber %rdi

From: Linus Torvalds
Date: Mon Aug 08 2016 - 14:46:06 EST


On Mon, Aug 8, 2016 at 11:37 AM, Borislav Petkov <bp@xxxxxxxxx> wrote:
>
> Ok, so do you think it would work too if I stated that the input
> register gets clobbered:
>
> asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT)
> : "="REG_OUT (res)
> : REG_IN (w)
> : REG_IN);

That would work (although the clobbered registers have a different
syntax than the in/out registers), but it would be wrong, in my
opinion.

We want the actual POPCNT instruction to be the common case, and that
instruction does *not* clobber any other registers than the output.

So I think it's much better to just say: "the __sw_hweight functions
should have the same semantics as popcnt" (although without the eflags
rules that we don't care about).

There's nothing wrong with keeping it as assembly language - it's not
like it's a maintenance headache once it is written.

Linus