public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Jiu Fu Guo <guojiufu@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc(refs/vendors/ibm/heads/perf)] libstdc++: Fix std::lexicographic_compare for unsigned char (PR 93972) Date: Thu, 19 Mar 2020 06:02:49 +0000 (GMT) [thread overview] Message-ID: <20200319060249.4162D38A2414@sourceware.org> (raw) https://gcc.gnu.org/g:d112e173ea093f55a16a14b26ef65088381ee09c commit d112e173ea093f55a16a14b26ef65088381ee09c Author: Jonathan Wakely <jwakely@redhat.com> Date: Mon Mar 2 17:03:28 2020 +0000 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. Diff: --- libstdc++-v3/ChangeLog | 5 ++ libstdc++-v3/include/bits/stl_algobase.h | 7 ++- .../25_algorithms/lexicographical_compare/uchar.cc | 61 ++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index d810b0887b5..28957dc5c99 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -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. diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index 5ec2f25424d..7a9d932b421 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -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()) { diff --git a/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/uchar.cc b/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/uchar.cc new file mode 100644 index 00000000000..990bb1e7489 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/uchar.cc @@ -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(); +}
reply other threads:[~2020-03-19 6:02 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20200319060249.4162D38A2414@sourceware.org \ --to=guojiufu@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).