From: Jonathan Wakely Date: Thu, 9 Mar 2017 15:42:02 +0000 (+0000) Subject: Define std::byte for C++17 (P0298R3) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=e0472d7e8cefd8f4eed658424b8283fcd8ca4b15;p=gcc.git 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 --- 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 + + * 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 * 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 @@ -459,18 +459,6 @@ Feature-testing recommendations for C++. __cpp_lib_has_unique_object_representations >= 201606 - - - Ordered by default - - - P0181R1 - - - No - __cpp_lib_default_order >= 201606 - - Polishing <chrono> @@ -729,6 +717,19 @@ Feature-testing recommendations for C++. __cpp_lib_scoped_lock >= 201703 + + + byte type definition + + + P0298R3 + + + 7 + ??? + + + 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 struct __byte_operand; + template<> struct __byte_operand { using __type = byte; }; + template<> struct __byte_operand { using __type = byte; }; + template<> struct __byte_operand { using __type = byte; }; + template<> struct __byte_operand { using __type = byte; }; +#ifdef _GLIBCXX_USE_WCHAR_T + template<> struct __byte_operand { using __type = byte; }; +#endif + template<> struct __byte_operand { using __type = byte; }; + template<> struct __byte_operand { using __type = byte; }; + template<> struct __byte_operand { using __type = byte; }; + template<> struct __byte_operand { using __type = byte; }; + template<> struct __byte_operand { using __type = byte; }; + template<> struct __byte_operand { using __type = byte; }; + template<> struct __byte_operand { using __type = byte; }; + template<> struct __byte_operand { using __type = byte; }; + template<> struct __byte_operand { using __type = byte; }; + template<> struct __byte_operand { 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 + { 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 + { 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 + { using __type = byte; }; +#endif + template + struct __byte_operand + : __byte_operand<_IntegerType> { }; + template + struct __byte_operand + : __byte_operand<_IntegerType> { }; + template + struct __byte_operand + : __byte_operand<_IntegerType> { }; + + template + using __byte_op_t = typename __byte_operand<_IntegerType>::__type; + + template + constexpr __byte_op_t<_IntegerType>& + operator<<=(byte& __b, _IntegerType __shift) noexcept + { return __b = byte(static_cast(__b) << __shift); } + + template + constexpr __byte_op_t<_IntegerType> + operator<<(byte __b, _IntegerType __shift) noexcept + { return byte(static_cast(__b) << __shift); } + + template + constexpr __byte_op_t<_IntegerType>& + operator>>=(byte& __b, _IntegerType __shift) noexcept + { return __b = byte(static_cast(__b) >> __shift); } + + template + constexpr __byte_op_t<_IntegerType> + operator>>(byte __b, _IntegerType __shift) noexcept + { return byte(static_cast(__b) >> __shift); } + + constexpr byte& + operator|=(byte& __l, byte __r) noexcept + { + return __l = + byte(static_cast(__l) | static_cast(__r)); + } + + constexpr byte + operator|(byte __l, byte __r) noexcept + { + return + byte(static_cast(__l) | static_cast(__r)); + } + + constexpr byte& + operator&=(byte& __l, byte __r) noexcept + { + return __l = + byte(static_cast(__l) & static_cast(__r)); + } + + constexpr byte + operator&(byte __l, byte __r) noexcept + { + return + byte(static_cast(__l) & static_cast(__r)); + } + + constexpr byte& + operator^=(byte& __l, byte __r) noexcept + { + return __l = + byte(static_cast(__l) ^ static_cast(__r)); + } + + constexpr byte + operator^(byte __l, byte __r) noexcept + { + return + byte(static_cast(__l) ^ static_cast(__r)); + } + + constexpr byte + operator~(byte __b) noexcept + { return byte(~static_cast(__b)); } + + template + 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 +// . + +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++1z } } + +#include + +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 +// . + +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++1z } } + +#include + +constexpr bool is_byte(std::byte) { return true; } +template constexpr bool is_byte(T) { return false; } + +template +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 +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 +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 +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 +constexpr bool test_to_integer(unsigned char c) +{ + std::byte b{c}; + return std::to_integer(b) == T(c); +} + +static_assert( test_to_integer(0) ); +static_assert( test_to_integer(255) ); +static_assert( test_to_integer(255) ); +static_assert( test_to_integer(0) ); +static_assert( test_to_integer(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 +// . + +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++1z } } + +#include + +static_assert( sizeof(std::byte) == sizeof(unsigned char) ); +static_assert( alignof(std::byte) == alignof(unsigned char) ); + +// Use the built-in to avoid depending on +__underlying_type(std::byte)* p = (unsigned char*)nullptr;