【发布时间】:2016-07-12 07:26:48
【问题描述】:
当我将复杂的 float(complex.h) 从 c++ 调用程序传递到 c 库时,在 32 位电源 PC 上运行时,该值无法正确传递。当我检测到这个问题时,我正在使用两个不同的开源软件库。我已经将它隔离到 C++ 将复杂值类型传递给纯 C 类型函数的边界。我写了一些简单的代码来演示一下。
#ifndef MMYLIB_3A8726C1_H
#define MMYLIB_3A8726C1_H
typedef struct aComplexStructure {
float r;
float i;
} myComplex_t;
#ifdef __cplusplus
#include <complex>
extern "C" {
void procWithComplex(float a, std::complex<float> *pb, std::complex<float> c, float d);
void procWithStruct(float a, myComplex_t *pb, myComplex_t c, float d);
}
#else /* __cplusplus */
#include <complex.h>
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 */
源C文件如下
#include <stdio.h>
#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->r, pb->i);
printf("c=%f + %fi\n", c.r, c.i);
printf("d=%f\n", d);
}
调用C++程序如下
#include <iostream>
#include "myLib.h"
int main()
{
float a = 1.2;
std::complex<float> b = 3.4 + 3.4I;
std::complex<float> 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 << "a=" << a << std::endl;
std::cout << "b=" << b << std::endl;
std::cout << "c=" << c << std::endl;
std::cout << "d=" << d << std::endl << 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, &b, c, d);
std::cout << std::endl;
// This is only here to demonstrate that a 64 bit value field
// does pass through the C++ to C boundry
procWithStruct(a, &b_s, c_s, d);
return 0;
}
通常我希望输出是
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
但是当我在嵌入式 power pc 机器上运行源代码时,我得到的输出显示 complex 的值类型没有被正确传递。
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
我从 gdb 以及调用和函数框架中检查了参数的大小,浮点数、复数浮点数指针、复数浮点数和浮点数的大小分别为 4 字节、4 字节、8 字节和 4 字节。
我意识到我可以将复杂值参数更改为指针,或者在跨越 c++ 到 c 边界时更改我自己的结构,但我想知道为什么我不能将复杂值类型从 c++ 传递给 c电源电脑。
我创建了另一个示例,只是这次我转储了一些程序集以及寄存器值。
int x = 22;
std::complex<float> y = 55 + 88I;
int z = 77;
void simpleProc(int x, complex float y, int z)
就在调用之前传入参数的地方。
x = 22
y = {_M_value = 55 + 88 * I}
Looking at raw data *(int*)&y = 1113325568
z = 77
这应该是汇编代码,它保存返回地址并保存要传递给例程的参数。
x0x10000b78 <main()+824> lwz r9,40(r31)
x0x10000b7c <main()+828> stw r9,72(r31)
x0x10000b80 <main()+832> lwz r9,44(r31)
x0x10000b84 <main()+836> stw r9,76(r31)
x0x10000b88 <main()+840> addi r9,r31,72
x0x10000b8c <main()+844> lwz r3,16(r31)
x0x10000b90 <main()+848> mr r4,r9
x0x10000b94 <main()+852> lwz r5,20(r31)
x0x10000b98 <main()+856> bl 0x10000f88 <simpleProc>
查看分支后的程序集:)
x0x10000f88 <simpleProc> stwu r1,-48(r1)
x0x10000f8c <simpleProc+4> mflr r0
x0x10000f90 <simpleProc+8> stw r0,52(r1)
x0x10000f94 <simpleProc+12> stw r29,36(r1)
x0x10000f98 <simpleProc+16> stw r30,40(r1)
x0x10000f9c <simpleProc+20> stw r31,44(r1)
x0x10000fa0 <simpleProc+24> mr r31,r1
x0x10000fa4 <simpleProc+28> stw r3,8(r31)
x0x10000fa8 <simpleProc+32> stw r5,12(r31)
x0x10000fac <simpleProc+36> stw r6,16(r31)
x0x10000fb0 <simpleProc+40> stw r7,20(r31)
x0x10000fb4 <simpleProc+44> lis r9,4096
这些是我们完全进入例程后的值(在分配变量值之后。
x = 22
y = 1.07899982e-43 + 0 * I
z = 265134296
$r3 = 22
$r4 = 0x9ffff938
*(int*)$r4 = 1113325568
$r5 = 77
*(int*)(&y) = 77
我的外行观点是看起来 C++ 将复杂值类型作为引用或指针类型传递?但是 C 将其视为值类型?那么这是power pc上gcc的问题吗?我正在使用 gcc4.7.1。我正在构建 gcc4.9.3 作为另一台机器上的交叉编译器。一旦我从较新的编译器获得输出,我将更新这篇文章。
在使交叉编译器工作时遇到问题,但查看原始问题的内存转储,确实表明在 power pc 平台上,复杂值不是按值传递的。我把结构体的例子放在这里是为了说明一个64位的值可以在32位的机器上按值传递。
【问题讨论】:
-
这不是问题,但是以下划线开头后跟大写字母 (
_MYLIB_H_) 的名称和包含两个连续下划线的名称保留给实现。不要使用它们。 -
您是否尝试过在您的 C 代码部分的 power pc 上创建预期的复杂值,并转储预期值的十六进制值和从 C++ 部分作为输入接收的值以查看它们如何不一样?
-
@John 他们是不兼容的,一个是类模板实例,另一个是内置类型,它们的参数传递约定预计不会相同,甚至如果二进制布局幸运地重合。
-
我同意@n.m。如果您希望 C 代码和 C++ 代码互操作,请对两者使用相同的类型。
myComplex_t起作用的原因是因为它对于您的 C 和 C++ 代码是相同的类型。 -
这只是我尝试运行代码(使用 gcc 4.7.1 构建)时的问题。在 32 位 powerpc 嵌入式平台上。在使用 gcc 4.8.5 编译的 x86_64 GNU/Linux 平台上,通过 c++ 到 c 边界传递复杂的浮点数可以正常工作。我目前无法访问其他平台,因此我无法验证这是否发生在除 power pc 之外的其他平台上。我怀疑它与电源 pc 是隔离的,因为这似乎其他人以前会遇到过这个问题。
标签: c++ c pass-by-value complex-numbers powerpc