[Patch/RFC] libstdc++/20610

Paolo Carlini pcarlini@suse.de
Thu Mar 24 10:31:00 GMT 2005


Hi,

Fredrik noticed that the code we produce for complex * real is
suboptimal. The primary template for operator*= is:

  template<typename _Tp>
    complex<_Tp>&
    complex<_Tp>::operator*=(const _Tp& __t)
    {
      _M_real *= __t;
      _M_imag *= __t;
      return *this;
    }

whereas the specializations read (f.i., for double):

  inline complex<double>&
  complex<double>::operator*=(double __d)
  {
    _M_value *= __d;
    return *this;
  }

A simple fix for this issue is expanding the latter to:

  inline complex<double>&
  complex<double>::operator*=(double __d)
  {
    __real__ _M_value *= __d;
    __imag__ _M_value *= __d;
    return *this;
  }

To give you an example, the assembly (for -O3 -march=pentium4
-mfpmath=sse) goes from:

_Z1fv:
.LFB1859:
        pushl   %ebp
.LCFI0:
        movl    %esp, %ebp
.LCFI1:
        movsd   b+8, %xmm3
        movsd   b, %xmm2
        movsd   c, %xmm4
        pxor    %xmm5, %xmm5
        movapd  %xmm2, %xmm0
        mulsd   %xmm5, %xmm0
        movapd  %xmm4, %xmm1
        mulsd   %xmm3, %xmm1
        addsd   %xmm1, %xmm0
        movsd   %xmm0, a+8
        mulsd   %xmm4, %xmm2
        mulsd   %xmm5, %xmm3
        subsd   %xmm3, %xmm2
        movsd   %xmm2, a
        popl    %ebp
        ret

to:

_Z1fv:
.LFB1859:
        pushl   %ebp
.LCFI0:
        movl    %esp, %ebp
.LCFI1:
        movsd   b, %xmm0
        movsd   c, %xmm2
        movapd  %xmm2, %xmm1
        mulsd   b+8, %xmm1
        movsd   %xmm1, a+8
        mulsd   %xmm2, %xmm0
        movsd   %xmm0, a
        popl    %ebp
        ret

Indeed, we have also a compiler problem here, because in principle,
for builtin complex types, the compiler should be able to expand
efficiently complex * real, still, while making sure that the issue
is investigated by the middle-end maintainers, the above change
probably cannot hurt (would be 4.0 material).

Comments?

In case, probably we should prepare something similar for
operator/= too.

Tested x86-linux.

Paolo.

////////////////
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CL_20610
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20050324/ca879a4c/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patch_20610
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20050324/ca879a4c/attachment-0001.ksh>


More information about the Libstdc++ mailing list