public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r10-10296] libstdc++: Do not use memset in constexpr calls to ranges::fill_n [PR101608]
Date: Fri, 26 Nov 2021 12:52:42 +0000 (GMT)	[thread overview]
Message-ID: <20211126125242.0F120385843D@sourceware.org> (raw)

https://gcc.gnu.org/g:d0ac292c6083fab4bad79a08d23533f537a885d4

commit r10-10296-gd0ac292c6083fab4bad79a08d23533f537a885d4
Author: Jonathan Wakely <jwakely@redhat.com>
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.
    
    (cherry picked from commit 82c3657dd74896b39937bb0a2aaeba9b8ca105fd)

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 aef80a38586..2fb558918f8 100644
--- a/libstdc++-v3/include/bits/ranges_algobase.h
+++ b/libstdc++-v3/include/bits/ranges_algobase.h
@@ -526,17 +526,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<remove_pointer_t<_Out>>::__value
-		      && integral<_Tp>)
-	  {
-	    __builtin_memset(__first, static_cast<unsigned char>(__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<remove_pointer_t<_Out>>::__value
+			  && integral<_Tp>)
+	      {
+#ifdef __cpp_lib_is_constant_evaluated
+		if (!std::is_constant_evaluated())
+#endif
+		  {
+		    __builtin_memset(__first,
+				     static_cast<unsigned char>(__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 e9ce8e8fb0a..09d3bd37630 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<typename T>
 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<int>());
+  static_assert(test02<unsigned char>()); // PR libstdc++/101608
 }


                 reply	other threads:[~2021-11-26 12:52 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=20211126125242.0F120385843D@sourceware.org \
    --to=redi@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: link
Be 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).