简体   繁体   English

将struct传递给c函数

[英]Passing struct to function in c

So, I am trying to add two vectors using some assembly code but I have problem with: 因此,我尝试使用一些汇编代码来添加两个向量,但是我遇到了问题:

sse_vector4_add(set_vector(2,2,2,2),set_vector(4,2,4,2));

I am not sure how to pass those vectors to that function. 我不确定如何将这些向量传递给该函数。

typedef struct vector4 {   
    float x, y, z, w;       
} vector4;

// Set values to a vector4
vector4 set_vector(float x, float y, float z, float w) {
    vector4 ret_vector = { x, y, z, w };
    return ret_vector;
}

// Use sse to add the elements of two vectors a + b
vector4 sse_vector4_add(vector4 *op_a,vector4 *op_b) {
    vector4 ret_vector;
    asm( 
        "mov %%eax,op_a \n"             // Load pointers into CPU regs
        "mov %%ebx,op_b \n"
        "movups %%XMM0,(%%EAX) \n"        // Move the vectors to SSE regs
        "movups %%XMM1,(%%EBX) \n"
        "addps  %%XMM0,%%XMM1 \n"       // Add elements
        "movups (ret_vector),%%XMM0 \n" // Save the return vector
        :
    );
    return ret_vector;
}

void main() {
    // Add two vectors
    sse_vector4_add(set_vector (2,2,2,2),set_vector(4,2,4,2));
    getchar();
}

Function sse_vector4_add was made from a similar function but in C++. 函数sse_vector4_add由类似的函数制成,但使用C ++。

Create variables, using set_vector to initialize them. 创建变量,使用set_vector进行初始化。 Then, pass their addresses to the function. 然后,将其地址传递给函数。

vector4 a = set_vector(2,2,2,2);
vector4 b = set_vector(4,2,4,2);
vector4 c = sse_vector4_add(&a, &b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM