Re: [PATCH -tip 2/2] x86/hweight: Use POPCNT when available with X86_NATIVE_CPU option
From: Ingo Molnar
Date: Sun Mar 30 2025 - 14:47:42 EST
* Borislav Petkov <bp@xxxxxxxxx> wrote:
> On Sun, Mar 30, 2025 at 05:15:16PM +0200, Uros Bizjak wrote:
> > You missed this part:
> >
> > --q--
> > ... where there is no need for an entry in the .altinstr_replacement
>
> Not really - .altinstr* memory gets jettisoned after boot.
>
> > section, shrinking all text sections by 9476 bytes:
>
> So if anything, this saves a whopping ~9K disk space and makes
> hweight* an unreadable mess.
What unreadable mess?
The proposed patch is:
+#ifdef __POPCNT__
+ asm_inline (ASM_FORCE_CLR "popcntl %[val], %[cnt]"
+ : [cnt] "=&r" (res)
+ : [val] ASM_INPUT_RM (w));
+#else
asm_inline (ALTERNATIVE(ANNOTATE_IGNORE_ALTERNATIVE
"call __sw_hweight32",
ASM_CLR "popcntl %[val], %[cnt]",
X86_FEATURE_POPCNT)
: [cnt] "=a" (res), ASM_CALL_CONSTRAINT
: [val] REG_IN (w));
-
+#endif
Which is 3 straightforward lines of assembly code and a straightforward #ifdef.
My main objection is different: if __POPCNT__ isn't defined during the
kernel build of major Linux distros, then this optimization almost
doesn't exist to our users. And I don't think it's defined.
Thanks,
Ingo