From: David Laight <david.laight.linux@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>, linux-kernel@vger.kernel.org
Cc: "David Laight" <david.laight.linux@gmail.com>,
"Uwe Kleine-König" <u.kleine-koenig@baylibre.com>,
"Nicolas Pitre" <npitre@baylibre.com>,
"Oleg Nesterov" <oleg@redhat.com>,
"Peter Zijlstra" <peterz@infradead.org>,
"Biju Das" <biju.das.jz@bp.renesas.com>
Subject: [PATCH 3/3] lib: Update the muldiv64 tests to verify the C on x86-64
Date: Sat, 5 Apr 2025 21:45:30 +0100 [thread overview]
Message-ID: <20250405204530.186242-4-david.laight.linux@gmail.com> (raw)
In-Reply-To: <20250405204530.186242-1-david.laight.linux@gmail.com>
div64.c contains a 128 by 64 division algorithm which x86-64 overrides
it with an asm implementation.
So running the muldiv64 tests only verifies the asm code.
Since x86-64 is the most likely test system compile the default
code into an x86-64 kernel (under a different name) when the tests
are being built.
Verify that both the asm and C functions generate the correct results.
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
lib/math/div64.c | 18 ++++++++++++++++--
lib/math/test_mul_u64_u64_div_u64.c | 26 ++++++++++++++++++++------
2 files changed, 36 insertions(+), 8 deletions(-)
diff --git a/lib/math/div64.c b/lib/math/div64.c
index 50e025174495..38ee5c01c288 100644
--- a/lib/math/div64.c
+++ b/lib/math/div64.c
@@ -25,6 +25,8 @@
#include <linux/minmax.h>
#include <linux/log2.h>
+#include <generated/autoconf.h>
+
/* Not needed on 64bit architectures */
#if BITS_PER_LONG == 32
@@ -183,10 +185,22 @@ u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
}
EXPORT_SYMBOL(iter_div_u64_rem);
-#if !defined(mul_u64_add_u64_div_u64)
+/*
+ * If the architecture overrides the implementation below and the test module
+ * is being built then compile the default implementation with a different name
+ * so that it can be tested.
+ */
+#if defined(mul_u64_add_u64_div_u64) && (defined(CONFIG_TEST_MULDIV64) || defined(CONFIG_TEST_MULDIV64_MODULE))
+#define TEST_MULDIV64
+#undef mul_u64_add_u64_div_u64
+#define mul_u64_add_u64_div_u64 mul_u64_add_u64_div_u64_test
+u64 mul_u64_add_u64_div_u64_test(u64 a, u64 b, u64 c, u64 d);
+#endif
+
+#if !defined( mul_u64_add_u64_div_u64) || defined(TEST_MULDIV64)
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_MULDIV64)
/* native 64x64=128 bits multiplication */
u128 prod = (u128)a * b + c;
diff --git a/lib/math/test_mul_u64_u64_div_u64.c b/lib/math/test_mul_u64_u64_div_u64.c
index 9548eb7458c7..e2289b412601 100644
--- a/lib/math/test_mul_u64_u64_div_u64.c
+++ b/lib/math/test_mul_u64_u64_div_u64.c
@@ -73,6 +73,10 @@ done
*/
+#ifdef mul_u64_add_u64_div_u64
+u64 mul_u64_add_u64_div_u64_test(u64 a, u64 b, u64 add, u64 c);
+#endif
+
static int __init test_init(void)
{
int errors = 0;
@@ -80,21 +84,31 @@ static int __init test_init(void)
pr_info("Starting mul_u64_u64_div_u64() test\n");
- for (i = 0; i < ARRAY_SIZE(test_values); i++) {
- u64 a = test_values[i].a;
- u64 b = test_values[i].b;
- u64 c = test_values[i].c;
- u64 expected_result = test_values[i].result;
+ for (i = 0; i < ARRAY_SIZE(test_values) * 2; i++) {
+ u64 a = test_values[i / 2].a;
+ u64 b = test_values[i / 2].b;
+ u64 c = test_values[i / 2].c;
+ u64 expected_result = test_values[i / 2].result;
u64 result = mul_u64_u64_div_u64(a, b, c);
u64 result_up = mul_u64_u64_div_u64_roundup(a, b, c);
+#ifdef mul_u64_add_u64_div_u64
+ if (i & 1) {
+ /* Verify the generic C version */
+ result = mul_u64_add_u64_div_u64_test(a, b, 0, c);
+ result_up = mul_u64_add_u64_div_u64_test(a, b, c - 1, c);
+ }
+#else
+ i++;
+#endif
+
if (result != expected_result) {
pr_err("ERROR: 0x%016llx * 0x%016llx / 0x%016llx\n", a, b, c);
pr_err("ERROR: expected result: %016llx\n", expected_result);
pr_err("ERROR: obtained result: %016llx\n", result);
errors++;
}
- expected_result += test_values[i].round_up;
+ expected_result += test_values[i / 2].round_up;
if (result_up != expected_result) {
pr_err("ERROR: 0x%016llx * 0x%016llx +/ 0x%016llx\n", a, b, c);
pr_err("ERROR: expected result: %016llx\n", expected_result);
--
2.39.5
next prev parent reply other threads:[~2025-04-05 20:45 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-05 20:45 [PATCH next 0/3] lib: Implement mul_u64_u64_div_u64_roundup() David Laight
2025-04-05 20:45 ` [PATCH 1/3] lib: Add mul_u64_add_u64_div_u64() and mul_u64_u64_div_u64_roundup() David Laight
2025-04-06 1:46 ` Nicolas Pitre
2025-04-06 3:06 ` Nicolas Pitre
2025-04-06 9:35 ` David Laight
2025-04-06 12:30 ` David Laight
2025-04-05 20:45 ` [PATCH 2/3] lib: Add tests for mul_u64_u64_div_u64_roundup() David Laight
2025-04-06 1:47 ` Nicolas Pitre
2025-04-05 20:45 ` David Laight [this message]
2025-04-06 2:26 ` [PATCH 3/3] lib: Update the muldiv64 tests to verify the C on x86-64 Nicolas Pitre
2025-05-16 9:47 ` [PATCH next 0/3] lib: Implement mul_u64_u64_div_u64_roundup() Uwe Kleine-König
2025-05-16 12:17 ` David Laight
2025-05-16 15:49 ` Nicolas Pitre
2025-05-18 13:43 ` David Laight
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=20250405204530.186242-4-david.laight.linux@gmail.com \
--to=david.laight.linux@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=biju.das.jz@bp.renesas.com \
--cc=linux-kernel@vger.kernel.org \
--cc=npitre@baylibre.com \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
--cc=u.kleine-koenig@baylibre.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.