From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 4811C3894C17; Wed, 2 Sep 2020 15:42:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4811C3894C17 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1599061335; bh=a+Fl1baJ/M1SEhEydMuV5RHoIhqoJxpY69S1hzm9r+g=; h=From:To:Subject:Date:From; b=VVGpaUZDLD4t/ZWhNcyUP6uOjr5yH+1Oz/fL22wCD5ZHO+9LZVj4BoINxKrdmW39e 1dEeDOxEc81BneeYkyJEWxYuY/C5yNHwf2I8CLN1qVOR4ikejjYbk6q1n4iaT8JYex c0Y/FEGMJRHm9m1etu8LZ+AcZ3O4izNc8/IyZSq8= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r10-8702] libstdc++: Fix three-way comparison for std::array [PR 96851] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: 7eb76b3b1721247bc2c9ab6a41c1655158ed3411 X-Git-Newrev: 33c34c4c2466fd4fd050ed8e2d5996c35ebdeef6 Message-Id: <20200902154215.4811C3894C17@sourceware.org> Date: Wed, 2 Sep 2020 15:42:15 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 02 Sep 2020 15:42:15 -0000 https://gcc.gnu.org/g:33c34c4c2466fd4fd050ed8e2d5996c35ebdeef6 commit r10-8702-g33c34c4c2466fd4fd050ed8e2d5996c35ebdeef6 Author: Jonathan Wakely Date: Wed Sep 2 15:17:24 2020 +0100 libstdc++: Fix three-way comparison for std::array [PR 96851] The spaceship operator for std::array uses memcmp when the __is_byte trait is true, but memcmp isn't usable in constexpr contexts. Also, memcmp should only be used for unsigned byte types, because it gives the wrong answer for signed chars with negative values. We can simply check std::is_constant_evaluated() so that we don't use memcmp during constant evaluation. To fix the problem of using memcmp for inappropriate types, this patch adds new __is_memcmp_ordered and __is_memcmp_ordered_with traits. These say whether using memcmp will give the right answer for ordering operations such as lexicographical_compare and three-way comparisons. The new traits can be used in several places. Unlike the trunk commit this was backported from, this commit for the branch doesn't extend the memcmp optimisations to all unsigned integers on big endian targets. Only narrow character types and std::byte will use memcmp. libstdc++-v3/ChangeLog: PR libstdc++/96851 * include/bits/cpp_type_traits.h (__is_memcmp_ordered): New trait that says if memcmp can be used for ordering. (__is_memcmp_ordered_with): Likewise, for two types. * include/bits/ranges_algo.h (__lexicographical_compare_fn): Use new traits instead of __is_byte and __numeric_traits. * include/bits/stl_algobase.h (__lexicographical_compare_aux1) (__is_byte_iter): Likewise. * include/std/array (operator<=>): Likewise. Only use memcmp when std::is_constant_evaluated() is false. * testsuite/23_containers/array/comparison_operators/96851.cc: New test. * testsuite/23_containers/array/tuple_interface/get_neg.cc: Adjust dg-error line numbers. (cherry picked from commit 2f983fa69005b603ea1758a013b4134d5b0f24a8) Diff: --- libstdc++-v3/include/bits/cpp_type_traits.h | 44 ++++++++ libstdc++-v3/include/bits/ranges_algo.h | 5 +- libstdc++-v3/include/bits/stl_algobase.h | 7 +- libstdc++-v3/include/std/array | 22 ++-- .../array/comparison_operators/96851.cc | 119 +++++++++++++++++++++ .../23_containers/array/tuple_interface/get_neg.cc | 6 +- 6 files changed, 182 insertions(+), 21 deletions(-) diff --git a/libstdc++-v3/include/bits/cpp_type_traits.h b/libstdc++-v3/include/bits/cpp_type_traits.h index 979ad9c2c69..ca83f590eb4 100644 --- a/libstdc++-v3/include/bits/cpp_type_traits.h +++ b/libstdc++-v3/include/bits/cpp_type_traits.h @@ -482,6 +482,50 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3) : __is_nonvolatile_trivially_copyable<_Tp> { }; + // Whether memcmp can be used to determine ordering for a type + // e.g. in std::lexicographical_compare or three-way comparisons. + // True for unsigned narrow character types (and std::byte). + template::__value> + struct __is_memcmp_ordered + { + static const bool __value = _Tp(-1) > _Tp(1); // is unsigned + }; + + template + struct __is_memcmp_ordered<_Tp, false> + { + static const bool __value = false; + }; + + // Whether two types can be compared using memcmp. + template + struct __is_memcmp_ordered_with + { + static const bool __value = __is_memcmp_ordered<_Tp>::__value + && __is_memcmp_ordered<_Up>::__value; + }; + + template + struct __is_memcmp_ordered_with<_Tp, _Up, false> + { + static const bool __value = false; + }; + +#if __cplusplus >= 201703L + // std::byte can only be compared to itself, not to other types. + template<> + struct __is_memcmp_ordered_with + { static constexpr bool __value = true; }; + + template + struct __is_memcmp_ordered_with<_Tp, std::byte, _SameSize> + { static constexpr bool __value = false; }; + + template + struct __is_memcmp_ordered_with + { static constexpr bool __value = false; }; +#endif + // // Move iterator type // diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h index d82c5873625..33c6778db47 100644 --- a/libstdc++-v3/include/bits/ranges_algo.h +++ b/libstdc++-v3/include/bits/ranges_algo.h @@ -3473,10 +3473,7 @@ namespace ranges // This condition is consistent with the one in // __lexicographical_compare_aux in . constexpr bool __use_memcmp - = (__is_byte<_ValueType1>::__value - && __is_byte<_ValueType2>::__value - && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed - && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed + = (__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value && __ptr_to_nonvolatile<_Iter1> && __ptr_to_nonvolatile<_Iter2> && (is_same_v<_Comp, ranges::less> diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index b1546521dfd..d41a7df57fb 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -1287,9 +1287,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER typedef typename iterator_traits<_II1>::value_type _ValueType1; typedef typename iterator_traits<_II2>::value_type _ValueType2; const bool __simple = - (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value - && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed - && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed + (__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value && __is_pointer<_II1>::__value && __is_pointer<_II2>::__value #if __cplusplus > 201703L && __cpp_lib_concepts @@ -1645,8 +1643,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO // or std::byte, suitable for comparison by memcmp. template concept __is_byte_iter = contiguous_iterator<_Iter> - && __is_byte>::__value != 0 - && !__gnu_cxx::__numeric_traits>::__is_signed; + && __is_memcmp_ordered>::__value; // Return a struct with two members, initialized to the smaller of x and y // (or x if they compare equal) and the result of the comparison x <=> y. diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array index f7b0dca48b1..3bb6f48c432 100644 --- a/libstdc++-v3/include/std/array +++ b/libstdc++-v3/include/std/array @@ -258,16 +258,20 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER constexpr __detail::__synth3way_t<_Tp> operator<=>(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b) { - if constexpr (_Nm && __is_byte<_Tp>::__value) - return __builtin_memcmp(__a.data(), __b.data(), _Nm) <=> 0; - else +#ifdef __cpp_lib_is_constant_evaluated + if constexpr (_Nm && __is_memcmp_ordered<_Tp>::__value) + if (!std::is_constant_evaluated()) + { + constexpr size_t __n = _Nm * sizeof(_Tp); + return __builtin_memcmp(__a.data(), __b.data(), __n) <=> 0; + } +#endif + + for (size_t __i = 0; __i < _Nm; ++__i) { - for (size_t __i = 0; __i < _Nm; ++__i) - { - auto __c = __detail::__synth3way(__a[__i], __b[__i]); - if (__c != 0) - return __c; - } + auto __c = __detail::__synth3way(__a[__i], __b[__i]); + if (__c != 0) + return __c; } return strong_ordering::equal; } diff --git a/libstdc++-v3/testsuite/23_containers/array/comparison_operators/96851.cc b/libstdc++-v3/testsuite/23_containers/array/comparison_operators/96851.cc new file mode 100644 index 00000000000..b2a464b8f68 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/array/comparison_operators/96851.cc @@ -0,0 +1,119 @@ +// Copyright (C) 2020 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 run { target c++2a } } + +#include +#include + +template +constexpr auto +cmp_array(T t) +{ + std::array a{ T{1}, t }; + std::array b{ T{1}, T{2} }; + return a <=> b; +} + +void +test01() +{ + static_assert( cmp_array((unsigned char)1) < 0 ); + static_assert( cmp_array((unsigned char)2) == 0 ); + static_assert( cmp_array((unsigned char)3) > 0 ); + + static_assert( cmp_array((signed char)-1) < 0 ); + static_assert( cmp_array((signed char)2) == 0 ); + static_assert( cmp_array((signed char)3) > 0 ); + + static_assert( cmp_array(std::byte{1}) < 0 ); + static_assert( cmp_array(std::byte{2}) == 0 ); + static_assert( cmp_array(std::byte{3}) > 0 ); + + static_assert( cmp_array((unsigned int)1) < 0 ); + static_assert( cmp_array((unsigned int)2) == 0 ); + static_assert( cmp_array((unsigned int)333) > 0 ); + static_assert( cmp_array((unsigned int)4444) > 0 ); + static_assert( cmp_array((unsigned int)55555) > 0 ); + static_assert( cmp_array((unsigned int)0x66666666) > 0 ); + + static_assert( cmp_array((signed int)-1) < 0 ); + static_assert( cmp_array((signed int)2) == 0 ); + static_assert( cmp_array((signed int)333) > 0 ); + static_assert( cmp_array((signed int)-4444) < 0 ); + static_assert( cmp_array((signed int)55555) > 0 ); + static_assert( cmp_array((signed int)-0x66666666) < 0 ); +} + +void +test02() +{ + unsigned char uc = 1; + VERIFY( cmp_array(uc) < 0 ); + uc = 2; + VERIFY( cmp_array(uc) == 0 ); + uc = 3; + VERIFY( cmp_array(uc) > 0 ); + + signed char sc = -1; + VERIFY( cmp_array(sc) < 0 ); + sc = 2; + VERIFY( cmp_array(sc) == 0 ); + sc = 3; + VERIFY( cmp_array(sc) > 0 ); + + std::byte b{1}; + VERIFY( cmp_array(b) < 0 ); + b = std::byte{2}; + VERIFY( cmp_array(b) == 0 ); + b = std::byte{3}; + VERIFY( cmp_array(b) > 0 ); + + unsigned int ui = 1; + VERIFY( cmp_array(ui) < 0 ); + ui = 2; + VERIFY( cmp_array(ui) == 0 ); + ui = 333; + VERIFY( cmp_array(ui) > 0 ); + ui = 4444; + VERIFY( cmp_array(ui) > 0 ); + ui = 555555; + VERIFY( cmp_array(ui) > 0 ); + ui = 0x66666666; + VERIFY( cmp_array(ui) > 0 ); + + signed int si = -1; + VERIFY( cmp_array(si) < 0 ); + si = 2; + VERIFY( cmp_array(si) == 0 ); + si = 333; + VERIFY( cmp_array(si) > 0 ); + si = -4444; + VERIFY( cmp_array(si) < 0 ); + si = 555555; + VERIFY( cmp_array(si) > 0 ); + si = -0x66666666; + VERIFY( cmp_array(si) < 0 ); +} + +int +main() +{ + test01(); + test02(); +} diff --git a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc index 6072d070f64..a2640318617 100644 --- a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc @@ -27,6 +27,6 @@ int n1 = std::get<1>(a); int n2 = std::get<1>(std::move(a)); int n3 = std::get<1>(ca); -// { dg-error "static assertion failed" "" { target *-*-* } 336 } -// { dg-error "static assertion failed" "" { target *-*-* } 345 } -// { dg-error "static assertion failed" "" { target *-*-* } 353 } +// { dg-error "static assertion failed" "" { target *-*-* } 340 } +// { dg-error "static assertion failed" "" { target *-*-* } 349 } +// { dg-error "static assertion failed" "" { target *-*-* } 357 }