From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2136) id 9D0D4393C8AC; Wed, 17 Jun 2020 19:21:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9D0D4393C8AC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1592421719; bh=LxyVeVLdI8F2I5VvHf9mDM2Rmj/knD8Rzzs0F4tf5b0=; h=From:To:Subject:Date:From; b=r7/Br3+Pj1CWSzVkuO9aij/4SD2F6Tk4nmtdz7ExfkriIvGKArVLj4vr49gkBWvwp GGxqmjh8ehOtSE4GkT+y7St+AGtJkW8GQPVY6rgDAG8YGXNZwqDG9RI9TYYIlMAwYa KN3jhHUAa2SF6xlrES0tJWYQa5cgo0oJ4LF/V+xg= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Aldy Hernandez To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc/devel/ranger] libstdc++: Fix std::lexicographic_compare for unsigned char (PR 93972) X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/devel/ranger X-Git-Oldrev: 4c391563407aeba981f6a71423297f36d5ece0ee X-Git-Newrev: d112e173ea093f55a16a14b26ef65088381ee09c Message-Id: <20200617192159.9D0D4393C8AC@sourceware.org> Date: Wed, 17 Jun 2020 19:21:59 +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, 17 Jun 2020 19:21:59 -0000 https://gcc.gnu.org/g:d112e173ea093f55a16a14b26ef65088381ee09c commit d112e173ea093f55a16a14b26ef65088381ee09c Author: Jonathan Wakely 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 + 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 + template _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 +// . + +// { dg-options "-fchar8_t" } +// { dg-do run } + +#include +#include + +// 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(); +}