From: Jonathan Wakely Date: Tue, 3 Jul 2018 21:04:45 +0000 (+0100) Subject: P0556R3 Integral power-of-2 operations, P0553R2 Bit operations X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=f3e91052bd7e5af791ca720cd14e9ac9227e3801;p=gcc.git 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. From-SVN: r262360 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 8d5e849d6e3..f6c39b6361c 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,29 @@ 2018-07-03 Jonathan Wakely + P0556R3 Integral power-of-2 operations, P0553R2 Bit operations + * 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. + * include/bits/alloc_traits.h: Remove redundant preprocessor condition. 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/Makefile.in b/libstdc++-v3/include/Makefile.in index aa8a0a4afd5..1a4dbbf8716 100644 --- a/libstdc++-v3/include/Makefile.in +++ b/libstdc++-v3/include/Makefile.in @@ -324,6 +324,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 // #include // #include // #include 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 @@ +// -*- 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 +// . + +/** @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 +#include + +namespace std _GLIBCXX_VISIBILITY(default) +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION + + template + 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 + 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 + constexpr int + __countl_zero(_Tp __x) noexcept + { + using __limits = numeric_limits<_Tp>; + + if (__x == 0) + return __limits::digits; + + using __limits_ull = numeric_limits; + using __limits_ul = numeric_limits; + using __limits_u = numeric_limits; + + 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 + 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 + constexpr int + __countr_zero(_Tp __x) noexcept + { + using __limits = numeric_limits<_Tp>; + + if (__x == 0) + return __limits::digits; + + using __limits_ull = numeric_limits; + using __limits_ul = numeric_limits; + using __limits_u = numeric_limits; + + 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 + 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 + constexpr int + __popcount(_Tp __x) noexcept + { + using __limits = numeric_limits<_Tp>; + + if (__x == 0) + return 0; + + using __limits_ull = numeric_limits; + using __limits_ul = numeric_limits; + using __limits_u = numeric_limits; + + 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 + constexpr bool + __ispow2(_Tp __x) noexcept + { return std::__popcount(__x) == 1; } + + template + 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 + 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 + 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> + struct _If_is_unsigned_integer_type { }; + + template + struct _If_is_unsigned_integer_type { }; + + template + struct _If_is_unsigned_integer_type<_Tp, _Up, true> + : enable_if>, _Up> { }; + + template + using _If_is_unsigned_integer + = typename _If_is_unsigned_integer_type<_Tp, _Up>::type; + +#if ! __STRICT_ANSI__ + // [bitops.rot], rotating + + template + constexpr _If_is_unsigned_integer<_Tp> + rotl(_Tp __x, unsigned int __s) noexcept + { return std::__rotl(__x, __s); } + + template + constexpr _If_is_unsigned_integer<_Tp> + rotr(_Tp __x, unsigned int __s) noexcept + { return std::__rotr(__x, __s); } + + // [bitops.count], counting + + template + constexpr _If_is_unsigned_integer<_Tp, int> + countl_zero(_Tp __x) noexcept + { return std::__countl_zero(__x); } + + template + constexpr _If_is_unsigned_integer<_Tp, int> + countl_one(_Tp __x) noexcept + { return std::__countl_one(__x); } + + template + constexpr _If_is_unsigned_integer<_Tp, int> + countr_zero(_Tp __x) noexcept + { return std::__countr_zero(__x); } + + template + constexpr _If_is_unsigned_integer<_Tp, int> + countr_one(_Tp __x) noexcept + { return std::__countr_one(__x); } + + template + constexpr _If_is_unsigned_integer<_Tp, int> + popcount(_Tp __x) noexcept + { return std::__popcount(__x); } +#endif + + // Integral power-of-two operations + + template + constexpr _If_is_unsigned_integer<_Tp, bool> + ispow2(_Tp __x) noexcept + { return std::__ispow2(__x); } + + template + constexpr _If_is_unsigned_integer<_Tp> + ceil2(_Tp __x) noexcept + { return std::__ceil2(__x); } + + template + constexpr _If_is_unsigned_integer<_Tp> + floor2(_Tp __x) noexcept + { return std::__floor2(__x); } + + template + 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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +template +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::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::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::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 +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +template +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::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::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::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 +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +template +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::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::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::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::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::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 +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +template +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::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::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::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 +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 +// . + +// { dg-do run { target c++11 } } + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +template +constexpr auto +test(UInt x) +-> decltype(std::countl_one(x)) +{ + static_assert( noexcept(std::countl_one(x)) ); + + constexpr unsigned digits = std::numeric_limits::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::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::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 +constexpr int bits = std::numeric_limits::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 +// . + +// { dg-do run { target c++11 } } + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +template +constexpr auto +test(UInt x) +-> decltype(std::countl_zero(x)) +{ + static_assert( noexcept(std::countl_zero(x)) ); + + constexpr unsigned digits = std::numeric_limits::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::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::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 +constexpr int bits = std::numeric_limits::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 +// . + +// { dg-do run { target c++11 } } + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +template +constexpr auto +test(UInt x) +-> decltype(std::countr_one(x)) +{ + static_assert( noexcept(std::countr_one(x)) ); + + constexpr unsigned digits = std::numeric_limits::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::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::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 +constexpr int bits = std::numeric_limits::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 +// . + +// { dg-do run { target c++11 } } + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +template +constexpr auto +test(UInt x) +-> decltype(std::countr_zero(x)) +{ + static_assert( noexcept(std::countr_zero(x)) ); + + constexpr unsigned digits = std::numeric_limits::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::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::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 +constexpr int bits = std::numeric_limits::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 +// . + +// { dg-do run { target c++11 } } + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +template +constexpr auto +test(UInt x) +-> decltype(std::rotl(x, 0u)) +{ + static_assert( noexcept(std::rotl(x, 0u)) ); + + constexpr unsigned digits = std::numeric_limits::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::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 +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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +template +constexpr auto +test(UInt x) +-> decltype(std::rotr(x, 0u)) +{ + static_assert( noexcept(std::rotr(x, 0u)) ); + + constexpr unsigned digits = std::numeric_limits::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::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 +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