mclow.lists created this revision.
mclow.lists added reviewers: EricWF, ldionne.
Herald added a subscriber: christof.

LWG adopted https://wg21.link/P0553 in Rapperswil, and suggested minor changes 
to https://wg21.link/P0556.

They kind of go together; for example, `ispow2` is easily implemented using 
`popcount` - and they share a bunch of infastructure.

I don't recommend landing this until P0556 is approved, but when I implemented 
one, the other was easy.
None of this stuff will be constexpr on Windows, because the underlying 
primitives are not constexpr on Windows.

Sorry for the large-ish diff, but (like span) it's 85%+ tests.


https://reviews.llvm.org/D51262

Files:
  include/bit
  test/libcxx/numerics/bit/bitops.count/countl_one.pass.cpp
  test/libcxx/numerics/bit/bitops.count/countl_zero.pass.cpp
  test/libcxx/numerics/bit/bitops.count/countr_one.pass.cpp
  test/libcxx/numerics/bit/bitops.count/countr_zero.pass.cpp
  test/libcxx/numerics/bit/bitops.count/popcount.pass.cpp
  test/libcxx/numerics/bit/bitops.rot/rotl.pass.cpp
  test/libcxx/numerics/bit/bitops.rot/rotr.pass.cpp
  test/std/numerics/bit/bit.pow.two/ceil2.fail.cpp
  test/std/numerics/bit/bit.pow.two/ceil2.pass.cpp
  test/std/numerics/bit/bit.pow.two/floor2.fail.cpp
  test/std/numerics/bit/bit.pow.two/floor2.pass.cpp
  test/std/numerics/bit/bit.pow.two/ispow2.fail.cpp
  test/std/numerics/bit/bit.pow.two/ispow2.pass.cpp
  test/std/numerics/bit/bit.pow.two/log2p1.fail.cpp
  test/std/numerics/bit/bit.pow.two/log2p1.pass.cpp
  test/std/numerics/bit/bitops.count/countl_one.fail.cpp
  test/std/numerics/bit/bitops.count/countl_one.pass.cpp
  test/std/numerics/bit/bitops.count/countl_zero.fail.cpp
  test/std/numerics/bit/bitops.count/countl_zero.pass.cpp
  test/std/numerics/bit/bitops.count/countr_one.fail.cpp
  test/std/numerics/bit/bitops.count/countr_one.pass.cpp
  test/std/numerics/bit/bitops.count/countr_zero.fail.cpp
  test/std/numerics/bit/bitops.count/countr_zero.pass.cpp
  test/std/numerics/bit/bitops.count/popcount.fail.cpp
  test/std/numerics/bit/bitops.count/popcount.pass.cpp
  test/std/numerics/bit/bitops.rot/rotl.fail.cpp
  test/std/numerics/bit/bitops.rot/rotl.pass.cpp
  test/std/numerics/bit/bitops.rot/rotr.fail.cpp
  test/std/numerics/bit/bitops.rot/rotr.pass.cpp
  test/std/numerics/bit/nothing_to_do.pass.cpp

