log2p1.pass.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
  10. // template <class T>
  11. // constexpr T log2p1(T x) noexcept;
  12. // If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any fractional part discarded.
  13. // Remarks: This function shall not participate in overload resolution unless
  14. // T is an unsigned integer type
  15. #include <bit>
  16. #include <cstdint>
  17. #include <cassert>
  18. #include "test_macros.h"
  19. class A{};
  20. enum E1 : unsigned char { rEd };
  21. enum class E2 : unsigned char { red };
  22. template <typename T>
  23. constexpr bool constexpr_test()
  24. {
  25. return std::log2p1(T(0)) == T(0)
  26. && std::log2p1(T(1)) == T(1)
  27. && std::log2p1(T(2)) == T(2)
  28. && std::log2p1(T(3)) == T(2)
  29. && std::log2p1(T(4)) == T(3)
  30. && std::log2p1(T(5)) == T(3)
  31. && std::log2p1(T(6)) == T(3)
  32. && std::log2p1(T(7)) == T(3)
  33. && std::log2p1(T(8)) == T(4)
  34. && std::log2p1(T(9)) == T(4)
  35. ;
  36. }
  37. template <typename T>
  38. void runtime_test()
  39. {
  40. ASSERT_SAME_TYPE(T, decltype(std::log2p1(T(0))));
  41. ASSERT_NOEXCEPT( std::log2p1(T(0)));
  42. assert( std::log2p1(T(0)) == T(0));
  43. assert( std::log2p1(T(1)) == T(1));
  44. assert( std::log2p1(T(2)) == T(2));
  45. assert( std::log2p1(T(3)) == T(2));
  46. assert( std::log2p1(T(4)) == T(3));
  47. assert( std::log2p1(T(5)) == T(3));
  48. assert( std::log2p1(T(6)) == T(3));
  49. assert( std::log2p1(T(7)) == T(3));
  50. assert( std::log2p1(T(8)) == T(4));
  51. assert( std::log2p1(T(9)) == T(4));
  52. assert( std::log2p1(T(121)) == T(7));
  53. assert( std::log2p1(T(122)) == T(7));
  54. assert( std::log2p1(T(123)) == T(7));
  55. assert( std::log2p1(T(124)) == T(7));
  56. assert( std::log2p1(T(125)) == T(7));
  57. assert( std::log2p1(T(126)) == T(7));
  58. assert( std::log2p1(T(127)) == T(7));
  59. assert( std::log2p1(T(128)) == T(8));
  60. assert( std::log2p1(T(129)) == T(8));
  61. assert( std::log2p1(T(130)) == T(8));
  62. }
  63. int main()
  64. {
  65. {
  66. auto lambda = [](auto x) -> decltype(std::log2p1(x)) {};
  67. using L = decltype(lambda);
  68. static_assert( std::is_invocable_v<L, unsigned char>, "");
  69. static_assert( std::is_invocable_v<L, unsigned int>, "");
  70. static_assert( std::is_invocable_v<L, unsigned long>, "");
  71. static_assert( std::is_invocable_v<L, unsigned long long>, "");
  72. static_assert( std::is_invocable_v<L, uint8_t>, "");
  73. static_assert( std::is_invocable_v<L, uint16_t>, "");
  74. static_assert( std::is_invocable_v<L, uint32_t>, "");
  75. static_assert( std::is_invocable_v<L, uint64_t>, "");
  76. static_assert( std::is_invocable_v<L, size_t>, "");
  77. static_assert( std::is_invocable_v<L, uintmax_t>, "");
  78. static_assert( std::is_invocable_v<L, uintptr_t>, "");
  79. static_assert(!std::is_invocable_v<L, int>, "");
  80. static_assert(!std::is_invocable_v<L, signed int>, "");
  81. static_assert(!std::is_invocable_v<L, long>, "");
  82. static_assert(!std::is_invocable_v<L, long long>, "");
  83. static_assert(!std::is_invocable_v<L, int8_t>, "");
  84. static_assert(!std::is_invocable_v<L, int16_t>, "");
  85. static_assert(!std::is_invocable_v<L, int32_t>, "");
  86. static_assert(!std::is_invocable_v<L, int64_t>, "");
  87. static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
  88. static_assert(!std::is_invocable_v<L, bool>, "");
  89. static_assert(!std::is_invocable_v<L, signed char>, "");
  90. static_assert(!std::is_invocable_v<L, char16_t>, "");
  91. static_assert(!std::is_invocable_v<L, char32_t>, "");
  92. #ifndef _LIBCPP_HAS_NO_INT128
  93. static_assert( std::is_invocable_v<L, __uint128_t>, "");
  94. static_assert(!std::is_invocable_v<L, __int128_t>, "");
  95. #endif
  96. static_assert(!std::is_invocable_v<L, A>, "");
  97. static_assert(!std::is_invocable_v<L, E1>, "");
  98. static_assert(!std::is_invocable_v<L, E2>, "");
  99. }
  100. static_assert(constexpr_test<unsigned char>(), "");
  101. static_assert(constexpr_test<unsigned short>(), "");
  102. static_assert(constexpr_test<unsigned>(), "");
  103. static_assert(constexpr_test<unsigned long>(), "");
  104. static_assert(constexpr_test<unsigned long long>(), "");
  105. static_assert(constexpr_test<uint8_t>(), "");
  106. static_assert(constexpr_test<uint16_t>(), "");
  107. static_assert(constexpr_test<uint32_t>(), "");
  108. static_assert(constexpr_test<uint64_t>(), "");
  109. static_assert(constexpr_test<size_t>(), "");
  110. static_assert(constexpr_test<uintmax_t>(), "");
  111. static_assert(constexpr_test<uintptr_t>(), "");
  112. #ifndef _LIBCPP_HAS_NO_INT128
  113. static_assert(constexpr_test<__uint128_t>(), "");
  114. #endif
  115. runtime_test<unsigned char>();
  116. runtime_test<unsigned>();
  117. runtime_test<unsigned short>();
  118. runtime_test<unsigned long>();
  119. runtime_test<unsigned long long>();
  120. runtime_test<uint8_t>();
  121. runtime_test<uint16_t>();
  122. runtime_test<uint32_t>();
  123. runtime_test<uint64_t>();
  124. runtime_test<size_t>();
  125. runtime_test<uintmax_t>();
  126. runtime_test<uintptr_t>();
  127. #ifndef _LIBCPP_HAS_NO_INT128
  128. runtime_test<__uint128_t>();
  129. {
  130. __uint128_t val = 128;
  131. val <<= 32;
  132. assert( std::log2p1(val-1) == 39);
  133. assert( std::log2p1(val) == 40);
  134. assert( std::log2p1(val+1) == 40);
  135. val <<= 2;
  136. assert( std::log2p1(val-1) == 41);
  137. assert( std::log2p1(val) == 42);
  138. assert( std::log2p1(val+1) == 42);
  139. val <<= 3;
  140. assert( std::log2p1(val-1) == 44);
  141. assert( std::log2p1(val) == 45);
  142. assert( std::log2p1(val+1) == 45);
  143. }
  144. #endif
  145. }