From: Uros Bizjak <ubizjak@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Uros Bizjak <ubizjak@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>
Subject: [PATCH] lib/bch.c: Use __builtin_parity() when available
Date: Wed, 29 Jan 2025 21:57:31 +0100 [thread overview]
Message-ID: <20250129205746.10963-1-ubizjak@gmail.com> (raw)
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 code on x86_64 from:
movl %edi, %edx
shrl %edx
xorl %edi, %edx
movl %edx, %eax
shrl $2, %eax
xorl %edx, %eax
andl $286331153, %eax
imull $286331153, %eax, %eax
shrl $28, %eax
andl $1, %eax
to an optimized:
movl %edi, %ecx
shrl $16, %ecx
xorl %edi, %ecx
xorl %eax, %eax
xorb %ch, %cl
setnp %al
Please note SETNP instruction that exercises hardware parity
calculation of x86 processors. When POPCNT instruction is
available, the generated code gets optimized even further:
popcntl %edi, %eax
andl $1, %eax
Compile-tested only.
Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
---
lib/bch.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/lib/bch.c b/lib/bch.c
index 1c0cb07cdfeb..7a266c160839 100644
--- a/lib/bch.c
+++ b/lib/bch.c
@@ -313,6 +313,9 @@ static inline int deg(unsigned int poly)
static inline int parity(unsigned int x)
{
+#if __has_builtin(__builtin_parity)
+ return __builtin_parity(x);
+#else
/*
* public domain code snippet, lifted from
* http://www-graphics.stanford.edu/~seander/bithacks.html
@@ -321,6 +324,7 @@ static inline int parity(unsigned int x)
x ^= x >> 2;
x = (x & 0x11111111U) * 0x11111111U;
return (x >> 28) & 1;
+#endif
}
/* Galois field basic operations: multiply, divide, inverse, etc. */
--
2.42.0
reply other threads:[~2025-01-29 20:57 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250129205746.10963-1-ubizjak@gmail.com \
--to=ubizjak@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome