P0556R3 Integral power-of-2 operations, P0553R2 Bit operations

Message ID 20180703210247.GA7287@redhat.com
State New
Headers show
Series
  • P0556R3 Integral power-of-2 operations, P0553R2 Bit operations
Related show

Commit Message

Jonathan Wakely July 3, 2018, 9:02 p.m.
P0553R2 is not in the C++2a working draft yet, but is likely to be
approved soon. Neither proposal supports std::byte but this adds
overloads of each function for std::byte, assuming that will also get
added.

	* include/Makefile.am: Add new header.
	* include/Makefile.in: Regenerate.
	* include/precompiled/stdc++.h: Include new header.
	* include/std/bit: New header.
	(__rotl, __rotr, __countl_zero, __countl_one, __countr_zero)
	(__countr_one, __popcount, __ispow2, __ceil2, __floor2, __log2p1):
	Define for C++14.
	[!__STRICT_ANSI__] (rotl, rotr, countl_zero, countl_one, countr_zero)
	(countr_one, popcount): Define for C++2a. Also overload for std::byte.
	(ispow2, ceil2, floor2, log2p1): Define for C++2a.
	[!__STRICT_ANSI__] (ispow2, ceil2, floor2, log2p1): Overload for
	std::byte.
	* testsuite/26_numerics/bit/bit.pow.two/ceil2.cc: New.
	* testsuite/26_numerics/bit/bit.pow.two/floor2.cc: New.
	* testsuite/26_numerics/bit/bit.pow.two/ispow2.cc: New.
	* testsuite/26_numerics/bit/bit.pow.two/log2p1.cc: New.
	* testsuite/26_numerics/bit/bitops.rot/rotl.cc: New.
	* testsuite/26_numerics/bit/bitops.rot/rotr.cc: New.
	* testsuite/26_numerics/bit/bitops.count/countl_one.cc: New.
	* testsuite/26_numerics/bit/bitops.count/countl_zero.cc: New.
	* testsuite/26_numerics/bit/bitops.count/countr_one.cc: New.
	* testsuite/26_numerics/bit/bitops.count/countr_zero.cc: New.

Tested powerpc64le-linux, committed to trunk.
commit 8e5c24b33c987bc72d429755ed90958f7931711b
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jul 3 21:33:20 2018 +0100

    P0556R3 Integral power-of-2 operations, P0553R2 Bit operations
    
    P0553R2 is not in the C++2a working draft yet, but is likely to be
    approved soon. Neither proposal supports std::byte but this adds
    overloads of each function for std::byte, assuming that will also get
    added.
    
            * include/Makefile.am: Add new header.
            * include/Makefile.in: Regenerate.
            * include/precompiled/stdc++.h: Include new header.
            * include/std/bit: New header.
            (__rotl, __rotr, __countl_zero, __countl_one, __countr_zero)
            (__countr_one, __popcount, __ispow2, __ceil2, __floor2, __log2p1):
            Define for C++14.
            [!__STRICT_ANSI__] (rotl, rotr, countl_zero, countl_one, countr_zero)
            (countr_one, popcount): Define for C++2a. Also overload for std::byte.
            (ispow2, ceil2, floor2, log2p1): Define for C++2a.
            [!__STRICT_ANSI__] (ispow2, ceil2, floor2, log2p1): Overload for
            std::byte.
            * testsuite/26_numerics/bit/bit.pow.two/ceil2.cc: New.
            * testsuite/26_numerics/bit/bit.pow.two/floor2.cc: New.
            * testsuite/26_numerics/bit/bit.pow.two/ispow2.cc: New.
            * testsuite/26_numerics/bit/bit.pow.two/log2p1.cc: New.
            * testsuite/26_numerics/bit/bitops.rot/rotl.cc: New.
            * testsuite/26_numerics/bit/bitops.rot/rotr.cc: New.
            * testsuite/26_numerics/bit/bitops.count/countl_one.cc: New.
            * testsuite/26_numerics/bit/bitops.count/countl_zero.cc: New.
            * testsuite/26_numerics/bit/bitops.count/countr_one.cc: New.
            * testsuite/26_numerics/bit/bitops.count/countr_zero.cc: New.

Comments

Jakub Jelinek July 3, 2018, 9:40 p.m. | #1
On Tue, Jul 03, 2018 at 10:02:47PM +0100, Jonathan Wakely wrote:
> +#ifndef _GLIBCXX_BIT

> +#define _GLIBCXX_BIT 1

> +

> +#pragma GCC system_header

> +

> +#if __cplusplus >= 201402L

> +

> +#include <type_traits>

> +#include <limits>

> +

> +namespace std _GLIBCXX_VISIBILITY(default)

