Search code examples
c++cmakefilesimdaltivec

SIMD program compilation issue


I have a simple SIMD program for vector addition

/*
 * FILE: vec_add.c
 */
#include <stdio.h>
#include <altivec.h>

/*
 * declares input/output scalar varialbes
 */
int a[4] __attribute__((aligned(16))) = { 1, 3, 5, 7 };
int b[4] __attribute__((aligned(16))) = { 2, 4, 6, 8 };
int c[4] __attribute__((aligned(16)));

int main(int argc, char **argv)
{
    /*
     * declares vector variables which points to scalar arrays
     */
    __vector signed int *va = (__vector signed int *) a;
    __vector signed int *vb = (__vector signed int *) b;
    __vector signed int *vc = (__vector signed int *) c;

    /*
     * adds four signed intergers at once
     */
    *vc = vec_add(*va, *vb);    // 1 + 2, 3 + 4, 5 + 6, 7 + 8

    /*
     * output results
     */
    printf("c[0]=%d, c[1]=%d, c[2]=%d, c[3]=%d\n", c[0], c[1], c[2], c[3]);

    return 0;
}

I am trying to compile this program using the Makefile here

CC     = gcc
CFLAGS = -maltivec -mabi=altivec

SOURCE = vec_add.c
TARGET = vec_add.elf

$(TARGET): $(SOURCE)
    $(CC) $(CFLAGS) $^ -o $@

clean:
    rm -f *.elf

On compiling the program ..I am getting the following error

pp@pa-Inspiron-N5050:~/Desktop/Proj/SIMD$ make
gcc -maltivec -mabi=altivec vec_add.c -o vec_add.elf
gcc: error: unrecognized argument in option ‘-mabi=altivec’
gcc: note: valid arguments to ‘-mabi=’ are: ms sysv
gcc: error: unrecognized command line option ‘-maltivec’
make: *** [vec_add.elf] Error 1

On simply compiling without using Makefile. I am getting

SIMD$ gcc vec_add.c
vec_add.c:5:21: fatal error: altivec.h: No such file or directory
 #include <altivec.h>
                     ^
compilation terminated.

So, I downloaded the altivec.h file and put it in the folder but still it is giving the same error.

I dont understand what is the issue with using -maltivec option. Is there some other way of compiling this?


Solution

  • Your program is written for PowerPC Altivec (aka VMX) SIMD extension, and your compiler is for x86. You should either use PowerPC cross-compiler or rewrite your code for x86 SIMD extensions (SSE or AVX).