bits.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include<__vic/bits.h>
  2. #include<iostream>
  3. #include<exception>
  4. #include<cassert>
  5. namespace tests {
  6. //----------------------------------------------------------------------------
  7. void nibble_tests()
  8. {
  9. char ch = '\xFA';
  10. std::cout << std::hex << std::uppercase <<
  11. unsigned(__vic::hi_nibble(ch)) << ' ' <<
  12. unsigned(__vic::lo_nibble(ch)) << '\n';
  13. assert(__vic::hi_nibble(ch) == 0xF);
  14. assert(__vic::lo_nibble(ch) == 0xA);
  15. }
  16. //----------------------------------------------------------------------------
  17. void ones_test()
  18. {
  19. std::cout << __vic::lsb_ones<int>(5) << '\n';
  20. assert(__vic::lsb_ones<int>(5) == 0x1F);
  21. std::cout << unsigned(__vic::msb_ones<uint8_t>(5)) << '\n';
  22. assert(__vic::msb_ones<uint8_t>(5) == uint8_t('\xF8'));
  23. }
  24. //----------------------------------------------------------------------------
  25. void get_lsbs_test()
  26. {
  27. std::cout << __vic::get_lsbs(~int(0), 6) << '\n';
  28. assert(__vic::get_lsbs(~int(0), 6) == 0x3F);
  29. }
  30. //----------------------------------------------------------------------------
  31. void ord_test()
  32. {
  33. char ch = -20;
  34. std::cout << std::dec << __vic::ord(ch) << '\n';
  35. assert(__vic::ord(ch) == 236);
  36. }
  37. //----------------------------------------------------------------------------
  38. void swap_nibbles_test()
  39. {
  40. assert(__vic::swapped_nibbles(0x12) == 0x21);
  41. }
  42. //----------------------------------------------------------------------------
  43. void rot_tests()
  44. {
  45. assert(__vic::rotl(uint32_t(0x12345678), 8) == uint32_t(0x34567812));
  46. assert(__vic::rotr(uint32_t(0x12345678), 8) == uint32_t(0x78123456));
  47. // corner case
  48. const uint32_t v = 1;
  49. assert(__vic::rotl(v, 0) == v);
  50. assert(__vic::rotr(v, 0) == v);
  51. }
  52. //----------------------------------------------------------------------------
  53. void popcount_tests()
  54. {
  55. assert(__vic::popcount(0U) == 0);
  56. assert(__vic::popcount(1U) == 1);
  57. assert(__vic::popcount(0x11U) == 2);
  58. assert(__vic::popcount(0x1111U) == 4);
  59. assert(__vic::popcount(0x0FF0U) == 8);
  60. assert(__vic::popcount(0x11UL) == 2);
  61. #ifdef __VIC_LONGLONG
  62. assert(__vic::popcount(static_cast<unsigned __VIC_LONGLONG>(0x11)) == 2);
  63. #endif
  64. assert(__vic::popcount(static_cast<unsigned short>(0x11)) == 2);
  65. assert(__vic::popcount(static_cast<unsigned char>(0x11)) == 2);
  66. }
  67. //----------------------------------------------------------------------------
  68. void msb_position_tests()
  69. {
  70. assert(__vic::msb_position(1U) == 0);
  71. assert(__vic::msb_position(2U) == 1);
  72. assert(__vic::msb_position(0x10U) == 4);
  73. assert(__vic::msb_position(0x8000U) == 15);
  74. assert(__vic::msb_position(static_cast<unsigned long>(0x10)) == 4);
  75. #ifdef __VIC_LONGLONG
  76. assert(__vic::msb_position(static_cast<unsigned __VIC_LONGLONG>(0x10)) == 4);
  77. #endif
  78. assert(__vic::msb_position(static_cast<unsigned short>(0x10)) == 4);
  79. assert(__vic::msb_position(static_cast<unsigned char>(0x10)) == 4);
  80. }
  81. //----------------------------------------------------------------------------
  82. void ispow2_tests()
  83. {
  84. assert(!__vic::ispow2(0U));
  85. assert( __vic::ispow2(1U));
  86. assert( __vic::ispow2(2U));
  87. assert(!__vic::ispow2(3U));
  88. assert( __vic::ispow2(4U));
  89. assert(!__vic::ispow2(5U));
  90. assert( __vic::ispow2(8U));
  91. assert( __vic::ispow2(0x100U));
  92. assert(__vic::ispow2(static_cast<unsigned long>(2)));
  93. #ifdef __VIC_LONGLONG
  94. assert(__vic::ispow2(static_cast<unsigned __VIC_LONGLONG>(2)));
  95. #endif
  96. assert(__vic::ispow2(static_cast<unsigned short>(2)));
  97. assert(__vic::ispow2(static_cast<unsigned char>(2)));
  98. }
  99. //----------------------------------------------------------------------------
  100. void ceil_log2_tests()
  101. {
  102. assert(__vic::ceil_log2(0U) == 0);
  103. assert(__vic::ceil_log2(1U) == 0);
  104. assert(__vic::ceil_log2(2U) == 1);
  105. assert(__vic::ceil_log2(3U) == 2);
  106. assert(__vic::ceil_log2(4U) == 2);
  107. assert(__vic::ceil_log2(5U) == 3);
  108. assert(__vic::ceil_log2(0x11U) == 5);
  109. assert(__vic::ceil_log2(static_cast<unsigned long>(0x11)) == 5);
  110. #ifdef __VIC_LONGLONG
  111. assert(__vic::ceil_log2(static_cast<unsigned __VIC_LONGLONG>(0x11)) == 5);
  112. #endif
  113. assert(__vic::ceil_log2(static_cast<unsigned short>(0x11)) == 5);
  114. assert(__vic::ceil_log2(static_cast<unsigned char>(0x11)) == 5);
  115. }
  116. //----------------------------------------------------------------------------
  117. void floor_log2_tests()
  118. {
  119. assert(__vic::floor_log2(0U) == 0);
  120. assert(__vic::floor_log2(1U) == 0);
  121. assert(__vic::floor_log2(2U) == 1);
  122. assert(__vic::floor_log2(3U) == 1);
  123. assert(__vic::floor_log2(4U) == 2);
  124. assert(__vic::floor_log2(5U) == 2);
  125. assert(__vic::floor_log2(0xFFU) == 7);
  126. assert(__vic::floor_log2(static_cast<unsigned long>(0xFF)) == 7);
  127. #ifdef __VIC_LONGLONG
  128. assert(__vic::floor_log2(static_cast<unsigned __VIC_LONGLONG>(0xFF)) == 7);
  129. #endif
  130. assert(__vic::floor_log2(static_cast<unsigned short>(0xFF)) == 7);
  131. assert(__vic::floor_log2(static_cast<unsigned char>(0xFF)) == 7);
  132. }
  133. //----------------------------------------------------------------------------
  134. void ceil2_tests()
  135. {
  136. assert(__vic::ceil2(0U) == 1);
  137. assert(__vic::ceil2(1U) == 1);
  138. assert(__vic::ceil2(2U) == 2);
  139. assert(__vic::ceil2(3U) == 4);
  140. assert(__vic::ceil2(4U) == 4);
  141. assert(__vic::ceil2(5U) == 8);
  142. assert(__vic::ceil2(0x11U) == 0x20);
  143. assert(__vic::ceil2(static_cast<unsigned long>(0x11)) == 0x20);
  144. #ifdef __VIC_LONGLONG
  145. assert(__vic::ceil2(static_cast<unsigned __VIC_LONGLONG>(0x11)) == 0x20);
  146. #endif
  147. assert(__vic::ceil2(static_cast<unsigned short>(0x11)) == 0x20);
  148. assert(__vic::ceil2(static_cast<unsigned char>(0x11)) == 0x20);
  149. }
  150. //----------------------------------------------------------------------------
  151. void floor2_tests()
  152. {
  153. assert(__vic::floor2(0U) == 0);
  154. assert(__vic::floor2(1U) == 1);
  155. assert(__vic::floor2(2U) == 2);
  156. assert(__vic::floor2(3U) == 2);
  157. assert(__vic::floor2(4U) == 4);
  158. assert(__vic::floor2(5U) == 4);
  159. assert(__vic::floor2(0xFFU) == 0x80);
  160. assert(__vic::floor2(static_cast<unsigned long>(0xFF)) == 0x80);
  161. #ifdef __VIC_LONGLONG
  162. assert(__vic::floor2(static_cast<unsigned __VIC_LONGLONG>(0xFF)) == 0x80);
  163. #endif
  164. assert(__vic::floor2(static_cast<unsigned short>(0xFF)) == 0x80);
  165. assert(__vic::floor2(static_cast<unsigned char>(0xFF)) == 0x80);
  166. }
  167. //----------------------------------------------------------------------------
  168. void run()
  169. {
  170. nibble_tests();
  171. ones_test();
  172. get_lsbs_test();
  173. ord_test();
  174. swap_nibbles_test();
  175. rot_tests();
  176. popcount_tests();
  177. msb_position_tests();
  178. ispow2_tests();
  179. ceil_log2_tests();
  180. floor_log2_tests();
  181. ceil2_tests();
  182. floor2_tests();
  183. }
  184. //----------------------------------------------------------------------------
  185. } // namespace
  186. int main()
  187. {
  188. try
  189. {
  190. tests::run();
  191. return 0;
  192. }
  193. catch(const std::exception &ex)
  194. {
  195. std::cerr << ex.what() << '\n';
  196. }
  197. return 1;
  198. }