> +{

> +_GLIBCXX_BEGIN_NAMESPACE_VERSION

> +

> +  template<typename _Tp>

> +    constexpr _Tp

> +    __rotl(_Tp __x, unsigned int __s) noexcept

> +    {

> +      constexpr auto _Nd = numeric_limits<_Tp>::digits;

> +      const unsigned __sN = __s % _Nd;

> +      if (__sN)

> +        return (__x << __sN) | (__x >> (_Nd - __sN));


Wouldn't it be better to use some branchless pattern that
GCC can also optimize well, like:
      return (__x << __sN) | (__x >> ((-_sN) & (_Nd - 1)));
(iff _Nd is always power of two), or perhaps
      return (__x << __sN) | (__x >> ((-_sN) % _Nd));
which is going to be folded into the above one for power of two constants?
E.g. ia32intrin.h also uses:
/* 64bit rol */
extern __inline unsigned long long
__attribute__((__gnu_inline__, __always_inline__, __artificial__))
__rolq (unsigned long long __X, int __C)
{
  __C &= 63;
  return (__X << __C) | (__X >> (-__C & 63));
}
etc.

	Jakub
Jonathan Wakely July 3, 2018, 10:24 p.m. | #2
On 03/07/18 23:40 +0200, Jakub Jelinek wrote:
>On Tue, Jul 03, 2018 at 10:02:47PM +0100, Jonathan Wakely wrote:

>> +#ifndef _GLIBCXX_BIT

>> +#define _GLIBCXX_BIT 1

>> +

>> +#pragma GCC system_header

>> +

>> +#if __cplusplus >= 201402L

>> +

>> +#include <type_traits>

>> +#include <limits>

>> +

>> +namespace std _GLIBCXX_VISIBILITY(default)

>> +{

>> +_GLIBCXX_BEGIN_NAMESPACE_VERSION

>> +

>> +  template<typename _Tp>

>> +    constexpr _Tp

>> +    __rotl(_Tp __x, unsigned int __s) noexcept

>> +    {

>> +      constexpr auto _Nd = numeric_limits<_Tp>::digits;

>> +      const unsigned __sN = __s % _Nd;

>> +      if (__sN)

>> +        return (__x << __sN) | (__x >> (_Nd - __sN));

>

>Wouldn't it be better to use some branchless pattern that

>GCC can also optimize well, like:

>      return (__x << __sN) | (__x >> ((-_sN) & (_Nd - 1)));

>(iff _Nd is always power of two),


_Nd is 20 for one of the INT_N types on msp340, but we could have a
special case for the rare integer types with unusual sizes.

>or perhaps

>      return (__x << __sN) | (__x >> ((-_sN) % _Nd));

>which is going to be folded into the above one for power of two constants?


That looks good.

>E.g. ia32intrin.h also uses:

>/* 64bit rol */

>extern __inline unsigned long long

>__attribute__((__gnu_inline__, __always_inline__, __artificial__))

>__rolq (unsigned long long __X, int __C)

>{

>  __C &= 63;

>  return (__X << __C) | (__X >> (-__C & 63));

>}

>etc.


Should we delegate to those intrinsics for x86, so that
__builtin_ia32_rolqi and __builtin_ia32_rolhi can be used when
relevant?
Jonathan Wakely July 4, 2018, 8:08 a.m. | #3
On 03/07/18 23:23 +0100, Jonathan Wakely wrote:
>On 03/07/18 23:40 +0200, Jakub Jelinek wrote:

>>On Tue, Jul 03, 2018 at 10:02:47PM +0100, Jonathan Wakely wrote:

>>>+#ifndef _GLIBCXX_BIT

>>>+#define _GLIBCXX_BIT 1

>>>+

>>>+#pragma GCC system_header

>>>+

>>>+#if __cplusplus >= 201402L

>>>+

>>>+#include <type_traits>

>>>+#include <limits>

>>>+

>>>+namespace std _GLIBCXX_VISIBILITY(default)

>>>+{

>>>+_GLIBCXX_BEGIN_NAMESPACE_VERSION

>>>+

>>>+  template<typename _Tp>

>>>+    constexpr _Tp

>>>+    __rotl(_Tp __x, unsigned int __s) noexcept

>>>+    {

>>>+      constexpr auto _Nd = numeric_limits<_Tp>::digits;

>>>+      const unsigned __sN = __s % _Nd;

>>>+      if (__sN)

>>>+        return (__x << __sN) | (__x >> (_Nd - __sN));

>>

>>Wouldn't it be better to use some branchless pattern that

>>GCC can also optimize well, like:

>>     return (__x << __sN) | (__x >> ((-_sN) & (_Nd - 1)));

>>(iff _Nd is always power of two),

>

>_Nd is 20 for one of the INT_N types on msp340, but we could have a

>special case for the rare integer types with unusual sizes.

>

>>or perhaps

>>     return (__x << __sN) | (__x >> ((-_sN) % _Nd));

>>which is going to be folded into the above one for power of two constants?

>

>That looks good.


Committed as attached (with a test I forgot to add for popcount).
commit 796e90be02c691bc57c89172940aa047caa199ab
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Jul 4 00:07:43 2018 +0100

    Optimize std::rotl and std::rotr, add test for std::popcount
    
            * include/std/bit (__rotl, __rotr): Avoid branch.
            (_If_is_unsigned_integer): Use remove_cv_t.
            * testsuite/26_numerics/bit/bitops.count/popcount.cc: New.

diff --git a/libstdc++-v3/include/std/bit b/libstdc++-v3/include/std/bit
index 76aa0957b56..ace88954030 100644
--- a/libstdc++-v3/include/std/bit
+++ b/libstdc++-v3/include/std/bit
@@ -46,9 +46,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
       constexpr auto _Nd = numeric_limits<_Tp>::digits;
       const unsigned __sN = __s % _Nd;
-      if (__sN)
-        return (__x << __sN) | (__x >> (_Nd - __sN));
-      return __x;
+      return (__x << __sN) | (__x >> ((-__sN) % _Nd));
     }
 
   template<typename _Tp>
@@ -57,9 +55,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
       constexpr auto _Nd = numeric_limits<_Tp>::digits;
       const unsigned __sN = __s % _Nd;
-      if (__sN)
-        return (__x >> __sN) | (__x << (_Nd - __sN));
-      return __x;
+      return (__x >> __sN) | (__x << ((-__sN) % _Nd));
     }
 
   template<typename _Tp>
@@ -237,7 +233,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   template<typename _Tp, typename _Up = _Tp>
     using _If_is_unsigned_integer
-      = typename _If_is_unsigned_integer_type<_Tp, _Up>::type;
+      = typename _If_is_unsigned_integer_type<remove_cv_t<_Tp>, _Up>::type;
 
 #if ! __STRICT_ANSI__
   // [bitops.rot], rotating
diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/popcount.cc b/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/popcount.cc
new file mode 100644
index 00000000000..2982cb19bbe
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/popcount.cc
@@ -0,0 +1,108 @@
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <bit>
+
+template<typename UInt>
+constexpr auto
+test(UInt x)
+-> decltype(std::popcount(x))
+{
+  static_assert( noexcept(std::popcount(x)) );
+
+  constexpr unsigned digits = std::numeric_limits<UInt>::digits;
+
+  static_assert( std::popcount((UInt)0) == 0 );
+  static_assert( std::popcount((UInt)-1) == digits );
+  static_assert( std::popcount((UInt)-2) == digits - 1 );
+  static_assert( std::popcount((UInt)127) == 7 );
+
+  static_assert( std::popcount((UInt)1) == 1 );
+  static_assert( std::popcount((UInt)2) == 1 );
+  static_assert( std::popcount((UInt)0x70) == 3 );
+
+  if constexpr (std::numeric_limits<UInt>::digits > 8)
+  {
+    static_assert( std::popcount((UInt)(0x101)) == 2 );
+    static_assert( std::popcount((UInt)(0xfff)) == 12 );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits > 64)
+  {
+    static_assert( std::popcount((UInt)0xffffffffffffffff) == 64 );
+    static_assert( std::popcount(0x5555555555555555 | ((UInt)1 << 64)) == 33 );
+    static_assert( std::popcount(0x5555555555555555 | ((UInt)3 << 64)) == 34 );
+  }
+
+  return true;
+}
+
+static_assert( test( (unsigned char)0 ) );
+static_assert( test( (unsigned short)0 ) );
+static_assert( test( (unsigned int)0 ) );
+static_assert( test( (unsigned long)0 ) );
+static_assert( test( (unsigned long long)0 ) );
+
+// std::popcount(T) shall not participate in overload resolution
+// unless T is an unsigned integer type.
+struct X { constexpr bool did_not_match() { return true; } };
+constexpr X test(...) { return X{}; }
+static_assert( test( (bool)0 ).did_not_match() );
+static_assert( test( (char)0 ).did_not_match() );
+static_assert( test( (int)0 ).did_not_match() );
+static_assert( test( (char16_t)0 ).did_not_match() );
+static_assert( test( (float)0 ).did_not_match() );
+static_assert( test( (void*)0 ).did_not_match() );
+static_assert( test( X{} ).did_not_match() );
+enum E : unsigned { e };
+static_assert( test( e ).did_not_match() );
+
+#ifndef __STRICT_ANSI__
+#include <cstddef>
+static_assert( std::popcount(std::byte{0x00}) == 0 );
+static_assert( std::popcount(std::byte{0x01}) == 1 );
+static_assert( std::popcount(std::byte{0x02}) == 1 );
+static_assert( std::popcount(std::byte{0x03}) == 2 );
+static_assert( std::popcount(std::byte{0x30}) == 2 );
+static_assert( std::popcount(std::byte{0x40}) == 1 );
+static_assert( std::popcount(std::byte{0x41}) == 2 );
+static_assert( std::popcount(std::byte{0xff}) == 8 );
+#else
+static_assert( test( (std::byte)0 ).did_not_match() );
+#endif
+
+#if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
+static_assert( test( (unsigned __int128)0 ) );
+static_assert( test( (__int128)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_0)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_1)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_2)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
+#endif
Jakub Jelinek July 4, 2018, 8:09 a.m. | #4
On Tue, Jul 03, 2018 at 11:24:00PM +0100, Jonathan Wakely wrote:
> > Wouldn't it be better to use some branchless pattern that

> > GCC can also optimize well, like:

> >      return (__x << __sN) | (__x >> ((-_sN) & (_Nd - 1)));

> > (iff _Nd is always power of two),

> 

> _Nd is 20 for one of the INT_N types on msp340, but we could have a

> special case for the rare integer types with unusual sizes.

> 

> > or perhaps

> >      return (__x << __sN) | (__x >> ((-_sN) % _Nd));

> > which is going to be folded into the above one for power of two constants?

> 

> That looks good.


Unfortunately it is not correct if _Nd is not power of two.
E.g. for __sN 1, -1U % 20 is 15, not 19.
So it would need to be 
      return (__x << __sN) | (__x >> ((_Nd - __sN) % _Nd));
Unfortunately, our rotate pattern recognizer handles
      return (__x << __sN) | (__x >> ((-__sN) % _Nd));
or
      return (__x << __sN) | (__x >> ((-__sN) & (_Nd - 1)));
but doesn't handle the _Nd - __sN case.
Is this C++17+ only?  Then perhaps
      if constexpr ((_Nd & (_Nd - 1)) == 0)
	return (__x << __sN) | (__x >> (-__sN & (_Nd - 1)));
      return (__x << __sN) | (__x >> ((_Nd - __sN) % _Nd));

Verify that on x86_64 for all the unsigned {char,short,int,long long} you
actually get a mere rol? instruction with perhaps some register movement,
but no masking, nor shifts etc.

> > E.g. ia32intrin.h also uses:

> > /* 64bit rol */

> > extern __inline unsigned long long

> > __attribute__((__gnu_inline__, __always_inline__, __artificial__))

> > __rolq (unsigned long long __X, int __C)

> > {

> >  __C &= 63;

> >  return (__X << __C) | (__X >> (-__C & 63));

> > }

> > etc.

> 

> Should we delegate to those intrinsics for x86, so that

> __builtin_ia32_rolqi and __builtin_ia32_rolhi can be used when

> relevant?


No, the pattern recognizers should handle (for power of two bitcounts)
even the char/short cases.  Those intrinsics predate the improvements
in rotate pattern recognition.

	Jakub
Jonathan Wakely July 4, 2018, 8:14 a.m. | #5
On 04/07/18 10:09 +0200, Jakub Jelinek wrote:
>On Tue, Jul 03, 2018 at 11:24:00PM +0100, Jonathan Wakely wrote:

>> > Wouldn't it be better to use some branchless pattern that

>> > GCC can also optimize well, like:

>> >      return (__x << __sN) | (__x >> ((-_sN) & (_Nd - 1)));

>> > (iff _Nd is always power of two),

>>

>> _Nd is 20 for one of the INT_N types on msp340, but we could have a

>> special case for the rare integer types with unusual sizes.

>>

>> > or perhaps

>> >      return (__x << __sN) | (__x >> ((-_sN) % _Nd));

>> > which is going to be folded into the above one for power of two constants?

>>

>> That looks good.

>

>Unfortunately it is not correct if _Nd is not power of two.

>E.g. for __sN 1, -1U % 20 is 15, not 19.

>So it would need to be

>      return (__x << __sN) | (__x >> ((_Nd - __sN) % _Nd));

>Unfortunately, our rotate pattern recognizer handles

>      return (__x << __sN) | (__x >> ((-__sN) % _Nd));

>or

>      return (__x << __sN) | (__x >> ((-__sN) & (_Nd - 1)));

>but doesn't handle the _Nd - __sN case.

>Is this C++17+ only?  Then perhaps


The std::rotr and std::rotl functions are C++2a only, but I've added
the __rotr and __rotl versions for our own internal use in C++14 and
later.

In practice I have no internal use for rotr and rotl, so I could
remove the __rot[rl] forms. However, won't ((_Nd & (_Nd - 1)) optimize
to a constant even without if-constexpr? I'll check.

>      if constexpr ((_Nd & (_Nd - 1)) == 0)

>	return (__x << __sN) | (__x >> (-__sN & (_Nd - 1)));

>      return (__x << __sN) | (__x >> ((_Nd - __sN) % _Nd));

>

>Verify that on x86_64 for all the unsigned {char,short,int,long long} you

>actually get a mere rol? instruction with perhaps some register movement,

>but no masking, nor shifts etc.


Will do.

>> > E.g. ia32intrin.h also uses:

>> > /* 64bit rol */

>> > extern __inline unsigned long long

>> > __attribute__((__gnu_inline__, __always_inline__, __artificial__))

>> > __rolq (unsigned long long __X, int __C)

>> > {

>> >  __C &= 63;

>> >  return (__X << __C) | (__X >> (-__C & 63));

>> > }

>> > etc.

>>

>> Should we delegate to those intrinsics for x86, so that

>> __builtin_ia32_rolqi and __builtin_ia32_rolhi can be used when

>> relevant?

>

>No, the pattern recognizers should handle (for power of two bitcounts)

>even the char/short cases.  Those intrinsics predate the improvements

>in rotate pattern recognition.


OK, good to know.
Jakub Jelinek July 4, 2018, 8:26 a.m. | #6
On Wed, Jul 04, 2018 at 09:14:04AM +0100, Jonathan Wakely wrote:
> > Unfortunately it is not correct if _Nd is not power of two.

> > E.g. for __sN 1, -1U % 20 is 15, not 19.

> > So it would need to be

> >      return (__x << __sN) | (__x >> ((_Nd - __sN) % _Nd));

> > Unfortunately, our rotate pattern recognizer handles

> >      return (__x << __sN) | (__x >> ((-__sN) % _Nd));

> > or

> >      return (__x << __sN) | (__x >> ((-__sN) & (_Nd - 1)));

> > but doesn't handle the _Nd - __sN case.

> > Is this C++17+ only?  Then perhaps

> 

> The std::rotr and std::rotl functions are C++2a only, but I've added

> the __rotr and __rotl versions for our own internal use in C++14 and

> later.

> 

> In practice I have no internal use for rotr and rotl, so I could

> remove the __rot[rl] forms. However, won't ((_Nd & (_Nd - 1)) optimize

> to a constant even without if-constexpr? I'll check.


It should, sure.

Anyway, I guess my C tests didn't test properly what happens in your
functions.  We actually do optimize:
unsigned long long foo (unsigned long long __x, unsigned int __s)
{
  constexpr int _Nd = 64;
  unsigned int _sN = __s % _Nd;
  return (__x << _sN) | (__x >> ((_Nd - _sN) % _Nd));
}
properly, just don't do it if it is:
unsigned long long foo (unsigned long long __x, unsigned int __s)
{
  int _Nd = 64;
  unsigned int _sN = __s % _Nd;
  return (__x << _sN) | (__x >> ((_Nd - _sN) % _Nd));
}
Apparently, the (64 - x) & 63 optimization to -x & 63 is only done
somewhere in the FEs and not in match.pd, which is something we should fix.

But with constexpr _Nd you actually can use
      return (__x << __sN) | (__x >> ((_Nd - __sN) % _Nd));
unconditionally.

	Jakub
Jonathan Wakely July 4, 2018, 2:31 p.m. | #7
On 04/07/18 10:26 +0200, Jakub Jelinek wrote:
>On Wed, Jul 04, 2018 at 09:14:04AM +0100, Jonathan Wakely wrote:

>> > Unfortunately it is not correct if _Nd is not power of two.

>> > E.g. for __sN 1, -1U % 20 is 15, not 19.

>> > So it would need to be

>> >      return (__x << __sN) | (__x >> ((_Nd - __sN) % _Nd));

>> > Unfortunately, our rotate pattern recognizer handles

>> >      return (__x << __sN) | (__x >> ((-__sN) % _Nd));

>> > or

>> >      return (__x << __sN) | (__x >> ((-__sN) & (_Nd - 1)));

>> > but doesn't handle the _Nd - __sN case.

>> > Is this C++17+ only?  Then perhaps

>>

>> The std::rotr and std::rotl functions are C++2a only, but I've added

>> the __rotr and __rotl versions for our own internal use in C++14 and

>> later.

>>

>> In practice I have no internal use for rotr and rotl, so I could

>> remove the __rot[rl] forms. However, won't ((_Nd & (_Nd - 1)) optimize

>> to a constant even without if-constexpr? I'll check.

>

>It should, sure.

>

>Anyway, I guess my C tests didn't test properly what happens in your

>functions.  We actually do optimize:

>unsigned long long foo (unsigned long long __x, unsigned int __s)

>{

>  constexpr int _Nd = 64;

>  unsigned int _sN = __s % _Nd;

>  return (__x << _sN) | (__x >> ((_Nd - _sN) % _Nd));

>}

>properly, just don't do it if it is:

>unsigned long long foo (unsigned long long __x, unsigned int __s)

>{

>  int _Nd = 64;

>  unsigned int _sN = __s % _Nd;

>  return (__x << _sN) | (__x >> ((_Nd - _sN) % _Nd));

>}

>Apparently, the (64 - x) & 63 optimization to -x & 63 is only done

>somewhere in the FEs and not in match.pd, which is something we should fix.

>

>But with constexpr _Nd you actually can use

>      return (__x << __sN) | (__x >> ((_Nd - __sN) % _Nd));

>unconditionally.


OK here's what I've just tested and committed to trunk.
commit 35640efe0911095ad6ce38771e40a22a6cbb1221
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Jul 4 14:37:57 2018 +0100

    Fix std::__rotl and std::__rotr
    
    2018-07-04  Jonathan Wakely  <jwakely@redhat.com>
                Jakub Jelinek  <jakub@redhat.com>
    
            * include/std/bit (__rotl, __rotr): Fix for non-power of two sizes.

diff --git a/libstdc++-v3/include/std/bit b/libstdc++-v3/include/std/bit
index ace88954030..a23f2ba60d1 100644
--- a/libstdc++-v3/include/std/bit
+++ b/libstdc++-v3/include/std/bit
@@ -46,7 +46,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
       constexpr auto _Nd = numeric_limits<_Tp>::digits;
       const unsigned __sN = __s % _Nd;
-      return (__x << __sN) | (__x >> ((-__sN) % _Nd));
+      return (__x << __sN) | (__x >> ((_Nd - __sN) % _Nd));
     }
 
   template<typename _Tp>
@@ -55,7 +55,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
       constexpr auto _Nd = numeric_limits<_Tp>::digits;
       const unsigned __sN = __s % _Nd;
-      return (__x >> __sN) | (__x << ((-__sN) % _Nd));
+      return (__x >> __sN) | (__x << ((_Nd - __sN) % _Nd));
     }
 
   template<typename _Tp>

Patch

diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am
index f91907df325..d1453a5abce 100644
--- a/libstdc++-v3/include/Makefile.am
+++ b/libstdc++-v3/include/Makefile.am
@@ -30,6 +30,7 @@  std_headers = \
 	${std_srcdir}/any \
 	${std_srcdir}/array \
 	${std_srcdir}/atomic \
+	${std_srcdir}/bit \
 	${std_srcdir}/bitset \
 	${std_srcdir}/charconv \
 	${std_srcdir}/chrono \
diff --git a/libstdc++-v3/include/precompiled/stdc++.h b/libstdc++-v3/include/precompiled/stdc++.h
index 9b056bb3467..80769233eb3 100644
--- a/libstdc++-v3/include/precompiled/stdc++.h
+++ b/libstdc++-v3/include/precompiled/stdc++.h
@@ -134,6 +134,7 @@ 
 #endif
 
 #if __cplusplus > 201703L
+#include <bit>
 // #include <compare>
 // #include <span>
 // #include <syncstream>
diff --git a/libstdc++-v3/include/std/bit b/libstdc++-v3/include/std/bit
new file mode 100644
index 00000000000..76aa0957b56
--- /dev/null
+++ b/libstdc++-v3/include/std/bit
@@ -0,0 +1,359 @@ 
+// <bit> -*- C++ -*-
+
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file include/bit
+ *  This is a Standard C++ Library header.
+ */
+
+#ifndef _GLIBCXX_BIT
+#define _GLIBCXX_BIT 1
+
+#pragma GCC system_header
+
+#if __cplusplus >= 201402L
+
+#include <type_traits>
+#include <limits>
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+  template<typename _Tp>
+    constexpr _Tp
+    __rotl(_Tp __x, unsigned int __s) noexcept
+    {
+      constexpr auto _Nd = numeric_limits<_Tp>::digits;
+      const unsigned __sN = __s % _Nd;
+      if (__sN)
+        return (__x << __sN) | (__x >> (_Nd - __sN));
+      return __x;
+    }
+
+  template<typename _Tp>
+    constexpr _Tp
+    __rotr(_Tp __x, unsigned int __s) noexcept
+    {
+      constexpr auto _Nd = numeric_limits<_Tp>::digits;
+      const unsigned __sN = __s % _Nd;
+      if (__sN)
+        return (__x >> __sN) | (__x << (_Nd - __sN));
+      return __x;
+    }
+
+  template<typename _Tp>
+    constexpr int
+    __countl_zero(_Tp __x) noexcept
+    {
+      using __limits = numeric_limits<_Tp>;
+
+      if (__x == 0)
+        return __limits::digits;
+
+      using __limits_ull = numeric_limits<unsigned long long>;
+      using __limits_ul = numeric_limits<unsigned long>;
+      using __limits_u = numeric_limits<unsigned>;
+
+      if _GLIBCXX17_CONSTEXPR (__limits::digits <= __limits_u::digits)
+	{
+	  constexpr int __diff = __limits_u::digits - __limits::digits;
+	  return __builtin_clz(__x) - __diff;
+	}
+      else if _GLIBCXX17_CONSTEXPR (__limits::digits <= __limits_ul::digits)
+	{
+	  constexpr int __diff = __limits_ul::digits - __limits::digits;
+	  return __builtin_clzl(__x) - __diff;
+	}
+      else if _GLIBCXX17_CONSTEXPR (__limits::digits <= __limits_ull::digits)
+	{
+	  constexpr int __diff = __limits_ull::digits - __limits::digits;
+	  return __builtin_clzll(__x) - __diff;
+	}
+      else // (__limits::digits > __limits_ull::digits)
+	{
+	  static_assert(__limits::digits <= (2 * __limits_ull::digits),
+			"Maximum supported integer size is 128-bit");
+
+	  unsigned long long __high = __x >> __limits_ull::digits;
+	  if (__high != 0)
+	    {
+	      constexpr int __diff
+		= (2 * __limits_ull::digits) - __limits::digits;
+	      return __builtin_clzll(__high) - __diff;
+	    }
+	  unsigned long long __low = __x & __limits_ull::max();
+	  return (__limits::digits - __limits_ull::digits)
+	    + __builtin_clzll(__low);
+	}
+    }
+
+  template<typename _Tp>
+    constexpr int
+    __countl_one(_Tp __x) noexcept
+    {
+      if (__x == numeric_limits<_Tp>::max())
+        return numeric_limits<_Tp>::digits;
+      return std::__countl_zero<_Tp>((_Tp)~__x);
+    }
+
+  template<typename _Tp>
+    constexpr int
+    __countr_zero(_Tp __x) noexcept
+    {
+      using __limits = numeric_limits<_Tp>;
+
+      if (__x == 0)
+        return __limits::digits;
+
+      using __limits_ull = numeric_limits<unsigned long long>;
+      using __limits_ul = numeric_limits<unsigned long>;
+      using __limits_u = numeric_limits<unsigned>;
+
+      if _GLIBCXX17_CONSTEXPR (__limits::digits <= __limits_u::digits)
+	return __builtin_ctz(__x);
+      else if _GLIBCXX17_CONSTEXPR (__limits::digits <= __limits_ul::digits)
+	return __builtin_ctzl(__x);
+      else if _GLIBCXX17_CONSTEXPR (__limits::digits <= __limits_ull::digits)
+	return __builtin_ctzll(__x);
+      else // (__limits::digits > __limits_ull::digits)
+	{
+	  static_assert(__limits::digits <= (2 * __limits_ull::digits),
+			"Maximum supported integer size is 128-bit");
+
+	  unsigned long long __low = __x & __limits_ull::max();
+	  if (__low != 0)
+	    return __builtin_ctzll(__low);
+	  unsigned long long __high = __x >> __limits_ull::digits;
+	  return __builtin_ctzll(__high) + __limits_ull::digits;
+	}
+    }
+
+  template<typename _Tp>
+    constexpr int
+    __countr_one(_Tp __x) noexcept
+    {
+      if (__x == numeric_limits<_Tp>::max())
+        return numeric_limits<_Tp>::digits;
+      return std::__countr_zero((_Tp)~__x);
+    }
+
+  template<typename _Tp>
+    constexpr int
+    __popcount(_Tp __x) noexcept
+    {
+      using __limits = numeric_limits<_Tp>;
+
+      if (__x == 0)
+        return 0;
+
+      using __limits_ull = numeric_limits<unsigned long long>;
+      using __limits_ul = numeric_limits<unsigned long>;
+      using __limits_u = numeric_limits<unsigned>;
+
+      if _GLIBCXX17_CONSTEXPR (__limits::digits <= __limits_u::digits)
+	return __builtin_popcount(__x);
+      else if _GLIBCXX17_CONSTEXPR (__limits::digits <= __limits_ul::digits)
+	return __builtin_popcountl(__x);
+      else if _GLIBCXX17_CONSTEXPR (__limits::digits <= __limits_ull::digits)
+	return __builtin_popcountll(__x);
+      else // (__limits::digits > __limits_ull::digits)
+	{
+	  static_assert(__limits::digits <= (2 * __limits_ull::digits),
+			"Maximum supported integer size is 128-bit");
+
+	  unsigned long long __low = __x & __limits_ull::max();
+	  unsigned long long __high = __x >> __limits_ull::digits;
+	  return __builtin_popcountll(__low) + __builtin_popcountll(__high);
+	}
+    }
+
+  template<typename _Tp>
+    constexpr bool
+    __ispow2(_Tp __x) noexcept
+    { return std::__popcount(__x) == 1; }
+
+  template<typename _Tp>
+    constexpr _Tp
+    __ceil2(_Tp __x) noexcept
+    {
+      constexpr auto _Nd = numeric_limits<_Tp>::digits;
+      if (__x == 0)
+        return 1;
+      return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x - 1u)));
+    }
+
+  template<typename _Tp>
+    constexpr _Tp
+    __floor2(_Tp __x) noexcept
+    {
+      constexpr auto _Nd = numeric_limits<_Tp>::digits;
+      if (__x == 0)
+        return 0;
+      return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
+    }
+
+  template<typename _Tp>
+    constexpr _Tp
+    __log2p1(_Tp __x) noexcept
+    {
+      constexpr auto _Nd = numeric_limits<_Tp>::digits;
+      if (__x == 0)
+	return 0;
+      return _Nd - std::__countl_zero(__x);
+    }
+
+#if __cplusplus > 201703L
+
+  template<typename _Tp, typename _Up, bool = is_integral_v<_Tp>>
+    struct _If_is_unsigned_integer_type { };
+
+  template<typename _Up>
+    struct _If_is_unsigned_integer_type<bool, _Up, true> { };
+
+  template<typename _Tp, typename _Up>
+    struct _If_is_unsigned_integer_type<_Tp, _Up, true>
+    : enable_if<is_same_v<_Tp, make_unsigned_t<_Tp>>, _Up> { };
+
+  template<typename _Tp, typename _Up = _Tp>
+    using _If_is_unsigned_integer
+      = typename _If_is_unsigned_integer_type<_Tp, _Up>::type;
+
+#if ! __STRICT_ANSI__
+  // [bitops.rot], rotating
+
+  template<typename _Tp>
+    constexpr _If_is_unsigned_integer<_Tp>
+    rotl(_Tp __x, unsigned int __s) noexcept
+    { return std::__rotl(__x, __s); }
+
+  template<typename _Tp>
+    constexpr _If_is_unsigned_integer<_Tp>
+    rotr(_Tp __x, unsigned int __s) noexcept
+    { return std::__rotr(__x, __s); }
+
+  // [bitops.count], counting
+
+  template<typename _Tp>
+    constexpr _If_is_unsigned_integer<_Tp, int>
+    countl_zero(_Tp __x) noexcept
+    { return std::__countl_zero(__x); }
+
+  template<typename _Tp>
+    constexpr _If_is_unsigned_integer<_Tp, int>
+    countl_one(_Tp __x) noexcept
+    { return std::__countl_one(__x); }
+
+  template<typename _Tp>
+    constexpr _If_is_unsigned_integer<_Tp, int>
+    countr_zero(_Tp __x) noexcept
+    { return std::__countr_zero(__x); }
+
+  template<typename _Tp>
+    constexpr _If_is_unsigned_integer<_Tp, int>
+    countr_one(_Tp __x) noexcept
+    { return std::__countr_one(__x); }
+
+  template<typename _Tp>
+    constexpr _If_is_unsigned_integer<_Tp, int>
+    popcount(_Tp __x) noexcept
+    { return std::__popcount(__x); }
+#endif
+
+  // Integral power-of-two operations
+
+  template<typename _Tp>
+    constexpr _If_is_unsigned_integer<_Tp, bool>
+    ispow2(_Tp __x) noexcept
+    { return std::__ispow2(__x); }
+
+  template<typename _Tp>
+    constexpr _If_is_unsigned_integer<_Tp>
+    ceil2(_Tp __x) noexcept
+    { return std::__ceil2(__x); }
+
+  template<typename _Tp>
+    constexpr _If_is_unsigned_integer<_Tp>
+    floor2(_Tp __x) noexcept
+    { return std::__floor2(__x); }
+
+  template<typename _Tp>
+    constexpr _If_is_unsigned_integer<_Tp>
+    log2p1(_Tp __x) noexcept
+    { return std::__log2p1(__x); }
+
+#if ! __STRICT_ANSI__
+  enum class byte : unsigned char;
+
+  constexpr byte
+  rotl(byte __x, unsigned int __s) noexcept
+  { return (byte)std::__rotl((unsigned char)__x, __s); }
+
+  constexpr byte
+  rotr(byte __x, unsigned int __s) noexcept
+  { return (byte)std::__rotr((unsigned char)__x, __s); }
+
+  constexpr int
+  countl_zero(byte __x) noexcept
+  { return std::__countl_zero((unsigned char)__x); }
+
+  constexpr int
+  countl_one(byte __x) noexcept
+  { return std::__countl_one((unsigned char)__x); }
+
+  constexpr int
+  countr_zero(byte __x) noexcept
+  { return std::__countr_zero((unsigned char)__x); }
+
+  constexpr int
+  countr_one(byte __x) noexcept
+  { return std::__countr_one((unsigned char)__x); }
+
+  constexpr int
+  popcount(byte __x) noexcept
+  { return std::__popcount((unsigned char)__x); }
+
+  constexpr bool
+  ispow2(byte __x) noexcept
+  { return std::__ispow2((unsigned char)__x); }
+
+  constexpr byte
+  ceil2(byte __x) noexcept
+  { return (byte)std::__ceil2((unsigned char)__x); }
+
+  constexpr byte
+  floor2(byte __x) noexcept
+  { return (byte)std::__floor2((unsigned char)__x); }
+
+  constexpr byte
+  log2p1(byte __x) noexcept
+  { return (byte)std::__log2p1((unsigned char)__x); }
+#endif
+
+#endif // C++2a
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace std
+
+#endif // C++14
+#endif // _GLIBCXX_BIT
diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ceil2.cc b/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ceil2.cc
new file mode 100644
index 00000000000..65e1569c277
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ceil2.cc
@@ -0,0 +1,108 @@ 
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <bit>
+
+template<typename UInt>
+constexpr auto
+test(UInt x)
+-> decltype(std::ceil2(x))
+{
+  static_assert( noexcept(std::ceil2(x)) );
+
+  static_assert( std::ceil2(UInt(0)) == 1 );
+  static_assert( std::ceil2(UInt(1)) == 1 );
+  static_assert( std::ceil2(UInt(2)) == 2 );
+  static_assert( std::ceil2(UInt(3)) == 4 );
+  static_assert( std::ceil2(UInt(4)) == 4 );
+  static_assert( std::ceil2(UInt(0x11)) == 0x20 );
+  static_assert( std::ceil2(UInt(0x20)) == 0x20 );
+
+  if constexpr (std::numeric_limits<UInt>::digits > 8)
+  {
+    static_assert( std::ceil2(UInt(0x201)) == 0x400 );
+    static_assert( std::ceil2(UInt(0x8ff)) == 0x1000 );
+    static_assert( std::ceil2(UInt(0x1000)) == 0x1000 );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits > 32)
+  {
+    static_assert( std::ceil2(UInt(0xabcdef)) == 0x1000000 );
+    static_assert( std::ceil2(UInt(0x1000000)) == 0x1000000 );
+    static_assert( std::ceil2(UInt(0x1000001)) == 0x2000000 );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits > 64)
+  {
+    static_assert( std::ceil2(UInt(1) << 64) == (UInt(1) << 64) );
+    static_assert( std::ceil2(UInt(3) << 64) == (UInt(4) << 64) );
+  }
+
+  return true;
+}
+
+static_assert( test( (unsigned char)0 ) );
+static_assert( test( (unsigned short)0 ) );
+static_assert( test( (unsigned int)0 ) );
+static_assert( test( (unsigned long)0 ) );
+static_assert( test( (unsigned long long)0 ) );
+
+// std::ceil2(T) shall not participate in overload resolution
+// unless T is an unsigned integer type.
+struct X { constexpr bool did_not_match() { return true; } };
+constexpr X test(...) { return X{}; }
+static_assert( test( (bool)0 ).did_not_match() );
+static_assert( test( (char)0 ).did_not_match() );
+static_assert( test( (int)0 ).did_not_match() );
+static_assert( test( (char16_t)0 ).did_not_match() );
+static_assert( test( (float)0 ).did_not_match() );
+static_assert( test( (void*)0 ).did_not_match() );
+static_assert( test( X{} ).did_not_match() );
+enum E : unsigned { e };
+static_assert( test( e ).did_not_match() );
+
+#ifndef __STRICT_ANSI__
+#include <cstddef>
+static_assert( std::ceil2(std::byte{0}) == std::byte{1} );
+static_assert( std::ceil2(std::byte{1}) == std::byte{1} );
+static_assert( std::ceil2(std::byte{2}) == std::byte{2} );
+static_assert( std::ceil2(std::byte{3}) == std::byte{4} );
+static_assert( std::ceil2(std::byte{100}) == std::byte{128} );
+static_assert( std::ceil2(std::byte{128}) == std::byte{128} );
+#else
+static_assert( test( (std::byte)0 ).did_not_match() );
+#endif
+
+#if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
+static_assert( test( (unsigned __int128)0 ) );
+static_assert( test( (__int128)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_0)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_1)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_2)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
+#endif
diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/floor2.cc b/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/floor2.cc
new file mode 100644
index 00000000000..0206110ec2f
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/floor2.cc
@@ -0,0 +1,109 @@ 
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <bit>
+
+template<typename UInt>
+constexpr auto
+test(UInt x)
+-> decltype(std::floor2(x))
+{
+  static_assert( noexcept(std::floor2(x)) );
+
+  static_assert( std::floor2(UInt(0)) == 0 );
+  static_assert( std::floor2(UInt(1)) == 1 );
+  static_assert( std::floor2(UInt(2)) == 2 );
+  static_assert( std::floor2(UInt(3)) == 2 );
+  static_assert( std::floor2(UInt(4)) == 4 );
+  static_assert( std::floor2(UInt(0x11)) == 0x10 );
+  static_assert( std::floor2(UInt(0x20)) == 0x20 );
+
+  if constexpr (std::numeric_limits<UInt>::digits > 8)
+  {
+    static_assert( std::floor2(UInt(0x201)) == 0x200 );
+    static_assert( std::floor2(UInt(0x8ff)) == 0x800 );
+    static_assert( std::floor2(UInt(0x1000)) == 0x1000 );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits > 32)
+  {
+    static_assert( std::floor2(UInt(0xabcdef)) == 0x800000 );
+    static_assert( std::floor2(UInt(0x1000000)) == 0x1000000 );
+    static_assert( std::floor2(UInt(0x1000001)) == 0x1000000 );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits > 64)
+  {
+    static_assert( std::floor2(UInt(1) << 64) == (UInt(1) << 64) );
+    static_assert( std::floor2(UInt(3) << 64) == (UInt(2) << 64) );
+  }
+
+  return true;
+}
+
+static_assert( test( (unsigned char)0 ) );
+static_assert( test( (unsigned short)0 ) );
+static_assert( test( (unsigned int)0 ) );
+static_assert( test( (unsigned long)0 ) );
+static_assert( test( (unsigned long long)0 ) );
+
+// std::floor2(T) shall not participate in overload resolution
+// unless T is an unsigned integer type.
+struct X { constexpr bool did_not_match() { return true; } };
+constexpr X test(...) { return X{}; }
+static_assert( test( (bool)0 ).did_not_match() );
+static_assert( test( (char)0 ).did_not_match() );
+static_assert( test( (int)0 ).did_not_match() );
+static_assert( test( (char16_t)0 ).did_not_match() );
+static_assert( test( (float)0 ).did_not_match() );
+static_assert( test( (void*)0 ).did_not_match() );
+static_assert( test( X{} ).did_not_match() );
+enum E : unsigned { e };
+static_assert( test( e ).did_not_match() );
+
+#ifndef __STRICT_ANSI__
+#include <cstddef>
+static_assert( std::floor2(std::byte{0}) == std::byte{0} );
+static_assert( std::floor2(std::byte{1}) == std::byte{1} );
+static_assert( std::floor2(std::byte{2}) == std::byte{2} );
+static_assert( std::floor2(std::byte{3}) == std::byte{2} );
+static_assert( std::floor2(std::byte{100}) == std::byte{64} );
+static_assert( std::floor2(std::byte{128}) == std::byte{128} );
+static_assert( std::floor2(std::byte{255}) == std::byte{128} );
+#else
+static_assert( test( (std::byte)0 ).did_not_match() );
+#endif
+
+#if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
+static_assert( test( (unsigned __int128)0 ) );
+static_assert( test( (__int128)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_0)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_1)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_2)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
+#endif
diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ispow2.cc b/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ispow2.cc
new file mode 100644
index 00000000000..ba8cfa8da7c
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ispow2.cc
@@ -0,0 +1,157 @@ 
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <bit>
+
+template<typename UInt>
+constexpr auto
+test(UInt x)
+-> decltype(std::ispow2(x))
+{
+  static_assert( noexcept(std::ispow2(x)) );
+
+  static_assert( ! std::ispow2( (UInt)0 ) );
+  static_assert( ! std::ispow2( (UInt)-1 ) );
+  static_assert( ! std::ispow2( (UInt)3 ) );
+  static_assert( ! std::ispow2( (UInt)0x0f ) );
+  static_assert( ! std::ispow2( (UInt)0xff ) );
+  static_assert( ! std::ispow2( (UInt)0x0a ) );
+  static_assert( ! std::ispow2( (UInt)0xa0 ) );
+
+  constexpr UInt one = 1;
+  static_assert( std::ispow2( (UInt)(one << 0) ) );
+
+  static_assert( std::ispow2( (UInt)(one << 1) ) );
+  static_assert( std::ispow2( (UInt)(one << 2) ) );
+  static_assert( std::ispow2( (UInt)(one << 3) ) );
+  static_assert( std::ispow2( (UInt)(one << 4) ) );
+  static_assert( std::ispow2( (UInt)(one << 5) ) );
+  static_assert( std::ispow2( (UInt)(one << 6) ) );
+  static_assert( std::ispow2( (UInt)(one << 7) ) );
+
+  if constexpr (std::numeric_limits<UInt>::digits > 8)
+  {
+    static_assert( std::ispow2( (UInt)(one << 8) ) );
+    static_assert( std::ispow2( (UInt)(one << 9) ) );
+    static_assert( std::ispow2( (UInt)(one << 10) ) );
+    static_assert( std::ispow2( (UInt)(one << 11) ) );
+    static_assert( std::ispow2( (UInt)(one << 12) ) );
+    static_assert( std::ispow2( (UInt)(one << 13) ) );
+    static_assert( std::ispow2( (UInt)(one << 14) ) );
+    static_assert( std::ispow2( (UInt)(one << 15) ) );
+
+    static_assert( ! std::ispow2( (UInt)0xf000 ) );
+    static_assert( ! std::ispow2( (UInt)0xff00 ) );
+    static_assert( ! std::ispow2( (UInt)0xf0f0 ) );
+    static_assert( ! std::ispow2( (UInt)0xf00f ) );
+    static_assert( ! std::ispow2( (UInt)0x0f0f ) );
+    static_assert( ! std::ispow2( (UInt)0x00ff ) );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits > 16)
+  {
+    static_assert( std::ispow2( (UInt)(one << 16) ) );
+    static_assert( std::ispow2( (UInt)(one << 17) ) );
+    static_assert( ! std::ispow2( (UInt)((one << 16) + 1) ) );
+    static_assert( ! std::ispow2( (UInt)((one << 16) + 0x10) ) );
+  }
+
+  // msp340 target has 20-bit __GLIBCXX_TYPE_INT_N_0 type
+  if constexpr (std::numeric_limits<UInt>::digits > 20)
+  {
+    static_assert( std::ispow2( (UInt)(one << 20) ) );
+    static_assert( std::ispow2( (UInt)(one << 21) ) );
+    static_assert( std::ispow2( (UInt)(one << 24) ) );
+    static_assert( std::ispow2( (UInt)(one << 28) ) );
+    static_assert( std::ispow2( (UInt)(one << 31) ) );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits > 32)
+  {
+    static_assert( std::ispow2( (UInt)(one << 32) ) );
+    static_assert( std::ispow2( (UInt)(one << 33) ) );
+    static_assert( std::ispow2( (UInt)(one << 41) ) );
+
+    static_assert( ! std::ispow2( (UInt)((one << 32) + 1) ) );
+    static_assert( ! std::ispow2( (UInt)((one << 32) + (one << 31)) ) );
+    static_assert( ! std::ispow2( (UInt)((one << 33) + 1) ) );
+    static_assert( ! std::ispow2( (UInt)((one << 33) + (one << 32)) ) );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits == 64)
+  {
+    static_assert( std::ispow2( (UInt)(one << 63) ) );
+
+    static_assert( ! std::ispow2( (UInt)((one << 63) + 1) ) );
+    static_assert( ! std::ispow2( (UInt)((one << 63) + (one << 8)) ) );
+    static_assert( ! std::ispow2( (UInt)((one << 63) + (one << 32)) ) );
+  }
+  return true;
+}
+
+static_assert( test( (unsigned char)0 ) );
+static_assert( test( (unsigned short)0 ) );
+static_assert( test( (unsigned int)0 ) );
+static_assert( test( (unsigned long)0 ) );
+static_assert( test( (unsigned long long)0 ) );
+
+// std::ispow2(T) shall not participate in overload resolution
+// unless T is an unsigned integer type.
+struct X { constexpr bool did_not_match() { return true; } };
+constexpr X test(...) { return X{}; }
+static_assert( test( (bool)0 ).did_not_match() );
+static_assert( test( (char)0 ).did_not_match() );
+static_assert( test( (int)0 ).did_not_match() );
+static_assert( test( (char16_t)0 ).did_not_match() );
+static_assert( test( (float)0 ).did_not_match() );
+static_assert( test( (void*)0 ).did_not_match() );
+static_assert( test( X{} ).did_not_match() );
+enum E : unsigned { e };
+static_assert( test( e ).did_not_match() );
+
+#ifndef __STRICT_ANSI__
+#include <cstddef>
+static_assert( std::ispow2(std::byte{0}) == false );
+static_assert( std::ispow2(std::byte{1}) == true );
+static_assert( std::ispow2(std::byte{2}) == true );
+static_assert( std::ispow2(std::byte{3}) == false );
+static_assert( std::ispow2(std::byte{100}) == false );
+static_assert( std::ispow2(std::byte{128}) == true );
+static_assert( std::ispow2(std::byte{255}) == false );
+#else
+static_assert( test( (std::byte)0 ).did_not_match() );
+#endif
+
+#if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
+static_assert( test( (unsigned __int128)0 ) );
+static_assert( test( (__int128)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_0)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_1)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_2)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
+#endif
diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/log2p1.cc b/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/log2p1.cc
new file mode 100644
index 00000000000..142f6ee857d
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/log2p1.cc
@@ -0,0 +1,109 @@ 
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <bit>
+
+template<typename UInt>
+constexpr auto
+test(UInt x)
+-> decltype(std::log2p1(x))
+{
+  static_assert( noexcept(std::log2p1(x)) );
+
+  static_assert( std::log2p1(UInt(0)) == 0 );
+  static_assert( std::log2p1(UInt(1)) == 1 );
+  static_assert( std::log2p1(UInt(2)) == 2 );
+  static_assert( std::log2p1(UInt(3)) == 2 );
+  static_assert( std::log2p1(UInt(4)) == 3 );
+  static_assert( std::log2p1(UInt(0x11)) == 5 );
+  static_assert( std::log2p1(UInt(0x20)) == 6 );
+
+  if constexpr (std::numeric_limits<UInt>::digits > 8)
+  {
+    static_assert( std::log2p1(UInt(0x201)) == 10 );
+    static_assert( std::log2p1(UInt(0x8ff)) == 12 );
+    static_assert( std::log2p1(UInt(0x1000)) == 13 );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits > 32)
+  {
+    static_assert( std::log2p1(UInt(0xabcdef)) == 24 );
+    static_assert( std::log2p1(UInt(0x1000000)) == 25 );
+    static_assert( std::log2p1(UInt(0x1000001)) == 25 );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits > 64)
+  {
+    static_assert( std::log2p1(UInt(1) << 64) == 65 );
+    static_assert( std::log2p1(UInt(3) << 64) == 66 );
+  }
+
+  return true;
+}
+
+static_assert( test( (unsigned char)0 ) );
+static_assert( test( (unsigned short)0 ) );
+static_assert( test( (unsigned int)0 ) );
+static_assert( test( (unsigned long)0 ) );
+static_assert( test( (unsigned long long)0 ) );
+
+// std::log2p1(T) shall not participate in overload resolution
+// unless T is an unsigned integer type.
+struct X { constexpr bool did_not_match() { return true; } };
+constexpr X test(...) { return X{}; }
+static_assert( test( (bool)0 ).did_not_match() );
+static_assert( test( (char)0 ).did_not_match() );
+static_assert( test( (int)0 ).did_not_match() );
+static_assert( test( (char16_t)0 ).did_not_match() );
+static_assert( test( (float)0 ).did_not_match() );
+static_assert( test( (void*)0 ).did_not_match() );
+static_assert( test( X{} ).did_not_match() );
+enum E : unsigned { e };
+static_assert( test( e ).did_not_match() );
+
+#ifndef __STRICT_ANSI__
+#include <cstddef>
+static_assert( std::log2p1(std::byte{0}) == std::byte{0} );
+static_assert( std::log2p1(std::byte{1}) == std::byte{1} );
+static_assert( std::log2p1(std::byte{2}) == std::byte{2} );
+static_assert( std::log2p1(std::byte{3}) == std::byte{2} );
+static_assert( std::log2p1(std::byte{100}) == std::byte{7} );
+static_assert( std::log2p1(std::byte{128}) == std::byte{8} );
+static_assert( std::log2p1(std::byte{255}) == std::byte{8} );
+#else
+static_assert( test( (std::byte)0 ).did_not_match() );
+#endif
+
+#if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
+static_assert( test( (unsigned __int128)0 ) );
+static_assert( test( (__int128)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_0)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_1)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_2)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
+#endif
diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/countl_one.cc b/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/countl_one.cc
new file mode 100644
index 00000000000..c9ef5538538
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/countl_one.cc
@@ -0,0 +1,103 @@ 
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <bit>
+
+template<typename UInt>
+constexpr auto
+test(UInt x)
+-> decltype(std::countl_one(x))
+{
+  static_assert( noexcept(std::countl_one(x)) );
+
+  constexpr unsigned digits = std::numeric_limits<UInt>::digits;
+
+  static_assert( std::countl_one((UInt)0) == 0 );
+  static_assert( std::countl_one((UInt)-1) == digits );
+
+  static_assert( std::countl_one((UInt)3) == 0 );
+  static_assert( std::countl_one((UInt)((UInt)1 << digits - 1)) == 1 );
+  static_assert( std::countl_one((UInt)((UInt)3 << digits - 2)) == 2 );
+  static_assert( std::countl_one((UInt)((UInt)7 << digits - 3)) == 3 );
+  static_assert( std::countl_one((UInt)((UInt)255 << digits - 8)) == 8 );
+
+  if constexpr (std::numeric_limits<UInt>::digits > 8)
+  {
+    static_assert( std::countl_one((UInt)((UInt)-1 ^ 0x100)) == digits - 9 );
+    static_assert( std::countl_one((UInt)((UInt)-1 ^ 0x101)) == digits - 9 );
+    static_assert( std::countl_one((UInt)((UInt)-1 ^ 0x201)) == digits - 10 );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits > 64)
+  {
+    static_assert( std::countl_one((UInt)-1 ^ ((UInt)1 << 70)) == digits - 71 );
+  }
+
+  return true;
+}
+
+static_assert( test( (unsigned char)0 ) );
+static_assert( test( (unsigned short)0 ) );
+static_assert( test( (unsigned int)0 ) );
+static_assert( test( (unsigned long)0 ) );
+static_assert( test( (unsigned long long)0 ) );
+
+// std::countl_one(T) shall not participate in overload resolution
+// unless T is an unsigned integer type.
+struct X { constexpr bool did_not_match() { return true; } };
+constexpr X test(...) { return X{}; }
+static_assert( test( (bool)0 ).did_not_match() );
+static_assert( test( (char)0 ).did_not_match() );
+static_assert( test( (int)0 ).did_not_match() );
+static_assert( test( (char16_t)0 ).did_not_match() );
+static_assert( test( (float)0 ).did_not_match() );
+static_assert( test( (void*)0 ).did_not_match() );
+static_assert( test( X{} ).did_not_match() );
+enum E : unsigned { e };
+static_assert( test( e ).did_not_match() );
+
+#ifndef __STRICT_ANSI__
+#include <cstddef>
+constexpr int bits = std::numeric_limits<unsigned char>::digits;
+static_assert( std::countl_one(std::byte{0}) == 0 );
+static_assert( std::countl_one(~std::byte{0}) == bits );
+static_assert( std::countl_one(~std::byte{0} ^ std::byte{7}) == bits - 3 );
+#else
+static_assert( test( (std::byte)0 ).did_not_match() );
+#endif
+
+#if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
+static_assert( test( (unsigned __int128)0 ) );
+static_assert( test( (__int128)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_0)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_1)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_2)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
+#endif
diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/countl_zero.cc b/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/countl_zero.cc
new file mode 100644
index 00000000000..7d619a7d697
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/countl_zero.cc
@@ -0,0 +1,104 @@ 
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <bit>
+
+template<typename UInt>
+constexpr auto
+test(UInt x)
+-> decltype(std::countl_zero(x))
+{
+  static_assert( noexcept(std::countl_zero(x)) );
+
+  constexpr unsigned digits = std::numeric_limits<UInt>::digits;
+
+  static_assert( std::countl_zero((UInt)0) == digits );
+  static_assert( std::countl_zero((UInt)-1) == 0 );
+
+  static_assert( std::countl_zero((UInt)1) == digits - 1 );
+  static_assert( std::countl_zero((UInt)3) == digits - 2 );
+  static_assert( std::countl_zero((UInt)0b0101'1010) == digits - 7 );
+
+  if constexpr (std::numeric_limits<UInt>::digits > 8)
+  {
+    static_assert( std::countl_zero((UInt)(1u << 8)) == digits - 9 );
+    static_assert( std::countl_zero((UInt)(3u << 9)) == digits - 11 );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits > 64)
+  {
+    static_assert( std::countl_zero((UInt)3 << 70) == digits - 72 );
+  }
+
+  return true;
+}
+
+static_assert( test( (unsigned char)0 ) );
+static_assert( test( (unsigned short)0 ) );
+static_assert( test( (unsigned int)0 ) );
+static_assert( test( (unsigned long)0 ) );
+static_assert( test( (unsigned long long)0 ) );
+
+// std::countl_zero(T) shall not participate in overload resolution
+// unless T is an unsigned integer type.
+struct X { constexpr bool did_not_match() { return true; } };
+constexpr X test(...) { return X{}; }
+static_assert( test( (bool)0 ).did_not_match() );
+static_assert( test( (char)0 ).did_not_match() );
+static_assert( test( (int)0 ).did_not_match() );
+static_assert( test( (char16_t)0 ).did_not_match() );
+static_assert( test( (float)0 ).did_not_match() );
+static_assert( test( (void*)0 ).did_not_match() );
+static_assert( test( X{} ).did_not_match() );
+enum E : unsigned { e };
+static_assert( test( e ).did_not_match() );
+
+#ifndef __STRICT_ANSI__
+#include <cstddef>
+constexpr int bits = std::numeric_limits<unsigned char>::digits;
+static_assert( std::countl_zero(std::byte{0}) == bits );
+static_assert( std::countl_zero(std::byte{0x01}) == bits - 1 );
+static_assert( std::countl_zero(std::byte{0x02}) == bits - 2 );
+static_assert( std::countl_zero(std::byte{0x03}) == bits - 2 );
+static_assert( std::countl_zero(std::byte{0x30}) == 2 );
+static_assert( std::countl_zero(std::byte{0x40}) == 1 );
+static_assert( std::countl_zero(std::byte{0x41}) == 1 );
+#else
+static_assert( test( (std::byte)0 ).did_not_match() );
+#endif
+
+#if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
+static_assert( test( (unsigned __int128)0 ) );
+static_assert( test( (__int128)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_0)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_1)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_2)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
+#endif
diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/countr_one.cc b/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/countr_one.cc
new file mode 100644
index 00000000000..bb1ddd6234d
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/countr_one.cc
@@ -0,0 +1,106 @@ 
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <bit>
+
+template<typename UInt>
+constexpr auto
+test(UInt x)
+-> decltype(std::countr_one(x))
+{
+  static_assert( noexcept(std::countr_one(x)) );
+
+  constexpr unsigned digits = std::numeric_limits<UInt>::digits;
+
+  static_assert( std::countr_one((UInt)0) == 0 );
+  static_assert( std::countr_one((UInt)-1) == digits );
+  static_assert( std::countr_one((UInt)127) == 7 );
+
+  static_assert( std::countr_one((UInt)1) == 1 );
+  static_assert( std::countr_one((UInt)3) == 2 );
+  static_assert( std::countr_one((UInt)0x1f) == 5 );
+  static_assert( std::countr_one((UInt)((UInt)-1 ^ 0x10)) == 4 );
+
+  if constexpr (std::numeric_limits<UInt>::digits > 8)
+  {
+    static_assert( std::countr_one((UInt)((UInt)-1 ^ 0x100)) == 8 );
+    static_assert( std::countr_one((UInt)((UInt)-1 ^ 0x101)) == 0 );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits > 64)
+  {
+    static_assert( std::countr_one((UInt)-1 ^ ((UInt)1 << 70)) == 70 );
+  }
+
+  return true;
+}
+
+static_assert( test( (unsigned char)0 ) );
+static_assert( test( (unsigned short)0 ) );
+static_assert( test( (unsigned int)0 ) );
+static_assert( test( (unsigned long)0 ) );
+static_assert( test( (unsigned long long)0 ) );
+
+// std::countr_one(T) shall not participate in overload resolution
+// unless T is an unsigned integer type.
+struct X { constexpr bool did_not_match() { return true; } };
+constexpr X test(...) { return X{}; }
+static_assert( test( (bool)0 ).did_not_match() );
+static_assert( test( (char)0 ).did_not_match() );
+static_assert( test( (int)0 ).did_not_match() );
+static_assert( test( (char16_t)0 ).did_not_match() );
+static_assert( test( (float)0 ).did_not_match() );
+static_assert( test( (void*)0 ).did_not_match() );
+static_assert( test( X{} ).did_not_match() );
+enum E : unsigned { e };
+static_assert( test( e ).did_not_match() );
+
+#ifndef __STRICT_ANSI__
+#include <cstddef>
+constexpr int bits = std::numeric_limits<unsigned char>::digits;
+static_assert( std::countr_one(std::byte{0}) == 0 );
+static_assert( std::countr_one(std::byte{0x01}) == 1 );
+static_assert( std::countr_one(std::byte{0x02}) == 0 );
+static_assert( std::countr_one(std::byte{0x03}) == 2 );
+static_assert( std::countr_one(std::byte{0x30}) == 0 );
+static_assert( std::countr_one(std::byte{0x0f}) == 4 );
+static_assert( std::countr_one(std::byte{0xff}) == 8 );
+#else
+static_assert( test( (std::byte)0 ).did_not_match() );
+#endif
+
+#if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
+static_assert( test( (unsigned __int128)0 ) );
+static_assert( test( (__int128)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_0)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_1)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_2)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
+#endif
diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/countr_zero.cc b/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/countr_zero.cc
new file mode 100644
index 00000000000..d3b63aa4984
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/bit/bitops.count/countr_zero.cc
@@ -0,0 +1,105 @@ 
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <bit>
+
+template<typename UInt>
+constexpr auto
+test(UInt x)
+-> decltype(std::countr_zero(x))
+{
+  static_assert( noexcept(std::countr_zero(x)) );
+
+  constexpr unsigned digits = std::numeric_limits<UInt>::digits;
+
+  static_assert( std::countr_zero((UInt)0) == digits );
+  static_assert( std::countr_zero((UInt)-1) == 0 );
+  static_assert( std::countr_zero((UInt)127) == 0 );
+
+  static_assert( std::countr_zero((UInt)1) == 0 );
+  static_assert( std::countr_zero((UInt)2) == 1 );
+  static_assert( std::countr_zero((UInt)0x70) == 4 );
+
+  if constexpr (std::numeric_limits<UInt>::digits > 8)
+  {
+    static_assert( std::countr_zero((UInt)(1u << 8)) == 8 );
+    static_assert( std::countr_zero((UInt)(4u << 9)) == 11 );
+  }
+
+  if constexpr (std::numeric_limits<UInt>::digits > 64)
+  {
+    static_assert( std::countr_zero((UInt)2 << 70) == 71 );
+  }
+
+  return true;
+}
+
+static_assert( test( (unsigned char)0 ) );
+static_assert( test( (unsigned short)0 ) );
+static_assert( test( (unsigned int)0 ) );
+static_assert( test( (unsigned long)0 ) );
+static_assert( test( (unsigned long long)0 ) );
+
+// std::countr_zero(T) shall not participate in overload resolution
+// unless T is an unsigned integer type.
+struct X { constexpr bool did_not_match() { return true; } };
+constexpr X test(...) { return X{}; }
+static_assert( test( (bool)0 ).did_not_match() );
+static_assert( test( (char)0 ).did_not_match() );
+static_assert( test( (int)0 ).did_not_match() );
+static_assert( test( (char16_t)0 ).did_not_match() );
+static_assert( test( (float)0 ).did_not_match() );
+static_assert( test( (void*)0 ).did_not_match() );
+static_assert( test( X{} ).did_not_match() );
+enum E : unsigned { e };
+static_assert( test( e ).did_not_match() );
+
+#ifndef __STRICT_ANSI__
+#include <cstddef>
+constexpr int bits = std::numeric_limits<unsigned char>::digits;
+static_assert( std::countr_zero(std::byte{0}) == bits );
+static_assert( std::countr_zero(std::byte{0x01}) == 0 );
+static_assert( std::countr_zero(std::byte{0x02}) == 1 );
+static_assert( std::countr_zero(std::byte{0x03}) == 0 );
+static_assert( std::countr_zero(std::byte{0x30}) == 4 );
+static_assert( std::countr_zero(std::byte{0x40}) == 6 );
+static_assert( std::countr_zero(std::byte{0x41}) == 0 );
+#else
+static_assert( test( (std::byte)0 ).did_not_match() );
+#endif
+
+#if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
+static_assert( test( (unsigned __int128)0 ) );
+static_assert( test( (__int128)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_0)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_1)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_2)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
+#endif
diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bitops.rot/rotl.cc b/libstdc++-v3/testsuite/26_numerics/bit/bitops.rot/rotl.cc
new file mode 100644
index 00000000000..a44da00040c
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/bit/bitops.rot/rotl.cc
@@ -0,0 +1,119 @@ 
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <bit>
+
+template<typename UInt>
+constexpr auto
+test(UInt x)
+-> decltype(std::rotl(x, 0u))
+{
+  static_assert( noexcept(std::rotl(x, 0u)) );
+
+  constexpr unsigned digits = std::numeric_limits<UInt>::digits;
+
+  static_assert( std::rotl((UInt)0, 0) == 0 );
+  static_assert( std::rotl((UInt)0, 1) == 0 );
+  static_assert( std::rotl((UInt)0, 4) == 0 );
+  static_assert( std::rotl((UInt)0, 8) == 0 );
+  static_assert( std::rotl((UInt)-1, 0) == (UInt)-1 );
+  static_assert( std::rotl((UInt)-1, 1) == (UInt)-1 );
+  static_assert( std::rotl((UInt)-1, 4) == (UInt)-1 );
+  static_assert( std::rotl((UInt)-1, 8) == (UInt)-1 );
+
+  static_assert( std::rotl((UInt)1, 0) == (UInt)1 << 0 );
+  static_assert( std::rotl((UInt)1, 1) == (UInt)1 << 1 );
+  static_assert( std::rotl((UInt)1, 4) == (UInt)1 << 4 );
+  static_assert( std::rotl((UInt)1, digits) == (UInt)1 );
+  static_assert( std::rotl((UInt)7, digits) == (UInt)7 );
+  static_assert( std::rotl((UInt)6, digits - 1) == (UInt)3 );
+  static_assert( std::rotl((UInt)3, 6) == (UInt)3 << 6 );
+
+  static_assert( std::rotl((UInt)0b0110'1100, 1) == 0b1101'1000 );
+  static_assert( std::rotl((UInt)0b0110'1100, digits - 1) == 0b0011'0110 );
+
+  static_assert( std::rotl((UInt)0x01, 0 ) == 0x01 );
+  static_assert( std::rotl((UInt)0x10, 0 ) == 0x10 );
+  static_assert( std::rotl((UInt)0x10, 1 ) == 0x20 );
+  static_assert( std::rotl((UInt)0x10, 2 ) == 0x40 );
+  static_assert( std::rotl((UInt)0x10, 3 ) == 0x80 );
+  static_assert( std::rotl((UInt)0x11, 1 ) == 0x22 );
+  static_assert( std::rotl((UInt)0x11, 2 ) == 0x44 );
+
+  if constexpr (std::numeric_limits<UInt>::digits > 8)
+  {
+    static_assert( std::rotl((UInt)0b0011'0111, 3) == 0b1'1011'1000 );
+    static_assert( std::rotl((UInt)0b1010'0101, 4) == 0b1010'0101'0000 );
+  }
+
+  return true;
+}
+
+static_assert( test( (unsigned char)0 ) );
+static_assert( test( (unsigned short)0 ) );
+static_assert( test( (unsigned int)0 ) );
+static_assert( test( (unsigned long)0 ) );
+static_assert( test( (unsigned long long)0 ) );
+
+// std::rotl(T) shall not participate in overload resolution
+// unless T is an unsigned integer type.
+struct X { constexpr bool did_not_match() { return true; } };
+constexpr X test(...) { return X{}; }
+static_assert( test( (bool)0 ).did_not_match() );
+static_assert( test( (char)0 ).did_not_match() );
+static_assert( test( (int)0 ).did_not_match() );
+static_assert( test( (char16_t)0 ).did_not_match() );
+static_assert( test( (float)0 ).did_not_match() );
+static_assert( test( (void*)0 ).did_not_match() );
+static_assert( test( X{} ).did_not_match() );
+enum E : unsigned { e };
+static_assert( test( e ).did_not_match() );
+
+#ifndef __STRICT_ANSI__
+#include <cstddef>
+static_assert( std::rotl(std::byte{0}, 4) == std::byte{0} );
+static_assert( std::rotl(std::byte{0x01}, 4) == std::byte{0x10} );
+static_assert( std::rotl(std::byte{0x02}, 3) == std::byte{0x10} );
+static_assert( std::rotl(std::byte{0x03}, 2) == std::byte{0x0c} );
+static_assert( std::rotl(std::byte{0x30}, 2) == std::byte{0xc0} );
+static_assert( std::rotl(std::byte{0x40}, 1) == std::byte{0x80} );
+static_assert( std::rotl(std::byte{0x41}, 9) == std::byte{0x82} );
+#else
+static_assert( test( (std::byte)0 ).did_not_match() );
+#endif
+
+#if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
+static_assert( test( (unsigned __int128)0 ) );
+static_assert( test( (__int128)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_0)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_1)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_2)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
+#endif
diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bitops.rot/rotr.cc b/libstdc++-v3/testsuite/26_numerics/bit/bitops.rot/rotr.cc
new file mode 100644
index 00000000000..c4a17689f79
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/bit/bitops.rot/rotr.cc
@@ -0,0 +1,119 @@ 
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <bit>
+
+template<typename UInt>
+constexpr auto
+test(UInt x)
+-> decltype(std::rotr(x, 0u))
+{
+  static_assert( noexcept(std::rotr(x, 0u)) );
+
+  constexpr unsigned digits = std::numeric_limits<UInt>::digits;
+
+  static_assert( std::rotr((UInt)0, 0) == 0 );
+  static_assert( std::rotr((UInt)0, 1) == 0 );
+  static_assert( std::rotr((UInt)0, 4) == 0 );
+  static_assert( std::rotr((UInt)0, 8) == 0 );
+  static_assert( std::rotr((UInt)-1, 0) == (UInt)-1 );
+  static_assert( std::rotr((UInt)-1, 1) == (UInt)-1 );
+  static_assert( std::rotr((UInt)-1, 4) == (UInt)-1 );
+  static_assert( std::rotr((UInt)-1, 8) == (UInt)-1 );
+
+  static_assert( std::rotr((UInt)128, 0) == (UInt)128 >> 0 );
+  static_assert( std::rotr((UInt)128, 1) == (UInt)128 >> 1 );
+  static_assert( std::rotr((UInt)128, 4) == (UInt)128 >> 4 );
+  static_assert( std::rotr((UInt)1, digits) == (UInt)1 );
+  static_assert( std::rotr((UInt)7, digits) == (UInt)7 );
+  static_assert( std::rotr((UInt)6, digits - 1) == (UInt)12 );
+  static_assert( std::rotr((UInt)36, digits - 2) == (UInt)144 );
+
+  static_assert( std::rotr((UInt)0b0110'1100, 1) == 0b0011'0110 );
+  static_assert( std::rotr((UInt)0b0110'1100, digits - 1) == 0b1101'1000 );
+
+  static_assert( std::rotr((UInt)0x01, 0 ) == 0x01 );
+  static_assert( std::rotr((UInt)0x10, 0 ) == 0x10 );
+  static_assert( std::rotr((UInt)0x10, 1 ) == 0x08 );
+  static_assert( std::rotr((UInt)0x10, 2 ) == 0x04 );
+  static_assert( std::rotr((UInt)0x10, 3 ) == 0x02 );
+  static_assert( std::rotr((UInt)0x11, digits - 1 ) == 0x22 );
+  static_assert( std::rotr((UInt)0x11, digits - 2 ) == 0x44 );
+
+  if constexpr (std::numeric_limits<UInt>::digits > 8)
+  {
+    static_assert( std::rotr((UInt)0b0011'0111, 3)
+			      == (0b0110 | ((UInt)0b0111 << digits - 3)) );
+    static_assert( std::rotr((UInt)0b1010'0101, 4)
+			      == (0b1010 | ((UInt)0b0101 << digits - 4)) );
+  }
+
+  return true;
+}
+
+static_assert( test( (unsigned char)0 ) );
+static_assert( test( (unsigned short)0 ) );
+static_assert( test( (unsigned int)0 ) );
+static_assert( test( (unsigned long)0 ) );
+static_assert( test( (unsigned long long)0 ) );
+
+// std::rotr(T) shall not participate in overload resolution
+// unless T is an unsigned integer type.
+struct X { constexpr bool did_not_match() { return true; } };
+constexpr X test(...) { return X{}; }
+static_assert( test( (bool)0 ).did_not_match() );
+static_assert( test( (char)0 ).did_not_match() );
+static_assert( test( (int)0 ).did_not_match() );
+static_assert( test( (char16_t)0 ).did_not_match() );
+static_assert( test( (float)0 ).did_not_match() );
+static_assert( test( (void*)0 ).did_not_match() );
+static_assert( test( X{} ).did_not_match() );
+enum E : unsigned { e };
+static_assert( test( e ).did_not_match() );
+
+#ifndef __STRICT_ANSI__
+#include <cstddef>
+static_assert( std::rotr(std::byte{0}, 4) == std::byte{0} );
+static_assert( std::rotr(std::byte{0x01}, 4) == std::byte{0x10} );
+static_assert( std::rotr(std::byte{0x02}, 3) == std::byte{0x40} );
+static_assert( std::rotr(std::byte{0x03}, 2) == std::byte{0xc0} );
+static_assert( std::rotr(std::byte{0x30}, 2) == std::byte{0x0c} );
+static_assert( std::rotr(std::byte{0x40}, 1) == std::byte{0x20} );
+static_assert( std::rotr(std::byte{0x41}, 9) == std::byte{0xa0} );
+#else
+static_assert( test( (std::byte)0 ).did_not_match() );
+#endif
+
+#if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
+static_assert( test( (unsigned __int128)0 ) );
+static_assert( test( (__int128)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_0)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_1)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_2)
+static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
+static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
+#endif