]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix three-way comparison for std::array [PR 96851]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 2 Sep 2020 14:17:24 +0000 (15:17 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 2 Sep 2020 14:57:32 +0000 (15:57 +0100)
The spaceship operator for std::array uses memcmp when the
__is_byte<value_type> 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)

libstdc++-v3/include/bits/cpp_type_traits.h
libstdc++-v3/include/bits/ranges_algo.h
libstdc++-v3/include/bits/stl_algobase.h
libstdc++-v3/include/std/array
libstdc++-v3/testsuite/23_containers/array/comparison_operators/96851.cc [new file with mode: 0644]
libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc

index 979ad9c2c690bd0b15f00c62634c67b8b7f4f201..ca83f590eb44bfac44acde9a4e335b9b2d59a3de 100644 (file)
@@ -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<typename _Tp, bool _TreatAsBytes = __is_byte<_Tp>::__value>
+    struct __is_memcmp_ordered
+    {
+      static const bool __value = _Tp(-1) > _Tp(1); // is unsigned
+    };
+
+  template<typename _Tp>
+    struct __is_memcmp_ordered<_Tp, false>
+    {
+      static const bool __value = false;
+    };
+
+  // Whether two types can be compared using memcmp.
+  template<typename _Tp, typename _Up, bool = sizeof(_Tp) == sizeof(_Up)>
+    struct __is_memcmp_ordered_with
+    {
+      static const bool __value = __is_memcmp_ordered<_Tp>::__value
+       && __is_memcmp_ordered<_Up>::__value;
+    };
+
+  template<typename _Tp, typename _Up>
+    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<std::byte, std::byte, true>
+    { static constexpr bool __value = true; };
+
+  template<typename _Tp, bool _SameSize>
+    struct __is_memcmp_ordered_with<_Tp, std::byte, _SameSize>
+    { static constexpr bool __value = false; };
+
+  template<typename _Up, bool _SameSize>
+    struct __is_memcmp_ordered_with<std::byte, _Up, _SameSize>
+    { static constexpr bool __value = false; };
+#endif
+
   //
   // Move iterator type
   //
index d82c58736251b5aedf39241a6a657cf9b80bd29d..33c6778db4773a4543c187bf195e92aca83ce241 100644 (file)
@@ -3473,10 +3473,7 @@ namespace ranges
                // This condition is consistent with the one in
                // __lexicographical_compare_aux in <bits/stl_algobase.h>.
                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>
index b1546521dfd0ac3640cd4fb1b81c21d566d55537..d41a7df57fbf385a62f1fac50193ff4382b817ea 100644 (file)
@@ -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<typename _Iter>
     concept __is_byte_iter = contiguous_iterator<_Iter>
-      && __is_byte<iter_value_t<_Iter>>::__value != 0
-      && !__gnu_cxx::__numeric_traits<iter_value_t<_Iter>>::__is_signed;
+      && __is_memcmp_ordered<iter_value_t<_Iter>>::__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.
index f7b0dca48b1149954e0638bc2f6036679f269f96..3bb6f48c4321c25dd1600fc34cf09ba6c6e6e393 100644 (file)
@@ -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 (file)
index 0000000..b2a464b
--- /dev/null
@@ -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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <array>
+#include <testsuite_hooks.h>
+
+template<typename T>
+constexpr auto
+cmp_array(T t)
+{
+  std::array<T, 2> a{ T{1}, t };
+  std::array<T, 2> 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();
+}
index 6072d070f640d3b8a6de077dc809ed9c2c0a31ef..a2640318617d90aba12d8a258916e4e189a2e0a9 100644 (file)
@@ -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 }