[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250211162412.477655-3-richard120310@gmail.com>
Date: Wed, 12 Feb 2025 00:24:12 +0800
From: I Hsin Cheng <richard120310@...il.com>
To: yury.norov@...il.com
Cc: anshuman.khandual@....com,
arnd@...db.de,
linux-kernel@...r.kernel.org,
jserv@...s.ncku.edu.tw,
skhan@...uxfoundation.org,
I Hsin Cheng <richard120310@...il.com>
Subject: [PATCH 2/2] uapi: Refactor __GENMASK_ULL() for speed-up
The calculation of "((~_ULL(0)) - (_ULL(1) << (l)) + 1)" is to generate
a bitmask with "l" trailing zeroes, which is equivalent to
"(~_ULL(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@...il.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:
[452858.156718] new __GENMASK_ULL() : 5908678
[452858.156722] old __GENMASK_ULL() : 5950998
Test script snippet:
/* switch to __BITS_PER_LONG_LONG when testing __GENMASK_ULL() */
\#define __GENMASK_ULL_NEW(h, l) \
((~_ULL(0) << (l)) & \
(~_ULL(0) >> (__BITS_PER_LONG_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_LONG; i++) {
for (int j = 0; j <= i; j++) {
unsigned result1, result2;
start = ktime_get();
result1 = __GENMASK_ULL_NEW(i, j);
end = ktime_get();
total1 += (end - start);
start = ktime_get();
result2 = __GENMASK_ULL(i, j);
end = ktime_get();
total2 += (end - start);
if (result1 != result2) {
pr_info("Wrong calculation of GENMASK_ULL_NEW()\n");
return 0;
}
}
}
}
pr_info("new __GENMASK_ULL() : %lld\n", total1);
pr_info("old __GENMASK_ULL() : %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 8fc7fea65288..965f6eb91ff3 100644
--- a/include/uapi/linux/bits.h
+++ b/include/uapi/linux/bits.h
@@ -9,7 +9,7 @@
(~_UL(0) >> (__BITS_PER_LONG - 1 - (h))))
#define __GENMASK_ULL(h, l) \
- (((~_ULL(0)) - (_ULL(1) << (l)) + 1) & \
+ ((~_ULL(0) << (l)) & \
(~_ULL(0) >> (__BITS_PER_LONG_LONG - 1 - (h))))
#define __GENMASK_U128(h, l) \
--
2.43.0
Powered by blists - more mailing lists