altivec

Can someone show me using xcode 3.0 how to write a standard tool that creates two unsigned char 16 component altivec vectors, adds them and prints them out. What #includes are required? What build settings need to be changed? etc.

1.6 ghz g5 1.5g ram, Mac OS X (10.5)

Posted on Dec 24, 2007 11:43 AM

Reply
2 replies

Dec 26, 2007 1:38 PM in response to Richard Gleeson

using a standard tool project and setting the -faltivec flag the following code:


#include <stdio.h>
#include <Accelerate/Accelerate.h>

int main (int argc, const char * argv[]) {

// vector unsigned char v0 = (vector unsigned char) ('H','e','l','l','o',' ','W','o','r','l','d',' ','1','2','3','4');

vUInt8 v1 = (vUInt8) {'H','e','l','l','o',' ','W','o','r','l','d',' ','1','2','3','4'};
vUInt8 v2 = (vUInt8) {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
vUInt8 v3=v2;
printf("message = %vc\n", v1);
printf("v1 = %vd\n", v1);
printf("v2 = %vd\n", v2);
printf("v3 = %vd\n", v3);
printf("v3 = %vhd\n", v3);
return 0;
}


gives this output:

[Session started at 2007-12-26 13:22:41 -0800.]
message = Hello World 1234
v1 = 72 101 108 108 111 32 87 111 114 108 100 32 49 50 51 52
v2 = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
v3 = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
v3 = 257 257 257 257 257 257 257 257

The Debugger has exited with status 0.

However, I can't get the commented out code to work and I don't know where to go from here inorder to play with the altivec ops.

I'm hoping if I can get some basic things going then I can the use the Freescale documents to learn with.

Help. Thanks.

Dec 27, 2007 9:30 AM in response to Richard Gleeson

Semi answering my own question. The compiler used by xcode apparently is different than the compiler used in the terminal. xcode recognizes the vector printf formats but doesn't recognize the standard altivec notation "vector vec_add(" etc. The compiler in the terminal recognizes the standard altivec notation but doesn't recognize the vector printf formats. Any way the following works in the terminal:


#include <stdio.h>
#include <Accelerate/Accelerate.h>
//gcc -Wall -faltivec -o dog dogaltivec.c
int main (int argc, const char * argv[]) {
vector unsigned char v0;
vUInt8 v1 = (vUInt8) {'H','e','l','l','o',' ','W','o','r','l','d',' ','1','2','3','4'};
vUInt8 v2 = (vUInt8) {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
vUInt8 v3=v2;
v0 = vec_add(v2,v3);

typedef union{
unsigned char c[16];
vector unsigned char vc;
}align;
align mychars;
int i;

mychars.vc = *((vector unsigned char *) &v1);
printf( "v1 = ");
for ( i = 0; i < 16; i++ ) printf( "%c ", mychars.c );
printf( "\n");
mychars.vc = *((vector unsigned char *) &v1);
printf( "v1 = ");
for ( i = 0; i < 16; i++ ) printf( "%i ", mychars.c
);
printf( "\n");

mychars.vc = *((vector unsigned char *) &v2);
printf( "v2 = ");
for ( i = 0; i < 16; i++ ) printf( "%i ", mychars.c );
printf( "\n");
mychars.vc = *((vector unsigned char *) &v3);
printf( "v3 = ");
for ( i = 0; i < 16; i++ ) printf( "%i ", mychars.c
);
printf( "\n");

mychars.vc = *((vector unsigned char *) &v0);
printf( "v0 = ");
for ( i = 0; i < 16; i++ ) printf( "%i ", mychars.c );
printf( "\n");
return 0;
}
and gives the output:
wish:gcc -Wall -faltivec -o dog dogaltivec.c
wish:./dog
v1 = H e l l o W o r l d 1 2 3 4
v1 = 72 101 108 108 111 32 87 111 114 108 100 32 49 50 51 52
v2 = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
v3 = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
v0 = 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
wish:

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

altivec

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.