Index: test/std/numerics/bit/nothing_to_do.pass.cpp
===================================================================
--- test/std/numerics/bit/nothing_to_do.pass.cpp
+++ test/std/numerics/bit/nothing_to_do.pass.cpp
@@ -0,0 +1,12 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+int main()
+{
+}
Index: test/std/numerics/bit/bitops.rot/rotr.pass.cpp
===================================================================
--- test/std/numerics/bit/bitops.rot/rotr.pass.cpp
+++ test/std/numerics/bit/bitops.rot/rotr.pass.cpp
@@ -0,0 +1,137 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int rotr(T x, unsigned int s) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	const T max = std::numeric_limits<T>::max();
+
+	return std::rotr(T(128), 0) == T(128)
+	   &&  std::rotr(T(128), 1) == T( 64)
+	   &&  std::rotr(T(128), 2) == T( 32)
+	   &&  std::rotr(T(128), 3) == T( 16)
+	   &&  std::rotr(T(128), 4) == T(  8)
+	   &&  std::rotr(T(128), 5) == T(  4)
+	   &&  std::rotr(T(128), 6) == T(  2)
+	   &&  std::rotr(T(128), 7) == T(  1)
+	   &&  std::rotr(max, 0)  == max
+	   &&  std::rotr(max, 1)  == max
+	   &&  std::rotr(max, 2)  == max
+	   &&  std::rotr(max, 3)  == max
+	   &&  std::rotr(max, 4)  == max
+	   &&  std::rotr(max, 5)  == max
+	   &&  std::rotr(max, 6)  == max
+	   &&  std::rotr(max, 7)  == max
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(T, decltype(std::rotr(T(0), 0)));
+	ASSERT_NOEXCEPT(             std::rotr(T(0), 0));
+	const T max = std::numeric_limits<T>::max();
+	const T val = std::numeric_limits<T>::max() - 1;
+
+	const T uppers [] = {
+		max,	          // not used
+		max - max,	      // 000 .. 0
+		max - (max >> 1), // 800 .. 0
+		max - (max >> 2), // C00 .. 0
+		max - (max >> 3), // E00 .. 0
+		max - (max >> 4), // F00 .. 0
+		max - (max >> 5), // F80 .. 0
+		max - (max >> 6), // FC0 .. 0
+		max - (max >> 7), // FE0 .. 0
+		};
+	
+	assert( std::rotr(val, 0) == val);
+	assert( std::rotr(val, 1) == T((val >> 1) +  uppers[1]));
+	assert( std::rotr(val, 2) == T((val >> 2) +  uppers[2]));
+	assert( std::rotr(val, 3) == T((val >> 3) +  uppers[3]));
+	assert( std::rotr(val, 4) == T((val >> 4) +  uppers[4]));
+	assert( std::rotr(val, 5) == T((val >> 5) +  uppers[5]));
+	assert( std::rotr(val, 6) == T((val >> 6) +  uppers[6]));
+	assert( std::rotr(val, 7) == T((val >> 7) +  uppers[7]));
+}
+
+int main()
+{
+	
+	static_assert(constexpr_test<unsigned char>(),      "");
+	static_assert(constexpr_test<unsigned short>(),     "");
+	static_assert(constexpr_test<unsigned>(),           "");
+	static_assert(constexpr_test<unsigned long>(),      "");
+	static_assert(constexpr_test<unsigned long long>(), "");
+
+	static_assert(constexpr_test<uint8_t>(),   "");
+	static_assert(constexpr_test<uint16_t>(),  "");
+	static_assert(constexpr_test<uint32_t>(),  "");
+	static_assert(constexpr_test<uint64_t>(),  "");
+	static_assert(constexpr_test<size_t>(),    "");
+	static_assert(constexpr_test<uintmax_t>(), "");
+	static_assert(constexpr_test<uintptr_t>(), "");
+
+//	static_assert(constexpr_test<std::byte>(),          "");
+#ifndef _LIBCPP_HAS_NO_INT128
+	static_assert(constexpr_test<__uint128_t>(),        "");
+#endif
+
+
+	runtime_test<unsigned char>();
+	runtime_test<unsigned>();
+	runtime_test<unsigned short>();
+	runtime_test<unsigned long>();
+	runtime_test<unsigned long long>();
+
+	runtime_test<uint8_t>();
+	runtime_test<uint16_t>();
+	runtime_test<uint32_t>();
+	runtime_test<uint64_t>();
+	runtime_test<size_t>();
+	runtime_test<uintmax_t>();
+	runtime_test<uintptr_t>();
+
+
+//	runtime_test<std::byte>();
+#ifndef _LIBCPP_HAS_NO_INT128
+	runtime_test<__uint128_t>();
+
+// 	{
+// 	const int dig = std::numeric_limits<__uint128_t>::digits;
+// 	__uint128_t val = 128;
+// 
+// 	val <<= 32;
+// 	assert( std::rotr(val-1) == dig - 39);
+// 	assert( std::rotr(val)   == dig - 40);
+// 	assert( std::rotr(val+1) == dig - 40);
+// 	val <<= 2;
+// 	assert( std::rotr(val-1) == dig - 41);
+// 	assert( std::rotr(val)   == dig - 42);
+// 	assert( std::rotr(val+1) == dig - 42);
+// 	val <<= 3;
+// 	assert( std::rotr(val-1) == dig - 44);
+// 	assert( std::rotr(val)   == dig - 45);
+// 	assert( std::rotr(val+1) == dig - 45);
+// 	}
+#endif
+}
Index: test/std/numerics/bit/bitops.rot/rotr.fail.cpp
===================================================================
--- test/std/numerics/bit/bitops.rot/rotr.fail.cpp
+++ test/std/numerics/bit/bitops.rot/rotr.fail.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr T rotr(T x, unsigned int s) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+class A{};
+enum       E1 : unsigned char { rEd };
+enum class E2 : unsigned char { red };
+
+int main()
+{
+	(void) std::rotr(static_cast<int>(2), 1);         // expected-error {{no matching function for call to 'rotr'}}	
+	(void) std::rotr(static_cast<signed int>(2), 1);  // expected-error {{no matching function for call to 'rotr'}}	
+	(void) std::rotr(static_cast<long>(2), 1);        // expected-error {{no matching function for call to 'rotr'}}	
+	(void) std::rotr(static_cast<long long >(2), 1);  // expected-error {{no matching function for call to 'rotr'}}	
+#ifndef _LIBCPP_HAS_NO_INT128
+	(void) std::rotr(static_cast<__int128_t>(2), 1);  // expected-error {{no matching function for call to 'rotr'}}	
+#endif
+
+	(void) std::rotr(static_cast<int8_t>(2), 1);      // expected-error {{no matching function for call to 'rotr'}}	
+	(void) std::rotr(static_cast<int16_t>(2), 1);     // expected-error {{no matching function for call to 'rotr'}}	
+	(void) std::rotr(static_cast<int32_t>(2), 1);     // expected-error {{no matching function for call to 'rotr'}}	
+	(void) std::rotr(static_cast<int64_t>(2), 1);     // expected-error {{no matching function for call to 'rotr'}}	
+
+	(void) std::rotr(static_cast<ptrdiff_t>(2), 1);   // expected-error {{no matching function for call to 'rotr'}}	
+
+	(void) std::rotr(true, 1);                        // expected-error {{no matching function for call to 'rotr'}}	
+	(void) std::rotr(static_cast<signed char>(2), 1); // expected-error {{no matching function for call to 'rotr'}}	
+	(void) std::rotr(static_cast<wchar_t>(2), 1);     // expected-error {{no matching function for call to 'rotr'}}	
+	(void) std::rotr(static_cast<char16_t>(2), 1);    // expected-error {{no matching function for call to 'rotr'}}	
+	(void) std::rotr(static_cast<char32_t>(2), 1);    // expected-error {{no matching function for call to 'rotr'}}	
+
+	(void) std::rotr(A{}, 1);                         // expected-error {{no matching function for call to 'rotr'}}	
+	(void) std::rotr(rEd, 1);                         // expected-error {{no matching function for call to 'rotr'}}	
+	(void) std::rotr(E2::red, 1);                     // expected-error {{no matching function for call to 'rotr'}}	
+}
Index: test/std/numerics/bit/bitops.rot/rotl.pass.cpp
===================================================================
--- test/std/numerics/bit/bitops.rot/rotl.pass.cpp
+++ test/std/numerics/bit/bitops.rot/rotl.pass.cpp
@@ -0,0 +1,125 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int rotl(T x, unsigned int s) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+#include <iostream>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	const T max = std::numeric_limits<T>::max();
+	return std::rotl(T(1), 0) == T( 1)
+	   &&  std::rotl(T(1), 1) == T( 2)
+	   &&  std::rotl(T(1), 2) == T( 4)
+	   &&  std::rotl(T(1), 3) == T( 8)
+	   &&  std::rotl(T(1), 4) == T( 16)
+	   &&  std::rotl(T(1), 5) == T( 32)
+	   &&  std::rotl(T(1), 6) == T( 64)
+	   &&  std::rotl(T(1), 7) == T(128)
+	   &&  std::rotl(max, 0)  == max
+	   &&  std::rotl(max, 1)  == max
+	   &&  std::rotl(max, 2)  == max
+	   &&  std::rotl(max, 3)  == max
+	   &&  std::rotl(max, 4)  == max
+	   &&  std::rotl(max, 5)  == max
+	   &&  std::rotl(max, 6)  == max
+	   &&  std::rotl(max, 7)  == max
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(T, decltype(std::rotl(T(0), 0)));
+	ASSERT_NOEXCEPT(             std::rotl(T(0), 0));
+	const T val = std::numeric_limits<T>::max() - 1;
+	
+	assert( std::rotl(val, 0) == val);
+	assert( std::rotl(val, 1) == T((val << 1) +   1));
+	assert( std::rotl(val, 2) == T((val << 2) +   3));
+	assert( std::rotl(val, 3) == T((val << 3) +   7));
+	assert( std::rotl(val, 4) == T((val << 4) +  15));
+	assert( std::rotl(val, 5) == T((val << 5) +  31));
+	assert( std::rotl(val, 6) == T((val << 6) +  63));
+	assert( std::rotl(val, 7) == T((val << 7) + 127));
+}
+
+int main()
+{
+	
+	static_assert(constexpr_test<unsigned char>(),      "");
+	static_assert(constexpr_test<unsigned short>(),     "");
+	static_assert(constexpr_test<unsigned>(),           "");
+	static_assert(constexpr_test<unsigned long>(),      "");
+	static_assert(constexpr_test<unsigned long long>(), "");
+
+	static_assert(constexpr_test<uint8_t>(),   "");
+	static_assert(constexpr_test<uint16_t>(),  "");
+	static_assert(constexpr_test<uint32_t>(),  "");
+	static_assert(constexpr_test<uint64_t>(),  "");
+	static_assert(constexpr_test<size_t>(),    "");
+	static_assert(constexpr_test<uintmax_t>(), "");
+	static_assert(constexpr_test<uintptr_t>(), "");
+
+//	static_assert(constexpr_test<std::byte>(),          "");
+#ifndef _LIBCPP_HAS_NO_INT128
+	static_assert(constexpr_test<__uint128_t>(),        "");
+#endif
+
+
+	runtime_test<unsigned char>();
+	runtime_test<unsigned short>();
+	runtime_test<unsigned>();
+	runtime_test<unsigned long>();
+	runtime_test<unsigned long long>();
+
+	runtime_test<uint8_t>();
+	runtime_test<uint16_t>();
+	runtime_test<uint32_t>();
+	runtime_test<uint64_t>();
+	runtime_test<size_t>();
+	runtime_test<uintmax_t>();
+	runtime_test<uintptr_t>();
+
+//	runtime_test<std::byte>();
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	runtime_test<__uint128_t>();
+
+// 	{
+// 	const int dig = std::numeric_limits<__uint128_t>::digits;
+// 	__uint128_t val = 128;
+// 
+// 	val <<= 32;
+// 	assert( std::rotl(val) == dig - 39);
+// 	assert( std::rotl(val)   == dig - 40);
+// 	assert( std::rotl(val+1) == dig - 40);
+// 	val <<= 2;
+// 	assert( std::rotl(val-1) == dig - 41);
+// 	assert( std::rotl(val)   == dig - 42);
+// 	assert( std::rotl(val+1) == dig - 42);
+// 	val <<= 3;
+// 	assert( std::rotl(val-1) == dig - 44);
+// 	assert( std::rotl(val)   == dig - 45);
+// 	assert( std::rotl(val+1) == dig - 45);
+// 	}
+#endif
+
+}
Index: test/std/numerics/bit/bitops.rot/rotl.fail.cpp
===================================================================
--- test/std/numerics/bit/bitops.rot/rotl.fail.cpp
+++ test/std/numerics/bit/bitops.rot/rotl.fail.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr T rotl(T x, unsigned int s) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+class A{};
+enum       E1 : unsigned char { rEd };
+enum class E2 : unsigned char { red };
+
+int main()
+{
+	(void) std::rotl(static_cast<int>(2), 1);         // expected-error {{no matching function for call to 'rotl'}}	
+	(void) std::rotl(static_cast<signed int>(2), 1);  // expected-error {{no matching function for call to 'rotl'}}	
+	(void) std::rotl(static_cast<long>(2), 1);        // expected-error {{no matching function for call to 'rotl'}}	
+	(void) std::rotl(static_cast<long long >(2), 1);  // expected-error {{no matching function for call to 'rotl'}}	
+#ifndef _LIBCPP_HAS_NO_INT128
+	(void) std::rotl(static_cast<__int128_t>(2), 1);  // expected-error {{no matching function for call to 'rotl'}}	
+#endif
+
+	(void) std::rotl(static_cast<int8_t>(2), 1);      // expected-error {{no matching function for call to 'rotl'}}	
+	(void) std::rotl(static_cast<int16_t>(2), 1);     // expected-error {{no matching function for call to 'rotl'}}	
+	(void) std::rotl(static_cast<int32_t>(2), 1);     // expected-error {{no matching function for call to 'rotl'}}	
+	(void) std::rotl(static_cast<int64_t>(2), 1);     // expected-error {{no matching function for call to 'rotl'}}	
+
+	(void) std::rotl(static_cast<ptrdiff_t>(2), 1);   // expected-error {{no matching function for call to 'rotl'}}	
+
+	(void) std::rotl(true, 1);                        // expected-error {{no matching function for call to 'rotl'}}	
+	(void) std::rotl(static_cast<signed char>(2), 1); // expected-error {{no matching function for call to 'rotl'}}	
+	(void) std::rotl(static_cast<wchar_t>(2), 1);     // expected-error {{no matching function for call to 'rotl'}}	
+	(void) std::rotl(static_cast<char16_t>(2), 1);    // expected-error {{no matching function for call to 'rotl'}}	
+	(void) std::rotl(static_cast<char32_t>(2), 1);    // expected-error {{no matching function for call to 'rotl'}}	
+
+	(void) std::rotl(A{}, 1);                         // expected-error {{no matching function for call to 'rotl'}}	
+	(void) std::rotl(rEd, 1);                         // expected-error {{no matching function for call to 'rotl'}}	
+	(void) std::rotl(E2::red, 1);                     // expected-error {{no matching function for call to 'rotl'}}	
+}
Index: test/std/numerics/bit/bitops.count/popcount.pass.cpp
===================================================================
--- test/std/numerics/bit/bitops.count/popcount.pass.cpp
+++ test/std/numerics/bit/bitops.count/popcount.pass.cpp
@@ -0,0 +1,120 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int popcount(T x) noexcept;
+
+// Returns: The number of bits set to one in the value of x.
+//
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	return std::popcount(T(0)) == 0
+	   &&  std::popcount(T(1)) == 1
+	   &&  std::popcount(T(2)) == 1
+	   &&  std::popcount(T(3)) == 2
+	   &&  std::popcount(T(4)) == 1
+	   &&  std::popcount(T(5)) == 2
+	   &&  std::popcount(T(6)) == 2
+	   &&  std::popcount(T(7)) == 3
+	   &&  std::popcount(T(8)) == 1
+	   &&  std::popcount(T(9)) == 2
+	   &&  std::popcount(std::numeric_limits<T>::max()) == std::numeric_limits<T>::digits
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(int, decltype(std::popcount(T(0))));
+	ASSERT_NOEXCEPT(               std::popcount(T(0)));
+	
+	assert( std::popcount(T(121)) == 5);
+	assert( std::popcount(T(122)) == 5);
+	assert( std::popcount(T(123)) == 6);
+	assert( std::popcount(T(124)) == 5);
+	assert( std::popcount(T(125)) == 6);
+	assert( std::popcount(T(126)) == 6);
+	assert( std::popcount(T(127)) == 7);
+	assert( std::popcount(T(128)) == 1);
+	assert( std::popcount(T(129)) == 2);
+	assert( std::popcount(T(130)) == 2);
+}
+
+int main()
+{
+	
+	static_assert(constexpr_test<unsigned char>(),      "");
+	static_assert(constexpr_test<unsigned short>(),     "");
+	static_assert(constexpr_test<unsigned>(),           "");
+	static_assert(constexpr_test<unsigned long>(),      "");
+	static_assert(constexpr_test<unsigned long long>(), "");
+
+	static_assert(constexpr_test<uint8_t>(),   "");
+	static_assert(constexpr_test<uint16_t>(),  "");
+	static_assert(constexpr_test<uint32_t>(),  "");
+	static_assert(constexpr_test<uint64_t>(),  "");
+	static_assert(constexpr_test<size_t>(),    "");
+	static_assert(constexpr_test<uintmax_t>(), "");
+	static_assert(constexpr_test<uintptr_t>(), "");
+
+	static_assert(constexpr_test<std::byte>(), "");
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	static_assert(constexpr_test<__uint128_t>(),        "");
+#endif
+
+
+	runtime_test<unsigned char>();
+	runtime_test<unsigned>();
+	runtime_test<unsigned short>();
+	runtime_test<unsigned long>();
+	runtime_test<unsigned long long>();
+
+	runtime_test<uint8_t>();
+	runtime_test<uint16_t>();
+	runtime_test<uint32_t>();
+	runtime_test<uint64_t>();
+	runtime_test<size_t>();
+	runtime_test<uintmax_t>();
+	runtime_test<uintptr_t>();
+
+	runtime_test<std::byte>();
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	runtime_test<__uint128_t>();
+
+	{
+	__uint128_t val = 128;
+
+	val <<= 32;
+	assert( std::popcount(val-1) == 39);
+	assert( std::popcount(val)   ==  1);
+	assert( std::popcount(val+1) ==  2);
+	val <<= 2;
+	assert( std::popcount(val-1) == 41);
+	assert( std::popcount(val)   ==  1);
+	assert( std::popcount(val+1) ==  2);
+	val <<= 3;
+	assert( std::popcount(val-1) == 44);
+	assert( std::popcount(val)   ==  1);
+	assert( std::popcount(val+1) ==  2);
+	}
+#endif
+}
Index: test/std/numerics/bit/bitops.count/popcount.fail.cpp
===================================================================
--- test/std/numerics/bit/bitops.count/popcount.fail.cpp
+++ test/std/numerics/bit/bitops.count/popcount.fail.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int popcount(T x) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+class A{};
+enum       E1 : unsigned char { rEd };
+enum class E2 : unsigned char { red };
+
+int main()
+{
+	(void) std::popcount(static_cast<int>(2));         // expected-error {{no matching function for call to 'popcount'}}	
+	(void) std::popcount(static_cast<signed int>(2));  // expected-error {{no matching function for call to 'popcount'}}	
+	(void) std::popcount(static_cast<long>(2));        // expected-error {{no matching function for call to 'popcount'}}	
+	(void) std::popcount(static_cast<long long >(2));  // expected-error {{no matching function for call to 'popcount'}}	
+#ifndef _LIBCPP_HAS_NO_INT128
+	(void) std::popcount(static_cast<__int128_t>(2));  // expected-error {{no matching function for call to 'popcount'}}	
+#endif
+
+	(void) std::popcount(static_cast<int8_t>(2));      // expected-error {{no matching function for call to 'popcount'}}	
+	(void) std::popcount(static_cast<int16_t>(2));     // expected-error {{no matching function for call to 'popcount'}}	
+	(void) std::popcount(static_cast<int32_t>(2));     // expected-error {{no matching function for call to 'popcount'}}	
+	(void) std::popcount(static_cast<int64_t>(2));     // expected-error {{no matching function for call to 'popcount'}}	
+
+	(void) std::popcount(static_cast<ptrdiff_t>(2));   // expected-error {{no matching function for call to 'popcount'}}	
+
+	(void) std::popcount(true);                        // expected-error {{no matching function for call to 'popcount'}}	
+	(void) std::popcount(static_cast<signed char>(2)); // expected-error {{no matching function for call to 'popcount'}}	
+	(void) std::popcount(static_cast<wchar_t>(2));     // expected-error {{no matching function for call to 'popcount'}}	
+	(void) std::popcount(static_cast<char16_t>(2));    // expected-error {{no matching function for call to 'popcount'}}	
+	(void) std::popcount(static_cast<char32_t>(2));    // expected-error {{no matching function for call to 'popcount'}}	
+
+	(void) std::popcount(A{});                         // expected-error {{no matching function for call to 'popcount'}}	
+	(void) std::popcount(rEd);                         // expected-error {{no matching function for call to 'popcount'}}	
+	(void) std::popcount(E2::red);                     // expected-error {{no matching function for call to 'popcount'}}	
+}
Index: test/std/numerics/bit/bitops.count/countr_zero.pass.cpp
===================================================================
--- test/std/numerics/bit/bitops.count/countr_zero.pass.cpp
+++ test/std/numerics/bit/bitops.count/countr_zero.pass.cpp
@@ -0,0 +1,121 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int countr_zero(T x) noexcept;
+
+// Returns: The number of consecutive 0 bits, starting from the most significant bit.
+//   [ Note: Returns N if x == 0. ]
+//
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	const int dig = std::numeric_limits<T>::digits;
+	return std::countr_zero(T(0)) == dig
+	   &&  std::countr_zero(T(1)) == 0
+	   &&  std::countr_zero(T(2)) == 1
+	   &&  std::countr_zero(T(3)) == 0
+	   &&  std::countr_zero(T(4)) == 2
+	   &&  std::countr_zero(T(5)) == 0
+	   &&  std::countr_zero(T(6)) == 1
+	   &&  std::countr_zero(T(7)) == 0
+	   &&  std::countr_zero(T(8)) == 3
+	   &&  std::countr_zero(T(9)) == 0
+	   &&  std::countr_zero(std::numeric_limits<T>::max()) == 0
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(int, decltype(std::countr_zero(T(0))));
+	ASSERT_NOEXCEPT(               std::countr_zero(T(0)));
+	
+	assert( std::countr_zero(T(121)) == 0);
+	assert( std::countr_zero(T(122)) == 1);
+	assert( std::countr_zero(T(123)) == 0);
+	assert( std::countr_zero(T(124)) == 2);
+	assert( std::countr_zero(T(125)) == 0);
+	assert( std::countr_zero(T(126)) == 1);
+	assert( std::countr_zero(T(127)) == 0);
+	assert( std::countr_zero(T(128)) == 7);
+	assert( std::countr_zero(T(129)) == 0);
+	assert( std::countr_zero(T(130)) == 1);
+}
+
+int main()
+{
+	
+	static_assert(constexpr_test<unsigned char>(),      "");
+	static_assert(constexpr_test<unsigned short>(),     "");
+	static_assert(constexpr_test<unsigned>(),           "");
+	static_assert(constexpr_test<unsigned long>(),      "");
+	static_assert(constexpr_test<unsigned long long>(), "");
+
+	static_assert(constexpr_test<uint8_t>(),   "");
+	static_assert(constexpr_test<uint16_t>(),  "");
+	static_assert(constexpr_test<uint32_t>(),  "");
+	static_assert(constexpr_test<uint64_t>(),  "");
+	static_assert(constexpr_test<size_t>(),    "");
+	static_assert(constexpr_test<uintmax_t>(), "");
+	static_assert(constexpr_test<uintptr_t>(), "");
+
+//	static_assert(constexpr_test<std::byte>(),          "");
+#ifndef _LIBCPP_HAS_NO_INT128
+	static_assert(constexpr_test<__uint128_t>(),        "");
+#endif
+
+	runtime_test<unsigned char>();
+	runtime_test<unsigned>();
+	runtime_test<unsigned short>();
+	runtime_test<unsigned long>();
+	runtime_test<unsigned long long>();
+
+	runtime_test<uint8_t>();
+	runtime_test<uint16_t>();
+	runtime_test<uint32_t>();
+	runtime_test<uint64_t>();
+	runtime_test<size_t>();
+	runtime_test<uintmax_t>();
+	runtime_test<uintptr_t>();
+
+//	runtime_test<std::byte>();
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	runtime_test<__uint128_t>();
+
+	{
+	__uint128_t val = 128;
+
+	val <<= 32;
+	assert( std::countr_zero(val-1) ==  0);
+	assert( std::countr_zero(val)   == 39);
+	assert( std::countr_zero(val+1) ==  0);
+	val <<= 2;
+	assert( std::countr_zero(val-1) ==  0);
+	assert( std::countr_zero(val)   == 41);
+	assert( std::countr_zero(val+1) ==  0);
+	val <<= 3;
+	assert( std::countr_zero(val-1) ==  0);
+	assert( std::countr_zero(val)   == 44);
+	assert( std::countr_zero(val+1) ==  0);
+	}
+#endif
+
+}
Index: test/std/numerics/bit/bitops.count/countr_zero.fail.cpp
===================================================================
--- test/std/numerics/bit/bitops.count/countr_zero.fail.cpp
+++ test/std/numerics/bit/bitops.count/countr_zero.fail.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int countr_zero(T x) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+class A{};
+enum       E1 : unsigned char { rEd };
+enum class E2 : unsigned char { red };
+
+int main()
+{
+	(void) std::countr_zero(static_cast<int>(2));         // expected-error {{no matching function for call to 'countr_zero'}}	
+	(void) std::countr_zero(static_cast<signed int>(2));  // expected-error {{no matching function for call to 'countr_zero'}}	
+	(void) std::countr_zero(static_cast<long>(2));        // expected-error {{no matching function for call to 'countr_zero'}}	
+	(void) std::countr_zero(static_cast<long long >(2));  // expected-error {{no matching function for call to 'countr_zero'}}	
+#ifndef _LIBCPP_HAS_NO_INT128
+	(void) std::countr_zero(static_cast<__int128_t>(2));  // expected-error {{no matching function for call to 'countr_zero'}}	
+#endif
+
+	(void) std::countr_zero(static_cast<int8_t>(2));      // expected-error {{no matching function for call to 'countr_zero'}}	
+	(void) std::countr_zero(static_cast<int16_t>(2));     // expected-error {{no matching function for call to 'countr_zero'}}	
+	(void) std::countr_zero(static_cast<int32_t>(2));     // expected-error {{no matching function for call to 'countr_zero'}}	
+	(void) std::countr_zero(static_cast<int64_t>(2));     // expected-error {{no matching function for call to 'countr_zero'}}	
+
+	(void) std::countr_zero(static_cast<ptrdiff_t>(2));   // expected-error {{no matching function for call to 'countr_zero'}}	
+
+	(void) std::countr_zero(true);                        // expected-error {{no matching function for call to 'countr_zero'}}	
+	(void) std::countr_zero(static_cast<signed char>(2)); // expected-error {{no matching function for call to 'countr_zero'}}	
+	(void) std::countr_zero(static_cast<wchar_t>(2));     // expected-error {{no matching function for call to 'countr_zero'}}	
+	(void) std::countr_zero(static_cast<char16_t>(2));    // expected-error {{no matching function for call to 'countr_zero'}}	
+	(void) std::countr_zero(static_cast<char32_t>(2));    // expected-error {{no matching function for call to 'countr_zero'}}	
+
+	(void) std::countr_zero(A{});                         // expected-error {{no matching function for call to 'countr_zero'}}	
+	(void) std::countr_zero(rEd);                         // expected-error {{no matching function for call to 'countr_zero'}}	
+	(void) std::countr_zero(E2::red);                     // expected-error {{no matching function for call to 'countr_zero'}}	
+}
Index: test/std/numerics/bit/bitops.count/countr_one.pass.cpp
===================================================================
--- test/std/numerics/bit/bitops.count/countr_one.pass.cpp
+++ test/std/numerics/bit/bitops.count/countr_one.pass.cpp
@@ -0,0 +1,122 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int countr_one(T x) noexcept;
+
+// Returns: The number of consecutive 1 bits, starting from the least significant bit.
+//   [ Note: Returns N if x == std::numeric_limits<T>::max(). ]
+//
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	const int dig = std::numeric_limits<T>::digits;
+	return std::countr_one(T(0)) == 0
+	   &&  std::countr_one(T(1)) == 1
+	   &&  std::countr_one(T(2)) == 0
+	   &&  std::countr_one(T(3)) == 2
+	   &&  std::countr_one(T(4)) == 0
+	   &&  std::countr_one(T(5)) == 1
+	   &&  std::countr_one(T(6)) == 0
+	   &&  std::countr_one(T(7)) == 3
+	   &&  std::countr_one(T(8)) == 0
+	   &&  std::countr_one(T(9)) == 1
+	   &&  std::countr_one(std::numeric_limits<T>::max()) == dig
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(int, decltype(std::countr_one(T(0))));
+	ASSERT_NOEXCEPT(               std::countr_one(T(0)));
+	
+	assert( std::countr_one(T(121)) == 1);
+	assert( std::countr_one(T(122)) == 0);
+	assert( std::countr_one(T(123)) == 2);
+	assert( std::countr_one(T(124)) == 0);
+	assert( std::countr_one(T(125)) == 1);
+	assert( std::countr_one(T(126)) == 0);
+	assert( std::countr_one(T(127)) == 7);
+	assert( std::countr_one(T(128)) == 0);
+	assert( std::countr_one(T(129)) == 1);
+	assert( std::countr_one(T(130)) == 0);
+}
+
+int main()
+{
+	
+	static_assert(constexpr_test<unsigned char>(),      "");
+	static_assert(constexpr_test<unsigned short>(),     "");
+	static_assert(constexpr_test<unsigned>(),           "");
+	static_assert(constexpr_test<unsigned long>(),      "");
+	static_assert(constexpr_test<unsigned long long>(), "");
+
+	static_assert(constexpr_test<uint8_t>(),   "");
+	static_assert(constexpr_test<uint16_t>(),  "");
+	static_assert(constexpr_test<uint32_t>(),  "");
+	static_assert(constexpr_test<uint64_t>(),  "");
+	static_assert(constexpr_test<size_t>(),    "");
+	static_assert(constexpr_test<uintmax_t>(), "");
+	static_assert(constexpr_test<uintptr_t>(), "");
+
+	static_assert(constexpr_test<std::byte>(),          "");
+#ifndef _LIBCPP_HAS_NO_INT128
+	static_assert(constexpr_test<__uint128_t>(),        "");
+#endif
+
+
+	runtime_test<unsigned char>();
+	runtime_test<unsigned>();
+	runtime_test<unsigned short>();
+	runtime_test<unsigned long>();
+	runtime_test<unsigned long long>();
+
+	runtime_test<uint8_t>();
+	runtime_test<uint16_t>();
+	runtime_test<uint32_t>();
+	runtime_test<uint64_t>();
+	runtime_test<size_t>();
+	runtime_test<uintmax_t>();
+	runtime_test<uintptr_t>();
+
+	runtime_test<std::byte>();
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	runtime_test<__uint128_t>();
+
+	{
+	__uint128_t val = 128;
+
+	val <<= 32;
+	assert( std::countr_one(val-1) == 39);
+	assert( std::countr_one(val)   ==  0);
+	assert( std::countr_one(val+1) ==  1);
+	val <<= 2;
+	assert( std::countr_one(val-1) == 41);
+	assert( std::countr_one(val)   ==  0);
+	assert( std::countr_one(val+1) ==  1);
+	val <<= 3;
+	assert( std::countr_one(val-1) == 44);
+	assert( std::countr_one(val)   ==  0);
+	assert( std::countr_one(val+1) ==  1);
+	}
+#endif
+
+}
Index: test/std/numerics/bit/bitops.count/countr_one.fail.cpp
===================================================================
--- test/std/numerics/bit/bitops.count/countr_one.fail.cpp
+++ test/std/numerics/bit/bitops.count/countr_one.fail.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int countr_one(T x) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+class A{};
+enum       E1 : unsigned char { rEd };
+enum class E2 : unsigned char { red };
+
+int main()
+{
+	(void) std::countr_one(static_cast<int>(2));         // expected-error {{no matching function for call to 'countr_one'}}	
+	(void) std::countr_one(static_cast<signed int>(2));  // expected-error {{no matching function for call to 'countr_one'}}	
+	(void) std::countr_one(static_cast<long>(2));        // expected-error {{no matching function for call to 'countr_one'}}	
+	(void) std::countr_one(static_cast<long long >(2));  // expected-error {{no matching function for call to 'countr_one'}}	
+#ifndef _LIBCPP_HAS_NO_INT128
+	(void) std::countr_one(static_cast<__int128_t>(2));  // expected-error {{no matching function for call to 'countr_one'}}	
+#endif
+
+	(void) std::countr_one(static_cast<int8_t>(2));      // expected-error {{no matching function for call to 'countr_one'}}	
+	(void) std::countr_one(static_cast<int16_t>(2));     // expected-error {{no matching function for call to 'countr_one'}}	
+	(void) std::countr_one(static_cast<int32_t>(2));     // expected-error {{no matching function for call to 'countr_one'}}	
+	(void) std::countr_one(static_cast<int64_t>(2));     // expected-error {{no matching function for call to 'countr_one'}}	
+
+	(void) std::countr_one(static_cast<ptrdiff_t>(2));   // expected-error {{no matching function for call to 'countr_one'}}	
+
+	(void) std::countr_one(true);                        // expected-error {{no matching function for call to 'countr_one'}}	
+	(void) std::countr_one(static_cast<signed char>(2)); // expected-error {{no matching function for call to 'countr_one'}}	
+	(void) std::countr_one(static_cast<wchar_t>(2));     // expected-error {{no matching function for call to 'countr_one'}}	
+	(void) std::countr_one(static_cast<char16_t>(2));    // expected-error {{no matching function for call to 'countr_one'}}	
+	(void) std::countr_one(static_cast<char32_t>(2));    // expected-error {{no matching function for call to 'countr_one'}}	
+
+	(void) std::countr_one(A{});                         // expected-error {{no matching function for call to 'countr_one'}}	
+	(void) std::countr_one(rEd);                         // expected-error {{no matching function for call to 'countr_one'}}	
+	(void) std::countr_one(E2::red);                     // expected-error {{no matching function for call to 'countr_one'}}	
+}
Index: test/std/numerics/bit/bitops.count/countl_zero.pass.cpp
===================================================================
--- test/std/numerics/bit/bitops.count/countl_zero.pass.cpp
+++ test/std/numerics/bit/bitops.count/countl_zero.pass.cpp
@@ -0,0 +1,123 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int countl_zero(T x) noexcept;
+
+// Returns: The number of consecutive 0 bits, starting from the most significant bit.
+//   [ Note: Returns N if x == 0. ]
+//
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	const int dig = std::numeric_limits<T>::digits;
+	return std::countl_zero(T(0)) == dig
+	   &&  std::countl_zero(T(1)) == dig - 1
+	   &&  std::countl_zero(T(2)) == dig - 2
+	   &&  std::countl_zero(T(3)) == dig - 2
+	   &&  std::countl_zero(T(4)) == dig - 3
+	   &&  std::countl_zero(T(5)) == dig - 3
+	   &&  std::countl_zero(T(6)) == dig - 3
+	   &&  std::countl_zero(T(7)) == dig - 3
+	   &&  std::countl_zero(T(8)) == dig - 4
+	   &&  std::countl_zero(T(9)) == dig - 4
+	   &&  std::countl_zero(std::numeric_limits<T>::max()) == 0
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(int, decltype(std::countl_zero(T(0))));
+	ASSERT_NOEXCEPT(               std::countl_zero(T(0)));
+	const int dig = std::numeric_limits<T>::digits;
+	
+	assert( std::countl_zero(T(121)) == dig - 7);
+	assert( std::countl_zero(T(122)) == dig - 7);
+	assert( std::countl_zero(T(123)) == dig - 7);
+	assert( std::countl_zero(T(124)) == dig - 7);
+	assert( std::countl_zero(T(125)) == dig - 7);
+	assert( std::countl_zero(T(126)) == dig - 7);
+	assert( std::countl_zero(T(127)) == dig - 7);
+	assert( std::countl_zero(T(128)) == dig - 8);
+	assert( std::countl_zero(T(129)) == dig - 8);
+	assert( std::countl_zero(T(130)) == dig - 8);
+}
+
+int main()
+{
+	
+	static_assert(constexpr_test<unsigned char>(),      "");
+	static_assert(constexpr_test<unsigned short>(),     "");
+	static_assert(constexpr_test<unsigned>(),           "");
+	static_assert(constexpr_test<unsigned long>(),      "");
+	static_assert(constexpr_test<unsigned long long>(), "");
+
+	static_assert(constexpr_test<uint8_t>(),   "");
+	static_assert(constexpr_test<uint16_t>(),  "");
+	static_assert(constexpr_test<uint32_t>(),  "");
+	static_assert(constexpr_test<uint64_t>(),  "");
+	static_assert(constexpr_test<size_t>(),    "");
+	static_assert(constexpr_test<uintmax_t>(), "");
+	static_assert(constexpr_test<uintptr_t>(), "");
+
+//	static_assert(constexpr_test<std::byte>(),          "");
+#ifndef _LIBCPP_HAS_NO_INT128
+	static_assert(constexpr_test<__uint128_t>(),        "");
+#endif
+
+
+	runtime_test<unsigned char>();
+	runtime_test<unsigned>();
+	runtime_test<unsigned short>();
+	runtime_test<unsigned long>();
+	runtime_test<unsigned long long>();
+
+	runtime_test<uint8_t>();
+	runtime_test<uint16_t>();
+	runtime_test<uint32_t>();
+	runtime_test<uint64_t>();
+	runtime_test<size_t>();
+	runtime_test<uintmax_t>();
+	runtime_test<uintptr_t>();
+
+//	runtime_test<std::byte>();
+#ifndef _LIBCPP_HAS_NO_INT128
+	runtime_test<__uint128_t>();
+
+	{
+	const int dig = std::numeric_limits<__uint128_t>::digits;
+	__uint128_t val = 128;
+
+	val <<= 32;
+	assert( std::countl_zero(val-1) == dig - 39);
+	assert( std::countl_zero(val)   == dig - 40);
+	assert( std::countl_zero(val+1) == dig - 40);
+	val <<= 2;
+	assert( std::countl_zero(val-1) == dig - 41);
+	assert( std::countl_zero(val)   == dig - 42);
+	assert( std::countl_zero(val+1) == dig - 42);
+	val <<= 3;
+	assert( std::countl_zero(val-1) == dig - 44);
+	assert( std::countl_zero(val)   == dig - 45);
+	assert( std::countl_zero(val+1) == dig - 45);
+	}
+#endif
+
+}
Index: test/std/numerics/bit/bitops.count/countl_zero.fail.cpp
===================================================================
--- test/std/numerics/bit/bitops.count/countl_zero.fail.cpp
+++ test/std/numerics/bit/bitops.count/countl_zero.fail.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int countl_zero(T x) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+class A{};
+enum       E1 : unsigned char { rEd };
+enum class E2 : unsigned char { red };
+
+int main()
+{
+	(void) std::countl_zero(static_cast<int>(2));         // expected-error {{no matching function for call to 'countl_zero'}}	
+	(void) std::countl_zero(static_cast<signed int>(2));  // expected-error {{no matching function for call to 'countl_zero'}}	
+	(void) std::countl_zero(static_cast<long>(2));        // expected-error {{no matching function for call to 'countl_zero'}}	
+	(void) std::countl_zero(static_cast<long long >(2));  // expected-error {{no matching function for call to 'countl_zero'}}	
+#ifndef _LIBCPP_HAS_NO_INT128
+	(void) std::countl_zero(static_cast<__int128_t>(2));  // expected-error {{no matching function for call to 'countl_zero'}}	
+#endif
+
+	(void) std::countl_zero(static_cast<int8_t>(2));      // expected-error {{no matching function for call to 'countl_zero'}}	
+	(void) std::countl_zero(static_cast<int16_t>(2));     // expected-error {{no matching function for call to 'countl_zero'}}	
+	(void) std::countl_zero(static_cast<int32_t>(2));     // expected-error {{no matching function for call to 'countl_zero'}}	
+	(void) std::countl_zero(static_cast<int64_t>(2));     // expected-error {{no matching function for call to 'countl_zero'}}	
+
+	(void) std::countl_zero(static_cast<ptrdiff_t>(2));   // expected-error {{no matching function for call to 'countl_zero'}}	
+
+	(void) std::countl_zero(true);                        // expected-error {{no matching function for call to 'countl_zero'}}	
+	(void) std::countl_zero(static_cast<signed char>(2)); // expected-error {{no matching function for call to 'countl_zero'}}	
+	(void) std::countl_zero(static_cast<wchar_t>(2));     // expected-error {{no matching function for call to 'countl_zero'}}	
+	(void) std::countl_zero(static_cast<char16_t>(2));    // expected-error {{no matching function for call to 'countl_zero'}}	
+	(void) std::countl_zero(static_cast<char32_t>(2));    // expected-error {{no matching function for call to 'countl_zero'}}	
+
+	(void) std::countl_zero(A{});                         // expected-error {{no matching function for call to 'countl_zero'}}	
+	(void) std::countl_zero(rEd);                         // expected-error {{no matching function for call to 'countl_zero'}}	
+	(void) std::countl_zero(E2::red);                     // expected-error {{no matching function for call to 'countl_zero'}}	
+}
Index: test/std/numerics/bit/bitops.count/countl_one.pass.cpp
===================================================================
--- test/std/numerics/bit/bitops.count/countl_one.pass.cpp
+++ test/std/numerics/bit/bitops.count/countl_one.pass.cpp
@@ -0,0 +1,116 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int countl_one(T x) noexcept;
+
+// The number of consecutive 1 bits, starting from the most significant bit.
+//   [ Note: Returns N if x == std::numeric_limits<T>::max(). ]
+//
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	const int dig = std::numeric_limits<T>::digits;
+	const T max   = std::numeric_limits<T>::max();
+	return std::countl_one(max) == dig
+	   &&  std::countl_one(T(max - 1)) == dig - 1
+	   &&  std::countl_one(T(max - 2)) == dig - 2
+	   &&  std::countl_one(T(max - 3)) == dig - 2
+	   &&  std::countl_one(T(max - 4)) == dig - 3
+	   &&  std::countl_one(T(max - 5)) == dig - 3
+	   &&  std::countl_one(T(max - 6)) == dig - 3
+	   &&  std::countl_one(T(max - 7)) == dig - 3
+	   &&  std::countl_one(T(max - 8)) == dig - 4
+	   &&  std::countl_one(T(max - 9)) == dig - 4
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(int, decltype(std::countl_one(T(0))));
+	ASSERT_NOEXCEPT(               std::countl_one(T(0)));
+	const int dig = std::numeric_limits<T>::digits;
+	
+	assert( std::countl_one(T(~121)) == dig - 7);
+	assert( std::countl_one(T(~122)) == dig - 7);
+	assert( std::countl_one(T(~123)) == dig - 7);
+	assert( std::countl_one(T(~124)) == dig - 7);
+	assert( std::countl_one(T(~125)) == dig - 7);
+	assert( std::countl_one(T(~126)) == dig - 7);
+	assert( std::countl_one(T(~127)) == dig - 7);
+	assert( std::countl_one(T(~128)) == dig - 8);
+	assert( std::countl_one(T(~129)) == dig - 8);
+	assert( std::countl_one(T(~130)) == dig - 8);
+}
+
+int main()
+{
+	
+	static_assert(constexpr_test<unsigned char>(),      "");
+	static_assert(constexpr_test<unsigned short>(),     "");
+	static_assert(constexpr_test<unsigned>(),           "");
+	static_assert(constexpr_test<unsigned long>(),      "");
+	static_assert(constexpr_test<unsigned long long>(), "");
+
+	static_assert(constexpr_test<uint8_t>(),   "");
+	static_assert(constexpr_test<uint16_t>(),  "");
+	static_assert(constexpr_test<uint32_t>(),  "");
+	static_assert(constexpr_test<uint64_t>(),  "");
+	static_assert(constexpr_test<size_t>(),    "");
+	static_assert(constexpr_test<uintmax_t>(), "");
+	static_assert(constexpr_test<uintptr_t>(), "");
+
+//	static_assert(constexpr_test<std::byte>(),          "");
+#ifndef _LIBCPP_HAS_NO_INT128
+	static_assert(constexpr_test<__uint128_t>(),        "");
+#endif
+
+
+	runtime_test<unsigned char>();
+	runtime_test<unsigned>();
+	runtime_test<unsigned short>();
+	runtime_test<unsigned long>();
+	runtime_test<unsigned long long>();
+
+	runtime_test<uint8_t>();
+	runtime_test<uint16_t>();
+	runtime_test<uint32_t>();
+	runtime_test<uint64_t>();
+	runtime_test<size_t>();
+	runtime_test<uintmax_t>();
+	runtime_test<uintptr_t>();
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	runtime_test<__uint128_t>();
+
+	{
+	const int dig = std::numeric_limits<__uint128_t>::digits;
+	__uint128_t val = 128;
+
+	val <<= 32;
+	assert( std::countl_one(~val)   == dig - 40);
+	val <<= 2;
+	assert( std::countl_one(~val)   == dig - 42);
+	val <<= 3;
+	assert( std::countl_one(~val)   == dig - 45);
+	}
+#endif
+
+}
Index: test/std/numerics/bit/bitops.count/countl_one.fail.cpp
===================================================================
--- test/std/numerics/bit/bitops.count/countl_one.fail.cpp
+++ test/std/numerics/bit/bitops.count/countl_one.fail.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int countl_one(T x) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+class A{};
+enum       E1 : unsigned char { rEd };
+enum class E2 : unsigned char { red };
+
+int main()
+{
+	(void) std::countl_one(static_cast<int>(2));         // expected-error {{no matching function for call to 'countl_one'}}	
+	(void) std::countl_one(static_cast<signed int>(2));  // expected-error {{no matching function for call to 'countl_one'}}	
+	(void) std::countl_one(static_cast<long>(2));        // expected-error {{no matching function for call to 'countl_one'}}	
+	(void) std::countl_one(static_cast<long long >(2));  // expected-error {{no matching function for call to 'countl_one'}}	
+#ifndef _LIBCPP_HAS_NO_INT128
+	(void) std::countl_one(static_cast<__int128_t>(2));  // expected-error {{no matching function for call to 'countl_one'}}	
+#endif
+
+	(void) std::countl_one(static_cast<int8_t>(2));      // expected-error {{no matching function for call to 'countl_one'}}	
+	(void) std::countl_one(static_cast<int16_t>(2));     // expected-error {{no matching function for call to 'countl_one'}}	
+	(void) std::countl_one(static_cast<int32_t>(2));     // expected-error {{no matching function for call to 'countl_one'}}	
+	(void) std::countl_one(static_cast<int64_t>(2));     // expected-error {{no matching function for call to 'countl_one'}}	
+
+	(void) std::countl_one(static_cast<ptrdiff_t>(2));   // expected-error {{no matching function for call to 'countl_one'}}	
+
+	(void) std::countl_one(true);                        // expected-error {{no matching function for call to 'countl_one'}}	
+	(void) std::countl_one(static_cast<signed char>(2)); // expected-error {{no matching function for call to 'countl_one'}}	
+	(void) std::countl_one(static_cast<wchar_t>(2));     // expected-error {{no matching function for call to 'countl_one'}}	
+	(void) std::countl_one(static_cast<char16_t>(2));    // expected-error {{no matching function for call to 'countl_one'}}	
+	(void) std::countl_one(static_cast<char32_t>(2));    // expected-error {{no matching function for call to 'countl_one'}}	
+
+	(void) std::countl_one(A{});                         // expected-error {{no matching function for call to 'countl_one'}}	
+	(void) std::countl_one(rEd);                         // expected-error {{no matching function for call to 'countl_one'}}	
+	(void) std::countl_one(E2::red);                     // expected-error {{no matching function for call to 'countl_one'}}	
+}
Index: test/std/numerics/bit/bit.pow.two/log2p1.pass.cpp
===================================================================
--- test/std/numerics/bit/bit.pow.two/log2p1.pass.cpp
+++ test/std/numerics/bit/bit.pow.two/log2p1.pass.cpp
@@ -0,0 +1,127 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr T log2p1(T x) noexcept;
+
+// If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any fractional part discarded.
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	return std::log2p1(T(0)) == T(0)
+	   &&  std::log2p1(T(1)) == T(1)
+	   &&  std::log2p1(T(2)) == T(2)
+	   &&  std::log2p1(T(3)) == T(2)
+	   &&  std::log2p1(T(4)) == T(3)
+	   &&  std::log2p1(T(5)) == T(3)
+	   &&  std::log2p1(T(6)) == T(3)
+	   &&  std::log2p1(T(7)) == T(3)
+	   &&  std::log2p1(T(8)) == T(4)
+	   &&  std::log2p1(T(9)) == T(4)
+	   ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(T, decltype(std::log2p1(T(0))));
+	ASSERT_NOEXCEPT(             std::log2p1(T(0)));
+	
+	assert( std::log2p1(T(0)) == T(0));
+	assert( std::log2p1(T(1)) == T(1));
+	assert( std::log2p1(T(2)) == T(2));
+	assert( std::log2p1(T(3)) == T(2));
+	assert( std::log2p1(T(4)) == T(3));
+	assert( std::log2p1(T(5)) == T(3));
+	assert( std::log2p1(T(6)) == T(3));
+	assert( std::log2p1(T(7)) == T(3));
+	assert( std::log2p1(T(8)) == T(4));
+	assert( std::log2p1(T(9)) == T(4));
+
+
+	assert( std::log2p1(T(121)) == T(7));
+	assert( std::log2p1(T(122)) == T(7));
+	assert( std::log2p1(T(123)) == T(7));
+	assert( std::log2p1(T(124)) == T(7));
+	assert( std::log2p1(T(125)) == T(7));
+	assert( std::log2p1(T(126)) == T(7));
+	assert( std::log2p1(T(127)) == T(7));
+	assert( std::log2p1(T(128)) == T(8));
+	assert( std::log2p1(T(129)) == T(8));
+	assert( std::log2p1(T(130)) == T(8));
+}
+
+int main()
+{
+	
+	static_assert(constexpr_test<unsigned char>(),      "");
+	static_assert(constexpr_test<unsigned short>(),     "");
+	static_assert(constexpr_test<unsigned>(),           "");
+	static_assert(constexpr_test<unsigned long>(),      "");
+	static_assert(constexpr_test<unsigned long long>(), "");
+
+	static_assert(constexpr_test<uint8_t>(),   "");
+	static_assert(constexpr_test<uint16_t>(),  "");
+	static_assert(constexpr_test<uint32_t>(),  "");
+	static_assert(constexpr_test<uint64_t>(),  "");
+	static_assert(constexpr_test<size_t>(),    "");
+	static_assert(constexpr_test<uintmax_t>(), "");
+	static_assert(constexpr_test<uintptr_t>(), "");
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	static_assert(constexpr_test<__uint128_t>(),        "");
+#endif
+
+
+	runtime_test<unsigned char>();
+	runtime_test<unsigned>();
+	runtime_test<unsigned short>();
+	runtime_test<unsigned long>();
+	runtime_test<unsigned long long>();
+
+	runtime_test<uint8_t>();
+	runtime_test<uint16_t>();
+	runtime_test<uint32_t>();
+	runtime_test<uint64_t>();
+	runtime_test<size_t>();
+	runtime_test<uintmax_t>();
+	runtime_test<uintptr_t>();
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	runtime_test<__uint128_t>();
+
+	{
+	__uint128_t val = 128;
+	val <<= 32;
+	assert( std::log2p1(val-1) == 39);
+	assert( std::log2p1(val)   == 40);
+	assert( std::log2p1(val+1) == 40);
+	val <<= 2;
+	assert( std::log2p1(val-1) == 41);
+	assert( std::log2p1(val)   == 42);
+	assert( std::log2p1(val+1) == 42);
+	val <<= 3;
+	assert( std::log2p1(val-1) == 44);
+	assert( std::log2p1(val)   == 45);
+	assert( std::log2p1(val+1) == 45);
+	}
+#endif
+
+}
Index: test/std/numerics/bit/bit.pow.two/log2p1.fail.cpp
===================================================================
--- test/std/numerics/bit/bit.pow.two/log2p1.fail.cpp
+++ test/std/numerics/bit/bit.pow.two/log2p1.fail.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr T log2p1(T x) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+class A{};
+enum       E1 : unsigned char { rEd };
+enum class E2 : unsigned char { red };
+
+int main()
+{
+	(void) std::log2p1(static_cast<int>(2));         // expected-error {{no matching function for call to 'log2p1'}}	
+	(void) std::log2p1(static_cast<signed int>(2));  // expected-error {{no matching function for call to 'log2p1'}}	
+	(void) std::log2p1(static_cast<long>(2));        // expected-error {{no matching function for call to 'log2p1'}}	
+	(void) std::log2p1(static_cast<long long >(2));  // expected-error {{no matching function for call to 'log2p1'}}	
+#ifndef _LIBCPP_HAS_NO_INT128
+	(void) std::log2p1(static_cast<__int128_t>(2));  // expected-error {{no matching function for call to 'log2p1'}}	
+#endif
+
+	(void) std::log2p1(static_cast<int8_t>(2));      // expected-error {{no matching function for call to 'log2p1'}}	
+	(void) std::log2p1(static_cast<int16_t>(2));     // expected-error {{no matching function for call to 'log2p1'}}	
+	(void) std::log2p1(static_cast<int32_t>(2));     // expected-error {{no matching function for call to 'log2p1'}}	
+	(void) std::log2p1(static_cast<int64_t>(2));     // expected-error {{no matching function for call to 'log2p1'}}	
+
+	(void) std::log2p1(static_cast<ptrdiff_t>(2));   // expected-error {{no matching function for call to 'log2p1'}}	
+
+	(void) std::log2p1(true);                        // expected-error {{no matching function for call to 'log2p1'}}	
+	(void) std::log2p1(static_cast<signed char>(2)); // expected-error {{no matching function for call to 'log2p1'}}	
+	(void) std::log2p1(static_cast<wchar_t>(2));     // expected-error {{no matching function for call to 'log2p1'}}	
+	(void) std::log2p1(static_cast<char16_t>(2));    // expected-error {{no matching function for call to 'log2p1'}}	
+	(void) std::log2p1(static_cast<char32_t>(2));    // expected-error {{no matching function for call to 'log2p1'}}	
+
+	(void) std::log2p1(A{});                         // expected-error {{no matching function for call to 'log2p1'}}	
+	(void) std::log2p1(rEd);                         // expected-error {{no matching function for call to 'log2p1'}}	
+	(void) std::log2p1(E2::red);                     // expected-error {{no matching function for call to 'log2p1'}}	
+}
Index: test/std/numerics/bit/bit.pow.two/ispow2.pass.cpp
===================================================================
--- test/std/numerics/bit/bit.pow.two/ispow2.pass.cpp
+++ test/std/numerics/bit/bit.pow.two/ispow2.pass.cpp
@@ -0,0 +1,111 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr bool ispow2(T x) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	return  std::ispow2(T(1))
+	   &&   std::ispow2(T(2))
+	   &&  !std::ispow2(T(3))
+	   &&   std::ispow2(T(4))
+	   &&  !std::ispow2(T(5))
+	   &&  !std::ispow2(T(6))
+	   &&  !std::ispow2(T(7))
+	   &&   std::ispow2(T(8))
+	   &&  !std::ispow2(T(9))
+	   ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(bool, decltype(std::ispow2(T(0))));
+	ASSERT_NOEXCEPT(                std::ispow2(T(0)));
+	
+	assert(!std::ispow2(T(121)));
+	assert(!std::ispow2(T(122)));
+	assert(!std::ispow2(T(123)));
+	assert(!std::ispow2(T(124)));
+	assert(!std::ispow2(T(125)));
+	assert(!std::ispow2(T(126)));
+	assert(!std::ispow2(T(127)));
+	assert( std::ispow2(T(128)));
+	assert(!std::ispow2(T(129)));
+	assert(!std::ispow2(T(130)));
+}
+
+int main()
+{
+	
+	static_assert(constexpr_test<unsigned char>(),      "");
+	static_assert(constexpr_test<unsigned short>(),     "");
+	static_assert(constexpr_test<unsigned>(),           "");
+	static_assert(constexpr_test<unsigned long>(),      "");
+	static_assert(constexpr_test<unsigned long long>(), "");
+
+	static_assert(constexpr_test<uint8_t>(),   "");
+	static_assert(constexpr_test<uint16_t>(),  "");
+	static_assert(constexpr_test<uint32_t>(),  "");
+	static_assert(constexpr_test<uint64_t>(),  "");
+	static_assert(constexpr_test<size_t>(),    "");
+	static_assert(constexpr_test<uintmax_t>(), "");
+	static_assert(constexpr_test<uintptr_t>(), "");
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	static_assert(constexpr_test<__uint128_t>(),        "");
+#endif
+
+	runtime_test<unsigned char>();
+	runtime_test<unsigned>();
+	runtime_test<unsigned short>();
+	runtime_test<unsigned long>();
+	runtime_test<unsigned long long>();
+
+	runtime_test<uint8_t>();
+	runtime_test<uint16_t>();
+	runtime_test<uint32_t>();
+	runtime_test<uint64_t>();
+	runtime_test<size_t>();
+	runtime_test<uintmax_t>();
+	runtime_test<uintptr_t>();
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	runtime_test<__uint128_t>();
+
+	{
+	__uint128_t val = 128;
+	val <<= 32;
+	assert(!std::ispow2(val-1));
+	assert( std::ispow2(val));
+	assert(!std::ispow2(val+1));
+	val <<= 2;
+	assert(!std::ispow2(val-1));
+	assert( std::ispow2(val));
+	assert(!std::ispow2(val+1));
+	val <<= 3;
+	assert(!std::ispow2(val-1));
+	assert( std::ispow2(val));
+	assert(!std::ispow2(val+1));
+	}
+#endif
+	
+}
Index: test/std/numerics/bit/bit.pow.two/ispow2.fail.cpp
===================================================================
--- test/std/numerics/bit/bit.pow.two/ispow2.fail.cpp
+++ test/std/numerics/bit/bit.pow.two/ispow2.fail.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr bool ispow2(T x) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+class A{};
+enum       E1 : unsigned char { rEd };
+enum class E2 : unsigned char { red };
+
+int main()
+{
+	(void) std::ispow2(static_cast<int>(2));         // expected-error {{no matching function for call to 'ispow2'}}	
+	(void) std::ispow2(static_cast<signed int>(2));  // expected-error {{no matching function for call to 'ispow2'}}	
+	(void) std::ispow2(static_cast<long>(2));        // expected-error {{no matching function for call to 'ispow2'}}	
+	(void) std::ispow2(static_cast<long long >(2));  // expected-error {{no matching function for call to 'ispow2'}}	
+#ifndef _LIBCPP_HAS_NO_INT128
+	(void) std::ispow2(static_cast<__int128_t>(2));  // expected-error {{no matching function for call to 'ispow2'}}	
+#endif
+
+	(void) std::ispow2(static_cast<int8_t>(2));      // expected-error {{no matching function for call to 'ispow2'}}	
+	(void) std::ispow2(static_cast<int16_t>(2));     // expected-error {{no matching function for call to 'ispow2'}}	
+	(void) std::ispow2(static_cast<int32_t>(2));     // expected-error {{no matching function for call to 'ispow2'}}	
+	(void) std::ispow2(static_cast<int64_t>(2));     // expected-error {{no matching function for call to 'ispow2'}}	
+
+	(void) std::ispow2(static_cast<ptrdiff_t>(2));   // expected-error {{no matching function for call to 'ispow2'}}	
+
+	(void) std::ispow2(true);                        // expected-error {{no matching function for call to 'ispow2'}}	
+	(void) std::ispow2(static_cast<signed char>(2)); // expected-error {{no matching function for call to 'ispow2'}}	
+	(void) std::ispow2(static_cast<wchar_t>(2));     // expected-error {{no matching function for call to 'ispow2'}}	
+	(void) std::ispow2(static_cast<char16_t>(2));    // expected-error {{no matching function for call to 'ispow2'}}	
+	(void) std::ispow2(static_cast<char32_t>(2));    // expected-error {{no matching function for call to 'ispow2'}}	
+
+	(void) std::ispow2(A{});                         // expected-error {{no matching function for call to 'ispow2'}}	
+	(void) std::ispow2(rEd);                         // expected-error {{no matching function for call to 'ispow2'}}	
+	(void) std::ispow2(E2::red);                     // expected-error {{no matching function for call to 'ispow2'}}	
+}
Index: test/std/numerics/bit/bit.pow.two/floor2.pass.cpp
===================================================================
--- test/std/numerics/bit/bit.pow.two/floor2.pass.cpp
+++ test/std/numerics/bit/bit.pow.two/floor2.pass.cpp
@@ -0,0 +1,113 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr T floor2(T x) noexcept;
+
+// Returns: If x == 0, 0; otherwise the maximal value y such that floor2(y) is true and y <= x.
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	return std::floor2(T(0)) == T(0)
+	   &&  std::floor2(T(1)) == T(1)
+	   &&  std::floor2(T(2)) == T(2)
+	   &&  std::floor2(T(3)) == T(2)
+	   &&  std::floor2(T(4)) == T(4)
+	   &&  std::floor2(T(5)) == T(4)
+	   &&  std::floor2(T(6)) == T(4)
+	   &&  std::floor2(T(7)) == T(4)
+	   &&  std::floor2(T(8)) == T(8)
+	   &&  std::floor2(T(9)) == T(8)
+	   ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(T, decltype(std::floor2(T(0))));
+	ASSERT_NOEXCEPT(             std::floor2(T(0)));
+	
+	assert( std::floor2(T(121)) == T(64));
+	assert( std::floor2(T(122)) == T(64));
+	assert( std::floor2(T(123)) == T(64));
+	assert( std::floor2(T(124)) == T(64));
+	assert( std::floor2(T(125)) == T(64));
+	assert( std::floor2(T(126)) == T(64));
+	assert( std::floor2(T(127)) == T(64));
+	assert( std::floor2(T(128)) == T(128));
+	assert( std::floor2(T(129)) == T(128));
+	assert( std::floor2(T(130)) == T(128));
+}
+
+int main()
+{
+	
+	static_assert(constexpr_test<unsigned char>(),      "");
+	static_assert(constexpr_test<unsigned short>(),     "");
+	static_assert(constexpr_test<unsigned>(),           "");
+	static_assert(constexpr_test<unsigned long>(),      "");
+	static_assert(constexpr_test<unsigned long long>(), "");
+
+	static_assert(constexpr_test<uint8_t>(),   "");
+	static_assert(constexpr_test<uint16_t>(),  "");
+	static_assert(constexpr_test<uint32_t>(),  "");
+	static_assert(constexpr_test<uint64_t>(),  "");
+	static_assert(constexpr_test<size_t>(),    "");
+	static_assert(constexpr_test<uintmax_t>(), "");
+	static_assert(constexpr_test<uintptr_t>(), "");
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	static_assert(constexpr_test<__uint128_t>(),        "");
+#endif
+
+	runtime_test<unsigned char>();
+	runtime_test<unsigned>();
+	runtime_test<unsigned short>();
+	runtime_test<unsigned long>();
+	runtime_test<unsigned long long>();
+
+	runtime_test<uint8_t>();
+	runtime_test<uint16_t>();
+	runtime_test<uint32_t>();
+	runtime_test<uint64_t>();
+	runtime_test<size_t>();
+	runtime_test<uintmax_t>();
+	runtime_test<uintptr_t>();
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	runtime_test<__uint128_t>();
+
+	{
+	__uint128_t val = 128;
+	val <<= 32;
+	assert( std::floor2(val-1) == val/2);
+	assert( std::floor2(val)   == val);
+	assert( std::floor2(val+1) == val);
+	val <<= 2;
+	assert( std::floor2(val-1) == val/2);
+	assert( std::floor2(val)   == val);
+	assert( std::floor2(val+1) == val);
+	val <<= 3;
+	assert( std::floor2(val-1) == val/2);
+	assert( std::floor2(val)   == val);
+	assert( std::floor2(val+1) == val);
+	}
+#endif
+
+}
Index: test/std/numerics/bit/bit.pow.two/floor2.fail.cpp
===================================================================
--- test/std/numerics/bit/bit.pow.two/floor2.fail.cpp
+++ test/std/numerics/bit/bit.pow.two/floor2.fail.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr T floor2(T x) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+class A{};
+enum       E1 : unsigned char { rEd };
+enum class E2 : unsigned char { red };
+
+int main()
+{
+	(void) std::floor2(static_cast<int>(2));         // expected-error {{no matching function for call to 'floor2'}}	
+	(void) std::floor2(static_cast<signed int>(2));  // expected-error {{no matching function for call to 'floor2'}}	
+	(void) std::floor2(static_cast<long>(2));        // expected-error {{no matching function for call to 'floor2'}}	
+	(void) std::floor2(static_cast<long long >(2));  // expected-error {{no matching function for call to 'floor2'}}	
+#ifndef _LIBCPP_HAS_NO_INT128
+	(void) std::floor2(static_cast<__int128_t>(2));  // expected-error {{no matching function for call to 'floor2'}}	
+#endif
+
+	(void) std::floor2(static_cast<int8_t>(2));      // expected-error {{no matching function for call to 'floor2'}}	
+	(void) std::floor2(static_cast<int16_t>(2));     // expected-error {{no matching function for call to 'floor2'}}	
+	(void) std::floor2(static_cast<int32_t>(2));     // expected-error {{no matching function for call to 'floor2'}}	
+	(void) std::floor2(static_cast<int64_t>(2));     // expected-error {{no matching function for call to 'floor2'}}	
+
+	(void) std::floor2(static_cast<ptrdiff_t>(2));   // expected-error {{no matching function for call to 'floor2'}}	
+
+	(void) std::floor2(true);                        // expected-error {{no matching function for call to 'floor2'}}	
+	(void) std::floor2(static_cast<signed char>(2)); // expected-error {{no matching function for call to 'floor2'}}	
+	(void) std::floor2(static_cast<wchar_t>(2));     // expected-error {{no matching function for call to 'floor2'}}	
+	(void) std::floor2(static_cast<char16_t>(2));    // expected-error {{no matching function for call to 'floor2'}}	
+	(void) std::floor2(static_cast<char32_t>(2));    // expected-error {{no matching function for call to 'floor2'}}	
+
+	(void) std::floor2(A{});                         // expected-error {{no matching function for call to 'floor2'}}	
+	(void) std::floor2(rEd);                         // expected-error {{no matching function for call to 'floor2'}}	
+	(void) std::floor2(E2::red);                     // expected-error {{no matching function for call to 'floor2'}}	
+}
Index: test/std/numerics/bit/bit.pow.two/ceil2.pass.cpp
===================================================================
--- test/std/numerics/bit/bit.pow.two/ceil2.pass.cpp
+++ test/std/numerics/bit/bit.pow.two/ceil2.pass.cpp
@@ -0,0 +1,114 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr T ceil2(T x) noexcept;
+
+// Returns: The minimal value y such that ispow2(y) is true and y >= x; 
+//    if y is not representable as a value of type T, the result is an unspecified value.
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	return std::ceil2(T(0)) == T(1)
+	   &&  std::ceil2(T(1)) == T(1)
+	   &&  std::ceil2(T(2)) == T(2)
+	   &&  std::ceil2(T(3)) == T(4)
+	   &&  std::ceil2(T(4)) == T(4)
+	   &&  std::ceil2(T(5)) == T(8)
+	   &&  std::ceil2(T(6)) == T(8)
+	   &&  std::ceil2(T(7)) == T(8)
+	   &&  std::ceil2(T(8)) == T(8)
+	   &&  std::ceil2(T(9)) == T(16)
+	   ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(T, decltype(std::ceil2(T(0))));
+	ASSERT_NOEXCEPT(             std::ceil2(T(0)));
+	
+	assert( std::ceil2(T(121)) == T(128));
+	assert( std::ceil2(T(122)) == T(128));
+	assert( std::ceil2(T(123)) == T(128));
+	assert( std::ceil2(T(124)) == T(128));
+	assert( std::ceil2(T(125)) == T(128));
+	assert( std::ceil2(T(126)) == T(128));
+	assert( std::ceil2(T(127)) == T(128));
+	assert( std::ceil2(T(128)) == T(128));
+	assert( std::ceil2(T(129)) == T(256));
+	assert( std::ceil2(T(130)) == T(256));
+}
+
+int main()
+{
+	
+	static_assert(constexpr_test<unsigned char>(),      "");
+	static_assert(constexpr_test<unsigned short>(),     "");
+	static_assert(constexpr_test<unsigned>(),           "");
+	static_assert(constexpr_test<unsigned long>(),      "");
+	static_assert(constexpr_test<unsigned long long>(), "");
+
+	static_assert(constexpr_test<uint8_t>(),   "");
+	static_assert(constexpr_test<uint16_t>(),  "");
+	static_assert(constexpr_test<uint32_t>(),  "");
+	static_assert(constexpr_test<uint64_t>(),  "");
+	static_assert(constexpr_test<size_t>(),    "");
+	static_assert(constexpr_test<uintmax_t>(), "");
+	static_assert(constexpr_test<uintptr_t>(), "");
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	static_assert(constexpr_test<__uint128_t>(),        "");
+#endif
+
+	runtime_test<unsigned char>();
+	runtime_test<unsigned>();
+	runtime_test<unsigned short>();
+	runtime_test<unsigned long>();
+	runtime_test<unsigned long long>();
+
+	runtime_test<uint8_t>();
+	runtime_test<uint16_t>();
+	runtime_test<uint32_t>();
+	runtime_test<uint64_t>();
+	runtime_test<size_t>();
+	runtime_test<uintmax_t>();
+	runtime_test<uintptr_t>();
+
+#ifndef _LIBCPP_HAS_NO_INT128
+	runtime_test<__uint128_t>();
+
+	{
+	__uint128_t val = 128;
+	val <<= 32;
+	assert( std::ceil2(val-1) == val);
+	assert( std::ceil2(val)   == val);
+	assert( std::ceil2(val+1) == val*2);
+	val <<= 2;
+	assert( std::ceil2(val-1) == val);
+	assert( std::ceil2(val)   == val);
+	assert( std::ceil2(val+1) == val*2);
+	val <<= 3;
+	assert( std::ceil2(val-1) == val);
+	assert( std::ceil2(val)   == val);
+	assert( std::ceil2(val+1) == val*2);
+	}
+#endif
+
+}
Index: test/std/numerics/bit/bit.pow.two/ceil2.fail.cpp
===================================================================
--- test/std/numerics/bit/bit.pow.two/ceil2.fail.cpp
+++ test/std/numerics/bit/bit.pow.two/ceil2.fail.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr T ceil2(T x) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+class A{};
+enum       E1 : unsigned char { rEd };
+enum class E2 : unsigned char { red };
+
+int main()
+{
+	(void) std::ceil2(static_cast<int>(2));         // expected-error {{no matching function for call to 'ceil2'}}	
+	(void) std::ceil2(static_cast<signed int>(2));  // expected-error {{no matching function for call to 'ceil2'}}	
+	(void) std::ceil2(static_cast<long>(2));        // expected-error {{no matching function for call to 'ceil2'}}	
+	(void) std::ceil2(static_cast<long long >(2));  // expected-error {{no matching function for call to 'ceil2'}}	
+#ifndef _LIBCPP_HAS_NO_INT128
+	(void) std::ceil2(static_cast<__int128_t>(2));  // expected-error {{no matching function for call to 'ceil2'}}	
+#endif
+
+	(void) std::ceil2(static_cast<int8_t>(2));      // expected-error {{no matching function for call to 'ceil2'}}	
+	(void) std::ceil2(static_cast<int16_t>(2));     // expected-error {{no matching function for call to 'ceil2'}}	
+	(void) std::ceil2(static_cast<int32_t>(2));     // expected-error {{no matching function for call to 'ceil2'}}	
+	(void) std::ceil2(static_cast<int64_t>(2));     // expected-error {{no matching function for call to 'ceil2'}}	
+
+	(void) std::ceil2(static_cast<ptrdiff_t>(2));   // expected-error {{no matching function for call to 'ceil2'}}	
+
+	(void) std::ceil2(true);                        // expected-error {{no matching function for call to 'ceil2'}}	
+	(void) std::ceil2(static_cast<signed char>(2)); // expected-error {{no matching function for call to 'ceil2'}}	
+	(void) std::ceil2(static_cast<wchar_t>(2));     // expected-error {{no matching function for call to 'ceil2'}}	
+	(void) std::ceil2(static_cast<char16_t>(2));    // expected-error {{no matching function for call to 'ceil2'}}	
+	(void) std::ceil2(static_cast<char32_t>(2));    // expected-error {{no matching function for call to 'ceil2'}}	
+
+	(void) std::ceil2(A{});                         // expected-error {{no matching function for call to 'ceil2'}}	
+	(void) std::ceil2(rEd);                         // expected-error {{no matching function for call to 'ceil2'}}	
+	(void) std::ceil2(E2::red);                     // expected-error {{no matching function for call to 'ceil2'}}	
+}
Index: test/libcxx/numerics/bit/bitops.rot/rotr.pass.cpp
===================================================================
--- test/libcxx/numerics/bit/bitops.rot/rotr.pass.cpp
+++ test/libcxx/numerics/bit/bitops.rot/rotr.pass.cpp
@@ -0,0 +1,82 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int rotr(T x, unsigned int s) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	const T max = T(255);
+
+	return std::rotr(T(128), 0) == T(128)
+	   &&  std::rotr(T(128), 1) == T( 64)
+	   &&  std::rotr(T(128), 2) == T( 32)
+	   &&  std::rotr(T(128), 3) == T( 16)
+	   &&  std::rotr(T(128), 4) == T(  8)
+	   &&  std::rotr(T(128), 5) == T(  4)
+	   &&  std::rotr(T(128), 6) == T(  2)
+	   &&  std::rotr(T(128), 7) == T(  1)
+	   &&  std::rotr(max, 0)  == max
+	   &&  std::rotr(max, 1)  == max
+	   &&  std::rotr(max, 2)  == max
+	   &&  std::rotr(max, 3)  == max
+	   &&  std::rotr(max, 4)  == max
+	   &&  std::rotr(max, 5)  == max
+	   &&  std::rotr(max, 6)  == max
+	   &&  std::rotr(max, 7)  == max
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(T, decltype(std::rotr(T(0), 0)));
+	ASSERT_NOEXCEPT(             std::rotr(T(0), 0));
+
+	const unsigned char max = 255;
+	const unsigned char val = 254;
+
+	const unsigned char uppers [] = {
+		max,	          // not used
+		max - max,	      // 000 .. 0
+		max - (max >> 1), // 800 .. 0
+		max - (max >> 2), // C00 .. 0
+		max - (max >> 3), // E00 .. 0
+		max - (max >> 4), // F00 .. 0
+		max - (max >> 5), // F80 .. 0
+		max - (max >> 6), // FC0 .. 0
+		max - (max >> 7), // FE0 .. 0
+		};
+	
+	assert( std::rotr(T(val), 0) == T(val));
+	assert( std::rotr(T(val), 1) == T((val >> 1) +  uppers[1]));
+	assert( std::rotr(T(val), 2) == T((val >> 2) +  uppers[2]));
+	assert( std::rotr(T(val), 3) == T((val >> 3) +  uppers[3]));
+	assert( std::rotr(T(val), 4) == T((val >> 4) +  uppers[4]));
+	assert( std::rotr(T(val), 5) == T((val >> 5) +  uppers[5]));
+	assert( std::rotr(T(val), 6) == T((val >> 6) +  uppers[6]));
+	assert( std::rotr(T(val), 7) == T((val >> 7) +  uppers[7]));
+}
+
+int main()
+{
+	static_assert(constexpr_test<std::byte>(), "");
+	runtime_test<std::byte>();
+}
Index: test/libcxx/numerics/bit/bitops.rot/rotl.pass.cpp
===================================================================
--- test/libcxx/numerics/bit/bitops.rot/rotl.pass.cpp
+++ test/libcxx/numerics/bit/bitops.rot/rotl.pass.cpp
@@ -0,0 +1,70 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int rotl(T x, unsigned int s) noexcept;
+
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+// As an extension, libc++ supports this for std::byte
+
+#include <bit>
+#include <cassert>
+#include <iostream>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	const T max = T(255);
+	return std::rotl(T(1), 0) == T( 1)
+	   &&  std::rotl(T(1), 1) == T( 2)
+	   &&  std::rotl(T(1), 2) == T( 4)
+	   &&  std::rotl(T(1), 3) == T( 8)
+	   &&  std::rotl(T(1), 4) == T( 16)
+	   &&  std::rotl(T(1), 5) == T( 32)
+	   &&  std::rotl(T(1), 6) == T( 64)
+	   &&  std::rotl(T(1), 7) == T(128)
+	   &&  std::rotl(max, 0)  == max
+	   &&  std::rotl(max, 1)  == max
+	   &&  std::rotl(max, 2)  == max
+	   &&  std::rotl(max, 3)  == max
+	   &&  std::rotl(max, 4)  == max
+	   &&  std::rotl(max, 5)  == max
+	   &&  std::rotl(max, 6)  == max
+	   &&  std::rotl(max, 7)  == max
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(T, decltype(std::rotl(T(0), 0)));
+	ASSERT_NOEXCEPT(             std::rotl(T(0), 0));
+	const unsigned char val = 254;
+	
+	assert( std::rotl(T(val), 0) == T(val));
+	assert( std::rotl(T(val), 1) == T((val << 1) +   1));
+	assert( std::rotl(T(val), 2) == T((val << 2) +   3));
+	assert( std::rotl(T(val), 3) == T((val << 3) +   7));
+	assert( std::rotl(T(val), 4) == T((val << 4) +  15));
+	assert( std::rotl(T(val), 5) == T((val << 5) +  31));
+	assert( std::rotl(T(val), 6) == T((val << 6) +  63));
+	assert( std::rotl(T(val), 7) == T((val << 7) + 127));
+}
+
+int main()
+{
+	static_assert(constexpr_test<std::byte>(), "");
+	runtime_test<std::byte>();
+}
Index: test/libcxx/numerics/bit/bitops.count/popcount.pass.cpp
===================================================================
--- test/libcxx/numerics/bit/bitops.count/popcount.pass.cpp
+++ test/libcxx/numerics/bit/bitops.count/popcount.pass.cpp
@@ -0,0 +1,64 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int popcount(T x) noexcept;
+
+// Returns: The number of bits set to one in the value of x.
+//
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	return std::popcount(T(0)) == 0
+	   &&  std::popcount(T(1)) == 1
+	   &&  std::popcount(T(2)) == 1
+	   &&  std::popcount(T(3)) == 2
+	   &&  std::popcount(T(4)) == 1
+	   &&  std::popcount(T(5)) == 2
+	   &&  std::popcount(T(6)) == 2
+	   &&  std::popcount(T(7)) == 3
+	   &&  std::popcount(T(8)) == 1
+	   &&  std::popcount(T(9)) == 2
+	   &&  std::popcount(T(255)) == 8
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(int, decltype(std::popcount(T(0))));
+	ASSERT_NOEXCEPT(               std::popcount(T(0)));
+	
+	assert( std::popcount(T(121)) == 5);
+	assert( std::popcount(T(122)) == 5);
+	assert( std::popcount(T(123)) == 6);
+	assert( std::popcount(T(124)) == 5);
+	assert( std::popcount(T(125)) == 6);
+	assert( std::popcount(T(126)) == 6);
+	assert( std::popcount(T(127)) == 7);
+	assert( std::popcount(T(128)) == 1);
+	assert( std::popcount(T(129)) == 2);
+	assert( std::popcount(T(130)) == 2);
+}
+
+int main()
+{
+	static_assert(constexpr_test<std::byte>(), "");
+	runtime_test<std::byte>();
+}
Index: test/libcxx/numerics/bit/bitops.count/countr_zero.pass.cpp
===================================================================
--- test/libcxx/numerics/bit/bitops.count/countr_zero.pass.cpp
+++ test/libcxx/numerics/bit/bitops.count/countr_zero.pass.cpp
@@ -0,0 +1,66 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int countr_zero(T x) noexcept;
+
+// Returns: The number of consecutive 0 bits, starting from the most significant bit.
+//   [ Note: Returns N if x == 0. ]
+//
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	const int dig = 8;
+	return std::countr_zero(T(0)) == dig
+	   &&  std::countr_zero(T(1)) == 0
+	   &&  std::countr_zero(T(2)) == 1
+	   &&  std::countr_zero(T(3)) == 0
+	   &&  std::countr_zero(T(4)) == 2
+	   &&  std::countr_zero(T(5)) == 0
+	   &&  std::countr_zero(T(6)) == 1
+	   &&  std::countr_zero(T(7)) == 0
+	   &&  std::countr_zero(T(8)) == 3
+	   &&  std::countr_zero(T(9)) == 0
+	   &&  std::countr_zero(T(255)) == 0
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(int, decltype(std::countr_zero(T(0))));
+	ASSERT_NOEXCEPT(               std::countr_zero(T(0)));
+	
+	assert( std::countr_zero(T(121)) == 0);
+	assert( std::countr_zero(T(122)) == 1);
+	assert( std::countr_zero(T(123)) == 0);
+	assert( std::countr_zero(T(124)) == 2);
+	assert( std::countr_zero(T(125)) == 0);
+	assert( std::countr_zero(T(126)) == 1);
+	assert( std::countr_zero(T(127)) == 0);
+	assert( std::countr_zero(T(128)) == 7);
+	assert( std::countr_zero(T(129)) == 0);
+	assert( std::countr_zero(T(130)) == 1);
+}
+
+int main()
+{
+	static_assert(constexpr_test<std::byte>(),          "");
+	runtime_test<std::byte>();
+}
Index: test/libcxx/numerics/bit/bitops.count/countr_one.pass.cpp
===================================================================
--- test/libcxx/numerics/bit/bitops.count/countr_one.pass.cpp
+++ test/libcxx/numerics/bit/bitops.count/countr_one.pass.cpp
@@ -0,0 +1,66 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int countr_one(T x) noexcept;
+
+// Returns: The number of consecutive 1 bits, starting from the least significant bit.
+//   [ Note: Returns N if x == std::numeric_limits<T>::max(). ]
+//
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	const int dig = 8;
+	return std::countr_one(T(0)) == 0
+	   &&  std::countr_one(T(1)) == 1
+	   &&  std::countr_one(T(2)) == 0
+	   &&  std::countr_one(T(3)) == 2
+	   &&  std::countr_one(T(4)) == 0
+	   &&  std::countr_one(T(5)) == 1
+	   &&  std::countr_one(T(6)) == 0
+	   &&  std::countr_one(T(7)) == 3
+	   &&  std::countr_one(T(8)) == 0
+	   &&  std::countr_one(T(9)) == 1
+	   &&  std::countr_one(T(255)) == dig
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(int, decltype(std::countr_one(T(0))));
+	ASSERT_NOEXCEPT(               std::countr_one(T(0)));
+	
+	assert( std::countr_one(T(121)) == 1);
+	assert( std::countr_one(T(122)) == 0);
+	assert( std::countr_one(T(123)) == 2);
+	assert( std::countr_one(T(124)) == 0);
+	assert( std::countr_one(T(125)) == 1);
+	assert( std::countr_one(T(126)) == 0);
+	assert( std::countr_one(T(127)) == 7);
+	assert( std::countr_one(T(128)) == 0);
+	assert( std::countr_one(T(129)) == 1);
+	assert( std::countr_one(T(130)) == 0);
+}
+
+int main()
+{
+	static_assert(constexpr_test<std::byte>(), "");
+	runtime_test<std::byte>();
+}
Index: test/libcxx/numerics/bit/bitops.count/countl_zero.pass.cpp
===================================================================
--- test/libcxx/numerics/bit/bitops.count/countl_zero.pass.cpp
+++ test/libcxx/numerics/bit/bitops.count/countl_zero.pass.cpp
@@ -0,0 +1,67 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int countl_zero(T x) noexcept;
+
+// Returns: The number of consecutive 0 bits, starting from the most significant bit.
+//   [ Note: Returns N if x == 0. ]
+//
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	const int dig = 8;
+	return std::countl_zero(T(0)) == dig
+	   &&  std::countl_zero(T(1)) == dig - 1
+	   &&  std::countl_zero(T(2)) == dig - 2
+	   &&  std::countl_zero(T(3)) == dig - 2
+	   &&  std::countl_zero(T(4)) == dig - 3
+	   &&  std::countl_zero(T(5)) == dig - 3
+	   &&  std::countl_zero(T(6)) == dig - 3
+	   &&  std::countl_zero(T(7)) == dig - 3
+	   &&  std::countl_zero(T(8)) == dig - 4
+	   &&  std::countl_zero(T(9)) == dig - 4
+	   &&  std::countl_zero(T(255)) == 0
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(int, decltype(std::countl_zero(T(0))));
+	ASSERT_NOEXCEPT(               std::countl_zero(T(0)));
+	const int dig = 8;
+	
+	assert( std::countl_zero(T(121)) == dig - 7);
+	assert( std::countl_zero(T(122)) == dig - 7);
+	assert( std::countl_zero(T(123)) == dig - 7);
+	assert( std::countl_zero(T(124)) == dig - 7);
+	assert( std::countl_zero(T(125)) == dig - 7);
+	assert( std::countl_zero(T(126)) == dig - 7);
+	assert( std::countl_zero(T(127)) == dig - 7);
+	assert( std::countl_zero(T(128)) == dig - 8);
+	assert( std::countl_zero(T(129)) == dig - 8);
+	assert( std::countl_zero(T(130)) == dig - 8);
+}
+
+int main()
+{
+	static_assert(constexpr_test<std::byte>(),          "");
+	runtime_test<std::byte>();
+}
Index: test/libcxx/numerics/bit/bitops.count/countl_one.pass.cpp
===================================================================
--- test/libcxx/numerics/bit/bitops.count/countl_one.pass.cpp
+++ test/libcxx/numerics/bit/bitops.count/countl_one.pass.cpp
@@ -0,0 +1,74 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 
+
+// template <class T>
+//   constexpr int countl_one(T x) noexcept;
+
+// The number of consecutive 1 bits, starting from the most significant bit.
+//   [ Note: Returns N if x == std::numeric_limits<T>::max(). ]
+//
+// Remarks: This function shall not participate in overload resolution unless 
+//	T is an unsigned integer type
+
+#include <bit>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+constexpr bool constexpr_test()
+{
+	const int dig = 8;
+	const unsigned char max = 255;
+	return std::countl_one(T(max)    ) == dig
+	   &&  std::countl_one(T(max - 1)) == dig - 1
+	   &&  std::countl_one(T(max - 2)) == dig - 2
+	   &&  std::countl_one(T(max - 3)) == dig - 2
+	   &&  std::countl_one(T(max - 4)) == dig - 3
+	   &&  std::countl_one(T(max - 5)) == dig - 3
+	   &&  std::countl_one(T(max - 6)) == dig - 3
+	   &&  std::countl_one(T(max - 7)) == dig - 3
+	   &&  std::countl_one(T(max - 8)) == dig - 4
+	   &&  std::countl_one(T(max - 9)) == dig - 4
+	  ;
+}
+
+
+template <typename T>
+void runtime_test()
+{
+	ASSERT_SAME_TYPE(int, decltype(std::countl_one(T(0))));
+	ASSERT_NOEXCEPT(               std::countl_one(T(0)));
+	
+	assert( std::countl_one(T(121)) == 0);
+	assert( std::countl_one(T(122)) == 0);
+	assert( std::countl_one(T(123)) == 0);
+	assert( std::countl_one(T(124)) == 0);
+	assert( std::countl_one(T(125)) == 0);
+	assert( std::countl_one(T(126)) == 0);
+	assert( std::countl_one(T(127)) == 0);
+	assert( std::countl_one(T(128)) == 1);
+	assert( std::countl_one(T(129)) == 1);
+	assert( std::countl_one(T(130)) == 1);
+
+	assert( std::countl_one(T(192)) == 2);
+	assert( std::countl_one(T(224)) == 3);
+	assert( std::countl_one(T(240)) == 4);
+	assert( std::countl_one(T(248)) == 5);
+	assert( std::countl_one(T(252)) == 6);
+	assert( std::countl_one(T(254)) == 7);
+	assert( std::countl_one(T(255)) == 8);
+}
+
+int main()
+{
+	static_assert(constexpr_test<std::byte>(), "");
+	runtime_test<std::byte>();
+}
Index: include/bit
===================================================================
--- include/bit
+++ include/bit
@@ -16,11 +16,40 @@
 
 namespace std {
 
+  template <class T>
+    constexpr bool ispow2(T x) noexcept;
+  template <class T>
+    constexpr T ceil2(T x) noexcept;
+  template <class T>
+    constexpr T floor2(T x) noexcept;
+  template <class T>
+    constexpr T log2p1(T x) noexcept;
+
+  // 23.20.2, rotating   
+  template<class T>
+    constexpr T rotl(T x, unsigned int s) noexcept;
+  template<class T>
+    constexpr T rotr(T x, unsigned int s) noexcept;
+
+  // 23.20.3, counting
+  template<class T>
+    constexpr int countl_zero(T x) noexcept;
+  template<class T>
+    constexpr int countl_one(T x) noexcept;
+  template<class T>
+    constexpr int countr_zero(T x) noexcept;
+  template<class T>
+    constexpr int countr_one(T x) noexcept;
+  template<class T>
+    constexpr int popcount(T x) noexcept;
+
 } // namespace std
 
 */
 
 #include <__config>
