div_test.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/test.h>
  19. #include <grub/dl.h>
  20. #include <grub/misc.h>
  21. GRUB_MOD_LICENSE ("GPLv3+");
  22. static grub_uint64_t vectors[][2] = {
  23. { 0xffffffffffffffffULL, 1},
  24. { 1, 0xffffffffffffffffULL},
  25. { 0xffffffffffffffffULL, 0xffffffffffffffffULL},
  26. { 1, 1 },
  27. { 2, 1 }
  28. };
  29. static void
  30. test32 (grub_uint32_t a, grub_uint32_t b)
  31. {
  32. grub_uint64_t q, r;
  33. if (b == 0)
  34. return;
  35. q = grub_divmod64 (a, b, &r);
  36. grub_test_assert (r < b, "remainder is larger than dividend: 0x%llx %% 0x%llx = 0x%llx",
  37. (long long) a, (long long) b, (long long) r);
  38. grub_test_assert (q * b + r == a, "division doesn't satisfy base property: 0x%llx * 0x%llx + 0x%llx != 0x%llx", (long long) q, (long long) b, (long long) r,
  39. (long long) a);
  40. /* Overflow check. */
  41. grub_test_assert ((q >> 32) == 0,
  42. "division overflow in 0x%llx, 0x%llx", (long long) a, (long long) b);
  43. grub_test_assert ((r >> 32) == 0,
  44. "division overflow in 0x%llx, 0x%llx", (long long) a, (long long) b);
  45. /* q * b + r is at most:
  46. 0xffffffff * 0xffffffff + 0xffffffff = 0xffffffff00000000
  47. so no overflow
  48. */
  49. grub_test_assert (q == (a / b),
  50. "C compiler division failure in 0x%llx, 0x%llx", (long long) a, (long long) b);
  51. grub_test_assert (r == (a % b),
  52. "C compiler modulo failure in 0x%llx, 0x%llx", (long long) a, (long long) b);
  53. }
  54. static void
  55. test64 (grub_uint64_t a, grub_uint64_t b)
  56. {
  57. grub_uint64_t q, r;
  58. grub_uint64_t x1, x2;
  59. q = grub_divmod64 (a, b, &r);
  60. grub_test_assert (r < b, "remainder is larger than dividend: 0x%llx %% 0x%llx = 0x%llx",
  61. (long long) a, (long long) b, (long long) r);
  62. grub_test_assert (q * b + r == a, "division doesn't satisfy base property: 0x%llx * 0x%llx + 0x%llx != 0x%llx", (long long) q, (long long) b, (long long) r,
  63. (long long) a);
  64. /* Overflow checks. */
  65. grub_test_assert ((q >> 32) * (b >> 32) == 0,
  66. "division overflow in 0x%llx, 0x%llx", (long long) a, (long long) b);
  67. x1 = (q >> 32) * (b & 0xffffffff);
  68. grub_test_assert (x1 < (1LL << 32),
  69. "division overflow in 0x%llx, 0x%llx", (long long) a, (long long) b);
  70. x1 <<= 32;
  71. x2 = (b >> 32) * (q & 0xffffffff);
  72. grub_test_assert (x2 < (1LL << 32),
  73. "division overflow in 0x%llx, 0x%llx", (long long) a, (long long) b);
  74. x2 <<= 32;
  75. grub_test_assert (x1 <= ~x2,
  76. "division overflow in 0x%llx, 0x%llx", (long long) a, (long long) b);
  77. x1 += x2;
  78. x2 = (q & 0xffffffff) * (b & 0xffffffff);
  79. grub_test_assert (x1 <= ~x2,
  80. "division overflow in 0x%llx, 0x%llx", (long long) a, (long long) b);
  81. x1 += x2;
  82. grub_test_assert (x1 <= ~r,
  83. "division overflow in 0x%llx, 0x%llx", (long long) a, (long long) b);
  84. x1 += r;
  85. grub_test_assert (a == x1,
  86. "division overflow test failure in 0x%llx, 0x%llx", (long long) a, (long long) b);
  87. #if GRUB_TARGET_SIZEOF_VOID_P == 8
  88. grub_test_assert (q == (a / b),
  89. "C compiler division failure in 0x%llx, 0x%llx", (long long) a, (long long) b);
  90. grub_test_assert (r == (a % b),
  91. "C compiler modulo failure in 0x%llx, 0x%llx", (long long) a, (long long) b);
  92. #endif
  93. }
  94. static grub_int64_t
  95. abs64(grub_int64_t a)
  96. {
  97. return a > 0 ? a : -a;
  98. }
  99. static void
  100. test32s (grub_int32_t a, grub_int32_t b)
  101. {
  102. grub_int64_t q, r;
  103. if (b == 0)
  104. return;
  105. q = grub_divmod64s (a, b, &r);
  106. grub_test_assert (a > 0 ? r >= 0 : r <= 0, "remainder sign mismatch: %lld %% %lld = %lld",
  107. (long long) a, (long long) b, (long long) r);
  108. grub_test_assert (((a > 0) == (b > 0)) ? q >= 0 : q <= 0, "quotient sign mismatch: %lld / %lld = %lld",
  109. (long long) a, (long long) b, (long long) q);
  110. grub_test_assert (abs64(r) < abs64(b), "remainder is larger than dividend: %lld %% %lld = %lld",
  111. (long long) a, (long long) b, (long long) r);
  112. grub_test_assert (q * b + r == a, "division doesn't satisfy base property: %lld * %lld + %lld != %lld", (long long) q, (long long) b, (long long) r,
  113. (long long) a);
  114. if (0) { grub_test_assert (q == (a / b),
  115. "C compiler division failure in 0x%llx, 0x%llx", (long long) a, (long long) b);
  116. grub_test_assert (r == (a % b),
  117. "C compiler modulo failure in 0x%llx, 0x%llx", (long long) a, (long long) b);
  118. }
  119. }
  120. static void
  121. test64s (grub_int64_t a, grub_int64_t b)
  122. {
  123. grub_int64_t q, r;
  124. q = grub_divmod64s (a, b, &r);
  125. grub_test_assert (a > 0 ? r >= 0 : r <= 0, "remainder sign mismatch: %lld %% %lld = %lld",
  126. (long long) a, (long long) b, (long long) r);
  127. grub_test_assert (((a > 0) == (b > 0)) ? q >= 0 : q <= 0, "quotient sign mismatch: %lld / %lld = %lld",
  128. (long long) a, (long long) b, (long long) q);
  129. grub_test_assert (abs64(r) < abs64(b), "remainder is larger than dividend: %lld %% %lld = %lld",
  130. (long long) a, (long long) b, (long long) r);
  131. grub_test_assert (q * b + r == a, "division doesn't satisfy base property: 0x%llx * 0x%llx + 0x%llx != 0x%llx", (long long) q, (long long) b, (long long) r,
  132. (long long) a);
  133. #if GRUB_TARGET_SIZEOF_VOID_P == 8
  134. grub_test_assert (q == (a / b),
  135. "C compiler division failure in 0x%llx, 0x%llx", (long long) a, (long long) b);
  136. grub_test_assert (r == (a % b),
  137. "C compiler modulo failure in 0x%llx, 0x%llx", (long long) a, (long long) b);
  138. #endif
  139. }
  140. static void
  141. test_all(grub_uint64_t a, grub_uint64_t b)
  142. {
  143. test64 (a, b);
  144. test32 (a, b);
  145. test64s (a, b);
  146. test32s (a, b);
  147. test64s (a, -b);
  148. test32s (a, -b);
  149. test64s (-a, b);
  150. test32s (-a, b);
  151. test64s (-a, -b);
  152. test32s (-a, -b);
  153. }
  154. static void
  155. div_test (void)
  156. {
  157. grub_uint64_t a = 404, b = 7;
  158. grub_size_t i;
  159. for (i = 0; i < ARRAY_SIZE (vectors); i++)
  160. {
  161. test_all (vectors[i][0], vectors[i][1]);
  162. }
  163. for (i = 0; i < 40000; i++)
  164. {
  165. a = 17 * a + 13 * b;
  166. b = 23 * a + 29 * b;
  167. if (b == 0)
  168. b = 1;
  169. if (a == 0)
  170. a = 1;
  171. test_all (a, b);
  172. }
  173. }
  174. /* Register example_test method as a functional test. */
  175. GRUB_FUNCTIONAL_TEST (div_test, div_test);