![]() |
Blitz Devel : |
From: Julian Cummings (cummings_at_[hidden])
Date: 2004-07-28 19:08:22
Theo,
I have no problems with this patch. I remember the discussion a while
back about whether it was more or less efficient to use const reference
rather than value type. A reference is clearly preferred for larger
objects such as the complex<T> type. For simple built-in types, I would
guess that most compilers will optimize the cost of dereferencing the
reference as needed. In other words, they will dereference the
reference once and store the value as a temporary in a register if they
see that the code is using the value repeatedly. The runtime penalty
will be tiny.
-- Julian C.
Theodore Papadopoulo wrote:
> Hi,
>
>I finally found the time to propose the various patches I have in my
>local blitz tree. Here is the first one...
>
>Basically, this adds two capabilities to blitz:
>
> - First, it fills in the "// NEEDS_WORK: const version"
> for multicomponent_traits.
>
> - Second it transforms the return type of operator()(XXX) const
> from T_numtype to const T_numtype& restrict. These are strictly
> equivalent but the later allows the recovery of the index of the
> element by subtracting the address with the first element of
> the array. For complex types it also avoids a call to the copy
> constructor. For simple cases (eg double), I also once verified
> that it does not incur a runtime penalty. I do not see what it
> should, but that might need some verification again if someone
> has a fear in this respect.
>
>This patch has been in my tree for almost one year without any
>compatibility problem, as far as I can tell.
>
>Julian, let me know if this is OK so that I can commit it.
>
> Theo.
>
>
>2004-07-28 Theodore Papadopoulo <Theodore.Papadopoulo_at_[hidden]>
>
> * blitz/array-impl.h: Made the return type of operator()(XXX) const
> be const references instead of values. Added constant case for
> multicomponent_traits.
>
>Index: blitz/array-impl.h
>===================================================================
>RCS file: /cvsroot/blitz/blitz/blitz/array-impl.h,v
>retrieving revision 1.16
>diff -c -3 -p -r1.16 array-impl.h
>*** blitz/array-impl.h 19 Jun 2004 03:14:11 -0000 1.16
>--- blitz/array-impl.h 28 Jul 2004 12:07:32 -0000
>*************** public:
>*** 1418,1424 ****
> //////////////////////////////////////////////
>
> template<int N_rank2>
>! T_numtype operator()(const TinyVector<int,N_rank2>& index) const
> {
> assertInRange(index);
> return data_[dot(index, stride_)];
>--- 1418,1424 ----
> //////////////////////////////////////////////
>
> template<int N_rank2>
>! const T_numtype& restrict operator()(const TinyVector<int,N_rank2>& index) const
> {
> assertInRange(index);
> return data_[dot(index, stride_)];
>*************** public:
>*** 1431,1437 ****
> return data_[dot(index, stride_)];
> }
>
>! T_numtype operator()(TinyVector<int,1> index) const
> {
> assertInRange(index[0]);
> return data_[index[0] * stride_[0]];
>--- 1431,1437 ----
> return data_[dot(index, stride_)];
> }
>
>! const T_numtype& restrict operator()(TinyVector<int,1> index) const
> {
> assertInRange(index[0]);
> return data_[index[0] * stride_[0]];
>*************** public:
>*** 1443,1449 ****
> return data_[index[0] * stride_[0]];
> }
>
>! T_numtype operator()(TinyVector<int,2> index) const
> {
> assertInRange(index[0], index[1]);
> return data_[index[0] * stride_[0] + index[1] * stride_[1]];
>--- 1443,1449 ----
> return data_[index[0] * stride_[0]];
> }
>
>! const T_numtype& restrict operator()(TinyVector<int,2> index) const
> {
> assertInRange(index[0], index[1]);
> return data_[index[0] * stride_[0] + index[1] * stride_[1]];
>*************** public:
>*** 1455,1461 ****
> return data_[index[0] * stride_[0] + index[1] * stride_[1]];
> }
>
>! T_numtype operator()(TinyVector<int,3> index) const
> {
> assertInRange(index[0], index[1], index[2]);
> return data_[index[0] * stride_[0] + index[1] * stride_[1]
>--- 1455,1461 ----
> return data_[index[0] * stride_[0] + index[1] * stride_[1]];
> }
>
>! const T_numtype& restrict operator()(TinyVector<int,3> index) const
> {
> assertInRange(index[0], index[1], index[2]);
> return data_[index[0] * stride_[0] + index[1] * stride_[1]
>*************** public:
>*** 1469,1475 ****
> + index[2] * stride_[2]];
> }
>
>! T_numtype operator()(const TinyVector<int,4>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3]);
> return data_[index[0] * stride_[0] + index[1] * stride_[1]
>--- 1469,1475 ----
> + index[2] * stride_[2]];
> }
>
>! const T_numtype& restrict operator()(const TinyVector<int,4>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3]);
> return data_[index[0] * stride_[0] + index[1] * stride_[1]
>*************** public:
>*** 1483,1489 ****
> + index[2] * stride_[2] + index[3] * stride_[3]];
> }
>
>! T_numtype operator()(const TinyVector<int,5>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4]);
>--- 1483,1489 ----
> + index[2] * stride_[2] + index[3] * stride_[3]];
> }
>
>! const T_numtype& restrict operator()(const TinyVector<int,5>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4]);
>*************** public:
>*** 1501,1507 ****
> + index[4] * stride_[4]];
> }
>
>! T_numtype operator()(const TinyVector<int,6>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4], index[5]);
>--- 1501,1507 ----
> + index[4] * stride_[4]];
> }
>
>! const T_numtype& restrict operator()(const TinyVector<int,6>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4], index[5]);
>*************** public:
>*** 1519,1525 ****
> + index[4] * stride_[4] + index[5] * stride_[5]];
> }
>
>! T_numtype operator()(const TinyVector<int,7>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4], index[5], index[6]);
>--- 1519,1525 ----
> + index[4] * stride_[4] + index[5] * stride_[5]];
> }
>
>! const T_numtype& restrict operator()(const TinyVector<int,7>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4], index[5], index[6]);
>*************** public:
>*** 1539,1545 ****
> + index[6] * stride_[6]];
> }
>
>! T_numtype operator()(const TinyVector<int,8>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4], index[5], index[6], index[7]);
>--- 1539,1545 ----
> + index[6] * stride_[6]];
> }
>
>! const T_numtype& restrict operator()(const TinyVector<int,8>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4], index[5], index[6], index[7]);
>*************** public:
>*** 1559,1565 ****
> + index[6] * stride_[6] + index[7] * stride_[7]];
> }
>
>! T_numtype operator()(const TinyVector<int,9>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4], index[5], index[6], index[7], index[8]);
>--- 1559,1565 ----
> + index[6] * stride_[6] + index[7] * stride_[7]];
> }
>
>! const T_numtype& restrict operator()(const TinyVector<int,9>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4], index[5], index[6], index[7], index[8]);
>*************** public:
>*** 1581,1587 ****
> + index[8] * stride_[8]];
> }
>
>! T_numtype operator()(const TinyVector<int,10>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4], index[5], index[6], index[7], index[8], index[9]);
>--- 1581,1587 ----
> + index[8] * stride_[8]];
> }
>
>! const T_numtype& restrict operator()(const TinyVector<int,10>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4], index[5], index[6], index[7], index[8], index[9]);
>*************** public:
>*** 1603,1609 ****
> + index[8] * stride_[8] + index[9] * stride_[9]];
> }
>
>! T_numtype operator()(const TinyVector<int,11>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4], index[5], index[6], index[7], index[8], index[9],
>--- 1603,1609 ----
> + index[8] * stride_[8] + index[9] * stride_[9]];
> }
>
>! const T_numtype& restrict operator()(const TinyVector<int,11>& index) const
> {
> assertInRange(index[0], index[1], index[2], index[3],
> index[4], index[5], index[6], index[7], index[8], index[9],
>*************** public:
>*** 1629,1635 ****
> + index[10] * stride_[10]];
> }
>
>! T_numtype operator()(int i0) const
> {
> assertInRange(i0);
> return data_[i0 * stride_[0]];
>--- 1629,1635 ----
> + index[10] * stride_[10]];
> }
>
>! const T_numtype& restrict operator()(int i0) const
> {
> assertInRange(i0);
> return data_[i0 * stride_[0]];
>*************** public:
>*** 1641,1647 ****
> return data_[i0 * stride_[0]];
> }
>
>! T_numtype operator()(int i0, int i1) const
> {
> assertInRange(i0, i1);
> return data_[i0 * stride_[0] + i1 * stride_[1]];
>--- 1641,1647 ----
> return data_[i0 * stride_[0]];
> }
>
>! const T_numtype& restrict operator()(int i0, int i1) const
> {
> assertInRange(i0, i1);
> return data_[i0 * stride_[0] + i1 * stride_[1]];
>*************** public:
>*** 1653,1659 ****
> return data_[i0 * stride_[0] + i1 * stride_[1]];
> }
>
>! T_numtype operator()(int i0, int i1, int i2) const
> {
> assertInRange(i0, i1, i2);
> return data_[i0 * stride_[0] + i1 * stride_[1]
>--- 1653,1659 ----
> return data_[i0 * stride_[0] + i1 * stride_[1]];
> }
>
>! const T_numtype& restrict operator()(int i0, int i1, int i2) const
> {
> assertInRange(i0, i1, i2);
> return data_[i0 * stride_[0] + i1 * stride_[1]
>*************** public:
>*** 1667,1673 ****
> + i2 * stride_[2]];
> }
>
>! T_numtype operator()(int i0, int i1, int i2, int i3) const
> {
> assertInRange(i0, i1, i2, i3);
> return data_[i0 * stride_[0] + i1 * stride_[1]
>--- 1667,1673 ----
> + i2 * stride_[2]];
> }
>
>! const T_numtype& restrict operator()(int i0, int i1, int i2, int i3) const
> {
> assertInRange(i0, i1, i2, i3);
> return data_[i0 * stride_[0] + i1 * stride_[1]
>*************** public:
>*** 1681,1687 ****
> + i2 * stride_[2] + i3 * stride_[3]];
> }
>
>! T_numtype operator()(int i0, int i1, int i2, int i3,
> int i4) const
> {
> assertInRange(i0, i1, i2, i3, i4);
>--- 1681,1687 ----
> + i2 * stride_[2] + i3 * stride_[3]];
> }
>
>! const T_numtype& restrict operator()(int i0, int i1, int i2, int i3,
> int i4) const
> {
> assertInRange(i0, i1, i2, i3, i4);
>*************** public:
>*** 1697,1703 ****
> + i2 * stride_[2] + i3 * stride_[3] + i4 * stride_[4]];
> }
>
>! T_numtype operator()(int i0, int i1, int i2, int i3,
> int i4, int i5) const
> {
> assertInRange(i0, i1, i2, i3, i4, i5);
>--- 1697,1703 ----
> + i2 * stride_[2] + i3 * stride_[3] + i4 * stride_[4]];
> }
>
>! const T_numtype& restrict operator()(int i0, int i1, int i2, int i3,
> int i4, int i5) const
> {
> assertInRange(i0, i1, i2, i3, i4, i5);
>*************** public:
>*** 1715,1721 ****
> + i5 * stride_[5]];
> }
>
>! T_numtype operator()(int i0, int i1, int i2, int i3,
> int i4, int i5, int i6) const
> {
> assertInRange(i0, i1, i2, i3, i4, i5, i6);
>--- 1715,1721 ----
> + i5 * stride_[5]];
> }
>
>! const T_numtype& restrict operator()(int i0, int i1, int i2, int i3,
> int i4, int i5, int i6) const
> {
> assertInRange(i0, i1, i2, i3, i4, i5, i6);
>*************** public:
>*** 1733,1739 ****
> + i5 * stride_[5] + i6 * stride_[6]];
> }
>
>! T_numtype operator()(int i0, int i1, int i2, int i3,
> int i4, int i5, int i6, int i7) const
> {
> assertInRange(i0, i1, i2, i3, i4, i5, i6, i7);
>--- 1733,1739 ----
> + i5 * stride_[5] + i6 * stride_[6]];
> }
>
>! const T_numtype& restrict operator()(int i0, int i1, int i2, int i3,
> int i4, int i5, int i6, int i7) const
> {
> assertInRange(i0, i1, i2, i3, i4, i5, i6, i7);
>*************** public:
>*** 1751,1757 ****
> + i5 * stride_[5] + i6 * stride_[6] + i7 * stride_[7]];
> }
>
>! T_numtype operator()(int i0, int i1, int i2, int i3,
> int i4, int i5, int i6, int i7, int i8) const
> {
> assertInRange(i0, i1, i2, i3, i4, i5, i6, i7, i8);
>--- 1751,1757 ----
> + i5 * stride_[5] + i6 * stride_[6] + i7 * stride_[7]];
> }
>
>! const T_numtype& restrict operator()(int i0, int i1, int i2, int i3,
> int i4, int i5, int i6, int i7, int i8) const
> {
> assertInRange(i0, i1, i2, i3, i4, i5, i6, i7, i8);
>*************** public:
>*** 1771,1777 ****
> + i8 * stride_[8]];
> }
>
>! T_numtype operator()(int i0, int i1, int i2, int i3,
> int i4, int i5, int i6, int i7, int i8, int i9) const
> {
> assertInRange(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9);
>--- 1771,1777 ----
> + i8 * stride_[8]];
> }
>
>! const T_numtype& restrict operator()(int i0, int i1, int i2, int i3,
> int i4, int i5, int i6, int i7, int i8, int i9) const
> {
> assertInRange(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9);
>*************** public:
>*** 1791,1797 ****
> + i8 * stride_[8] + i9 * stride_[9]];
> }
>
>! T_numtype operator()(int i0, int i1, int i2, int i3,
> int i4, int i5, int i6, int i7, int i8, int i9, int i10) const
> {
> assertInRange(i0, i1, i2, i3, i4, i5, i6, i7, i8,
>--- 1791,1797 ----
> + i8 * stride_[8] + i9 * stride_[9]];
> }
>
>! const T_numtype& restrict operator()(int i0, int i1, int i2, int i3,
> int i4, int i5, int i6, int i7, int i8, int i9, int i10) const
> {
> assertInRange(i0, i1, i2, i3, i4, i5, i6, i7, i8,
>*************** public:
>*** 2155,2169 ****
> * multicomponent_traits.
> */
>
>- // NEEDS_WORK: const version
> Array<typename multicomponent_traits<T_numtype>::T_element,N_rank>
>! operator[](int component)
>! {
>! typedef typename multicomponent_traits<T_numtype>::T_element
>! T_compType;
>
>! return extractComponent(T_compType(),
>! component, multicomponent_traits<T_numtype>::numComponents);
> }
>
> //////////////////////////////////////////////
>--- 2155,2174 ----
> * multicomponent_traits.
> */
>
> Array<typename multicomponent_traits<T_numtype>::T_element,N_rank>
>! operator[](const unsigned component) {
>! typedef typename multicomponent_traits<T_numtype>::T_element T_compType;
>!
>! return extractComponent(T_compType(),component,
>! multicomponent_traits<T_numtype>::numComponents);
>! }
>!
>! const Array<typename multicomponent_traits<T_numtype>::T_element,N_rank>
>! operator[](const unsigned component) const {
>! typedef typename multicomponent_traits<T_numtype>::T_element T_compType;
>
>! return extractComponent(T_compType(),component,
>! multicomponent_traits<T_numtype>::numComponents);
> }
>
> //////////////////////////////////////////////
>
>
>--------------------------------------------------------------------
>Theodore Papadopoulo
>Email: Theodore.Papadopoulo_at_[hidden] Tel: (33) 04 92 38 76 01
> --------------------------------------------------------------------
>
>
>_______________________________________________
>Blitz-dev mailing list
>Blitz-dev_at_[hidden]
>http://www.oonumerics.org/mailman/listinfo.cgi/blitz-dev
>
>
-- Dr. Julian C. Cummings E-mail: cummings_at_[hidden] California Institute of Technology Phone: 626-395-2543 1200 E. California Blvd., Mail Code 158-79 Fax: 626-584-5917 Pasadena, CA 91125