Change the #if in div64.c so that test_mul_u64_u64_div_u64.c
can compile and test the generic version (including the 'long multiply')
on architectures (eg amd64) that define their own copy.
Test the kernel version and the locally compiled version on all arch.
Output the time taken (in ns) on the 'test completed' trace.
For reference, on my zen 5, the optimised version takes ~220ns and the
generic version ~3350ns.
Using the native multiply saves ~200ns and adding back the ilog2() 'optimisation'
test adds ~50ms.
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
Changes for v4:
- Fix build on non x86 (eg arm32)
lib/math/div64.c | 8 +++--
lib/math/test_mul_u64_u64_div_u64.c | 51 +++++++++++++++++++++++++----
2 files changed, 50 insertions(+), 9 deletions(-)
diff --git a/lib/math/div64.c b/lib/math/div64.c
index 25295daebde9..f92e7160feb6 100644
--- a/lib/math/div64.c
+++ b/lib/math/div64.c
@@ -177,16 +177,18 @@ EXPORT_SYMBOL(div64_s64);
* Iterative div/mod for use when dividend is not expected to be much
* bigger than divisor.
*/
+#ifndef iter_div_u64_rem
u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
{
return __iter_div_u64_rem(dividend, divisor, remainder);
}
EXPORT_SYMBOL(iter_div_u64_rem);
+#endif
-#ifndef mul_u64_add_u64_div_u64
+#if !defined(mul_u64_add_u64_div_u64) || defined(test_mul_u64_add_u64_div_u64)
u64 mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d)
{
-#if defined(__SIZEOF_INT128__)
+#if defined(__SIZEOF_INT128__) && !defined(test_mul_u64_add_u64_div_u64)
/* native 64x64=128 bits multiplication */
u128 prod = (u128)a * b + c;
@@ -267,5 +269,7 @@ u64 mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d)
return res;
}
+#if !defined(test_mul_u64_add_u64_div_u64)
EXPORT_SYMBOL(mul_u64_add_u64_div_u64);
#endif
+#endif
diff --git a/lib/math/test_mul_u64_u64_div_u64.c b/lib/math/test_mul_u64_u64_div_u64.c
index 4d5e4e5dac67..a3c5e54f37ef 100644
--- a/lib/math/test_mul_u64_u64_div_u64.c
+++ b/lib/math/test_mul_u64_u64_div_u64.c
@@ -73,21 +73,34 @@ done
*/
-static int __init test_init(void)
+static u64 test_mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d);
+
+static int __init test_run(unsigned int fn_no, const char *fn_name)
{
+ u64 start_time;
int errors = 0;
int tests = 0;
int i;
- pr_info("Starting mul_u64_u64_div_u64() test\n");
+ start_time = ktime_get_ns();
for (i = 0; i < ARRAY_SIZE(test_values); i++) {
u64 a = test_values[i].a;
u64 b = test_values[i].b;
u64 d = test_values[i].d;
u64 expected_result = test_values[i].result;
- u64 result = mul_u64_u64_div_u64(a, b, d);
- u64 result_up = mul_u64_u64_div_u64_roundup(a, b, d);
+ u64 result, result_up;
+
+ switch (fn_no) {
+ default:
+ result = mul_u64_u64_div_u64(a, b, d);
+ result_up = mul_u64_u64_div_u64_roundup(a, b, d);
+ break;
+ case 1:
+ result = test_mul_u64_add_u64_div_u64(a, b, 0, d);
+ result_up = test_mul_u64_add_u64_div_u64(a, b, d - 1, d);
+ break;
+ }
tests += 2;
@@ -106,15 +119,39 @@ static int __init test_init(void)
}
}
- pr_info("Completed mul_u64_u64_div_u64() test, %d tests, %d errors\n",
- tests, errors);
- return errors ? -EINVAL : 0;
+ pr_info("Completed %s() test, %d tests, %d errors, %llu ns\n",
+ fn_name, tests, errors, ktime_get_ns() - start_time);
+ return errors;
+}
+
+static int __init test_init(void)
+{
+ pr_info("Starting mul_u64_u64_div_u64() test\n");
+ if (test_run(0, "mul_u64_u64_div_u64"))
+ return -EINVAL;
+ if (test_run(1, "test_mul_u64_u64_div_u64"))
+ return -EINVAL;
+ return 0;
}
static void __exit test_exit(void)
{
}
+/* Compile the generic mul_u64_add_u64_div_u64() code */
+#define __div64_32 __div64_32
+#define div_s64_rem div_s64_rem
+#define div64_u64_rem div64_u64_rem
+#define div64_u64 div64_u64
+#define div64_s64 div64_s64
+#define iter_div_u64_rem iter_div_u64_rem
+
+#undef mul_u64_add_u64_div_u64
+#define mul_u64_add_u64_div_u64 test_mul_u64_add_u64_div_u64
+#define test_mul_u64_add_u64_div_u64 test_mul_u64_add_u64_div_u64
+
+#include "div64.c"
+
module_init(test_init);
module_exit(test_exit);
--
2.39.5
Hi David,
kernel test robot noticed the following build warnings:
[auto build test WARNING on next-20251029]
url: https://github.com/intel-lab-lkp/linux/commits/David-Laight/lib-mul_u64_u64_div_u64-rename-parameter-c-to-d/20251030-025633
base: next-20251029
patch link: https://lore.kernel.org/r/20251029173828.3682-7-david.laight.linux%40gmail.com
patch subject: [PATCH v4 next 6/9] lib: test_mul_u64_u64_div_u64: Test both generic and arch versions
config: i386-buildonly-randconfig-004-20251102 (https://download.01.org/0day-ci/archive/20251102/202511020421.ZZPBAIIw-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251102/202511020421.ZZPBAIIw-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202511020421.ZZPBAIIw-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> lib/math/test_mul_u64_u64_div_u64.c:142:9: warning: '__div64_32' macro redefined [-Wmacro-redefined]
142 | #define __div64_32 __div64_32
| ^
arch/x86/include/asm/div64.h:78:9: note: previous definition is here
78 | #define __div64_32
| ^
1 warning generated.
vim +/__div64_32 +142 lib/math/test_mul_u64_u64_div_u64.c
140
141 /* Compile the generic mul_u64_add_u64_div_u64() code */
> 142 #define __div64_32 __div64_32
143 #define div_s64_rem div_s64_rem
144 #define div64_u64_rem div64_u64_rem
145 #define div64_u64 div64_u64
146 #define div64_s64 div64_s64
147 #define iter_div_u64_rem iter_div_u64_rem
148
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
On Sun, 2 Nov 2025 04:59:10 +0800 kernel test robot <lkp@intel.com> wrote: > Hi David, > > kernel test robot noticed the following build warnings: > > [auto build test WARNING on next-20251029] > > url: https://github.com/intel-lab-lkp/linux/commits/David-Laight/lib-mul_u64_u64_div_u64-rename-parameter-c-to-d/20251030-025633 > base: next-20251029 > patch link: https://lore.kernel.org/r/20251029173828.3682-7-david.laight.linux%40gmail.com > patch subject: [PATCH v4 next 6/9] lib: test_mul_u64_u64_div_u64: Test both generic and arch versions > config: i386-buildonly-randconfig-004-20251102 (https://download.01.org/0day-ci/archive/20251102/202511020421.ZZPBAIIw-lkp@intel.com/config) > compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) > reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251102/202511020421.ZZPBAIIw-lkp@intel.com/reproduce) > > If you fix the issue in a separate patch/commit (i.e. not just a new version of > the same patch/commit), kindly add following tags > | Reported-by: kernel test robot <lkp@intel.com> > | Closes: https://lore.kernel.org/oe-kbuild-all/202511020421.ZZPBAIIw-lkp@intel.com/ > > All warnings (new ones prefixed by >>): > > >> lib/math/test_mul_u64_u64_div_u64.c:142:9: warning: '__div64_32' macro redefined [-Wmacro-redefined] > 142 | #define __div64_32 __div64_32 > | ^ > arch/x86/include/asm/div64.h:78:9: note: previous definition is here > 78 | #define __div64_32 That is preceded by a comment that says it can't happen for x86-64. I think it would be better as '#define __div64_32 @@@' so that you get a compile error. But that isn't part of this change, > | ^ > 1 warning generated. > > > vim +/__div64_32 +142 lib/math/test_mul_u64_u64_div_u64.c > > 140 > 141 /* Compile the generic mul_u64_add_u64_div_u64() code */ > > 142 #define __div64_32 __div64_32 It needs a preceding #undef Although I'm not sure why a normal build doesn't show it. Looks like I'll need to do a v5 :-( David > 143 #define div_s64_rem div_s64_rem > 144 #define div64_u64_rem div64_u64_rem > 145 #define div64_u64 div64_u64 > 146 #define div64_s64 div64_s64 > 147 #define iter_div_u64_rem iter_div_u64_rem > 148 >
Hi David,
kernel test robot noticed the following build warnings:
[auto build test WARNING on next-20251029]
url: https://github.com/intel-lab-lkp/linux/commits/David-Laight/lib-mul_u64_u64_div_u64-rename-parameter-c-to-d/20251030-025633
base: next-20251029
patch link: https://lore.kernel.org/r/20251029173828.3682-7-david.laight.linux%40gmail.com
patch subject: [PATCH v4 next 6/9] lib: test_mul_u64_u64_div_u64: Test both generic and arch versions
config: i386-randconfig-052-20251102 (https://download.01.org/0day-ci/archive/20251102/202511020341.TEhkaR65-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251102/202511020341.TEhkaR65-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202511020341.TEhkaR65-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> lib/math/test_mul_u64_u64_div_u64.c:142:9: warning: "__div64_32" redefined
142 | #define __div64_32 __div64_32
| ^~~~~~~~~~
In file included from include/linux/math.h:6,
from include/linux/math64.h:6,
from include/linux/time.h:6,
from include/linux/stat.h:19,
from include/linux/module.h:13,
from lib/math/test_mul_u64_u64_div_u64.c:9:
arch/x86/include/asm/div64.h:78:9: note: this is the location of the previous definition
78 | #define __div64_32
| ^~~~~~~~~~
vim +/__div64_32 +142 lib/math/test_mul_u64_u64_div_u64.c
140
141 /* Compile the generic mul_u64_add_u64_div_u64() code */
> 142 #define __div64_32 __div64_32
143 #define div_s64_rem div_s64_rem
144 #define div64_u64_rem div64_u64_rem
145 #define div64_u64 div64_u64
146 #define div64_s64 div64_s64
147 #define iter_div_u64_rem iter_div_u64_rem
148
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
On Wed, 29 Oct 2025, David Laight wrote:
> Change the #if in div64.c so that test_mul_u64_u64_div_u64.c
> can compile and test the generic version (including the 'long multiply')
> on architectures (eg amd64) that define their own copy.
>
> Test the kernel version and the locally compiled version on all arch.
> Output the time taken (in ns) on the 'test completed' trace.
>
> For reference, on my zen 5, the optimised version takes ~220ns and the
> generic version ~3350ns.
> Using the native multiply saves ~200ns and adding back the ilog2() 'optimisation'
> test adds ~50ms.
>
> Signed-off-by: David Laight <david.laight.linux@gmail.com>
Reviewed-by: Nicolas Pitre <npitre@baylibre.com>
> ---
>
> Changes for v4:
> - Fix build on non x86 (eg arm32)
>
> lib/math/div64.c | 8 +++--
> lib/math/test_mul_u64_u64_div_u64.c | 51 +++++++++++++++++++++++++----
> 2 files changed, 50 insertions(+), 9 deletions(-)
>
> diff --git a/lib/math/div64.c b/lib/math/div64.c
> index 25295daebde9..f92e7160feb6 100644
> --- a/lib/math/div64.c
> +++ b/lib/math/div64.c
> @@ -177,16 +177,18 @@ EXPORT_SYMBOL(div64_s64);
> * Iterative div/mod for use when dividend is not expected to be much
> * bigger than divisor.
> */
> +#ifndef iter_div_u64_rem
> u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
> {
> return __iter_div_u64_rem(dividend, divisor, remainder);
> }
> EXPORT_SYMBOL(iter_div_u64_rem);
> +#endif
>
> -#ifndef mul_u64_add_u64_div_u64
> +#if !defined(mul_u64_add_u64_div_u64) || defined(test_mul_u64_add_u64_div_u64)
> u64 mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d)
> {
> -#if defined(__SIZEOF_INT128__)
> +#if defined(__SIZEOF_INT128__) && !defined(test_mul_u64_add_u64_div_u64)
>
> /* native 64x64=128 bits multiplication */
> u128 prod = (u128)a * b + c;
> @@ -267,5 +269,7 @@ u64 mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d)
>
> return res;
> }
> +#if !defined(test_mul_u64_add_u64_div_u64)
> EXPORT_SYMBOL(mul_u64_add_u64_div_u64);
> #endif
> +#endif
> diff --git a/lib/math/test_mul_u64_u64_div_u64.c b/lib/math/test_mul_u64_u64_div_u64.c
> index 4d5e4e5dac67..a3c5e54f37ef 100644
> --- a/lib/math/test_mul_u64_u64_div_u64.c
> +++ b/lib/math/test_mul_u64_u64_div_u64.c
> @@ -73,21 +73,34 @@ done
>
> */
>
> -static int __init test_init(void)
> +static u64 test_mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d);
> +
> +static int __init test_run(unsigned int fn_no, const char *fn_name)
> {
> + u64 start_time;
> int errors = 0;
> int tests = 0;
> int i;
>
> - pr_info("Starting mul_u64_u64_div_u64() test\n");
> + start_time = ktime_get_ns();
>
> for (i = 0; i < ARRAY_SIZE(test_values); i++) {
> u64 a = test_values[i].a;
> u64 b = test_values[i].b;
> u64 d = test_values[i].d;
> u64 expected_result = test_values[i].result;
> - u64 result = mul_u64_u64_div_u64(a, b, d);
> - u64 result_up = mul_u64_u64_div_u64_roundup(a, b, d);
> + u64 result, result_up;
> +
> + switch (fn_no) {
> + default:
> + result = mul_u64_u64_div_u64(a, b, d);
> + result_up = mul_u64_u64_div_u64_roundup(a, b, d);
> + break;
> + case 1:
> + result = test_mul_u64_add_u64_div_u64(a, b, 0, d);
> + result_up = test_mul_u64_add_u64_div_u64(a, b, d - 1, d);
> + break;
> + }
>
> tests += 2;
>
> @@ -106,15 +119,39 @@ static int __init test_init(void)
> }
> }
>
> - pr_info("Completed mul_u64_u64_div_u64() test, %d tests, %d errors\n",
> - tests, errors);
> - return errors ? -EINVAL : 0;
> + pr_info("Completed %s() test, %d tests, %d errors, %llu ns\n",
> + fn_name, tests, errors, ktime_get_ns() - start_time);
> + return errors;
> +}
> +
> +static int __init test_init(void)
> +{
> + pr_info("Starting mul_u64_u64_div_u64() test\n");
> + if (test_run(0, "mul_u64_u64_div_u64"))
> + return -EINVAL;
> + if (test_run(1, "test_mul_u64_u64_div_u64"))
> + return -EINVAL;
> + return 0;
> }
>
> static void __exit test_exit(void)
> {
> }
>
> +/* Compile the generic mul_u64_add_u64_div_u64() code */
> +#define __div64_32 __div64_32
> +#define div_s64_rem div_s64_rem
> +#define div64_u64_rem div64_u64_rem
> +#define div64_u64 div64_u64
> +#define div64_s64 div64_s64
> +#define iter_div_u64_rem iter_div_u64_rem
> +
> +#undef mul_u64_add_u64_div_u64
> +#define mul_u64_add_u64_div_u64 test_mul_u64_add_u64_div_u64
> +#define test_mul_u64_add_u64_div_u64 test_mul_u64_add_u64_div_u64
> +
> +#include "div64.c"
> +
> module_init(test_init);
> module_exit(test_exit);
>
> --
> 2.39.5
>
>
© 2016 - 2025 Red Hat, Inc.