IBM extension

Initialization of vectors

A vector type is initialized by a vector literal or any expression having the same vector type. For example:

vector unsigned int v1;
vector unsigned int v2 = (vector unsigned int)(10);
v1 = v2;

XL C/C++ extends the AltiVec specification to allow a vector type to be initialized by an initializer list. This feature is an extension for compatibility with GNU C.

Read syntax diagramSkip visual syntax diagramVector initializer list syntax
 
                                    .- ,----------.
                                    V             |
>>-vector_type----identifier--=--{----initializer-+--}--;------><
 

The number of values in a braced initializer list must be less than or equal to the number of elements of the vector type. Any uninitialized element will be initialized to zero.

The following are examples of vector initialization using initializer lists:

vector unsigned int v1 = {1};  // initialize the first 4 bytes of v1 with 1
                               //	 and the remaining 12 bytes with zeros 

vector unsigned int v2 = {1,2};  // initialize the first 8 bytes of v2 with 1 and 2
                                 // and the remaining 8 bytes with zeros

vector unsigned int v3 = {1,2,3,4};  // equivalent to the vector literal
                                     // (vector unsigned int) (1,2,3,4) 

Unlike vector literals, the values in the initializer list do not have to be constant expressions unless the initialized vector variable has static duration. Thus, the following is legal:

int i=1;
int foo() { return 2; }
int main()
{
   vector unsigned int v1 = {i, foo()};
   return 0;
}
End of IBM extension