gcc 中的 SSE(SIMD 扩展)支持

发布于 2024-10-10 11:24:30 字数 1035 浏览 0 评论 0原文

我看到如下代码:

#include "stdio.h"

#define VECTOR_SIZE         4
typedef float v4sf __attribute__ ((vector_size(sizeof(float)*VECTOR_SIZE))); 
 // vector of four single floats

typedef union f4vector
{
    v4sf    v;
    float   f[VECTOR_SIZE];
} f4vector;

void print_vector (f4vector *v)
{
    printf("%f,%f,%f,%f\n", v->f[0], v->f[1], v->f[2], v->f[3]);
}

int main()
{
    union f4vector a, b, c;

    a.v = (v4sf){1.2, 2.3, 3.4, 4.5};
    b.v = (v4sf){5., 6., 7., 8.};
    c.v = a.v + b.v;

    print_vector(&a);
    print_vector(&b);
    print_vector(&c);
}

此代码使用 gcc 构建良好并按预期工作(它是内置 SSE / MMX 扩展和向量数据类型。此代码使用 4 个单浮点进行 SIMD 向量加法。

我想详细了解每个浮点数的作用此 typedef 行上的关键字/函数调用的含义和作用是:

typedef float v4sf __attribute__ ((vector_size(sizeof(float)*VECTOR_SIZE))); 

Vector_size() 函数返回是什么;

__attribute__ 关键字是什么

Here is the float data type being type Defined to vfsf type 的

?休息部分,

谢谢,

-AD

I see a code as below:

#include "stdio.h"

#define VECTOR_SIZE         4
typedef float v4sf __attribute__ ((vector_size(sizeof(float)*VECTOR_SIZE))); 
 // vector of four single floats

typedef union f4vector
{
    v4sf    v;
    float   f[VECTOR_SIZE];
} f4vector;

void print_vector (f4vector *v)
{
    printf("%f,%f,%f,%f\n", v->f[0], v->f[1], v->f[2], v->f[3]);
}

int main()
{
    union f4vector a, b, c;

    a.v = (v4sf){1.2, 2.3, 3.4, 4.5};
    b.v = (v4sf){5., 6., 7., 8.};
    c.v = a.v + b.v;

    print_vector(&a);
    print_vector(&b);
    print_vector(&c);
}

This code builds fine and works expectedly using gcc (it's inbuild SSE / MMX extensions and vector data types. this code is doing a SIMD vector addition using 4 single floats.

I want to understand in detail what does each keyword/function call on this typedef line means and does:

typedef float v4sf __attribute__ ((vector_size(sizeof(float)*VECTOR_SIZE))); 

What is the vector_size() function return;

What is the __attribute__ keyword for

Here is the float data type being type defined to vfsf type?

I understand the rest part.

thanks,

-AD

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

农村范ル 2024-10-17 11:24:35

__attribute__ 是 GCC 从编译器公开 C 或 C++ 标准中不包含的功能的方式。
__attribute__((vector_size(x))) 指示 GCC 将类型视为大小为 x 的向量。对于 SSE,这是 16 个字节。

但是,我建议使用各种 <*mmintrin.h> 标头中找到的 __m128、__m128i 或 __m128d 类型。它们在编译器之间更具可移植性。

__attribute__ is GCCs way of exposing functionality from the compiler that isn't in the C or C++ standards.
__attribute__((vector_size(x))) instructs GCC to treat the type as a vector of size x. For SSE this is 16 bytes.

However, I would suggest using the __m128, __m128i or __m128d types found in the various <*mmintrin.h> headers. They are more portable across compilers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文