public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/autopar_devel] libstdc++: Handle non-integral sizes in std::uninitialized_fill_n
@ 2020-08-22 22:45 Giuliano Belinassi
0 siblings, 0 replies; only message in thread
From: Giuliano Belinassi @ 2020-08-22 22:45 UTC (permalink / raw)
To: gcc-cvs, libstdc++-cvs
https://gcc.gnu.org/g:d879418353af9b4a0790b0cd06db61628456615e
commit d879418353af9b4a0790b0cd06db61628456615e
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Wed Jun 17 20:53:45 2020 +0100
libstdc++: Handle non-integral sizes in std::uninitialized_fill_n
The std::uninitialized_fill_n algorithm uses sd::fill_n for trivial
types, but that algorithm has a stronger requirement that the Size
parameter is convertible to an integral type. As the new test shows,
there are types which are valid for std::uninitialized_fill_n but which
produce a different result when converted to an integral type, or cannot
be converted at all. Only use the std::fill_n optimization when the Size
type is already an integral type.
The std::__uninitialized_default_n extension has the same problem, and
so does C++17's std::uninitialized_value_construct_n which uses it.
* include/bits/stl_uninitialized.h (uninitialized_fill_n): Only
use std::fill_n when the size is an integral type.
(__uninitialized_default_n): Likewise.
* testsuite/20_util/specialized_algorithms/uninitialized_default_n/sizes.cc:
New test.
* testsuite/20_util/specialized_algorithms/uninitialized_fill_n/sizes.cc:
New test.
* testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc:
New test.
Diff:
---
libstdc++-v3/include/bits/stl_uninitialized.h | 26 +++++---
.../uninitialized_default_n/sizes.cc | 72 +++++++++++++++++++++
.../uninitialized_fill_n/sizes.cc | 62 ++++++++++++++++++
.../uninitialized_value_construct_n/sizes.cc | 73 ++++++++++++++++++++++
4 files changed, 224 insertions(+), 9 deletions(-)
diff --git a/libstdc++-v3/include/bits/stl_uninitialized.h b/libstdc++-v3/include/bits/stl_uninitialized.h
index 3109a89462d..3771ddfd973 100644
--- a/libstdc++-v3/include/bits/stl_uninitialized.h
+++ b/libstdc++-v3/include/bits/stl_uninitialized.h
@@ -273,19 +273,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
typedef typename iterator_traits<_ForwardIterator>::value_type
_ValueType;
+
+ // Trivial types do not need a constructor to begin their lifetime,
+ // so try to use std::fill_n to benefit from its memmove optimization.
+ // For arbitrary class types and floating point types we can't assume
+ // that __n > 0 and std::__size_to_integer(__n) > 0 are equivalent,
+ // so only use std::fill_n when _Size is already an integral type.
#if __cplusplus < 201103L
- const bool __assignable = true;
+ const bool __can_fill = __is_integer<_Size>::__value;
#else
- // Trivial types can have deleted copy constructor, but the std::fill
+ // Trivial types can have deleted copy constructor, but the std::fill_n
// optimization that uses memmove would happily "copy" them anyway.
static_assert(is_constructible<_ValueType, const _Tp&>::value,
"result type must be constructible from input type");
- // Trivial types can have deleted assignment, so using std::fill
- // would be ill-formed. Require assignability before using std::fill:
- const bool __assignable = is_copy_assignable<_ValueType>::value;
+ // Trivial types can have deleted assignment, so using std::fill_n
+ // would be ill-formed. Require assignability before using std::fill_n:
+ constexpr bool __can_fill
+ = __and_<is_integral<_Size>, is_copy_assignable<_ValueType>>::value;
#endif
- return __uninitialized_fill_n<__is_trivial(_ValueType) && __assignable>::
+ return __uninitialized_fill_n<__is_trivial(_ValueType) && __can_fill>::
__uninit_fill_n(__first, __n, __x);
}
@@ -615,11 +622,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
typedef typename iterator_traits<_ForwardIterator>::value_type
_ValueType;
- // trivial types can have deleted assignment
- const bool __assignable = is_copy_assignable<_ValueType>::value;
+ // See uninitialized_fill_n for the conditions for using std::fill_n.
+ constexpr bool __can_fill
+ = __and_<is_integral<_Size>, is_copy_assignable<_ValueType>>::value;
return __uninitialized_default_n_1<__is_trivial(_ValueType)
- && __assignable>::
+ && __can_fill>::
__uninit_default_n(__first, __n);
}
diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default_n/sizes.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default_n/sizes.cc
new file mode 100644
index 00000000000..1bb4c6a9fb1
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default_n/sizes.cc
@@ -0,0 +1,72 @@
+// 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-do run { target c++11 } }
+
+#include <memory>
+#include <algorithm>
+#include <testsuite_hooks.h>
+
+struct Value
+{
+ int value = 0x1234;
+};
+
+void
+test01()
+{
+ alignas(Value) unsigned char buf[3 * sizeof(Value) + 1];
+ std::fill(std::begin(buf), std::end(buf), 0xff);
+ const auto p = reinterpret_cast<Value*>(buf);
+ std::__uninitialized_default_n(p, 2.0001);
+ VERIFY( p[0].value == 0x1234 );
+ VERIFY( p[1].value == 0x1234 );
+ VERIFY( p[2].value == 0x1234 );
+ VERIFY( *std::prev(std::end(buf)) == 0xff );
+}
+
+void
+test02()
+{
+ // The standard only requires that n>0 and --n are valid expressions.
+ struct Size
+ {
+ int value;
+
+ void operator--() { --value; }
+
+ int operator>(void*) { return value != 0; }
+ };
+
+ alignas(Value) unsigned char buf[4 * sizeof(Value) + 1];
+ std::fill(std::begin(buf), std::end(buf), 0xff);
+ const auto p = reinterpret_cast<Value*>(buf);
+ Size n = {4};
+ std::__uninitialized_default_n(p, n);
+ VERIFY( p[0].value == 0x1234 );
+ VERIFY( p[1].value == 0x1234 );
+ VERIFY( p[2].value == 0x1234 );
+ VERIFY( p[3].value == 0x1234 );
+ VERIFY( *std::prev(std::end(buf)) == 0xff );
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}
diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill_n/sizes.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill_n/sizes.cc
new file mode 100644
index 00000000000..eb957e148da
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill_n/sizes.cc
@@ -0,0 +1,62 @@
+// 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-do run }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ int i[4] = { };
+ std::uninitialized_fill_n(i, 2.0001, 0xabcd);
+ VERIFY( i[0] == 0xabcd );
+ VERIFY( i[1] == 0xabcd );
+ VERIFY( i[2] == 0xabcd );
+ VERIFY( i[3] == 0 );
+}
+
+void
+test02()
+{
+ // The standard only requires that n>0 and --n are valid expressions.
+ struct Size
+ {
+ int value;
+
+ void operator--() { --value; }
+
+ int operator>(void*) { return value != 0; }
+ };
+
+ int i[5] = { };
+ Size n = {4};
+ std::uninitialized_fill_n(i, n, 0xdcba);
+ VERIFY( i[0] == 0xdcba );
+ VERIFY( i[1] == 0xdcba );
+ VERIFY( i[2] == 0xdcba );
+ VERIFY( i[3] == 0xdcba );
+ VERIFY( i[4] == 0 );
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}
diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc
new file mode 100644
index 00000000000..e822b198928
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc
@@ -0,0 +1,73 @@
+// 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 "-std=gnu++17" }
+// { dg-do run { target c++17 } }
+
+#include <memory>
+#include <algorithm>
+#include <testsuite_hooks.h>
+
+struct Value
+{
+ int value = 0x1234;
+};
+
+void
+test01()
+{
+ alignas(Value) unsigned char buf[3 * sizeof(Value) + 1];
+ std::fill(std::begin(buf), std::end(buf), 0xff);
+ const auto p = reinterpret_cast<Value*>(buf);
+ std::uninitialized_value_construct_n(p, 2.0001);
+ VERIFY( p[0].value == 0x1234 );
+ VERIFY( p[1].value == 0x1234 );
+ VERIFY( p[2].value == 0x1234 );
+ VERIFY( *std::prev(std::end(buf)) == 0xff );
+}
+
+void
+test02()
+{
+ // The standard only requires that n>0 and --n are valid expressions.
+ struct Size
+ {
+ int value;
+
+ void operator--() { --value; }
+
+ int operator>(void*) { return value != 0; }
+ };
+
+ alignas(Value) unsigned char buf[4 * sizeof(Value) + 1];
+ std::fill(std::begin(buf), std::end(buf), 0xff);
+ const auto p = reinterpret_cast<Value*>(buf);
+ Size n = {4};
+ std::uninitialized_value_construct_n(p, n);
+ VERIFY( p[0].value == 0x1234 );
+ VERIFY( p[1].value == 0x1234 );
+ VERIFY( p[2].value == 0x1234 );
+ VERIFY( p[3].value == 0x1234 );
+ VERIFY( *std::prev(std::end(buf)) == 0xff );
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2020-08-22 22:45 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-22 22:45 [gcc/devel/autopar_devel] libstdc++: Handle non-integral sizes in std::uninitialized_fill_n Giuliano Belinassi
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).