This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
libstdc++: Problems with gslices (patch included)
- To: gcc-patches at gcc dot gnu dot org
- Subject: libstdc++: Problems with gslices (patch included)
- From: Peter Schmid <schmid at snake dot iap dot physik dot tu-darmstadt dot de>
- Date: Sat, 27 Jan 2001 17:01:05 +0100 (CET)
- cc: libstdc++ at gcc dot gnu dot org
When running the following program tgs compiled by g++-20010125 on
i686-pc-linux-gnu
#include <iostream>
#include <valarray>
using namespace std;
template<class T>
void aus_val(const valarray<T>& va)
{
for (size_t i=0; i < va.size(); ++i)
{
cout << va[i];
}
cout << endl;
}
int main()
{
valarray<char> v0("abcdefghijklmnop", 16);
aus_val(v0);
valarray<char> v1("ABCDEF", 6);
aus_val(v1);
valarray<char> v2 = v0;
const size_t lv[] = {2, 3};
const size_t sv[] = {7, 2};
const valarray<size_t> len(lv, 2), str(sv, 2);
v0[gslice(3, len, str)] = v1;
aus_val(v0); // expected output "abcAeBgCijDlEnFp"
aus_val(v2);
valarray<char> v3 = v2[gslice(3, len, str)];
aus_val(v3); // expected output "dfhkmo"
valarray<char> v4("abcdefghijklmnop", 16);
valarray<char> v5 = v4[slice(2, 5, 3)];
aus_val(v5); // expected output "cfilo"
}
incorrect output is obtained.
./tgs
abcdefghijklmnop
ABCDEF
abcAeCBEDjFlmnop
abcdefghijklmnop
dgfihkjmlon
cfilo
Closer examination shows that the arguments of len and str in the
gslice implementation seem to be reversed.
Applying the following patch to libstdc++-v3/src/valarray-inst.cc
fixes the problem.
ChangeLog
+ 2001-01-09 Peter Schmid<schmid@snake.iap.physik.tu-darmstadt.de>
+ * libstdc++-v3/src/valarray-inst.cc Fix gslices
+
*** libstdc++-v3/src/valarray-inst.cc~ Sat Jan 27 16:33:53 2001
--- libstdc++-v3/src/valarray-inst.cc Sat Jan 27 16:37:38 2001
*************** namespace std
*** 32,38 ****
const valarray<size_t>& __s,
valarray<size_t>& __i)
{
! size_t __n = __l.size();
size_t* const __t = static_cast<size_t*>
(__builtin_alloca(__n*sizeof(size_t)));
__valarray_fill(__t, __n, size_t(0));
--- 32,38 ----
const valarray<size_t>& __s,
valarray<size_t>& __i)
{
! size_t __n = __s.size();
size_t* const __t = static_cast<size_t*>
(__builtin_alloca(__n*sizeof(size_t)));
__valarray_fill(__t, __n, size_t(0));
*************** namespace std
*** 40,49 ****
__valarray_fill(&__i[0], __z, __o);
for (size_t __j=0; __j<__z; ++__j) {
for (size_t __k=0; __k<__n; ++__k)
! __i[__j] += __s[__k]*__t[__k];
++__t[__n-1];
for (size_t __k=__n-1; __k; --__k) {
! if (__t[__k] >= __l[__k]) {
__t[__k] = 0;
++__t[__k-1];
}
--- 40,49 ----
__valarray_fill(&__i[0], __z, __o);
for (size_t __j=0; __j<__z; ++__j) {
for (size_t __k=0; __k<__n; ++__k)
! __i[__j] += __l[__k]*__t[__k];
++__t[__n-1];
for (size_t __k=__n-1; __k; --__k) {
! if (__t[__k] >= __s[__k]) {
__t[__k] = 0;
++__t[__k-1];
}
*************** namespace std
*** 53,60 ****
gslice::_Indexer::_Indexer(size_t __o, const valarray<size_t>& __s,
const valarray<size_t>& __l)
! : _M_count(1), _M_start(__o), _M_size(__s), _M_stride(__l),
! _M_index(__l.size() ? __valarray_product(__l) : 0)
{ __gslice_to_index(__o, __l, __s, _M_index); }
}
--- 53,60 ----
gslice::_Indexer::_Indexer(size_t __o, const valarray<size_t>& __s,
const valarray<size_t>& __l)
! : _M_count(1), _M_start(__o), _M_size(__l), _M_stride(__s),
! _M_index(__s.size() ? __valarray_product(__s) : 0)
{ __gslice_to_index(__o, __l, __s, _M_index); }
}
After applying the patch the correct output is obtained.
./tgs
abcdefghijklmnop
ABCDEF
abcAeBgCijDlEnFp
abcdefghijklmnop
dfhkmo
cfilo
Also libstdc++/1720 is fixed by applying the patch.
Peter Schmid