Unary and binary operators
Vector data types can use some of the unary operators and binary 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.
Unary operatorsFor unary operators, each element in the vector has the operation applied to it.
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 |
*1 | Yes | Yes | Yes |
- Although the preferred alignment for vector data types is at a multiple of 16 bytes, pointers can point to vector objects at any alignment.
For binary operators, each element has the operation applied to it with the same position element in the second operand. Binary operators also include relational operators. 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.
Operator | Integer vector types | Vector double | Bool vector types |
---|---|---|---|
= | Yes | Yes | Yes |
+ | 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 |
[]1 | Yes | Yes | Yes |
== | Yes | Yes | Yes |
!= | Yes | Yes | Yes |
< | Yes | Yes | No |
> | Yes | Yes | No |
<= | Yes | Yes | No |
>= | Yes | Yes | No |
- The [] operator returns the vector element at the position specified. If the position specified is outside of the valid range, the behavior is undefined.
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.