0

I'm trying to follow a simple sample altivec initialization like this:

 1 // Example1.c
 2 #include <stdio.h>
   #include <altivec.h>
 3
 4 int main()
 5 {
 6 __vector unsigned char v1;
 7
 8 // Assign 16 8-bit elements to vector v1
 9 v1 = (__vector unsigned char)(
10 '0', '1', '2', '3',
11 '4', '5', '6', '7',
12 '8', '9', 'A', 'B',
13 'C', 'D', 'E', 'F');
14
15 // Print out the contents of v1 char by char
16 // &v1 is casted to (unsigned char *)
17 for(int i = 0; i < 16; i++)
18 printf("%c", ((unsigned char *)(&v1))[i]);;
19 
20
21 return 0;
22 }

However, when I compile I get an error saying error: can't convert between vector values of different size.

Would anyone know why this is so? My gcc version is 4.4.6.

I get it working now with Paul's solution.

HOwever, when I try to print it, it's giving me error as I commented under Paul's comment.

Would anyone know why thi sis so?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Tal_
  • 761
  • 2
  • 6
  • 13

1 Answers1

0

You need to set the vector values as part of the initialisation:

 __vector unsigned char v1 = (__vector unsigned char)( '0', '1', '2', ... );

However with gcc you should probably use the more normal syntax with curly braces, rather than the old Motorola syntax:

 __vector unsigned char v1 = { '0', '1', '2', ... };

In order to print the vector, some versions of gcc allow for a Motorola extension to printf for AltiVec vectors, e.g.

printf("%vc\n", v1);

If that doesn't work then you may need to roll your own print routines, e.g. using a union:

void vec_print_u8(__vector unsigned char v)
{
    union {
        __vector unsigned char v;
        unsigned char a[16];
    } u;
    int i;

    u.v = v;
    for (i = 0; i < 16; ++i)
    {
        printf("%2c", u.a[i]);
    }
    printf("\n");
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Hi, with the first line I still wouldn't be able to initialize it. the second one works, however, when I tried to print it with printf("%d\n", (unsigned char )v1[i]); I get an error saying rabind.c:91: error: subscripted value is neither array nor pointer Would you know how I could access each element in the vector? – Tal_ Sep 16 '13 at 18:35