diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2017-03-09 15:42:02 +0000 |
---|---|---|
committer | Jonathan Wakely <redi@gcc.gnu.org> | 2017-03-09 15:42:02 +0000 |
commit | e0472d7e8cefd8f4eed658424b8283fcd8ca4b15 (patch) | |
tree | bd1fef6b0530b3691d23d9b68d125ad7f1405b70 | |
parent | 1f1fd3e2721cf68b2fe8b9516f260aee89872f60 (diff) | |
download | gcc-e0472d7e8cefd8f4eed658424b8283fcd8ca4b15.tar.gz |
Define std::byte for C++17 (P0298R3)
* doc/xml/manual/status_cxx2017.xml: Document std::byte support.
* include/c_global/cstddef (std::byte): Define for C++17.
* testsuite/18_support/byte/global_neg.cc: New test.
* testsuite/18_support/byte/ops.cc: New test.
* testsuite/18_support/byte/requirements.cc: New test.
From-SVN: r246005
-rw-r--r-- | libstdc++-v3/ChangeLog | 8 | ||||
-rw-r--r-- | libstdc++-v3/doc/xml/manual/status_cxx2017.xml | 25 | ||||
-rw-r--r-- | libstdc++-v3/include/c_global/cstddef | 129 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/18_support/byte/global_neg.cc | 24 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/18_support/byte/ops.cc | 224 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/18_support/byte/requirements.cc | 27 |
6 files changed, 425 insertions, 12 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index ef48d8767cf..dabac4ad128 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2017-03-09 Jonathan Wakely <jwakely@redhat.com> + + * doc/xml/manual/status_cxx2017.xml: Document std::byte support. + * include/c_global/cstddef (std::byte): Define for C++17. + * testsuite/18_support/byte/global_neg.cc: New test. + * testsuite/18_support/byte/ops.cc: New test. + * testsuite/18_support/byte/requirements.cc: New test. + 2017-03-05 Jonathan Wakely <jwakely@redhat.com> * doc/xml/manual/status_cxx2017.xml: Document P0156R2 status. diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml index 1053f2dd84b..e0e4e8e7c80 100644 --- a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml +++ b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml @@ -460,18 +460,6 @@ Feature-testing recommendations for C++</link>. </row> <row> - <?dbhtml bgcolor="#C8B0B0" ?> - <entry> Ordered by default </entry> - <entry> - <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0181r1.html"> - P0181R1 - </link> - </entry> - <entry align="center"> No </entry> - <entry><code> __cpp_lib_default_order >= 201606</code></entry> - </row> - - <row> <entry> Polishing <code><chrono></code> </entry> <entry> <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0092r1.html"> @@ -729,6 +717,19 @@ Feature-testing recommendations for C++</link>. <entry><code> __cpp_lib_scoped_lock >= 201703 </code></entry> </row> + <row> + <?dbhtml bgcolor="#C8B0B0" ?> + <entry> </entry> byte type definition + <entry> + <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0298r3.pdf"> + P0298R3 + </link> + </entry> + <entry align="center"> 7 </entry> + <entry><code> ??? </code></entry> + </row> + + </tbody> </tgroup> </table> diff --git a/libstdc++-v3/include/c_global/cstddef b/libstdc++-v3/include/c_global/cstddef index a571e80c2f6..09754ee45da 100644 --- a/libstdc++-v3/include/c_global/cstddef +++ b/libstdc++-v3/include/c_global/cstddef @@ -57,4 +57,133 @@ namespace std } #endif +#if __cplusplus > 201402L +namespace std +{ + /// std::byte + enum class byte : unsigned char {}; + + template<typename _IntegerType> struct __byte_operand; + template<> struct __byte_operand<bool> { using __type = byte; }; + template<> struct __byte_operand<char> { using __type = byte; }; + template<> struct __byte_operand<signed char> { using __type = byte; }; + template<> struct __byte_operand<unsigned char> { using __type = byte; }; +#ifdef _GLIBCXX_USE_WCHAR_T + template<> struct __byte_operand<wchar_t> { using __type = byte; }; +#endif + template<> struct __byte_operand<char16_t> { using __type = byte; }; + template<> struct __byte_operand<char32_t> { using __type = byte; }; + template<> struct __byte_operand<short> { using __type = byte; }; + template<> struct __byte_operand<unsigned short> { using __type = byte; }; + template<> struct __byte_operand<int> { using __type = byte; }; + template<> struct __byte_operand<unsigned int> { using __type = byte; }; + template<> struct __byte_operand<long> { using __type = byte; }; + template<> struct __byte_operand<unsigned long> { using __type = byte; }; + template<> struct __byte_operand<long long> { using __type = byte; }; + template<> struct __byte_operand<unsigned long long> { using __type = byte; }; +#if defined(__GLIBCXX_TYPE_INT_N_0) + template<> struct __byte_operand<__GLIBCXX_TYPE_INT_N_0> + { using __type = byte; }; + template<> struct __byte_operand<unsigned __GLIBCXX_TYPE_INT_N_0> + { using __type = byte; }; +#endif +#if defined(__GLIBCXX_TYPE_INT_N_1) + template<> struct __byte_operand<__GLIBCXX_TYPE_INT_N_1> + { using __type = byte; }; + template<> struct __byte_operand<unsigned __GLIBCXX_TYPE_INT_N_1> + { using __type = byte; }; +#endif +#if defined(__GLIBCXX_TYPE_INT_N_2) + template<> struct __byte_operand<__GLIBCXX_TYPE_INT_N_2> + { using __type = byte; }; + template<> struct __byte_operand<unsigned __GLIBCXX_TYPE_INT_N_2> + { using __type = byte; }; +#endif + template<typename _IntegerType> + struct __byte_operand<const _IntegerType> + : __byte_operand<_IntegerType> { }; + template<typename _IntegerType> + struct __byte_operand<volatile _IntegerType> + : __byte_operand<_IntegerType> { }; + template<typename _IntegerType> + struct __byte_operand<const volatile _IntegerType> + : __byte_operand<_IntegerType> { }; + + template<typename _IntegerType> + using __byte_op_t = typename __byte_operand<_IntegerType>::__type; + + template<typename _IntegerType> + constexpr __byte_op_t<_IntegerType>& + operator<<=(byte& __b, _IntegerType __shift) noexcept + { return __b = byte(static_cast<unsigned char>(__b) << __shift); } + + template<typename _IntegerType> + constexpr __byte_op_t<_IntegerType> + operator<<(byte __b, _IntegerType __shift) noexcept + { return byte(static_cast<unsigned char>(__b) << __shift); } + + template<typename _IntegerType> + constexpr __byte_op_t<_IntegerType>& + operator>>=(byte& __b, _IntegerType __shift) noexcept + { return __b = byte(static_cast<unsigned char>(__b) >> __shift); } + + template<typename _IntegerType> + constexpr __byte_op_t<_IntegerType> + operator>>(byte __b, _IntegerType __shift) noexcept + { return byte(static_cast<unsigned char>(__b) >> __shift); } + + constexpr byte& + operator|=(byte& __l, byte __r) noexcept + { + return __l = + byte(static_cast<unsigned char>(__l) | static_cast<unsigned char>(__r)); + } + + constexpr byte + operator|(byte __l, byte __r) noexcept + { + return + byte(static_cast<unsigned char>(__l) | static_cast<unsigned char>(__r)); + } + + constexpr byte& + operator&=(byte& __l, byte __r) noexcept + { + return __l = + byte(static_cast<unsigned char>(__l) & static_cast<unsigned char>(__r)); + } + + constexpr byte + operator&(byte __l, byte __r) noexcept + { + return + byte(static_cast<unsigned char>(__l) & static_cast<unsigned char>(__r)); + } + + constexpr byte& + operator^=(byte& __l, byte __r) noexcept + { + return __l = + byte(static_cast<unsigned char>(__l) ^ static_cast<unsigned char>(__r)); + } + + constexpr byte + operator^(byte __l, byte __r) noexcept + { + return + byte(static_cast<unsigned char>(__l) ^ static_cast<unsigned char>(__r)); + } + + constexpr byte + operator~(byte __b) noexcept + { return byte(~static_cast<unsigned char>(__b)); } + + template<typename _IntegerType> + constexpr _IntegerType + to_integer(__byte_op_t<_IntegerType> __b) noexcept + { return _IntegerType(__b); } + +} // namespace std +#endif + #endif // _GLIBCXX_CSTDDEF diff --git a/libstdc++-v3/testsuite/18_support/byte/global_neg.cc b/libstdc++-v3/testsuite/18_support/byte/global_neg.cc new file mode 100644 index 00000000000..722f65860f7 --- /dev/null +++ b/libstdc++-v3/testsuite/18_support/byte/global_neg.cc @@ -0,0 +1,24 @@ +// Copyright (C) 2017 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++17" } +// { dg-do compile { target c++1z } } + +#include <stddef.h> + +byte b; // { dg-error "does not name a type" } +using std::byte; // { dg-error "has not been declared" } diff --git a/libstdc++-v3/testsuite/18_support/byte/ops.cc b/libstdc++-v3/testsuite/18_support/byte/ops.cc new file mode 100644 index 00000000000..6573bd9b1e3 --- /dev/null +++ b/libstdc++-v3/testsuite/18_support/byte/ops.cc @@ -0,0 +1,224 @@ +// Copyright (C) 2017 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++17" } +// { dg-do compile { target c++1z } } + +#include <cstddef> + +constexpr bool is_byte(std::byte) { return true; } +template<typename T> constexpr bool is_byte(T) { return false; } + +template<typename T> +constexpr bool test_lshift_assign(unsigned char c, T t) +{ + std::byte b{c}; + b <<= t; + return b == std::byte(c << t); +} + +static_assert( test_lshift_assign(0, 1) ); +static_assert( test_lshift_assign(0, 1u) ); +static_assert( test_lshift_assign(0, 1ll) ); +static_assert( test_lshift_assign(4, 1) ); +static_assert( test_lshift_assign(9, 2u) ); +static_assert( test_lshift_assign(16, 3ll) ); +static_assert( test_lshift_assign(127, 1) ); +static_assert( test_lshift_assign(255, 2u) ); +static_assert( test_lshift_assign(63, 3ll) ); + +template<typename T> +constexpr bool test_lshift(unsigned char c, T t) +{ + const std::byte b{c}; + const std::byte b2 = b << t; + return b2 == std::byte(c << t); +} + +static_assert( test_lshift(0, 1) ); +static_assert( test_lshift(0, 1u) ); +static_assert( test_lshift(0, 1ll) ); +static_assert( test_lshift(4, 1) ); +static_assert( test_lshift(9, 2u) ); +static_assert( test_lshift(16, 3ll) ); +static_assert( test_lshift(127, 1) ); +static_assert( test_lshift(255, 2u) ); +static_assert( test_lshift(63, 3ll) ); + +template<typename T> +constexpr bool test_rshift_assign(unsigned char c, T t) +{ + std::byte b{c}; + b >>= t; + return b == std::byte(c >> t); +} + +static_assert( test_rshift_assign(0, 1) ); +static_assert( test_rshift_assign(0, 1u) ); +static_assert( test_rshift_assign(0, 1ll) ); +static_assert( test_rshift_assign(4, 1) ); +static_assert( test_rshift_assign(9, 2u) ); +static_assert( test_rshift_assign(16, 3ll) ); +static_assert( test_rshift_assign(127, 1) ); +static_assert( test_rshift_assign(255, 2u) ); +static_assert( test_rshift_assign(63, 3ll) ); + +template<typename T> +constexpr bool test_rshift(unsigned char c, T t) +{ + const std::byte b{c}; + const std::byte b2 = b >> t; + return b2 == std::byte(c >> t); +} + +static_assert( test_rshift(0, 1) ); +static_assert( test_rshift(0, 1u) ); +static_assert( test_rshift(0, 1ll) ); +static_assert( test_rshift(4, 1) ); +static_assert( test_rshift(9, 2u) ); +static_assert( test_rshift(16, 3ll) ); +static_assert( test_rshift(127, 1) ); +static_assert( test_rshift(255, 2u) ); +static_assert( test_rshift(63, 3ll) ); + +constexpr bool test_or_assign(unsigned char l, unsigned char r) +{ + std::byte b{l}; + b |= std::byte{r}; + return b == std::byte(l | r); +} + +static_assert( test_or_assign(0, 1) ); +static_assert( test_or_assign(4, 1) ); +static_assert( test_or_assign(9, 2) ); +static_assert( test_or_assign(16, 3) ); +static_assert( test_or_assign(63, 3) ); +static_assert( test_or_assign(127, 1) ); +static_assert( test_or_assign(255, 2) ); + +constexpr bool test_or(unsigned char l, unsigned char r) +{ + const std::byte b1{l}; + const std::byte b2{r}; + return (b1 | b2) == std::byte(l | r); +} + +static_assert( test_or(0, 1) ); +static_assert( test_or(0, 1u) ); +static_assert( test_or(0, 1ll) ); +static_assert( test_or(4, 1) ); +static_assert( test_or(9, 2u) ); +static_assert( test_or(16, 3ll) ); +static_assert( test_or(127, 1) ); +static_assert( test_or(255, 2u) ); +static_assert( test_or(63, 3ll) ); + +constexpr bool test_and_assign(unsigned char l, unsigned char r) +{ + std::byte b{l}; + b &= std::byte{r}; + return b == std::byte(l & r); +} + +static_assert( test_and_assign(0, 1) ); +static_assert( test_and_assign(0, 1u) ); +static_assert( test_and_assign(0, 1ll) ); +static_assert( test_and_assign(4, 1) ); +static_assert( test_and_assign(9, 2u) ); +static_assert( test_and_assign(16, 3ll) ); +static_assert( test_and_assign(127, 1) ); +static_assert( test_and_assign(255, 2u) ); +static_assert( test_and_assign(63, 3ll) ); + +constexpr bool test_and(unsigned char l, unsigned char r) +{ + const std::byte b1{l}; + const std::byte b2{r}; + return (b1 & b2) == std::byte(l & r); +} + +static_assert( test_and(0, 1) ); +static_assert( test_and(0, 1u) ); +static_assert( test_and(0, 1ll) ); +static_assert( test_and(4, 1) ); +static_assert( test_and(9, 2u) ); +static_assert( test_and(16, 3ll) ); +static_assert( test_and(127, 1) ); +static_assert( test_and(255, 2u) ); +static_assert( test_and(63, 3ll) ); + +constexpr bool test_xor_assign(unsigned char l, unsigned char r) +{ + std::byte b{l}; + b ^= std::byte{r}; + return b == std::byte(l ^ r); +} + +static_assert( test_xor_assign(0, 1) ); +static_assert( test_xor_assign(0, 1u) ); +static_assert( test_xor_assign(0, 1ll) ); +static_assert( test_xor_assign(4, 1) ); +static_assert( test_xor_assign(9, 2u) ); +static_assert( test_xor_assign(16, 3ll) ); +static_assert( test_xor_assign(127, 1) ); +static_assert( test_xor_assign(255, 2u) ); +static_assert( test_xor_assign(63, 3ll) ); + +constexpr bool test_xor(unsigned char l, unsigned char r) +{ + const std::byte b1{l}; + const std::byte b2{r}; + return (b1 ^ b2) == std::byte(l ^ r); +} + +static_assert( test_xor(0, 1) ); +static_assert( test_xor(0, 1u) ); +static_assert( test_xor(0, 1ll) ); +static_assert( test_xor(4, 1) ); +static_assert( test_xor(9, 2u) ); +static_assert( test_xor(16, 3ll) ); +static_assert( test_xor(127, 1) ); +static_assert( test_xor(255, 2u) ); +static_assert( test_xor(63, 3ll) ); + +constexpr bool test_complement(unsigned char c) +{ + const std::byte b{c}; + return ~b == std::byte(~c); +} + +static_assert( test_complement(0) ); +static_assert( test_complement(4) ); +static_assert( test_complement(9) ); +static_assert( test_complement(16) ); +static_assert( test_complement(63) ); +static_assert( test_complement(127) ); +static_assert( test_complement(255) ); + +template<typename T> +constexpr bool test_to_integer(unsigned char c) +{ + std::byte b{c}; + return std::to_integer<T>(b) == T(c); +} + +static_assert( test_to_integer<int>(0) ); +static_assert( test_to_integer<int>(255) ); +static_assert( test_to_integer<signed char>(255) ); +static_assert( test_to_integer<unsigned>(0) ); +static_assert( test_to_integer<unsigned>(255) ); + diff --git a/libstdc++-v3/testsuite/18_support/byte/requirements.cc b/libstdc++-v3/testsuite/18_support/byte/requirements.cc new file mode 100644 index 00000000000..da5fc91a12a --- /dev/null +++ b/libstdc++-v3/testsuite/18_support/byte/requirements.cc @@ -0,0 +1,27 @@ +// Copyright (C) 2017 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++17" } +// { dg-do compile { target c++1z } } + +#include <cstddef> + +static_assert( sizeof(std::byte) == sizeof(unsigned char) ); +static_assert( alignof(std::byte) == alignof(unsigned char) ); + +// Use the built-in to avoid depending on <type_traits> +__underlying_type(std::byte)* p = (unsigned char*)nullptr; |