This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [Patch] libstdc++/30416
Gabriel Dos Reis wrote:
We want the NRVO to apply. So, we should return the same variable in
all return statements -- I know an implementation can be smarter than
that, but I don't know whether g++ has been improved (since the last
time I looked into its NRVO) to apply the optimization when different
expressions are used in the return statements.
No problem, I'm committing the below follow-up, tested x86-linux.
Thanks for your (slightly late ;) review!
Paolo.
/////////
2007-01-12 Paolo Carlini <pcarlini@suse.de>
PR libstdc++/30416 (continued)
* include/std/valarray (valarray<>::shift, valarray<>::cshift):
Allways return the same variable, thus facilitating NRVO.
Index: include/std/valarray
===================================================================
--- include/std/valarray (revision 120720)
+++ include/std/valarray (working copy)
@@ -782,14 +782,18 @@
inline valarray<_Tp>
valarray<_Tp>::shift(int __n) const
{
- if (_M_size == 0 || __n == 0)
- return *this;
-
valarray<_Tp> __ret;
+
+ if (_M_size == 0)
+ return __ret;
+
_Tp* __restrict__ __tmp_M_data =
std::__valarray_get_storage<_Tp>(_M_size);
- if (__n > 0) // shift left
+ if (__n == 0)
+ std::__valarray_copy_construct(_M_data,
+ _M_data + _M_size, __tmp_M_data);
+ else if (__n > 0) // shift left
{
if (size_t(__n) > _M_size)
__n = _M_size;
@@ -799,7 +803,7 @@
std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
__tmp_M_data + _M_size);
}
- else // shift right
+ else // shift right
{
if (size_t(-__n) > _M_size)
__n = -_M_size;
@@ -819,14 +823,18 @@
inline valarray<_Tp>
valarray<_Tp>::cshift(int __n) const
{
- if (_M_size == 0 || __n == 0)
- return *this;
+ valarray<_Tp> __ret;
- valarray<_Tp> __ret;
+ if (_M_size == 0)
+ return __ret;
+
_Tp* __restrict__ __tmp_M_data =
std::__valarray_get_storage<_Tp>(_M_size);
-
- if (__n > 0) // cshift left
+
+ if (__n == 0)
+ std::__valarray_copy_construct(_M_data,
+ _M_data + _M_size, __tmp_M_data);
+ else if (__n > 0) // cshift left
{
if (size_t(__n) > _M_size)
__n = __n % _M_size;
@@ -836,7 +844,7 @@
std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
__tmp_M_data);
}
- else // cshift right
+ else // cshift right
{
if (size_t(-__n) > _M_size)
__n = -(-__n % _M_size);