SPRUI04G June 2015 – August 2025
When an arithmetic operator is applied to a vector, the operator is applied to each element in the vector in parallel. Each element in the resulting vector is the result of applying the operator to the corresponding elements in the source vector(s).
The following tables list operations supported for vectors. The "Legal For" column indicates whether the operator can be used with integer vectors, float vectors, complex vectors, or multiple vector types.
| Operator | Description | Legal For |
|---|---|---|
| - | negate | all |
| + | returns value unchanged (does not make the value positive) | all |
| ~ | bitwise complement | integer |
| ! | logical not | integer, float |
| & | address of a vector object, which is a pointer to the vector (see Section 7.15.2.1 for examples) | all |
| ++, -- | increment / decrement operators (prefix and postfix) | integer, complex(1) |
The following example declares an int4 vector called pos_i4 and initializes it to the values 1, 2, 3, and 4. It then uses the negate operator to initialize the values of another int4 vector, neg_i4, to the values -1, -2, -3, and -4.
int4 pos_i4 = int4(1, 2, 3, 4);
int4 neg_i4 = -pos_i4;
| Operator | Description | Legal For |
|---|---|---|
| +, - , *, / | arithmetic operators | all |
| =, +=, -=, *=, /=, | assignment operators | integer, float |
| % | modulo operator | integer |
| &, |, ^, <<, >> | bitwise operators | integer |
| &=, |=, ^=, <<=, >>= | bitwise compound assignment operators | integer |
| >, >=, ==, !=, <=, < | relational operators | integer, float |
| &&, || | logical operators. Note that &&= and ||= are not supported. | integer, float |
Unlike operations on scalar values, operations on vectors do not perform the usual arithmetic conversions. For example, if you add two vectors of type char, the result is a vector of char, not a vector of int.
For all operators, each input vector must have the same element count and type as other inputs. Resulting vectors will have the same element count as the inputs. For arithmetic operators, the result has the same type as the input vectors. For logical and comparison operators, the result has integer elements of the same size as the elements of the input vector. For example, (float4==float4) gives an int4, and double8==double8 gives a longlong8. Every lane of the result of a vector binary logical operator is all-bits-set (-1) (for true) or 0 (for false).
The following example uses the =, ++, and + operators on vectors of type int4. Assume that the iv4 argument initially contains (1, 2, 3, 4). On exit from foo(), iv4 will contain (3, 4, 5, 6).
void foo(int4 iv4)
{
int4 local_iva = iv4++; /* local_iva = (1, 2, 3, 4) */
int4 local_ivb = iv4++; /* local_ivb = (2, 3, 4, 5) */
int4 local_ivc = local_iva + local_ivb; /* local_ivc = (3, 5, 7, 9) */
}
The arithmetic operators and increment / decrement operators can be used with complex vector types. The increment / decrement operators add or subtract by 1+0i.
The following example multiplies and divides complex vectors of type cfloat2. For details about the rules for complex multiplication and division, please see Annex G of the C99 C language specification.
void foo()
{
cfloat2 va = cfloat2 (1.0, -2.0, 3.0, -4.0);
cfloat2 vb = cfloat2 (4.0, -2.0, -4.0, 2.0);
/* vc = (0.0, -10.0), (-4.0, 22.0) */
cfloat2 vc = va * vb;
/* vd = (0.4, -0.3), (-1.0, 0.5) */
cfloat2 vd = va / vb;
...
}
On C64+ and C6740, the * and / operators in the previous example call a built-in function to perform the complex multiply and divide operations. On C6600, the compiler generates a CMPYSP instruction to carry out the complex multiply or divide operation.