Complex numbers passed by-value from C++ to C does not seem to work on powerpc
<p>When I'm passing a complex float(complex.h) from a c++ caller to a c library, the value does not pass correctly when running on a 32 bit power pc.  I was using two different open source software libraries when I detected this problem.  I've isolated it down to the boundry of when C++ is passing a complex value type to a pure C type function.  I wrote up some simple code to demonstrate it.</p>

<pre><code>#ifndef MMYLIB_3A8726C1_H
#define MMYLIB_3A8726C1_H

typedef struct aComplexStructure {
    float r;
    float i;
} myComplex_t;

#ifdef __cplusplus
#include &lt;complex&gt;
extern "C" {
    void procWithComplex(float a, std::complex&lt;float&gt; *pb, std::complex&lt;float&gt; c, float d);
    void procWithStruct(float a, myComplex_t *pb, myComplex_t c, float d);
}

#else  /* __cplusplus */

#include &lt;complex.h&gt;
void procWithComplex(float a, float complex *pb, float complex c, float d);
void procWithStruct(float a, myComplex_t *pb, myComplex_t c, float d);

#endif

#endif /* MYLIB_3A8726C1_H */
</code></pre>

<p>The source C file is as follows</p>

<pre><code>#include &lt;stdio.h&gt;
#include "myLib.h"

void procWithComplex(float a, complex float * pb, complex float  c, float d)
{
    printf("a=%f\n", a);
    printf("b=%f + %fi\n", creal(*pb), cimag(*pb));
    printf("c=%f + %fi\n", creal(c), cimag(c));
    printf("d=%f\n", d);
}


void procWithStruct(float a, myComplex_t* pb, myComplex_t c, float d)
{
    printf("a=%f\n", a);
    printf("b=%f + %fi\n", pb-&gt;r, pb-&gt;i);
    printf("c=%f + %fi\n", c.r, c.i);
    printf("d=%f\n", d);
}
</code></pre>

<p>The calling C++ program is as follows</p>

<pre><code>#include &lt;iostream&gt;
#include "myLib.h"

int main()
{
    float a = 1.2;
    std::complex&lt;float&gt; b = 3.4 + 3.4I;
    std::complex&lt;float&gt; c = 5.6 + 5.6I;
    float d = 9.876;

    myComplex_t b_s, c_s;

    b_s.r = b.real();
    b_s.i = b.imag();

    c_s.r = c.real();
    c_s.i = c.imag();

    std::cout &lt;&lt; "a=" &lt;&lt; a &lt;&lt; std::endl;
    std::cout &lt;&lt; "b=" &lt;&lt; b &lt;&lt; std::endl;
    std::cout &lt;&lt; "c=" &lt;&lt; c &lt;&lt; std::endl;
    std::cout &lt;&lt; "d=" &lt;&lt; d &lt;&lt; std::endl &lt;&lt; std::endl;

    // c is a 64 bit structure being passed by value.
    // on my 32 bit embedded powerpc platform, it is being
    // passed by reference, but the underlying C library is
    // reading it by value.
    procWithComplex(a, &amp;b, c, d);
    std::cout &lt;&lt; std::endl;

    // This is only here to demonstrate that a 64 bit value field
    // does pass through the C++ to C boundry
    procWithStruct(a, &amp;b_s, c_s, d);
    return 0;
}
</code></pre>

<p>Normally I would expect the output to be</p>

<pre><code>a=1.2
b=(3.4,3.4)
c=(5.6,5.6)
d=9.876

a=1.200000
b=3.400000 + 3.400000i
c=5.600000 + 5.600000i
d=9.876000

a=1.200000
b=3.400000 + 3.400000i
c=5.600000 + 5.600000i
d=9.876000
</code></pre>

<p>But when I run the source on an embedded power pc machine I get output that shows that the value type for complex is not being passed properly.</p>

<pre><code>a=1.2
b=(3.4,3.4)
c=(5.6,5.6)
d=9.876

a=1.200000
b=3.400000 + 3.400000i
c=-0.000000 + 9.876000i
d=0.000000

