mul_test.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. test64(grub_uint64_t a, grub_uint64_t b)
  31. {
  32. grub_uint64_t r1 = a * b, r2 = 0, r3;
  33. int i;
  34. for (i = 0; i < 64; i++)
  35. if ((a & (1LL << i)))
  36. r2 += b << i;
  37. r3 = ((grub_int64_t) a) * ((grub_int64_t) b);
  38. grub_test_assert (r1 == r2,
  39. "multiplication mismatch (u): 0x%llx x 0x%llx = 0x%llx != 0x%llx",
  40. (long long) a, (long long) b, (long long) r2, (long long) r1);
  41. grub_test_assert (r3 == r2,
  42. "multiplication mismatch (s): 0x%llx x 0x%llx = 0x%llx != 0x%llx",
  43. (long long) a, (long long) b, (long long) r2, (long long) r3);
  44. }
  45. static void
  46. mul_test (void)
  47. {
  48. grub_uint64_t a = 404, b = 7;
  49. grub_size_t i;
  50. for (i = 0; i < ARRAY_SIZE (vectors); i++)
  51. {
  52. test64 (vectors[i][0], vectors[i][1]);
  53. }
  54. for (i = 0; i < 40000; i++)
  55. {
  56. a = 17 * a + 13 * b;
  57. b = 23 * a + 29 * b;
  58. if (b == 0)
  59. b = 1;
  60. if (a == 0)
  61. a = 1;
  62. test64 (a, b);
  63. }
  64. }
  65. /* Register example_test method as a functional test. */
  66. GRUB_FUNCTIONAL_TEST (mul_test, mul_test);