floor2.pass.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 floor2(T x) noexcept;
  12. // Returns: If x == 0, 0; otherwise the maximal value y such that floor2(y) is true and y <= x.
  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 <type_traits>
  18. #include <cassert>
  19. #include "test_macros.h"
  20. class A{};
  21. enum E1 : unsigned char { rEd };
  22. enum class E2 : unsigned char { red };
  23. template <typename T>
  24. constexpr bool constexpr_test()
  25. {
  26. return std::floor2(T(0)) == T(0)
  27. && std::floor2(T(1)) == T(1)
  28. && std::floor2(T(2)) == T(2)
  29. && std::floor2(T(3)) == T(2)
  30. && std::floor2(T(4)) == T(4)
  31. && std::floor2(T(5)) == T(4)
  32. && std::floor2(T(6)) == T(4)
  33. && std::floor2(T(7)) == T(4)
  34. && std::floor2(T(8)) == T(8)
  35. && std::floor2(T(9)) == T(8)
  36. ;
  37. }
  38. template <typename T>
  39. void runtime_test()
  40. {
  41. ASSERT_SAME_TYPE(T, decltype(std::floor2(T(0))));
  42. ASSERT_NOEXCEPT( std::floor2(T(0)));
  43. assert( std::floor2(T(121)) == T(64));
  44. assert( std::floor2(T(122)) == T(64));
  45. assert( std::floor2(T(123)) == T(64));
  46. assert( std::floor2(T(124)) == T(64));
  47. assert( std::floor2(T(125)) == T(64));
  48. assert( std::floor2(T(126)) == T(64));
  49. assert( std::floor2(T(127)) == T(64));
  50. assert( std::floor2(T(128)) == T(128));
  51. assert( std::floor2(T(129)) == T(128));
  52. assert( std::floor2(T(130)) == T(128));
  53. }
  54. int main()
  55. {
  56. {
  57. auto lambda = [](auto x) -> decltype(std::floor2(x)) {};
  58. using L = decltype(lambda);
  59. static_assert( std::is_invocable_v<L, unsigned char>, "");
  60. static_assert( std::is_invocable_v<L, unsigned int>, "");
  61. static_assert( std::is_invocable_v<L, unsigned long>, "");
  62. static_assert( std::is_invocable_v<L, unsigned long long>, "");
  63. static_assert( std::is_invocable_v<L, uint8_t>, "");
  64. static_assert( std::is_invocable_v<L, uint16_t>, "");
  65. static_assert( std::is_invocable_v<L, uint32_t>, "");
  66. static_assert( std::is_invocable_v<L, uint64_t>, "");
  67. static_assert( std::is_invocable_v<L, size_t>, "");
  68. static_assert( std::is_invocable_v<L, uintmax_t>, "");
  69. static_assert( std::is_invocable_v<L, uintptr_t>, "");
  70. static_assert(!std::is_invocable_v<L, int>, "");
  71. static_assert(!std::is_invocable_v<L, signed int>, "");
  72. static_assert(!std::is_invocable_v<L, long>, "");
  73. static_assert(!std::is_invocable_v<L, long long>, "");
  74. static_assert(!std::is_invocable_v<L, int8_t>, "");
  75. static_assert(!std::is_invocable_v<L, int16_t>, "");
  76. static_assert(!std::is_invocable_v<L, int32_t>, "");
  77. static_assert(!std::is_invocable_v<L, int64_t>, "");
  78. static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
  79. static_assert(!std::is_invocable_v<L, bool>, "");
  80. static_assert(!std::is_invocable_v<L, signed char>, "");
  81. static_assert(!std::is_invocable_v<L, char16_t>, "");
  82. static_assert(!std::is_invocable_v<L, char32_t>, "");
  83. #ifndef _LIBCPP_HAS_NO_INT128
  84. static_assert( std::is_invocable_v<L, __uint128_t>, "");
  85. static_assert(!std::is_invocable_v<L, __int128_t>, "");
  86. #endif
  87. static_assert(!std::is_invocable_v<L, A>, "");
  88. static_assert(!std::is_invocable_v<L, E1>, "");
  89. static_assert(!std::is_invocable_v<L, E2>, "");
  90. }
  91. static_assert(constexpr_test<unsigned char>(), "");
  92. static_assert(constexpr_test<unsigned short>(), "");
  93. static_assert(constexpr_test<unsigned>(), "");
  94. static_assert(constexpr_test<unsigned long>(), "");
  95. static_assert(constexpr_test<unsigned long long>(), "");
  96. static_assert(constexpr_test<uint8_t>(), "");
  97. static_assert(constexpr_test<uint16_t>(), "");
  98. static_assert(constexpr_test<uint32_t>(), "");
  99. static_assert(constexpr_test<uint64_t>(), "");
  100. static_assert(constexpr_test<size_t>(), "");
  101. static_assert(constexpr_test<uintmax_t>(), "");
  102. static_assert(constexpr_test<uintptr_t>(), "");
  103. #ifndef _LIBCPP_HAS_NO_INT128
  104. static_assert(constexpr_test<__uint128_t>(), "");
  105. #endif
  106. runtime_test<unsigned char>();
  107. runtime_test<unsigned>();
  108. runtime_test<unsigned short>();
  109. runtime_test<unsigned long>();
  110. runtime_test<unsigned long long>();
  111. runtime_test<uint8_t>();
  112. runtime_test<uint16_t>();
  113. runtime_test<uint32_t>();
  114. runtime_test<uint64_t>();
  115. runtime_test<size_t>();
  116. runtime_test<uintmax_t>();
  117. runtime_test<uintptr_t>();
  118. #ifndef _LIBCPP_HAS_NO_INT128
  119. runtime_test<__uint128_t>();
  120. {
  121. __uint128_t val = 128;
  122. val <<= 32;
  123. assert( std::floor2(val-1) == val/2);
  124. assert( std::floor2(val) == val);
  125. assert( std::floor2(val+1) == val);
  126. val <<= 2;
  127. assert( std::floor2(val-1) == val/2);
  128. assert( std::floor2(val) == val);
  129. assert( std::floor2(val+1) == val);
  130. val <<= 3;
  131. assert( std::floor2(val-1) == val/2);
  132. assert( std::floor2(val) == val);
  133. assert( std::floor2(val+1) == val);
  134. }
  135. #endif
  136. }