[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CAFULd4YZeCZ4vuZyb-ViXYELS0FXDNX9jUNUbQEnqEf+N=uD8A@mail.gmail.com>
Date: Fri, 31 Jan 2025 16:15:58 +0100
From: Uros Bizjak <ubizjak@...il.com>
To: x86@...nel.org, linux-kernel@...r.kernel.org
Cc: Thomas Gleixner <tglx@...utronix.de>, Ingo Molnar <mingo@...hat.com>, Borislav Petkov <bp@...en8.de>,
Dave Hansen <dave.hansen@...ux.intel.com>, "H. Peter Anvin" <hpa@...or.com>
Subject: Re: [PATCH 2/2] x86/bootflag: Use __builtin_parity() when available
On Wed, Jan 29, 2025 at 4:49 PM Uros Bizjak <ubizjak@...il.com> wrote:
>
> Compilers (GCC and clang) provide optimized __builtin_parity() function
> that returns the parity of X, i.e. the number of 1-bits in X modulo 2.
>
> Use __builtin_parity() built-in function to optimize parity(). This
> improves generated parity code to just:
>
> 57: 84 db test %bl,%bl
> 59: 7a 5c jp b7 <sbf_init+0xa7>
>
> where TEST instruction sets parity flag (PF) in EFLAGS, exercised by the
> follow-up jump instruction.
Unfortunately, this approach won't fly. As said by Pedro elsewhere:
__builtin functions aren't strictly required to be inlined and can
generate a library call if the compiler so chooses (how often this
happens depends on the optimization level and architecture). e.g:
https://godbolt.org/z/zKPb9hbaM
Patch is retracted.
Uros.
>
> Signed-off-by: Uros Bizjak <ubizjak@...il.com>
> Cc: Thomas Gleixner <tglx@...utronix.de>
> Cc: Ingo Molnar <mingo@...hat.com>
> Cc: Borislav Petkov <bp@...en8.de>
> Cc: Dave Hansen <dave.hansen@...ux.intel.com>
> Cc: "H. Peter Anvin" <hpa@...or.com>
> ---
> arch/x86/kernel/bootflag.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/kernel/bootflag.c b/arch/x86/kernel/bootflag.c
> index 4d89a2d80d0f..d63247d7abd4 100644
> --- a/arch/x86/kernel/bootflag.c
> +++ b/arch/x86/kernel/bootflag.c
> @@ -22,6 +22,9 @@ int sbf_port __initdata = -1; /* set via acpi_boot_init() */
>
> static bool __init parity(u8 v)
> {
> +#if __has_builtin(__builtin_parity)
> + int x = __builtin_parity(v);
> +#else
> int x = 0;
> int i;
>
> @@ -29,7 +32,7 @@ static bool __init parity(u8 v)
> x ^= (v & 1);
> v >>= 1;
> }
> -
> +#endif
> return !!x;
> }
>
> --
> 2.42.0
>
Powered by blists - more mailing lists