1234567891011121314151617181920212223242526272829303132333435363738 |
- #include "simple/support/bits.hpp"
- #include <climits>
- using namespace simple::support;
- template <int n>
- constexpr void powers_of_two()
- {
- static_assert(count_trailing_zeros(1 << n) == n);
- static_assert(count_leading_zeros(1 << n) == sizeof(1) * CHAR_BIT - 1 - n);
- powers_of_two<n-1>();
- };
- template <>
- constexpr void powers_of_two<0>(){}
- int main()
- {
- static_assert(count_ones(0) == 0);
- static_assert(count_ones(-1) == sizeof(int) * CHAR_BIT);
- static_assert(count_ones(0b1) == 1);
- static_assert(count_ones(0b1010101) == 4);
- // TODO; these large literal tests are dubious, not a given that they won't overflow
- static_assert(count_ones(0x1000'0101'1010'0000LL) == 5);
- static_assert(count_trailing_zeros(0b1) == 0);
- static_assert(count_trailing_zeros(0b1000) == 3);
- static_assert(count_trailing_zeros(0x1000'0101'1010'0000LL) == 5*4);
- static_assert(count_trailing_zeros(0x0000'0101'1010'0001LL) == 0);
- static_assert(count_trailing_zeros(1 << 0) == 0);
- static_assert(count_leading_zeros(0b1) == sizeof(0b1) * CHAR_BIT - 1);
- static_assert(count_leading_zeros(0b1000) == sizeof(0b1000) * CHAR_BIT - 4);
- static_assert(count_leading_zeros(0b10101011000) == sizeof(0b1000) * CHAR_BIT - 11);
- static_assert(count_leading_zeros(1 << 0) == sizeof(1) * CHAR_BIT - 1);
- powers_of_two<sizeof(int) * CHAR_BIT - 1>();
- return 0;
- }
|