a=1.200000
b=3.400000 + 3.400000i
c=5.600000 + 5.600000i
d=9.876000
</code></pre>

<p>I checked the sizeof the paramaters from gdb and from both the calling and the function frame the sizes are 4 bytes, 4 bytes, 8 bytes and 4 bytes, for the float, complex float pointer, complex float, and float.  </p>

<p>I realize I can just change the complex value parameter as a pointer, or my own struct when crossing the c++ to c boundry, but I want to know why I can't pass a complex value type from c++ to c on a power pc.  </p>

<p>I created another example only this time I dumped some of the assembly as well as the register values.</p>

<pre><code>int x = 22;
std::complex&lt;float&gt; y = 55 + 88I;
int z = 77;
void simpleProc(int x, complex float y, int z)
</code></pre>

<p>Right before call where the parameters are passed in.  </p>

<pre><code>x = 22
y =  {_M_value = 55 + 88 * I} 
Looking at raw data *(int*)&amp;y = 1113325568
z = 77
</code></pre>

<p>This should be assembly code where it saves the the return address and saves the paramters to pass into the routine.</p>

<pre><code>x0x10000b78 &lt;main()+824&gt; lwz     r9,40(r31)
x0x10000b7c &lt;main()+828&gt; stw     r9,72(r31)
x0x10000b80 &lt;main()+832&gt; lwz     r9,44(r31)
x0x10000b84 &lt;main()+836&gt; stw     r9,76(r31)   
x0x10000b88 &lt;main()+840&gt; addi    r9,r31,72    
x0x10000b8c &lt;main()+844&gt; lwz     r3,16(r31)   
x0x10000b90 &lt;main()+848&gt; mr      r4,r9       
x0x10000b94 &lt;main()+852&gt; lwz     r5,20(r31)   
x0x10000b98 &lt;main()+856&gt; bl      0x10000f88 &lt;simpleProc&gt;  
</code></pre>

<p>Looking at the assembly right after the branch :)</p>

<pre><code>x0x10000f88 &lt;simpleProc&gt;         stwu    r1,-48(r1)  
x0x10000f8c &lt;simpleProc+4&gt;       mflr    r0      
x0x10000f90 &lt;simpleProc+8&gt;       stw     r0,52(r1)   
x0x10000f94 &lt;simpleProc+12&gt;      stw     r29,36(r1)  
x0x10000f98 &lt;simpleProc+16&gt;      stw     r30,40(r1)  
x0x10000f9c &lt;simpleProc+20&gt;      stw     r31,44(r1)  
x0x10000fa0 &lt;simpleProc+24&gt;      mr      r31,r1      
x0x10000fa4 &lt;simpleProc+28&gt;      stw     r3,8(r31)  
x0x10000fa8 &lt;simpleProc+32&gt;      stw     r5,12(r31) 
x0x10000fac &lt;simpleProc+36&gt;      stw     r6,16(r31)  
x0x10000fb0 &lt;simpleProc+40&gt;      stw     r7,20(r31)  
x0x10000fb4 &lt;simpleProc+44&gt;      lis     r9,4096  
</code></pre>

<p>These are the value once we are completely in the routine (after variable values are assigned.</p>

<pre><code>x = 22
y =  1.07899982e-43 + 0 * I
z = 265134296

$r3 = 22
$r4 = 0x9ffff938
*(int*)$r4 = 1113325568
$r5 = 77

*(int*)(&amp;y) = 77
</code></pre>

<p>My laymans view is it looks like the C++ is passing the complex value type as a reference or pointer type? but C is treating it like a value type?  So is this a problem with gcc on the power pc?  I am using gcc4.7.1.  I am in the process of building gcc4.9.3 as a cross compiler on another machine.  I will update this post either way once I get the output from a newer compiler working.</p>

<p>Having problems getting the cross compiler working, but looking at the memory dump of the original problem, it does show that on the power pc platform, the complex value is not being passed by value.  I put the example of the struct here to show that a 64 bit value can be pass through by value, on a 32 bit machine.</p>