public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: complex - direct access to members?
@ 1999-01-31 23:58 Jan Reimers
  1999-01-31 23:58 ` Joe Buck
  1999-01-31 23:58 ` Gabriel Dos Reis
  0 siblings, 2 replies; 11+ messages in thread
From: Jan Reimers @ 1999-01-31 23:58 UTC (permalink / raw)
  To: 'nbecker@fred.net'; +Cc: 'egcs@cygnus.com'

What about an implementation that stores polar values R,Theta?  The
following code sets real(a), and generates a lot assembly code (egcs g++
1.1).  In particular it appears that the inlined a.imag=imag(a)
assignment does not get optimized away.

#include <complex>

complex<double> a(1.0,1.0);

void foo(double myreal)
{
  a=complex<double>(myreal,imag(a)); //Reset real(a).
}

Assembly after -O2 compilation:

_foo__Fd:
	pushl %ebp
	movl %esp,%ebp
	subl $16,%esp
	fldl _a+8
	fldl 8(%ebp)
	fstpl -16(%ebp)
	movl -16(%ebp),%eax
	fstpl -8(%ebp)
	movl %eax,_a
	movl $_a,%edx
	movl -12(%ebp),%eax
	movl %eax,4(%edx)
	movl -8(%ebp),%eax
	movl %eax,8(%edx)
	movl -4(%ebp),%eax
	movl %eax,12(%edx)
	movl %ebp,%esp
	popl %ebp
	ret



JR

> ----------
> From: 	nbecker@fred.net[SMTP:nbecker@fred.net]
> Sent: 	Tuesday, January 12, 1999 5:52 AM
> To: 	egcs@cygnus.com
> Subject: 	complex - direct access to members?
> 
> As a heavy user of c++ for numeric computation, I think it would be a
> great improvement if we made a simple modification to complex, so that
> 
> the member functions real() and imag() returned a reference.  This
> would allow modification of the values directly.  I really can't see
> any argument against it.  I don't think this violates any
> encapsulation.  I can't imagine any implementation of complex<double>
> in which there is not somewhere a double value for the real and for
> the imaginary parts.
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: complex - direct access to members?
@ 1999-01-31 23:58 Mike Stump
  1999-01-31 23:58 ` Gabriel Dos Reis
  0 siblings, 1 reply; 11+ messages in thread
From: Mike Stump @ 1999-01-31 23:58 UTC (permalink / raw)
  To: Gabriel.Dos-Reis, nbecker; +Cc: egcs

You can lobby ANSI to `clarify' this point, if you want...  See
comp.std.c++...  Best bet.  If you want, you can also to this in user
code, with apparently no loss in performace.

// You want:

//	c.imag() = 12;

// You can only do:

//	c = C(c.real(), 12)

// First, we define helper classes to adapt the syntax:

template <class T>
class Hi {
  T& val;
public:
  Hi(T& t) : val(t) { }
  template <class Y>
  Hi& operator = (const Y& y) {
    val = T(val.real (), y);
    return *this;
  }
  template <class Y>
  operator Y() {
    return val.imag();
  }
};

template <class T>
class Hr {
  T& val;
public:
  Hr(T& t) : val(t) { }
  template <class Y>
  Hr& operator = (const Y& y) {
    val = T(y, val.imag());
    return *this;
  }
  template <class Y>
  operator Y() {
    return val.real();
  }
};

#include <complex.h>

template <class T>
class MyComplex {
  complex<T> i;
public:
  const Hi<complex<T> > imag() { return Hi<complex<T> >(i); }
};

void foo1();
void foo2();
void foo3();

int main() {
  complex<float> i;
  MyComplex<float> j;

  foo1();

  i = complex<float>(i.real(),12);

  foo2();

  j.imag() = 12;

  foo3();
}


Simple, easy, reasonably direct, all brought to you by the power of
C++.  Yes, it is a bit of a pain, but I think you can do it.  The
above obviously isn't complete, and you might find other ways to do
it, the above is only a starting point, but enough to prove the idea.

^ permalink raw reply	[flat|nested] 11+ messages in thread
* complex - direct access to members?
@ 1999-01-31 23:58 nbecker
  1999-01-31 23:58 ` Joe Buck
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: nbecker @ 1999-01-31 23:58 UTC (permalink / raw)
  To: egcs

As a heavy user of c++ for numeric computation, I think it would be a
great improvement if we made a simple modification to complex, so that 
the member functions real() and imag() returned a reference.  This
would allow modification of the values directly.  I really can't see
any argument against it.  I don't think this violates any
encapsulation.  I can't imagine any implementation of complex<double>
in which there is not somewhere a double value for the real and for
the imaginary parts.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~1999-01-31 23:58 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-01-31 23:58 complex - direct access to members? Jan Reimers
1999-01-31 23:58 ` Joe Buck
1999-01-31 23:58 ` Gabriel Dos Reis
  -- strict thread matches above, loose matches on Subject: below --
1999-01-31 23:58 Mike Stump
1999-01-31 23:58 ` Gabriel Dos Reis
1999-01-31 23:58 nbecker
1999-01-31 23:58 ` Joe Buck
1999-01-31 23:58 ` Gabriel Dos Reis
1999-01-31 23:58   ` nbecker
1999-01-31 23:58 ` Craig Burley
1999-01-31 23:58   ` Michael Hayes

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).