Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

lib: test_mul_u64_u64_div_u64(): test both generic and arch versions

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.

Link: https://lkml.kernel.org/r/20251105201035.64043-7-david.laight.linux@gmail.com
Signed-off-by: David Laight <david.laight.linux@gmail.com>
Reviewed-by: Nicolas Pitre <npitre@baylibre.com>
Cc: Biju Das <biju.das.jz@bp.renesas.com>
Cc: Borislav Betkov <bp@alien8.de>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Li RongQing <lirongqing@baidu.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleinxer <tglx@linutronix.de>
Cc: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

David Laight and committed by
Andrew Morton
f0bff2eb 500db219

+51 -9
+6 -2
lib/math/div64.c
··· 177 177 * Iterative div/mod for use when dividend is not expected to be much 178 178 * bigger than divisor. 179 179 */ 180 + #ifndef iter_div_u64_rem 180 181 u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder) 181 182 { 182 183 return __iter_div_u64_rem(dividend, divisor, remainder); 183 184 } 184 185 EXPORT_SYMBOL(iter_div_u64_rem); 186 + #endif 185 187 186 - #ifndef mul_u64_add_u64_div_u64 188 + #if !defined(mul_u64_add_u64_div_u64) || defined(test_mul_u64_add_u64_div_u64) 187 189 u64 mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d) 188 190 { 189 - #if defined(__SIZEOF_INT128__) 191 + #if defined(__SIZEOF_INT128__) && !defined(test_mul_u64_add_u64_div_u64) 190 192 191 193 /* native 64x64=128 bits multiplication */ 192 194 u128 prod = (u128)a * b + c; ··· 269 267 270 268 return res; 271 269 } 270 + #if !defined(test_mul_u64_add_u64_div_u64) 272 271 EXPORT_SYMBOL(mul_u64_add_u64_div_u64); 272 + #endif 273 273 #endif
+45 -7
lib/math/test_mul_u64_u64_div_u64.c
··· 73 73 74 74 */ 75 75 76 - static int __init test_init(void) 76 + static u64 test_mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d); 77 + 78 + static int __init test_run(unsigned int fn_no, const char *fn_name) 77 79 { 80 + u64 start_time; 78 81 int errors = 0; 79 82 int tests = 0; 80 83 int i; 81 84 82 - pr_info("Starting mul_u64_u64_div_u64() test\n"); 85 + start_time = ktime_get_ns(); 83 86 84 87 for (i = 0; i < ARRAY_SIZE(test_values); i++) { 85 88 u64 a = test_values[i].a; 86 89 u64 b = test_values[i].b; 87 90 u64 d = test_values[i].d; 88 91 u64 expected_result = test_values[i].result; 89 - u64 result = mul_u64_u64_div_u64(a, b, d); 90 - u64 result_up = mul_u64_u64_div_u64_roundup(a, b, d); 92 + u64 result, result_up; 93 + 94 + switch (fn_no) { 95 + default: 96 + result = mul_u64_u64_div_u64(a, b, d); 97 + result_up = mul_u64_u64_div_u64_roundup(a, b, d); 98 + break; 99 + case 1: 100 + result = test_mul_u64_add_u64_div_u64(a, b, 0, d); 101 + result_up = test_mul_u64_add_u64_div_u64(a, b, d - 1, d); 102 + break; 103 + } 91 104 92 105 tests += 2; 93 106 ··· 119 106 } 120 107 } 121 108 122 - pr_info("Completed mul_u64_u64_div_u64() test, %d tests, %d errors\n", 123 - tests, errors); 124 - return errors ? -EINVAL : 0; 109 + pr_info("Completed %s() test, %d tests, %d errors, %llu ns\n", 110 + fn_name, tests, errors, ktime_get_ns() - start_time); 111 + return errors; 112 + } 113 + 114 + static int __init test_init(void) 115 + { 116 + pr_info("Starting mul_u64_u64_div_u64() test\n"); 117 + if (test_run(0, "mul_u64_u64_div_u64")) 118 + return -EINVAL; 119 + if (test_run(1, "test_mul_u64_u64_div_u64")) 120 + return -EINVAL; 121 + return 0; 125 122 } 126 123 127 124 static void __exit test_exit(void) 128 125 { 129 126 } 127 + 128 + /* Compile the generic mul_u64_add_u64_div_u64() code */ 129 + #undef __div64_32 130 + #define __div64_32 __div64_32 131 + #define div_s64_rem div_s64_rem 132 + #define div64_u64_rem div64_u64_rem 133 + #define div64_u64 div64_u64 134 + #define div64_s64 div64_s64 135 + #define iter_div_u64_rem iter_div_u64_rem 136 + 137 + #undef mul_u64_add_u64_div_u64 138 + #define mul_u64_add_u64_div_u64 test_mul_u64_add_u64_div_u64 139 + #define test_mul_u64_add_u64_div_u64 test_mul_u64_add_u64_div_u64 140 + 141 + #include "div64.c" 130 142 131 143 module_init(test_init); 132 144 module_exit(test_exit);