test_bitfield.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Test cases for bitfield helpers.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/bitfield.h>
  9. #define CHECK_ENC_GET_U(tp, v, field, res) do { \
  10. { \
  11. u##tp _res; \
  12. \
  13. _res = u##tp##_encode_bits(v, field); \
  14. if (_res != res) { \
  15. pr_warn("u" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != " #res "\n",\
  16. (u64)_res); \
  17. return -EINVAL; \
  18. } \
  19. if (u##tp##_get_bits(_res, field) != v) \
  20. return -EINVAL; \
  21. } \
  22. } while (0)
  23. #define CHECK_ENC_GET_LE(tp, v, field, res) do { \
  24. { \
  25. __le##tp _res; \
  26. \
  27. _res = le##tp##_encode_bits(v, field); \
  28. if (_res != cpu_to_le##tp(res)) { \
  29. pr_warn("le" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx\n",\
  30. (u64)le##tp##_to_cpu(_res), \
  31. (u64)(res)); \
  32. return -EINVAL; \
  33. } \
  34. if (le##tp##_get_bits(_res, field) != v) \
  35. return -EINVAL; \
  36. } \
  37. } while (0)
  38. #define CHECK_ENC_GET_BE(tp, v, field, res) do { \
  39. { \
  40. __be##tp _res; \
  41. \
  42. _res = be##tp##_encode_bits(v, field); \
  43. if (_res != cpu_to_be##tp(res)) { \
  44. pr_warn("be" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx\n",\
  45. (u64)be##tp##_to_cpu(_res), \
  46. (u64)(res)); \
  47. return -EINVAL; \
  48. } \
  49. if (be##tp##_get_bits(_res, field) != v) \
  50. return -EINVAL; \
  51. } \
  52. } while (0)
  53. #define CHECK_ENC_GET(tp, v, field, res) do { \
  54. CHECK_ENC_GET_U(tp, v, field, res); \
  55. CHECK_ENC_GET_LE(tp, v, field, res); \
  56. CHECK_ENC_GET_BE(tp, v, field, res); \
  57. } while (0)
  58. static int test_constants(void)
  59. {
  60. /*
  61. * NOTE
  62. * This whole function compiles (or at least should, if everything
  63. * is going according to plan) to nothing after optimisation.
  64. */
  65. CHECK_ENC_GET(16, 1, 0x000f, 0x0001);
  66. CHECK_ENC_GET(16, 3, 0x00f0, 0x0030);
  67. CHECK_ENC_GET(16, 5, 0x0f00, 0x0500);
  68. CHECK_ENC_GET(16, 7, 0xf000, 0x7000);
  69. CHECK_ENC_GET(16, 14, 0x000f, 0x000e);
  70. CHECK_ENC_GET(16, 15, 0x00f0, 0x00f0);
  71. CHECK_ENC_GET_U(8, 1, 0x0f, 0x01);
  72. CHECK_ENC_GET_U(8, 3, 0xf0, 0x30);
  73. CHECK_ENC_GET_U(8, 14, 0x0f, 0x0e);
  74. CHECK_ENC_GET_U(8, 15, 0xf0, 0xf0);
  75. CHECK_ENC_GET(32, 1, 0x00000f00, 0x00000100);
  76. CHECK_ENC_GET(32, 3, 0x0000f000, 0x00003000);
  77. CHECK_ENC_GET(32, 5, 0x000f0000, 0x00050000);
  78. CHECK_ENC_GET(32, 7, 0x00f00000, 0x00700000);
  79. CHECK_ENC_GET(32, 14, 0x0f000000, 0x0e000000);
  80. CHECK_ENC_GET(32, 15, 0xf0000000, 0xf0000000);
  81. CHECK_ENC_GET(64, 1, 0x00000f0000000000ull, 0x0000010000000000ull);
  82. CHECK_ENC_GET(64, 3, 0x0000f00000000000ull, 0x0000300000000000ull);
  83. CHECK_ENC_GET(64, 5, 0x000f000000000000ull, 0x0005000000000000ull);
  84. CHECK_ENC_GET(64, 7, 0x00f0000000000000ull, 0x0070000000000000ull);
  85. CHECK_ENC_GET(64, 14, 0x0f00000000000000ull, 0x0e00000000000000ull);
  86. CHECK_ENC_GET(64, 15, 0xf000000000000000ull, 0xf000000000000000ull);
  87. return 0;
  88. }
  89. #define CHECK(tp, mask) do { \
  90. u64 v; \
  91. \
  92. for (v = 0; v < 1 << hweight32(mask); v++) \
  93. if (tp##_encode_bits(v, mask) != v << __ffs64(mask)) \
  94. return -EINVAL; \
  95. } while (0)
  96. static int test_variables(void)
  97. {
  98. CHECK(u8, 0x0f);
  99. CHECK(u8, 0xf0);
  100. CHECK(u8, 0x38);
  101. CHECK(u16, 0x0038);
  102. CHECK(u16, 0x0380);
  103. CHECK(u16, 0x3800);
  104. CHECK(u16, 0x8000);
  105. CHECK(u32, 0x80000000);
  106. CHECK(u32, 0x7f000000);
  107. CHECK(u32, 0x07e00000);
  108. CHECK(u32, 0x00018000);
  109. CHECK(u64, 0x8000000000000000ull);
  110. CHECK(u64, 0x7f00000000000000ull);
  111. CHECK(u64, 0x0001800000000000ull);
  112. CHECK(u64, 0x0000000080000000ull);
  113. CHECK(u64, 0x000000007f000000ull);
  114. CHECK(u64, 0x0000000018000000ull);
  115. CHECK(u64, 0x0000001f8000000ull);
  116. return 0;
  117. }
  118. static int __init test_bitfields(void)
  119. {
  120. int ret = test_constants();
  121. if (ret) {
  122. pr_warn("constant tests failed!\n");
  123. return ret;
  124. }
  125. ret = test_variables();
  126. if (ret) {
  127. pr_warn("variable tests failed!\n");
  128. return ret;
  129. }
  130. #ifdef TEST_BITFIELD_COMPILE
  131. /* these should fail compilation */
  132. CHECK_ENC_GET(16, 16, 0x0f00, 0x1000);
  133. u32_encode_bits(7, 0x06000000);
  134. /* this should at least give a warning */
  135. u16_encode_bits(0, 0x60000);
  136. #endif
  137. pr_info("tests passed\n");
  138. return 0;
  139. }
  140. module_init(test_bitfields)
  141. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  142. MODULE_LICENSE("GPL");