Reputation: 207
I am trying to use SIMD instructions in my C program. I am using CodeBlocks to write in.
I tried following this tutorial: https://www.kernel.org/pub/linux/kernel/people/geoff/cell/ps3-linux-docs/CellProgrammingTutorial/BasicsOfSIMDProgramming.html
I am trying to do both integer and floating point SIMD addition, subtraction, etc.
However, the code explained in the page does not work in CodeBlocks/C. How do I use SIMD here?
#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)) ===|
Upvotes: 0
Views: 807
Reputation: 11
the tuition has something wrong that the typedef origin date type should be the float.
For a compile through example is as follow
#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]);
}
Upvotes: 1
Reputation: 3917
You need to make sure your CPU supports the vector type intrinsics and vector instructions you want to use before you compile and/or execute.
I'm guessing your CPU is x86, but Windows should have a way to verify that. With Linux you can run something like grep avx2 /proc/cpuinfo
.
Upvotes: 1
Reputation:
The tutorial you are trying to use is for SIMD programming for the Cell CPU (i.e, in the Playstation 3). It is not applicable to x86 programming.
Use a tutorial that is applicable to the compiler you are using (GCC, Clang, or Visual C++).
Upvotes: 2