Vector types

XL C/C++ supports vector processing technologies through language extensions. XL C/C++ implements and extends the AltiVec Programming Interface specification. In the extended syntax, type qualifiers and storage class specifiers can precede the keyword vector (or its alternative spelling, __vector) in a declaration.

Most of the legal forms of the syntax are captured in the following diagram. Some variations have been omitted from the diagram for the sake of clarity: type qualifiers such as const and storage class specifiers such as static can appear in any order within the declaration, as long as neither immediately follows the keyword vector (or __vector).

Read syntax diagramSkip visual syntax diagram
Vector declaration syntax

   .-----------------------------.   
   V                             |   
>>---+-------------------------+-+------------------------------>
     +-type_qualifier----------+     
     '-storage_class_specifier-'     

>--+-vector---+-+-+-bool-----+--+-char-----------+-+-----------><
   '-__vector-' | +-signed---+  +-short--+-----+-+ |   
                | '-unsigned-'  |        '-int-' | |   
                |               +-int------------+ |   
                |               '-long long------' |   
                +-pixel----------------------------+   
                +-__pixel--------------------------+   
                +-float----------------------------+   
                '-double---------------------------'   

Notes:
  1. The keyword vector is recognized in a declaration context only when used as a type specifier and when vector support is enabled. The keywords pixel, __pixel and bool are recognized as valid type specifiers only when preceded by the keyword vector or __vector.
  2. Duplicate type specifiers are ignored in a vector declaration context.

The following table lists the supported vector data types, the size and possible values for each type.

Table 1. Vector data types
Type Interpretation of content Range of values
vector unsigned char 16 unsigned char 0..255
vector signed char 16 signed char -128..127
vector bool char 16 unsigned char 0, 255
vector unsigned short 8 unsigned short 0..65535
vector unsigned short int
vector signed short 8 signed short -32768..32767
vector signed short int
vector bool short 8 unsigned short 0, 65535
vector bool short int
vector unsigned int 4 unsigned int 0..232-1
vector signed int 4 signed int -231..231-1
vector bool int 4 unsigned int 0, 232-1
vector unsigned long long 2 unsigned long long 0..264-1
vector bool long long 0, 264-1
vector signed long long 2 signed long long -263..263-1
vector float 4 float IEEE-754 single (32 bit) precision floating-point values
vector double 2 double IEEE-754 double (64 bit) precision floating-point values
vector pixel 8 unsigned short 1/5/5/5 pixel

All vector types are aligned on a 16-byte boundary. An aggregate that contains one or more vector types is aligned on a 16-byte boundary, and padded, if necessary, so that each member of vector type is also 16-byte aligned.

Vector data type operators

Vector data types can use some of the unary, binary, and relational operators that are used with primitive data types. Note that all operators require compatible types as operands unless otherwise stated. These operators are not supported at global scope or for objects with static duration, and there is no constant folding.

For unary operators, each element in the vector has the operation applied to it.

Table 2. Unary operators
Operator Integer vector types Vector double Bool vector types
++ Yes Yes No
−− Yes Yes No
+ Yes Yes No
Yes (except unsigned vectors) Yes No
~ Yes No Yes

For binary operators, each element has the operation applied to it with the same position element in the second operand. Binary operators also include assignment operators.

Table 3. Binary operators
Operator Integer vector types Vector double Bool vector types
+ Yes Yes No
Yes Yes No
* Yes Yes No
/ Yes Yes No
% Yes No No
& Yes No Yes
| Yes No Yes
^ Yes No Yes
<< Yes No Yes
>> Yes No Yes
[] Yes Yes Yes
Note: The [] operator returns the vector element at the position specified. If the position specified is outside of the valid range, the behavior is undefined.

For relational operators, each element has the operation applied to it with the same position element in the second operand and the results have the AND operator applied to them to get a final result of a single value.

Table 4. Relational operators
Operator Integer vector types Vector double Bool vector types
== Yes Yes Yes
!= Yes Yes Yes
< Yes Yes No
> Yes Yes No
<= Yes Yes No
>= Yes Yes No
For the following code:
vector unsigned int a = {1,2,3,4};
vector unsigned int b = {2,4,6,8};
vector unsigned int c = a + b;
int e = b > a;
int f = a[2];
vector unsigned int d = ++a;
c would have the value (3,6,9,12), d would have the value (2,3,4,5), e would have a non-zero value, and f would have the value 3.

Pointer arithmetic

Pointer arithmetic is defined for pointer to vector types. Given:
vector unsigned int *v;
the expression v + 1 represents a pointer to the vector following v.

Vector type casts

Vector types can be cast to other vector types. The cast does not perform a conversion: it preserves the 128-bit pattern, but not necessarily the value. A cast between a vector type and a scalar type is not allowed.

Vector pointers and pointers to non-vector types can be cast back and forth to each other. When a pointer to a non-vector type is cast to a vector pointer, the address should be 16-byte aligned. The referenced object of the pointer to a non-vector type can be aligned on a 16-byte boundary by using __attribute__((aligned(16))).



Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us