test-bitops.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Test bitops routines
  3. *
  4. * This work is licensed under the terms of the GNU LGPL, version 2 or later.
  5. * See the COPYING.LIB file in the top-level directory.
  6. *
  7. */
  8. #include "qemu/osdep.h"
  9. #include "qemu/bitops.h"
  10. typedef struct {
  11. uint32_t value;
  12. int start;
  13. int length;
  14. int32_t result;
  15. } S32Test;
  16. typedef struct {
  17. uint64_t value;
  18. int start;
  19. int length;
  20. int64_t result;
  21. } S64Test;
  22. static const S32Test test_s32_data[] = {
  23. { 0x38463983, 4, 4, -8 },
  24. { 0x38463983, 12, 8, 0x63 },
  25. { 0x38463983, 0, 32, 0x38463983 },
  26. };
  27. static const S64Test test_s64_data[] = {
  28. { 0x8459826734967223ULL, 60, 4, -8 },
  29. { 0x8459826734967223ULL, 0, 64, 0x8459826734967223LL },
  30. };
  31. static void test_sextract32(void)
  32. {
  33. int i;
  34. for (i = 0; i < ARRAY_SIZE(test_s32_data); i++) {
  35. const S32Test *test = &test_s32_data[i];
  36. int32_t r = sextract32(test->value, test->start, test->length);
  37. g_assert_cmpint(r, ==, test->result);
  38. }
  39. }
  40. static void test_sextract64(void)
  41. {
  42. int i;
  43. for (i = 0; i < ARRAY_SIZE(test_s32_data); i++) {
  44. const S32Test *test = &test_s32_data[i];
  45. int64_t r = sextract64(test->value, test->start, test->length);
  46. g_assert_cmpint(r, ==, test->result);
  47. }
  48. for (i = 0; i < ARRAY_SIZE(test_s64_data); i++) {
  49. const S64Test *test = &test_s64_data[i];
  50. int64_t r = sextract64(test->value, test->start, test->length);
  51. g_assert_cmpint(r, ==, test->result);
  52. }
  53. }
  54. typedef struct {
  55. uint32_t unshuffled;
  56. uint32_t shuffled;
  57. } Shuffle32Test;
  58. typedef struct {
  59. uint64_t unshuffled;
  60. uint64_t shuffled;
  61. } Shuffle64Test;
  62. static const Shuffle32Test test_shuffle32_data[] = {
  63. { 0x0000FFFF, 0x55555555 },
  64. { 0x000081C5, 0x40015011 },
  65. };
  66. static const Shuffle64Test test_shuffle64_data[] = {
  67. { 0x00000000FFFFFFFFULL, 0x5555555555555555ULL },
  68. { 0x00000000493AB02CULL, 0x1041054445000450ULL },
  69. };
  70. static void test_half_shuffle32(void)
  71. {
  72. int i;
  73. for (i = 0; i < ARRAY_SIZE(test_shuffle32_data); i++) {
  74. const Shuffle32Test *test = &test_shuffle32_data[i];
  75. uint32_t r = half_shuffle32(test->unshuffled);
  76. g_assert_cmpint(r, ==, test->shuffled);
  77. }
  78. }
  79. static void test_half_shuffle64(void)
  80. {
  81. int i;
  82. for (i = 0; i < ARRAY_SIZE(test_shuffle64_data); i++) {
  83. const Shuffle64Test *test = &test_shuffle64_data[i];
  84. uint64_t r = half_shuffle64(test->unshuffled);
  85. g_assert_cmpint(r, ==, test->shuffled);
  86. }
  87. }
  88. static void test_half_unshuffle32(void)
  89. {
  90. int i;
  91. for (i = 0; i < ARRAY_SIZE(test_shuffle32_data); i++) {
  92. const Shuffle32Test *test = &test_shuffle32_data[i];
  93. uint32_t r = half_unshuffle32(test->shuffled);
  94. g_assert_cmpint(r, ==, test->unshuffled);
  95. }
  96. }
  97. static void test_half_unshuffle64(void)
  98. {
  99. int i;
  100. for (i = 0; i < ARRAY_SIZE(test_shuffle64_data); i++) {
  101. const Shuffle64Test *test = &test_shuffle64_data[i];
  102. uint64_t r = half_unshuffle64(test->shuffled);
  103. g_assert_cmpint(r, ==, test->unshuffled);
  104. }
  105. }
  106. int main(int argc, char **argv)
  107. {
  108. g_test_init(&argc, &argv, NULL);
  109. g_test_add_func("/bitops/sextract32", test_sextract32);
  110. g_test_add_func("/bitops/sextract64", test_sextract64);
  111. g_test_add_func("/bitops/half_shuffle32", test_half_shuffle32);
  112. g_test_add_func("/bitops/half_shuffle64", test_half_shuffle64);
  113. g_test_add_func("/bitops/half_unshuffle32", test_half_unshuffle32);
  114. g_test_add_func("/bitops/half_unshuffle64", test_half_unshuffle64);
  115. return g_test_run();
  116. }