bitop.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (c) 2006-2018 Maxim Khizhinsky
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <cds_test/ext_gtest.h>
  6. #include <cds/algo/int_algo.h>
  7. //#include <cds/details/bit_reverse_counter.h>
  8. namespace {
  9. class bitop : public ::testing::Test
  10. {};
  11. TEST_F( bitop, bitop32 )
  12. {
  13. uint32_t n = 0;
  14. EXPECT_EQ( cds::bitop::MSB(n), 0 ) << "n=" << n;
  15. EXPECT_EQ( cds::bitop::LSB( n ), 0 ) << "n=" << n;
  16. EXPECT_EQ( cds::bitop::SBC( n ), 0 ) << "n=" << n;
  17. EXPECT_EQ( cds::bitop::ZBC( n ), static_cast<int>( sizeof( n ) * 8 )) << "n=" << n;
  18. int nBit = 1;
  19. for ( n = 1; n != 0; n *= 2 ) {
  20. EXPECT_EQ( cds::bitop::MSB( n ), nBit ) << "n=" << n;
  21. EXPECT_EQ( cds::bitop::LSB( n ), nBit ) << "n=" << n;
  22. EXPECT_EQ( cds::bitop::MSBnz( n ), nBit - 1 ) << "n=" << n;
  23. EXPECT_EQ( cds::bitop::LSBnz( n ), nBit - 1 ) << "n=" << n;
  24. EXPECT_EQ( cds::bitop::SBC( n ), 1 ) << "n=" << n;
  25. EXPECT_EQ( cds::bitop::ZBC( n ), static_cast<int>( sizeof( n ) * 8 - 1 )) << "n=" << n;
  26. ++nBit;
  27. }
  28. }
  29. TEST_F( bitop, bitop64 )
  30. {
  31. uint64_t n = 0;
  32. EXPECT_EQ( cds::bitop::MSB( n ), 0 ) << "n=" << n;
  33. EXPECT_EQ( cds::bitop::LSB( n ), 0 ) << "n=" << n;
  34. EXPECT_EQ( cds::bitop::SBC( n ), 0 ) << "n=" << n;
  35. EXPECT_EQ( cds::bitop::ZBC( n ), static_cast<int>( sizeof( n ) * 8 )) << "n=" << n;
  36. int nBit = 1;
  37. for ( n = 1; n != 0; n *= 2 ) {
  38. EXPECT_EQ( cds::bitop::MSB( n ), nBit ) << "n=" << n;
  39. EXPECT_EQ( cds::bitop::LSB( n ), nBit ) << "n=" << n;
  40. EXPECT_EQ( cds::bitop::MSBnz( n ), nBit - 1 ) << "n=" << n;
  41. EXPECT_EQ( cds::bitop::LSBnz( n ), nBit - 1 ) << "n=" << n;
  42. EXPECT_EQ( cds::bitop::SBC( n ), 1 ) << "n=" << n;
  43. EXPECT_EQ( cds::bitop::ZBC( n ), static_cast<int>( sizeof( n ) * 8 - 1 )) << "n=" << n;
  44. ++nBit;
  45. }
  46. }
  47. TEST_F( bitop, floor_pow2 )
  48. {
  49. EXPECT_EQ( cds::beans::floor2( 0u ), 1u );
  50. EXPECT_EQ( cds::beans::floor2( 1u ), 1u );
  51. EXPECT_EQ( cds::beans::floor2( 2u ), 2u );
  52. EXPECT_EQ( cds::beans::floor2( 3u ), 2u );
  53. EXPECT_EQ( cds::beans::floor2( 4u ), 4u );
  54. EXPECT_EQ( cds::beans::floor2( 5u ), 4u );
  55. EXPECT_EQ( cds::beans::floor2( 7u ), 4u );
  56. EXPECT_EQ( cds::beans::floor2( 8u ), 8u );
  57. EXPECT_EQ( cds::beans::floor2( 9u ), 8u );
  58. for ( uint32_t n = 2; n; n <<= 1 )
  59. {
  60. EXPECT_EQ( cds::beans::floor2( n - 1 ), n / 2 );
  61. EXPECT_EQ( cds::beans::floor2( n + 1 ), n );
  62. }
  63. for ( uint64_t n = 2; n; n <<= 1 )
  64. {
  65. EXPECT_EQ( cds::beans::floor2( n - 1 ), n / 2 );
  66. EXPECT_EQ( cds::beans::floor2( n ), n );
  67. EXPECT_EQ( cds::beans::floor2( n + 1 ), n );
  68. }
  69. }
  70. TEST_F( bitop, ceil_pow2 )
  71. {
  72. EXPECT_EQ( cds::beans::ceil2( 0u ), 1u );
  73. EXPECT_EQ( cds::beans::ceil2( 1u ), 1u );
  74. EXPECT_EQ( cds::beans::ceil2( 2u ), 2u );
  75. EXPECT_EQ( cds::beans::ceil2( 3u ), 4u );
  76. EXPECT_EQ( cds::beans::ceil2( 4u ), 4u );
  77. EXPECT_EQ( cds::beans::ceil2( 5u ), 8u );
  78. EXPECT_EQ( cds::beans::ceil2( 7u ), 8u );
  79. EXPECT_EQ( cds::beans::ceil2( 8u ), 8u );
  80. EXPECT_EQ( cds::beans::ceil2( 9u ), 16u );
  81. for ( uint32_t n = 4; n < (uint32_t(1) << 31); n <<= 1 )
  82. {
  83. EXPECT_EQ( cds::beans::ceil2( n - 1 ), n );
  84. EXPECT_EQ( cds::beans::ceil2( n ), n );
  85. EXPECT_EQ( cds::beans::ceil2( n + 1 ), n * 2 );
  86. }
  87. for ( uint64_t n = 4; n < (uint64_t(1) << 63); n <<= 1 )
  88. {
  89. EXPECT_EQ( cds::beans::ceil2( n - 1 ), n );
  90. EXPECT_EQ( cds::beans::ceil2( n ), n );
  91. EXPECT_EQ( cds::beans::ceil2( n + 1 ), n * 2 );
  92. }
  93. }
  94. /*
  95. TEST_F( bitop, bit_reverse_counter )
  96. {
  97. cds::bitop::bit_reverse_counter<> c;
  98. while ( c.value() < 8 ) {
  99. size_t res = c.inc();
  100. std::cout << "inc result: " << res
  101. << " value: " << c.value()
  102. << " reversed: " << c.reversed_value()
  103. << " high_bit: " << c.high_bit() << "\n";
  104. }
  105. while ( c.value() > 0 ) {
  106. size_t res = c.dec();
  107. std::cout << "dec result: " << res
  108. << " value: " << c.value()
  109. << " reversed: " << c.reversed_value()
  110. << " high_bit: " << c.high_bit() << "\n";
  111. }
  112. }
  113. */
  114. } // namespace