From: I Hsin Cheng <richard120310@gmail.com>
To: yury.norov@gmail.com
Cc: anshuman.khandual@arm.com, arnd@arndb.de,
linux-kernel@vger.kernel.org, jserv@ccns.ncku.edu.tw,
skhan@linuxfoundation.org, I Hsin Cheng <richard120310@gmail.com>
Subject: [PATCH 1/2] uapi: Refactor __GENMASK() for speed-up
Date: Wed, 12 Feb 2025 00:24:11 +0800 [thread overview]
Message-ID: <20250211162412.477655-2-richard120310@gmail.com> (raw)
In-Reply-To: <20250211162412.477655-1-richard120310@gmail.com>
The calculation of "((~_UL(0)) - (_UL(1) << (l)) + 1)" is to generate a
bitmask with "l" trailing zeroes, which is equivalent to
"(~_UL(0) << (l))".
Refactor the calculation so the number of arithmetic instruction will be
reduced from 3 to 1.
Signed-off-by: I Hsin Cheng <richard120310@gmail.com>
---
Test is done to show the speed-up we can get from reducing the number of
instruction. The test machine runs with 6.9.0-0-generic kernel on x86_64
architecture with processor AMD Ryzen 7 5700X3D 8-Core Processor.
The test executes in the form of kernel module.
Test result:
[451675.644026] new __GENMASK() : 5985416
[451675.644030] old __GENMASK() : 6006406
Test script snippet:
/* switch to __BITS_PER_LONG_LONG when testing __GENMASK_ULL() */
\#define __GENMASK_NEW(h, l) \
((~_UL(0) << (l)) & \
(~_UL(0) >> (__BITS_PER_LONG - 1 - (h))))
int init_module(void)
{
ktime_t start, end, total1 = 0, total2 = 0;
for (int k = 0; k < 100; k++) {
for (int i = 0; i < __BITS_PER_LONG; i++) {
for (int j = 0; j <= i; j++) {
unsigned result1, result2;
start = ktime_get();
result1 = __GENMASK_NEW(i, j);
end = ktime_get();
total1 += (end - start);
start = ktime_get();
result2 = __GENMASK(i, j);
end = ktime_get();
total2 += (end - start);
if (result1 != result2) {
pr_info("Wrong calculation of GENMASK_NEW()\n");
return 0;
}
}
}
}
pr_info("new __GENMASK() : %lld\n", total1);
pr_info("old __GENMASK() : %lld\n", total2);
return 0;
}
---
include/uapi/linux/bits.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/uapi/linux/bits.h b/include/uapi/linux/bits.h
index 5ee30f882736..8fc7fea65288 100644
--- a/include/uapi/linux/bits.h
+++ b/include/uapi/linux/bits.h
@@ -5,7 +5,7 @@
#define _UAPI_LINUX_BITS_H
#define __GENMASK(h, l) \
- (((~_UL(0)) - (_UL(1) << (l)) + 1) & \
+ ((~_UL(0) << (l)) & \
(~_UL(0) >> (__BITS_PER_LONG - 1 - (h))))
#define __GENMASK_ULL(h, l) \
--
2.43.0
next prev parent reply other threads:[~2025-02-11 16:24 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-11 16:24 [PATCH 0/2] uapi: Refactor __GENMASK() and __GENMASK_ULL() for I Hsin Cheng
2025-02-11 16:24 ` I Hsin Cheng [this message]
2025-02-11 18:39 ` [PATCH 1/2] uapi: Refactor __GENMASK() for speed-up Yury Norov
2025-02-11 18:44 ` Yury Norov
2025-02-12 14:01 ` I Hsin Cheng
2025-02-13 19:54 ` Yury Norov
2025-02-14 14:51 ` I Hsin Cheng
2025-02-14 15:55 ` Yury Norov
2025-02-12 13:50 ` I Hsin Cheng
2025-02-11 16:24 ` [PATCH 2/2] uapi: Refactor __GENMASK_ULL() " I Hsin Cheng
2025-02-11 22:30 ` David Laight
2025-02-12 12:39 ` I Hsin Cheng
2025-02-12 13:52 ` David Laight
2025-02-12 14:12 ` Yury Norov
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=20250211162412.477655-2-richard120310@gmail.com \
--to=richard120310@gmail.com \
--cc=anshuman.khandual@arm.com \
--cc=arnd@arndb.de \
--cc=jserv@ccns.ncku.edu.tw \
--cc=linux-kernel@vger.kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=yury.norov@gmail.com \
/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
all inboxes | Powered by JetHome®