From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id CC55B385843B; Thu, 25 Nov 2021 20:03:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC55B385843B MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-5530] libstdc++: Do not use memset in constexpr calls to ranges::fill_n [PR101608] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 6ea5fb3cc7f3cc9b731d72183c66c23543876f5a X-Git-Newrev: 82c3657dd74896b39937bb0a2aaeba9b8ca105fd Message-Id: <20211125200342.CC55B385843B@sourceware.org> Date: Thu, 25 Nov 2021 20:03:42 +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: Thu, 25 Nov 2021 20:03:42 -0000 https://gcc.gnu.org/g:82c3657dd74896b39937bb0a2aaeba9b8ca105fd commit r12-5530-g82c3657dd74896b39937bb0a2aaeba9b8ca105fd Author: Jonathan Wakely Date: Wed Nov 24 13:17:54 2021 +0000 libstdc++: Do not use memset in constexpr calls to ranges::fill_n [PR101608] libstdc++-v3/ChangeLog: PR libstdc++/101608 * include/bits/ranges_algobase.h (__fill_n_fn): Check for constant evaluation before using memset. * testsuite/25_algorithms/fill_n/constrained.cc: Check byte-sized values as well. Diff: --- libstdc++-v3/include/bits/ranges_algobase.h | 28 ++++++++++++++-------- .../testsuite/25_algorithms/fill_n/constrained.cc | 6 +++-- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/libstdc++-v3/include/bits/ranges_algobase.h b/libstdc++-v3/include/bits/ranges_algobase.h index c8c4d032983..9929e5e828b 100644 --- a/libstdc++-v3/include/bits/ranges_algobase.h +++ b/libstdc++-v3/include/bits/ranges_algobase.h @@ -527,17 +527,25 @@ namespace ranges if (__n <= 0) return __first; - // TODO: Generalize this optimization to contiguous iterators. - if constexpr (is_pointer_v<_Out> - // Note that __is_byte already implies !is_volatile. - && __is_byte>::__value - && integral<_Tp>) - { - __builtin_memset(__first, static_cast(__value), __n); - return __first + __n; - } - else if constexpr (is_scalar_v<_Tp>) + if constexpr (is_scalar_v<_Tp>) { + // TODO: Generalize this optimization to contiguous iterators. + if constexpr (is_pointer_v<_Out> + // Note that __is_byte already implies !is_volatile. + && __is_byte>::__value + && integral<_Tp>) + { +#ifdef __cpp_lib_is_constant_evaluated + if (!std::is_constant_evaluated()) +#endif + { + __builtin_memset(__first, + static_cast(__value), + __n); + return __first + __n; + } + } + const auto __tmp = __value; for (; __n > 0; --__n, (void)++__first) *__first = __tmp; diff --git a/libstdc++-v3/testsuite/25_algorithms/fill_n/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/fill_n/constrained.cc index 6a015d34a89..1d1e1c104d4 100644 --- a/libstdc++-v3/testsuite/25_algorithms/fill_n/constrained.cc +++ b/libstdc++-v3/testsuite/25_algorithms/fill_n/constrained.cc @@ -73,11 +73,12 @@ test01() } } +template constexpr bool test02() { bool ok = true; - int x[6] = { 1, 2, 3, 4, 5, 6 }; + T x[6] = { 1, 2, 3, 4, 5, 6 }; const int y[6] = { 1, 2, 3, 4, 5, 6 }; const int z[6] = { 17, 17, 17, 4, 5, 6 }; @@ -94,5 +95,6 @@ int main() { test01(); - static_assert(test02()); + static_assert(test02()); + static_assert(test02()); // PR libstdc++/101608 }