shift_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2015 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[] = {
  23. 0xffffffffffffffffULL, 1, 2, 0, 0x0102030405060708ULL
  24. };
  25. /* We're testing shifts, don't replace access to this with a shift. */
  26. static const grub_uint8_t bitmask[] =
  27. { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  28. typedef union {
  29. grub_uint64_t v64;
  30. grub_uint8_t v8[8];
  31. } grub_raw_u64_t;
  32. static int
  33. get_bit64 (grub_uint64_t v, int b)
  34. {
  35. grub_raw_u64_t vr = { .v64 = v };
  36. grub_uint8_t *p = vr.v8;
  37. if (b >= 64)
  38. return 0;
  39. #ifdef GRUB_CPU_WORDS_BIGENDIAN
  40. p += 7 - b / 8;
  41. #else
  42. p += b / 8;
  43. #endif
  44. return !!(*p & bitmask[b % 8]);
  45. }
  46. static grub_uint64_t
  47. set_bit64 (grub_uint64_t v, int b)
  48. {
  49. grub_raw_u64_t vr = { .v64 = v };
  50. grub_uint8_t *p = vr.v8;
  51. if (b >= 64)
  52. return v;
  53. #ifdef GRUB_CPU_WORDS_BIGENDIAN
  54. p += 7 - b / 8;
  55. #else
  56. p += b / 8;
  57. #endif
  58. *p |= bitmask[b % 8];
  59. return vr.v64;
  60. }
  61. static grub_uint64_t
  62. left_shift64 (grub_uint64_t v, int s)
  63. {
  64. grub_uint64_t r = 0;
  65. int i;
  66. for (i = 0; i + s < 64; i++)
  67. if (get_bit64 (v, i))
  68. r = set_bit64 (r, i + s);
  69. return r;
  70. }
  71. static grub_uint64_t
  72. right_shift64 (grub_uint64_t v, int s)
  73. {
  74. grub_uint64_t r = 0;
  75. int i;
  76. for (i = s; i < 64; i++)
  77. if (get_bit64 (v, i))
  78. r = set_bit64 (r, i - s);
  79. return r;
  80. }
  81. static grub_uint64_t
  82. arithmetic_right_shift64 (grub_uint64_t v, int s)
  83. {
  84. grub_uint64_t r = 0;
  85. int i;
  86. for (i = s; i < 64; i++)
  87. if (get_bit64 (v, i))
  88. r = set_bit64 (r, i - s);
  89. if (get_bit64 (v, 63))
  90. for (i -= s; i < 64; i++)
  91. r = set_bit64 (r, i);
  92. return r;
  93. }
  94. static void
  95. test64 (grub_uint64_t v)
  96. {
  97. int i;
  98. for (i = 0; i < 64; i++)
  99. {
  100. grub_test_assert ((v << i) == left_shift64 (v, i),
  101. "lshift wrong: 0x%llx << %d: 0x%llx, 0x%llx",
  102. (long long) v, i,
  103. (long long) (v << i), (long long) left_shift64 (v, i));
  104. grub_test_assert ((v >> i) == right_shift64 (v, i),
  105. "rshift wrong: 0x%llx >> %d: 0x%llx, 0x%llx",
  106. (long long) v, i,
  107. (long long) (v >> i), (long long) right_shift64 (v, i));
  108. grub_test_assert ((((grub_int64_t) v) >> i) == (grub_int64_t) arithmetic_right_shift64 (v, i),
  109. "arithmetic rshift wrong: ((grub_int64_t) 0x%llx) >> %d: 0x%llx, 0x%llx",
  110. (long long) v, i,
  111. (long long) (((grub_int64_t) v) >> i), (long long) arithmetic_right_shift64 (v, i));
  112. }
  113. }
  114. static void
  115. test_all(grub_uint64_t a)
  116. {
  117. test64 (a);
  118. }
  119. static void
  120. shift_test (void)
  121. {
  122. grub_uint64_t a = 404, b = 7;
  123. grub_size_t i;
  124. for (i = 0; i < ARRAY_SIZE (vectors); i++)
  125. {
  126. test_all (vectors[i]);
  127. }
  128. for (i = 0; i < 4000; i++)
  129. {
  130. a = 17 * a + 13 * b;
  131. b = 23 * a + 29 * b;
  132. if (b == 0)
  133. b = 1;
  134. if (a == 0)
  135. a = 1;
  136. test_all (a);
  137. test_all (b);
  138. }
  139. }
  140. /* Register example_test method as a functional test. */
  141. GRUB_FUNCTIONAL_TEST (shift_test, shift_test);