// SPDX-License-Identifier: GPL-2.0
/*
* Copyright ( C ) 2024 BayLibre SAS
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/init.h>
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/math64.h>
typedef struct { u64 a; u64 b; u64 c; u64 result; } test_params;
static test_params test_values[] = {
/* this contains many edge values followed by a couple random values */
{ 0 xb, 0 x7, 0 x3, 0 x19 },
{ 0 xffff0000, 0 xffff0000, 0 xf, 0 x1110eeef00000000 },
{ 0 xffffffff, 0 xffffffff, 0 x1, 0 xfffffffe00000001 },
{ 0 xffffffff, 0 xffffffff, 0 x2, 0 x7fffffff00000000 },
{ 0 x1ffffffff, 0 xffffffff, 0 x2, 0 xfffffffe80000000 },
{ 0 x1ffffffff, 0 xffffffff, 0 x3, 0 xaaaaaaa9aaaaaaab },
{ 0 x1ffffffff, 0 x1ffffffff, 0 x4, 0 xffffffff00000000 },
{ 0 xffff000000000000, 0 xffff000000000000, 0 xffff000000000001, 0 xfffeffffffffffff },
{ 0 x3333333333333333, 0 x3333333333333333, 0 x5555555555555555, 0 x1eb851eb851eb851 },
{ 0 x7fffffffffffffff, 0 x2, 0 x3, 0 x5555555555555554 },
{ 0 xffffffffffffffff, 0 x2, 0 x8000000000000000, 0 x3 },
{ 0 xffffffffffffffff, 0 x2, 0 xc000000000000000, 0 x2 },
{ 0 xffffffffffffffff, 0 x4000000000000004, 0 x8000000000000000, 0 x8000000000000007 },
{ 0 xffffffffffffffff, 0 x4000000000000001, 0 x8000000000000000, 0 x8000000000000001 },
{ 0 xffffffffffffffff, 0 x8000000000000001, 0 xffffffffffffffff, 0 x8000000000000001 },
{ 0 xfffffffffffffffe, 0 x8000000000000001, 0 xffffffffffffffff, 0 x8000000000000000 },
{ 0 xffffffffffffffff, 0 x8000000000000001, 0 xfffffffffffffffe, 0 x8000000000000001 },
{ 0 xffffffffffffffff, 0 x8000000000000001, 0 xfffffffffffffffd, 0 x8000000000000002 },
{ 0 x7fffffffffffffff, 0 xffffffffffffffff, 0 xc000000000000000, 0 xaaaaaaaaaaaaaaa8 },
{ 0 xffffffffffffffff, 0 x7fffffffffffffff, 0 xa000000000000000, 0 xccccccccccccccca },
{ 0 xffffffffffffffff, 0 x7fffffffffffffff, 0 x9000000000000000, 0 xe38e38e38e38e38b },
{ 0 x7fffffffffffffff, 0 x7fffffffffffffff, 0 x5000000000000000, 0 xccccccccccccccc9 },
{ 0 xffffffffffffffff, 0 xfffffffffffffffe, 0 xffffffffffffffff, 0 xfffffffffffffffe },
{ 0 xe6102d256d7ea3ae, 0 x70a77d0be4c31201, 0 xd63ec35ab3220357, 0 x78f8bf8cc86c6e18 },
{ 0 xf53bae05cb86c6e1, 0 x3847b32d2f8d32e0, 0 xcfd4f55a647f403c, 0 x42687f79d8998d35 },
{ 0 x9951c5498f941092, 0 x1f8c8bfdf287a251, 0 xa3c8dc5f81ea3fe2, 0 x1d887cb25900091f },
{ 0 x374fee9daa1bb2bb, 0 x0d0bfbff7b8ae3ef, 0 xc169337bd42d5179, 0 x03bb2dbaffcbb961 },
{ 0 xeac0d03ac10eeaf0, 0 x89be05dfa162ed9b, 0 x92bb1679a41f0e4b, 0 xdc5f5cc9e270d216 },
};
/*
* The above table can be verified with the following shell script :
*
* # ! / bin / sh
* sed - ne ' s / ^ { \ + \ ( . * \ ) , \ + \ ( . * \ ) , \ + \ ( . * \ ) , \ + \ ( . * \ ) } , $ / \ 1 \ 2 \ 3 \ 4 / p ' \
* lib / math / test_mul_u64_u64_div_u64 . c |
* while read a b c r ; do
* expected = $ ( printf " obase = 16 ; ibase = 16 ; % X * % X / % X \ n " $ a $ b $ c | bc )
* given = $ ( printf " % X \ n " $ r )
* if [ " $ expected " = " $ given " ] ; then
* echo " $ a * $ b / $ c = $ r OK "
* else
* echo " $ a * $ b / $ c = $ r is wrong " > & 2
* echo " should be equivalent to 0 x $ expected " > & 2
* exit 1
* fi
* done
*/
static int __init test_init(void )
{
int i;
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;
u64 result = mul_u64_u64_div_u64(a, b, c);
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);
}
}
pr_info("Completed mul_u64_u64_div_u64() test\n" );
return 0 ;
}
static void __exit test_exit(void )
{
}
module_init(test_init);
module_exit(test_exit);
MODULE_AUTHOR("Nicolas Pitre" );
MODULE_LICENSE("GPL" );
MODULE_DESCRIPTION("mul_u64_u64_div_u64() test module" );
Messung V0.5 in Prozent C=97 H=86 G=91
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-09-29)
¤
*© Formatika GbR, Deutschland