cmp_test.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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[][2] = {
  23. { 0xffffffffffffffffULL, 1},
  24. { 1, 0xffffffffffffffffULL},
  25. { 0xffffffffffffffffULL, 0xffffffffffffffffULL},
  26. { 1, 1 },
  27. { 2, 1 }
  28. };
  29. /* Don't change those to use shift as shift may call to compile rt
  30. functions and we're not testing them now.
  31. */
  32. static int
  33. leading_bit64 (grub_uint64_t a)
  34. {
  35. return !!(a & 0x8000000000000000LL);
  36. }
  37. static int
  38. leading_bit32 (grub_uint32_t a)
  39. {
  40. return !!(a & 0x80000000);
  41. }
  42. /* Computes (a < b) without involving comparison operator. */
  43. static int
  44. is_less32 (grub_uint32_t a, grub_uint32_t b)
  45. {
  46. if (leading_bit32(a) && !leading_bit32(b))
  47. return 0;
  48. if (!leading_bit32(a) && leading_bit32(b))
  49. return 1;
  50. return leading_bit32(a - b);
  51. }
  52. static void
  53. test32 (grub_uint32_t a, grub_uint32_t b)
  54. {
  55. grub_test_assert ((a < b) == is_less32(a, b), "comparison result mismatch: %lld, %lld",
  56. (long long) a, (long long) b);
  57. grub_test_assert ((a > b) == is_less32(b, a), "comparison result mismatch: %lld, %lld",
  58. (long long) a, (long long) b);
  59. grub_test_assert ((b < a) == is_less32(b, a), "comparison result mismatch: %lld, %lld",
  60. (long long) a, (long long) b);
  61. grub_test_assert ((b > a) == is_less32(a, b), "comparison result mismatch: %lld, %lld",
  62. (long long) a, (long long) b);
  63. grub_test_assert (!(is_less32(a, b) && is_less32(b, a)), "comparison inconsistent: %lld, %lld",
  64. (long long) a, (long long) b);
  65. }
  66. /* Computes (a > b) without involving comparison operator. */
  67. static int
  68. is_less32s (grub_int32_t a, grub_int32_t b)
  69. {
  70. if (leading_bit32(a) && !leading_bit32(b))
  71. return 1; /* a < 0 && b >= 0. */
  72. if (!leading_bit32(a) && leading_bit32(b))
  73. return 0; /* b < 0 && a >= 0. */
  74. return leading_bit32(a - b);
  75. }
  76. static void
  77. test32s (grub_int32_t a, grub_int32_t b)
  78. {
  79. grub_test_assert ((a < b) == is_less32s(a, b), "comparison result mismatch: %lld, %lld",
  80. (long long) a, (long long) b);
  81. grub_test_assert ((a > b) == is_less32s(b, a), "comparison result mismatch: %lld, %lld",
  82. (long long) a, (long long) b);
  83. grub_test_assert ((b < a) == is_less32s(b, a), "comparison result mismatch: %lld, %lld",
  84. (long long) a, (long long) b);
  85. grub_test_assert ((b > a) == is_less32s(a, b), "comparison result mismatch: %lld, %lld",
  86. (long long) a, (long long) b);
  87. grub_test_assert (!(is_less32s(a, b) && is_less32s(b, a)), "comparison inconsistent: %lld, %lld",
  88. (long long) a, (long long) b);
  89. }
  90. /* Computes (a > b) without involving comparison operator. */
  91. static int
  92. is_less64 (grub_uint64_t a, grub_uint64_t b)
  93. {
  94. if (leading_bit64(a) && !leading_bit64(b))
  95. return 0;
  96. if (!leading_bit64(a) && leading_bit64(b))
  97. return 1;
  98. return leading_bit64(a - b);
  99. }
  100. static void
  101. test64 (grub_uint64_t a, grub_uint64_t b)
  102. {
  103. grub_test_assert ((a < b) == is_less64(a, b), "comparison result mismatch: %lld, %lld",
  104. (long long) a, (long long) b);
  105. grub_test_assert ((a > b) == is_less64(b, a), "comparison result mismatch: %lld, %lld",
  106. (long long) a, (long long) b);
  107. grub_test_assert ((b < a) == is_less64(b, a), "comparison result mismatch: %lld, %lld",
  108. (long long) a, (long long) b);
  109. grub_test_assert ((b > a) == is_less64(a, b), "comparison result mismatch: %lld, %lld",
  110. (long long) a, (long long) b);
  111. grub_test_assert (!(is_less64(a, b) && is_less64(b, a)), "comparison inconsistent: %lld, %lld",
  112. (long long) a, (long long) b);
  113. }
  114. /* Computes (a > b) without involving comparison operator. */
  115. static int
  116. is_less64s (grub_int64_t a, grub_int64_t b)
  117. {
  118. if (leading_bit64(a) && !leading_bit64(b))
  119. return 1; /* a < 0 && b >= 0. */
  120. if (!leading_bit64(a) && leading_bit64(b))
  121. return 0; /* b < 0 && a >= 0. */
  122. return leading_bit64(a - b);
  123. }
  124. static void
  125. test64s (grub_int64_t a, grub_int64_t b)
  126. {
  127. grub_test_assert ((a < b) == is_less64s(a, b), "comparison result mismatch: %lld, %lld",
  128. (long long) a, (long long) b);
  129. grub_test_assert ((a > b) == is_less64s(b, a), "comparison result mismatch: %lld, %lld",
  130. (long long) a, (long long) b);
  131. grub_test_assert ((b < a) == is_less64s(b, a), "comparison result mismatch: %lld, %lld",
  132. (long long) a, (long long) b);
  133. grub_test_assert ((b > a) == is_less64s(a, b), "comparison result mismatch: %lld, %lld",
  134. (long long) a, (long long) b);
  135. grub_test_assert (!(is_less64s(a, b) && is_less64s(b, a)), "comparison inconsistent: %lld, %lld",
  136. (long long) a, (long long) b);
  137. }
  138. static void
  139. test_all(grub_uint64_t a, grub_uint64_t b)
  140. {
  141. test64 (a, b);
  142. test32 (a, b);
  143. test64s (a, b);
  144. test32s (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. }
  152. static void
  153. cmp_test (void)
  154. {
  155. grub_uint64_t a = 404, b = 7;
  156. grub_size_t i;
  157. for (i = 0; i < ARRAY_SIZE (vectors); i++)
  158. {
  159. test_all (vectors[i][0], vectors[i][1]);
  160. }
  161. for (i = 0; i < 40000; i++)
  162. {
  163. a = 17 * a + 13 * b;
  164. b = 23 * a + 29 * b;
  165. if (b == 0)
  166. b = 1;
  167. if (a == 0)
  168. a = 1;
  169. test_all (a, b);
  170. }
  171. }
  172. /* Register example_test method as a functional test. */
  173. GRUB_FUNCTIONAL_TEST (cmp_test, cmp_test);