我正在尝试在我的C程序中使用SIMD指令。我正在使用CodeBlocks进行写入。
我正在尝试做整数和浮点SIMD加法、减法等。
但是,页面中解释的代码在CodeBlocks/C中不起作用。我如何在这里使用SIMD?
#include <stdio.h>
typedef int v4sf __attribute__ ((mode(V4SF))); // vector of four single floats
union f4vector
{
v4sf v;
float f[4];
};
int main()
{
union f4vector a, b, c;
a.f[0] = 1; a.f[1] = 2; a.f[2] = 3; a.f[3] = 4;
b.f[0] = 5; b.f[1] = 6; b.f[2] = 7; b.f[3] = 8;
c.v = a.v + b.v;
printf("%f, %f, %f, %f\n", c.f[0], c.f[1], c.f[2], c.f[3]);
}C:\Things\New Text Document.c|2|warning: specifying vector types with __attribute__ ((mode)) is deprecated [-Wattributes]|
C:\Things\New Text Document.c|2|warning: use __attribute__ ((vector_size)) instead [-Wattributes]|
C:\Things\New Text Document.c|2|error: mode 'V4SF' applied to inappropriate type|
||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|发布于 2014-09-27 03:08:57
您尝试使用的教程是用于Cell CPU的SIMD编程(即,在PlayStation3中)。它不适用于x86编程。
使用适用于您正在使用的编译器(GCC、Clang或Visual C++)的教程。
发布于 2014-09-27 03:51:06
在编译和/或执行之前,您需要确保您的CPU支持您想要使用的向量类型内部函数和向量指令。
我猜你的CPU是x86的,但是Windows应该有办法验证这一点。在Linux中,你可以运行像grep avx2 /proc/cpuinfo这样的东西。
发布于 2018-07-27 15:42:35
tuition有错误,type起始日期类型应该是float。
对于编译直通示例,如下所示
#include <stdio.h>
typedef float v4sf __attribute__((vector_size(16)));
union f4vector
{
v4sf v;
float f[4];
};
int main()
{
union f4vector a, b, c;
a.f[0] = 1; a.f[1] = 2; a.f[2] = 3; a.f[3] = 4;
b.f[0] = 5; b.f[1] = 6; b.f[2] = 7; b.f[3] = 8;
c.v = a.v + b.v;
printf("%f, %f, %f, %f\n", c.f[0], c.f[1], c.f[2], c.f[3]);
}https://stackoverflow.com/questions/26065980
复制相似问题