A vector literal is a constant expression for which the value is interpreted as a vector type. The data type of a vector literal is represented by a parenthesized vector type, and its value is a set of constant expressions that represent the vector elements and are enclosed in parentheses or braces. When all vector elements have the same value, the value of the literal can be represented by a single constant expression. You can initialize vector types with vector literals.
The vector_type is a supported vector type; see Vector types (IBM extension) for a list of these.Vector literal syntax .-,-----------------------. V | >>-(--vector_type--)--+-(-+------constant_expression---+--+-)-+->< '-{-' '-}-'
All elements of the vector are initialized to the specified value.
Each element of the vector is initialized to the respectively specified value. The number of the constant expressions must be exactly:
The following table shows the supported vector literals and how the compiler interprets them to determine their values.
| Syntax | Interpreted by the compiler as |
|---|---|
(vector unsigned char)(unsigned int) (vector unsigned char){unsigned int} |
A set of 16 unsigned 8-bit quantities that all have the value of the single integer. |
(vector unsigned char)(unsigned int, ...) (vector unsigned char){unsigned int, ...} |
A set of 16 unsigned 8-bit quantities with the value specified by each of the 16 integers. |
(vector signed char)(int) (vector signed char){int} |
A set of 16 signed 8-bit quantities that all have the value of the single integer. |
(vector signed char)(int, ...) (vector signed char){int, ...} |
A set of 16 signed 8-bit quantities with the value specified by each of the 16 integers. |
(vector bool char)(unsigned int) (vector bool char){unsigned int} |
A set of 16 unsigned 8-bit quantities that all have the value of the single integer. |
(vector bool char)(unsigned int, ...) (vector bool char){unsigned int, ...} |
A set of 16 unsigned 8-bit quantities with a value specified by each of 16 integers. |
(vector unsigned short)(unsigned int) (vector unsigned short){unsigned int} |
A set of 8 unsigned 16-bit quantities that all have the value of the single integer. |
(vector unsigned short)(unsigned int, ...) (vector unsigned short){unsigned int, ...} |
A set of 8 unsigned 16-bit quantities with a value specified by each of the 8 integers. |
(vector signed short)(int) (vector signed short){int} |
A set of 8 signed 16-bit quantities that all have the value of the single integer. |
(vector signed short)(int, ...) (vector signed short){int, ...} |
A set of 8 signed 16-bit quantities with a value specified by each of the 8 integers. |
(vector bool short)(unsigned int) (vector bool short){unsigned int} |
A set of 8 unsigned 16-bit quantities that all have the value of the single integer. |
(vector bool short)(unsigned int, ...) (vector bool short){unsigned int, ...} |
A set of 8 unsigned 16-bit quantities with a value specified by each of the 8 integers. |
(vector unsigned int)(unsigned int) (vector unsigned int){unsigned int} |
A set of 4 unsigned 32-bit quantities that all have the value of the single integer. |
(vector unsigned int)(unsigned int, ...) (vector unsigned int){unsigned int, ...} |
A set of 4 unsigned 32-bit quantities with a value specified by each of the 4 integers. |
(vector signed int)(int) (vector signed int){int} |
A set of 4 signed 32-bit quantities that all have the value of the single integer. |
(vector signed int)(int, ...) (vector signed int){int, ...} |
A set of 4 signed 32-bit quantities with a value specified by each of the 4 integers. |
(vector bool int)(unsigned int) (vector bool int){unsigned int} |
A set of 4 unsigned 32-bit quantities that all have the value of the single integer. |
(vector bool int)(unsigned int, ...) (vector bool int){unsigned int, ...} |
A set of 4 unsigned 32-bit quantities with a value specified by each of the 4 integers. |
(vector unsigned long long)(unsigned long long) (vector unsigned long long){unsigned long long} |
A set of 2 unsigned 64-bit quantities that both have the value of the single long long. |
(vector unsigned long long)(unsigned long long, ...) (vector unsigned long long){unsigned long long, ...} |
A set of 2 unsigned 64-bit quantities specified with a value by each of the 2 unsigned long longs. |
(vector signed long long)(signed long long) (vector signed long long){signed long long} |
A set of 2 signed 64-bit quantities that both have the value of the single long long. |
(vector signed long long)(signed long long, ...) (vector signed long long){signed long long, ...} |
A set of 2 signed 64-bit quantities with a value specified by each of the 2 long longs. |
(vector bool long long)(unsigned long long) (vector bool long long){unsigned long long} |
A set of 2 boolean 64-bit quantities with a value specified by the single unsigned long long. |
(vector bool long long)(unsigned long long, ...) (vector bool long long){unsigned long long, ...} |
A set of 2 boolean 64-bit quantities with a value specified by each of the 2 unsigned long longs. |
(vector float)(float) (vector float){float} |
A set of 4 32-bit single-precision floating-point quantities that all have the value of the single float. |
(vector float)(float, ...) (vector float){float, ...} |
A set of 4 32-bit single-precision floating-point quantities with a value specified by each of the 4 floats. |
(vector double)(double) (vector double){double} |
A set of 2 64-bit double-precision floating-point quantities that both have the value of the single double. |
(vector double)(double, double) (vector double){double, double} |
A set of 2 64-bit double-precision floating-point quantities with a value specified by each of the 2 doubles. |
(vector pixel)(unsigned int) (vector pixel){unsigned int} |
A set of 8 unsigned 16-bit quantities that all have the value of the single integer. |
(vector pixel)(unsigned int, ...) (vector pixel){unsigned int, ...} |
A set of 8 unsigned 16-bit quantities with a value specified by each of the 8 integers. |
(vector unsigned int)(10) /* initializes all four elements to a value of 10 */
(vector unsigned int)(14, 82, 73, 700) /* initializes the first element
to 14, the second element to 82,
the third element to 73, and the
fourth element to 700 */
You can cast vector literals with the Cast operator (). Enclosing the vector literal to be cast in parentheses can improve the readability of the code. For example, you can use the following code to cast a vector signed int literal to a vector unsigned char literal:
(vector unsigned char)((vector signed int)(-1, -1, 0, 0))