ctz_test.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /* ull version is not used on i386 other than in this test.
  23. Avoid requiring extra function.
  24. */
  25. #if defined (__i386__)
  26. #define SKIP_ULL 1
  27. #endif
  28. static grub_uint64_t vectors[] = {
  29. 0xffffffffffffffffULL, 1, 2, 0, 0x0102030405060708ULL
  30. };
  31. static void
  32. test_ui (unsigned int a)
  33. {
  34. int i;
  35. a |= 1;
  36. for (i = 0; i < (int) (8 * sizeof (a)); i++)
  37. {
  38. grub_test_assert (__builtin_ctz(a << i) == i,
  39. "ctz mismatch: ctz(0x%llx) != 0x%x",
  40. (long long) (a << i), __builtin_ctz(a << i));
  41. }
  42. }
  43. static void
  44. test_ul (unsigned long a)
  45. {
  46. int i;
  47. a |= 1;
  48. for (i = 0; i < (int) (8 * sizeof (a)); i++)
  49. {
  50. grub_test_assert (__builtin_ctzl(a << i) == i,
  51. "ctzl mismatch: ctzl(0x%llx) != 0x%x",
  52. (long long) (a << i), __builtin_ctz(a << i));
  53. }
  54. }
  55. #ifndef SKIP_ULL
  56. static void
  57. test_ull (unsigned long long a)
  58. {
  59. int i;
  60. a |= 1;
  61. for (i = 0; i < (int) (8 * sizeof (a)); i++)
  62. {
  63. grub_test_assert (__builtin_ctzll(a << i) == i,
  64. "ctzll mismatch: ctzll(0x%llx) != 0x%x",
  65. (long long) (a << i), __builtin_ctz(a << i));
  66. }
  67. }
  68. #endif
  69. static void
  70. test_all(grub_uint64_t a)
  71. {
  72. test_ui (a);
  73. test_ul (a);
  74. #ifndef SKIP_ULL
  75. test_ull (a);
  76. #endif
  77. }
  78. static void
  79. ctz_test (void)
  80. {
  81. grub_uint64_t a = 404, b = 7;
  82. grub_size_t i;
  83. for (i = 0; i < ARRAY_SIZE (vectors); i++)
  84. {
  85. test_all (vectors[i]);
  86. }
  87. for (i = 0; i < 40000; i++)
  88. {
  89. a = 17 * a + 13 * b;
  90. b = 23 * a + 29 * b;
  91. if (b == 0)
  92. b = 1;
  93. if (a == 0)
  94. a = 1;
  95. test_all (a);
  96. test_all (b);
  97. }
  98. }
  99. /* Register example_test method as a functional test. */
  100. GRUB_FUNCTIONAL_TEST (ctz_test, ctz_test);