test_mul_u64_u64_div_u64.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2024 BayLibre SAS
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/printk.h>
  9. #include <linux/math64.h>
  10. typedef struct { u64 a; u64 b; u64 d; u64 result; uint round_up;} test_params;
  11. static test_params test_values[] = {
  12. /* this contains many edge values followed by a couple random values */
  13. { 0xb, 0x7, 0x3, 0x19, 1 },
  14. { 0xffff0000, 0xffff0000, 0xf, 0x1110eeef00000000, 0 },
  15. { 0xffffffff, 0xffffffff, 0x1, 0xfffffffe00000001, 0 },
  16. { 0xffffffff, 0xffffffff, 0x2, 0x7fffffff00000000, 1 },
  17. { 0x1ffffffff, 0xffffffff, 0x2, 0xfffffffe80000000, 1 },
  18. { 0x1ffffffff, 0xffffffff, 0x3, 0xaaaaaaa9aaaaaaab, 0 },
  19. { 0x1ffffffff, 0x1ffffffff, 0x4, 0xffffffff00000000, 1 },
  20. { 0xffff000000000000, 0xffff000000000000, 0xffff000000000001, 0xfffeffffffffffff, 1 },
  21. { 0x3333333333333333, 0x3333333333333333, 0x5555555555555555, 0x1eb851eb851eb851, 1 },
  22. { 0x7fffffffffffffff, 0x2, 0x3, 0x5555555555555554, 1 },
  23. { 0xffffffffffffffff, 0x2, 0x8000000000000000, 0x3, 1 },
  24. { 0xffffffffffffffff, 0x2, 0xc000000000000000, 0x2, 1 },
  25. { 0xffffffffffffffff, 0x4000000000000004, 0x8000000000000000, 0x8000000000000007, 1 },
  26. { 0xffffffffffffffff, 0x4000000000000001, 0x8000000000000000, 0x8000000000000001, 1 },
  27. { 0xffffffffffffffff, 0x8000000000000001, 0xffffffffffffffff, 0x8000000000000001, 0 },
  28. { 0xfffffffffffffffe, 0x8000000000000001, 0xffffffffffffffff, 0x8000000000000000, 1 },
  29. { 0xffffffffffffffff, 0x8000000000000001, 0xfffffffffffffffe, 0x8000000000000001, 1 },
  30. { 0xffffffffffffffff, 0x8000000000000001, 0xfffffffffffffffd, 0x8000000000000002, 1 },
  31. { 0x7fffffffffffffff, 0xffffffffffffffff, 0xc000000000000000, 0xaaaaaaaaaaaaaaa8, 1 },
  32. { 0xffffffffffffffff, 0x7fffffffffffffff, 0xa000000000000000, 0xccccccccccccccca, 1 },
  33. { 0xffffffffffffffff, 0x7fffffffffffffff, 0x9000000000000000, 0xe38e38e38e38e38b, 1 },
  34. { 0x7fffffffffffffff, 0x7fffffffffffffff, 0x5000000000000000, 0xccccccccccccccc9, 1 },
  35. { 0xffffffffffffffff, 0xfffffffffffffffe, 0xffffffffffffffff, 0xfffffffffffffffe, 0 },
  36. { 0xe6102d256d7ea3ae, 0x70a77d0be4c31201, 0xd63ec35ab3220357, 0x78f8bf8cc86c6e18, 1 },
  37. { 0xf53bae05cb86c6e1, 0x3847b32d2f8d32e0, 0xcfd4f55a647f403c, 0x42687f79d8998d35, 1 },
  38. { 0x9951c5498f941092, 0x1f8c8bfdf287a251, 0xa3c8dc5f81ea3fe2, 0x1d887cb25900091f, 1 },
  39. { 0x374fee9daa1bb2bb, 0x0d0bfbff7b8ae3ef, 0xc169337bd42d5179, 0x03bb2dbaffcbb961, 1 },
  40. { 0xeac0d03ac10eeaf0, 0x89be05dfa162ed9b, 0x92bb1679a41f0e4b, 0xdc5f5cc9e270d216, 1 },
  41. };
  42. /*
  43. * The above table can be verified with the following shell script:
  44. #!/bin/sh
  45. sed -ne 's/^{ \+\(.*\), \+\(.*\), \+\(.*\), \+\(.*\), \+\(.*\) },$/\1 \2 \3 \4 \5/p' \
  46. lib/math/test_mul_u64_u64_div_u64.c |
  47. while read a b d r e; do
  48. expected=$( printf "obase=16; ibase=16; %X * %X / %X\n" $a $b $d | bc )
  49. given=$( printf "%X\n" $r )
  50. if [ "$expected" = "$given" ]; then
  51. echo "$a * $b / $d = $r OK"
  52. else
  53. echo "$a * $b / $d = $r is wrong" >&2
  54. echo "should be equivalent to 0x$expected" >&2
  55. exit 1
  56. fi
  57. expected=$( printf "obase=16; ibase=16; (%X * %X + %X) / %X\n" $a $b $((d-1)) $d | bc )
  58. given=$( printf "%X\n" $((r + e)) )
  59. if [ "$expected" = "$given" ]; then
  60. echo "$a * $b +/ $d = $(printf '%#x' $((r + e))) OK"
  61. else
  62. echo "$a * $b +/ $d = $(printf '%#x' $((r + e))) is wrong" >&2
  63. echo "should be equivalent to 0x$expected" >&2
  64. exit 1
  65. fi
  66. done
  67. */
  68. static u64 test_mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d);
  69. #if __LONG_WIDTH__ >= 64
  70. #define TEST_32BIT_DIV
  71. static u64 test_mul_u64_add_u64_div_u64_32bit(u64 a, u64 b, u64 c, u64 d);
  72. #endif
  73. static int __init test_run(unsigned int fn_no, const char *fn_name)
  74. {
  75. u64 start_time;
  76. int errors = 0;
  77. int tests = 0;
  78. int i;
  79. start_time = ktime_get_ns();
  80. for (i = 0; i < ARRAY_SIZE(test_values); i++) {
  81. u64 a = test_values[i].a;
  82. u64 b = test_values[i].b;
  83. u64 d = test_values[i].d;
  84. u64 expected_result = test_values[i].result;
  85. u64 result, result_up;
  86. switch (fn_no) {
  87. default:
  88. result = mul_u64_u64_div_u64(a, b, d);
  89. result_up = mul_u64_u64_div_u64_roundup(a, b, d);
  90. break;
  91. case 1:
  92. result = test_mul_u64_add_u64_div_u64(a, b, 0, d);
  93. result_up = test_mul_u64_add_u64_div_u64(a, b, d - 1, d);
  94. break;
  95. #ifdef TEST_32BIT_DIV
  96. case 2:
  97. result = test_mul_u64_add_u64_div_u64_32bit(a, b, 0, d);
  98. result_up = test_mul_u64_add_u64_div_u64_32bit(a, b, d - 1, d);
  99. break;
  100. #endif
  101. }
  102. tests += 2;
  103. if (result != expected_result) {
  104. pr_err("ERROR: 0x%016llx * 0x%016llx / 0x%016llx\n", a, b, d);
  105. pr_err("ERROR: expected result: %016llx\n", expected_result);
  106. pr_err("ERROR: obtained result: %016llx\n", result);
  107. errors++;
  108. }
  109. expected_result += test_values[i].round_up;
  110. if (result_up != expected_result) {
  111. pr_err("ERROR: 0x%016llx * 0x%016llx +/ 0x%016llx\n", a, b, d);
  112. pr_err("ERROR: expected result: %016llx\n", expected_result);
  113. pr_err("ERROR: obtained result: %016llx\n", result_up);
  114. errors++;
  115. }
  116. }
  117. pr_info("Completed %s() test, %d tests, %d errors, %llu ns\n",
  118. fn_name, tests, errors, ktime_get_ns() - start_time);
  119. return errors;
  120. }
  121. static int __init test_init(void)
  122. {
  123. pr_info("Starting mul_u64_u64_div_u64() test\n");
  124. if (test_run(0, "mul_u64_u64_div_u64"))
  125. return -EINVAL;
  126. if (test_run(1, "test_mul_u64_u64_div_u64"))
  127. return -EINVAL;
  128. #ifdef TEST_32BIT_DIV
  129. if (test_run(2, "test_mul_u64_u64_div_u64_32bit"))
  130. return -EINVAL;
  131. #endif
  132. return 0;
  133. }
  134. static void __exit test_exit(void)
  135. {
  136. }
  137. /* Compile the generic mul_u64_add_u64_div_u64() code */
  138. #undef __div64_32
  139. #define __div64_32 __div64_32
  140. #define div_s64_rem div_s64_rem
  141. #define div64_u64_rem div64_u64_rem
  142. #define div64_u64 div64_u64
  143. #define div64_s64 div64_s64
  144. #define iter_div_u64_rem iter_div_u64_rem
  145. #undef mul_u64_add_u64_div_u64
  146. #define mul_u64_add_u64_div_u64 test_mul_u64_add_u64_div_u64
  147. #define test_mul_u64_add_u64_div_u64 test_mul_u64_add_u64_div_u64
  148. #include "div64.c"
  149. #ifdef TEST_32BIT_DIV
  150. /* Recompile the generic code for 32bit long */
  151. #undef test_mul_u64_add_u64_div_u64
  152. #define test_mul_u64_add_u64_div_u64 test_mul_u64_add_u64_div_u64_32bit
  153. #undef BITS_PER_ITER
  154. #define BITS_PER_ITER 16
  155. #define mul_u64_u64_add_u64 mul_u64_u64_add_u64_32bit
  156. #undef mul_u64_long_add_u64
  157. #undef add_u64_long
  158. #undef mul_add
  159. #include "div64.c"
  160. #endif
  161. module_init(test_init);
  162. module_exit(test_exit);
  163. MODULE_AUTHOR("Nicolas Pitre");
  164. MODULE_LICENSE("GPL");
  165. MODULE_DESCRIPTION("mul_u64_u64_div_u64() test module");