+#include <cstddef>
+#include <limits>
 
 #if defined(__IBMCPP__)
 #include "support/ibm/support.h"
@@ -33,38 +62,41 @@
 #pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 #ifndef _LIBCPP_COMPILER_MSVC
 
-inline _LIBCPP_INLINE_VISIBILITY
-int __ctz(unsigned __x)           { return __builtin_ctz(__x); }
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
+int __ctz(unsigned __x)           _NOEXCEPT { return __builtin_ctz(__x); }
 
-inline _LIBCPP_INLINE_VISIBILITY
-int __ctz(unsigned long __x)      { return __builtin_ctzl(__x); }
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
+int __ctz(unsigned long __x)      _NOEXCEPT { return __builtin_ctzl(__x); }
 
-inline _LIBCPP_INLINE_VISIBILITY
-int __ctz(unsigned long long __x) { return __builtin_ctzll(__x); }
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
+int __ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); }
 
 
-inline _LIBCPP_INLINE_VISIBILITY
-int __clz(unsigned __x)           { return __builtin_clz(__x); }
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
+int __clz(unsigned __x)           _NOEXCEPT { return __builtin_clz(__x); }
 
-inline _LIBCPP_INLINE_VISIBILITY
-int __clz(unsigned long __x)      { return __builtin_clzl(__x); }
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
+int __clz(unsigned long __x)      _NOEXCEPT { return __builtin_clzl(__x); }
 
