libstdc++: Fix std::lexicographic_compare for unsigned char (PR 93972)
The new 25_algorithms/lexicographical_compare/93972.cc test fails on targets where char is unsigned, revealing an existing regression with the std::__memcmp helper that had gone unnoticed in std::lexicographical_compare. When comparing char and unsigned char, the memcmp optimisation is enabled, but the new std::__memcmp function fails to compile for mismatched types. PR libstdc++/93972 * include/bits/stl_algobase.h (__memcmp): Allow pointer types to differ. * testsuite/25_algorithms/lexicographical_compare/uchar.cc: New test.
This commit is contained in:
parent
4c39156340
commit
d112e173ea
@ -1,5 +1,10 @@
|
||||
2020-03-02 Jonathan Wakely <jwakely@redhat.com>
|
||||
|
||||
PR libstdc++/93972
|
||||
* include/bits/stl_algobase.h (__memcmp): Allow pointer types to
|
||||
differ.
|
||||
* testsuite/25_algorithms/lexicographical_compare/uchar.cc: New test.
|
||||
|
||||
* include/std/ranges (__detail::__maybe_empty_t): Rename to
|
||||
__maybe_present_t.
|
||||
(__adaptor::_RangeAdaptor, join_view, split_view): Use new name.
|
||||
|
@ -84,11 +84,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
* A constexpr wrapper for __builtin_memcmp.
|
||||
* @param __num The number of elements of type _Tp (not bytes).
|
||||
*/
|
||||
template<typename _Tp>
|
||||
template<typename _Tp, typename _Up>
|
||||
_GLIBCXX14_CONSTEXPR
|
||||
inline int
|
||||
__memcmp(const _Tp* __first1, const _Tp* __first2, size_t __num)
|
||||
__memcmp(const _Tp* __first1, const _Up* __first2, size_t __num)
|
||||
{
|
||||
#if __cplusplus >= 201103L
|
||||
static_assert(sizeof(_Tp) == sizeof(_Up), "can be compared with memcmp");
|
||||
#endif
|
||||
#ifdef __cpp_lib_is_constant_evaluated
|
||||
if (std::is_constant_evaluated())
|
||||
{
|
||||
|
@ -0,0 +1,61 @@
|
||||
// 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 "-fchar8_t" }
|
||||
// { dg-do run }
|
||||
|
||||
#include <algorithm>
|
||||
#include <testsuite_hooks.h>
|
||||
|
||||
// Check that the memcmp optimization in std::lexicographical_compare
|
||||
// still works for mixed-type comparisons.
|
||||
|
||||
void
|
||||
test01()
|
||||
{
|
||||
unsigned char a = 0;
|
||||
char8_t b = 1;
|
||||
VERIFY( std::lexicographical_compare(&a, &a + 1, &b, &b + 1) );
|
||||
VERIFY( ! std::lexicographical_compare(&b, &b + 1, &a, &a + 1) );
|
||||
}
|
||||
|
||||
void
|
||||
test02()
|
||||
{
|
||||
// N.B. if char is signed this won't actually use the memcmp optimization.
|
||||
unsigned char a = 0;
|
||||
char b = 1;
|
||||
VERIFY( std::lexicographical_compare(&a, &a + 1, &b, &b + 1) );
|
||||
VERIFY( ! std::lexicographical_compare(&b, &b + 1, &a, &a + 1) );
|
||||
}
|
||||
|
||||
void
|
||||
test03()
|
||||
{
|
||||
// N.B. if char is signed this won't actually use the memcmp optimization.
|
||||
char a = 0;
|
||||
char8_t b = 1;
|
||||
VERIFY( std::lexicographical_compare(&a, &a + 1, &b, &b + 1) );
|
||||
VERIFY( ! std::lexicographical_compare(&b, &b + 1, &a, &a + 1) );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test01();
|
||||
test02();
|
||||
test03();
|
||||
}
|
Loading…
Reference in New Issue
Block a user