~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/math/test_mul_u64_u64_div_u64.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * Copyright (C) 2024 BayLibre SAS
  4  */
  5 
  6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7 
  8 #include <linux/init.h>
  9 #include <linux/module.h>
 10 #include <linux/printk.h>
 11 #include <linux/math64.h>
 12 
 13 typedef struct { u64 a; u64 b; u64 c; u64 result; } test_params;
 14 
 15 static test_params test_values[] = {
 16 /* this contains many edge values followed by a couple random values */
 17 {                0xb,                0x7,                0x3,               0x19 },
 18 {         0xffff0000,         0xffff0000,                0xf, 0x1110eeef00000000 },
 19 {         0xffffffff,         0xffffffff,                0x1, 0xfffffffe00000001 },
 20 {         0xffffffff,         0xffffffff,                0x2, 0x7fffffff00000000 },
 21 {        0x1ffffffff,         0xffffffff,                0x2, 0xfffffffe80000000 },
 22 {        0x1ffffffff,         0xffffffff,                0x3, 0xaaaaaaa9aaaaaaab },
 23 {        0x1ffffffff,        0x1ffffffff,                0x4, 0xffffffff00000000 },
 24 { 0xffff000000000000, 0xffff000000000000, 0xffff000000000001, 0xfffeffffffffffff },
 25 { 0x3333333333333333, 0x3333333333333333, 0x5555555555555555, 0x1eb851eb851eb851 },
 26 { 0x7fffffffffffffff,                0x2,                0x3, 0x5555555555555554 },
 27 { 0xffffffffffffffff,                0x2, 0x8000000000000000,                0x3 },
 28 { 0xffffffffffffffff,                0x2, 0xc000000000000000,                0x2 },
 29 { 0xffffffffffffffff, 0x4000000000000004, 0x8000000000000000, 0x8000000000000007 },
 30 { 0xffffffffffffffff, 0x4000000000000001, 0x8000000000000000, 0x8000000000000001 },
 31 { 0xffffffffffffffff, 0x8000000000000001, 0xffffffffffffffff, 0x8000000000000001 },
 32 { 0xfffffffffffffffe, 0x8000000000000001, 0xffffffffffffffff, 0x8000000000000000 },
 33 { 0xffffffffffffffff, 0x8000000000000001, 0xfffffffffffffffe, 0x8000000000000001 },
 34 { 0xffffffffffffffff, 0x8000000000000001, 0xfffffffffffffffd, 0x8000000000000002 },
 35 { 0x7fffffffffffffff, 0xffffffffffffffff, 0xc000000000000000, 0xaaaaaaaaaaaaaaa8 },
 36 { 0xffffffffffffffff, 0x7fffffffffffffff, 0xa000000000000000, 0xccccccccccccccca },
 37 { 0xffffffffffffffff, 0x7fffffffffffffff, 0x9000000000000000, 0xe38e38e38e38e38b },
 38 { 0x7fffffffffffffff, 0x7fffffffffffffff, 0x5000000000000000, 0xccccccccccccccc9 },
 39 { 0xffffffffffffffff, 0xfffffffffffffffe, 0xffffffffffffffff, 0xfffffffffffffffe },
 40 { 0xe6102d256d7ea3ae, 0x70a77d0be4c31201, 0xd63ec35ab3220357, 0x78f8bf8cc86c6e18 },
 41 { 0xf53bae05cb86c6e1, 0x3847b32d2f8d32e0, 0xcfd4f55a647f403c, 0x42687f79d8998d35 },
 42 { 0x9951c5498f941092, 0x1f8c8bfdf287a251, 0xa3c8dc5f81ea3fe2, 0x1d887cb25900091f },
 43 { 0x374fee9daa1bb2bb, 0x0d0bfbff7b8ae3ef, 0xc169337bd42d5179, 0x03bb2dbaffcbb961 },
 44 { 0xeac0d03ac10eeaf0, 0x89be05dfa162ed9b, 0x92bb1679a41f0e4b, 0xdc5f5cc9e270d216 },
 45 };
 46 
 47 /*
 48  * The above table can be verified with the following shell script:
 49  *
 50  * #!/bin/sh
 51  * sed -ne 's/^{ \+\(.*\), \+\(.*\), \+\(.*\), \+\(.*\) },$/\1 \2 \3 \4/p' \
 52  *     lib/math/test_mul_u64_u64_div_u64.c |
 53  * while read a b c r; do
 54  *   expected=$( printf "obase=16; ibase=16; %X * %X / %X\n" $a $b $c | bc )
 55  *   given=$( printf "%X\n" $r )
 56  *   if [ "$expected" = "$given" ]; then
 57  *     echo "$a * $b / $c = $r OK"
 58  *   else
 59  *     echo "$a * $b / $c = $r is wrong" >&2
 60  *     echo "should be equivalent to 0x$expected" >&2
 61  *     exit 1
 62  *   fi
 63  * done
 64  */
 65 
 66 static int __init test_init(void)
 67 {
 68         int i;
 69 
 70         pr_info("Starting mul_u64_u64_div_u64() test\n");
 71 
 72         for (i = 0; i < ARRAY_SIZE(test_values); i++) {
 73                 u64 a = test_values[i].a;
 74                 u64 b = test_values[i].b;
 75                 u64 c = test_values[i].c;
 76                 u64 expected_result = test_values[i].result;
 77                 u64 result = mul_u64_u64_div_u64(a, b, c);
 78 
 79                 if (result != expected_result) {
 80                         pr_err("ERROR: 0x%016llx * 0x%016llx / 0x%016llx\n", a, b, c);
 81                         pr_err("ERROR: expected result: %016llx\n", expected_result);
 82                         pr_err("ERROR: obtained result: %016llx\n", result);
 83                 }
 84         }
 85 
 86         pr_info("Completed mul_u64_u64_div_u64() test\n");
 87         return 0;
 88 }
 89 
 90 static void __exit test_exit(void)
 91 {
 92 }
 93 
 94 module_init(test_init);
 95 module_exit(test_exit);
 96 
 97 MODULE_AUTHOR("Nicolas Pitre");
 98 MODULE_LICENSE("GPL");
 99 MODULE_DESCRIPTION("mul_u64_u64_div_u64() test module");
100 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php