-inline _LIBCPP_INLINE_VISIBILITY
-int __clz(unsigned long long __x) { return __builtin_clzll(__x); }
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
+int __clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); }
 
 
-inline _LIBCPP_INLINE_VISIBILITY
-int __popcount(unsigned __x)           { return __builtin_popcount(__x); }
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
+int __popcount(unsigned __x)           _NOEXCEPT { return __builtin_popcount(__x); }
 
-inline _LIBCPP_INLINE_VISIBILITY
-int __popcount(unsigned long __x)      { return __builtin_popcountl(__x); }
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
+int __popcount(unsigned long __x)      _NOEXCEPT { return __builtin_popcountl(__x); }
 
-inline _LIBCPP_INLINE_VISIBILITY
-int __popcount(unsigned long long __x) { return __builtin_popcountll(__x); }
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
+int __popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); }
 
 #else  // _LIBCPP_COMPILER_MSVC
 
@@ -152,6 +184,246 @@
 
 #endif // _LIBCPP_COMPILER_MSVC
 
+#if _LIBCPP_STD_VER > 17
+
+template <class _Tp>
+struct __bitop_unsigned_integer
+    : public integral_constant<bool,
+         is_integral_v<_Tp> &&
+         is_unsigned_v<_Tp> &&
+        !is_same_v<remove_cv_t<_Tp>, bool> &&
+        !is_same_v<remove_cv_t<_Tp>, char> &&
+        !is_same_v<remove_cv_t<_Tp>, wchar_t> &&
+        !is_same_v<remove_cv_t<_Tp>, char16_t> &&
+        !is_same_v<remove_cv_t<_Tp>, char32_t>
+     > {};
+
+
+// rotl
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
+rotl(_Tp __t, unsigned int __cnt) noexcept
+{
+    const unsigned int __dig = numeric_limits<_Tp>::digits;
+    return (__cnt % __dig) == 0
+        ? __t
+        : (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
+}
+
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+byte rotl(byte __b, unsigned int __cnt) noexcept
+{ return byte{rotl(static_cast<unsigned char>(__b), __cnt)}; }
+
+
+// rotr
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
+rotr(_Tp __t, unsigned int __cnt) noexcept
+{
+    const unsigned int __dig = numeric_limits<_Tp>::digits;
+    return (__cnt % __dig) == 0
+        ? __t
+        : (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
+}
+
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+byte rotr(byte __b, unsigned int __cnt) noexcept
+{ return byte{rotr(static_cast<unsigned char>(__b), __cnt)}; }
+
+
+// countl_zero
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+int countl_zero(byte __b) noexcept
+{
+    return __b != byte{} 
+        ? __clz(static_cast<unsigned int>(__b))
+            - (numeric_limits<unsigned int>::digits - numeric_limits<unsigned char>::digits)
+        : numeric_limits<unsigned char>::digits;
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
+countl_zero(_Tp __t) noexcept
+{
+    if (__t == 0)
+        return numeric_limits<_Tp>::digits;
+
+    if      constexpr (sizeof(_Tp) <= sizeof(unsigned int))
+    	return __clz(static_cast<unsigned int>(__t))
+              - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
+    else if constexpr (sizeof(_Tp) <= sizeof(unsigned long))
+    	return __clz(static_cast<unsigned long>(__t))
+              - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
+    else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long))
+    	return __clz(static_cast<unsigned long long>(__t))
+              - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
+	else
+	{
+		int __ret = 0;
+		int __iter = 0;
+		const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
+		while (true) {
+			__t = rotr<_Tp>(__t, __ulldigits);
+			if ((__iter = countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
+				break;
+			__ret += __iter;
+			}
+		return __ret + __iter;
+	}
+}
+
+
+// countl_one
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
+countl_one(_Tp __t) noexcept
+{
+    return __t != numeric_limits<_Tp>::max()
+        ? countl_zero(static_cast<_Tp>(~__t))
+        : numeric_limits<_Tp>::digits;
+}
+
+
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+int countl_one(byte __b) noexcept
+{
+//  If I could assume that sizeof(byte) < sizeof(unsigned int), I could simplify this a lot
+    return __b != byte{255}
+        ? countl_zero(static_cast<byte>(~__b))
+        : numeric_limits<unsigned char>::digits;
+}
+
+// countr_zero
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+int countr_zero(byte __b) noexcept 
+{   
+    return __b != byte{} 
+        ? __ctz(static_cast<unsigned int>(__b))
+        : numeric_limits<unsigned char>::digits;
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
+countr_zero(_Tp __t) noexcept
+{
+    if (__t == 0)
+        return numeric_limits<_Tp>::digits;
+
+    if      constexpr (sizeof(_Tp) <= sizeof(unsigned int))
+    	return __ctz(static_cast<unsigned int>(__t));
+    else if constexpr (sizeof(_Tp) <= sizeof(unsigned long))
+    	return __ctz(static_cast<unsigned long>(__t));
+    else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long))
+    	return __ctz(static_cast<unsigned long long>(__t));
+	else
+	{
+		int __ret = 0;
+		int __iter = 0;
+		const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
+		while ((__iter = countr_zero(static_cast<unsigned long long>(__t))) == __ulldigits)
+		{
+			__ret += __iter;
+			__t >>= __ulldigits;
+		}
+		return __ret + __iter;
+	}
+}
+
+
+// countr_one
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
+countr_one(_Tp __t) noexcept
+{
+    return __t != numeric_limits<_Tp>::max()
+        ? countr_zero(static_cast<_Tp>(~__t))
+        : numeric_limits<_Tp>::digits;
+}
+
+
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+int countr_one(byte __b) noexcept
+{
+//  If I could assume that sizeof(byte) < sizeof(unsigned int), I could simplify this a lot
+    return __b != byte{255}
+        ? countr_zero(static_cast<byte>(~__b))
+        : numeric_limits<unsigned char>::digits;
+}
+
+// popcount
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+int popcount(byte __b) noexcept { return __popcount(static_cast<unsigned int>(__b)); }
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
+popcount(_Tp __t) noexcept
+{
+    if      constexpr (sizeof(_Tp) <= sizeof(unsigned int))
+    	return __popcount(static_cast<unsigned int>(__t));
+    else if constexpr (sizeof(_Tp) <= sizeof(unsigned long))
+    	return __popcount(static_cast<unsigned long>(__t));
+    else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long))
+    	return __popcount(static_cast<unsigned long long>(__t));
+    else
+    {
+		int __ret = 0;
+		while (__t != 0) // not a fixed loop here b/c we can exit early
+		{
+			__ret += __popcount(static_cast<unsigned long long>(__t));
+			__t >>= numeric_limits<unsigned long long>::digits;
+		}
+		return __ret;
+    }
+}
+
+
+// integral log base 2
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
+__bit_log2(_Tp __t) noexcept
+{ return __t == 0 ? 0 : std::numeric_limits<_Tp>::digits - 1 - countl_zero(__t); }
+
+
+template <class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool>
+ispow2(_Tp __t) noexcept { return popcount(__t) == 1; }
+
+template <class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
+floor2(_Tp __t) noexcept { return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t); }
+
+template <class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
+ceil2(_Tp __t) noexcept
+{
+    if (__t == 0) return 1;
+    _Tp __ret = floor2(__t);
+    return __ret == __t ? __t : __ret << 1;
+}
+
+template <class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY constexpr
+enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
+log2p1(_Tp __t) noexcept
+{ return __t == 0 ? 0 : __bit_log2(__t) + 1; }
+
+
+
+#endif // _LIBCPP_STD_VER > 17
+
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP_BIT
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D51262: Implement P0... Marshall Clow via Phabricator via cfe-commits

Reply via email to