gpt4 book ai didi

c++ - 按值从 C++ 传递到 C 的复数似乎在 powerpc 上不起作用

转载 作者:可可西里 更新时间:2023-11-01 16:17:13 24 4
gpt4 key购买 nike

当我将复杂的 float(complex.h) 从 C++ 调用程序传递到 C 库时,在 32 位 power 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 和调用帧和函数帧中检查了参数的大小,对于 float 、复数 float 指针、复数 float 和 float ,大小分别为 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 位机器上按值传递。

最佳答案

您的代码会导致未定义的行为。在 C++ 单元中,函数声明为:

extern "C" void procWithComplex(float a, std::complex<float> *pb, std::complex<float> c, float d);

但是函数体是:

void procWithComplex(float a, complex float * pb, complex float  c, float d)

不匹配。

为了帮助编译器诊断此错误,您应该避免使用预处理器为同一函数切换不同的原型(prototype)。

为避免此错误,您需要让函数原型(prototype)仅使用在 C 和 C++ 中均有效的类型。正如您在 myComplex_t 示例中所做的那样。

关于c++ - 按值从 C++ 传递到 C 的复数似乎在 powerpc 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36204706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com