Vector literals

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.

Read syntax diagramSkip visual syntax diagram
Vector literal syntax

>>-(--vector_type--)--+-(--literal_list--)-+-------------------><
                      '-{--literal_list--}-'   

literal_list

   .-,-------------------.   
   V                     |   
|----constant_expression-+--------------------------------------|

The vector_type is a supported vector type; see Vector data types for a list of these.
The literal_list can be either of the following expressions:
  • A single expression.

    If the single expression is enclosed with parentheses, all elements of the vector are initialized to the specified value. If the single expression is enclosed with braces, the first element of the vector is initialized to the specified value, and the remaining elements of the vector are initialized to 0.

  • A comma-separated list of expressions. Each element of the vector is initialized to the respectively specified value.

    The number of constant expressions is determined by the type of the vector and whether it is enclosed with braces or parentheses.

    If the comma-separated list of expressions is enclosed with braces, the number of constant expressions can be equal to or less than the number of elements in the vector. If the number of constant expressions is less than the number of elements in the vector, the values of the unspecified elements are 0.

    If the comma-separated list of expressions is enclosed with parentheses, the number of constant expressions must match the number of elements in the vector as follows:

    2
    For vector unsigned long long, vector signed long long, vector bool long long, and vector double types.
    4
    For vector unsigned int, vector signed int, and vector bool int types.
    8
    For vector unsigned short, vector signed short, and vector bool short types.
    16
    For vector unsigned char, vector signed char, and vector bool char types.

The following table shows the supported vector literals and how the compiler interprets them to determine their values.

Table 1. Vector literals
Syntax Interpreted by the compiler as

(vector unsigned char)(unsigned int)

A list 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 list of 16 unsigned 8-bit quantities with the value specified by each of the 16 integers.

(vector signed char)(int)

A list of 16 signed 8-bit quantities that all have the value of the single integer.

(vector signed char)(int, ...)

(vector signed char){int, ...}

A list of 16 signed 8-bit quantities with the value specified by each of the 16 integers.

(vector bool char)(unsigned int)

A list 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 list of 16 unsigned 8-bit quantities with a value specified by each of 16 integers.

(vector unsigned short)(unsigned int)

A list 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 list of 8 unsigned 16-bit quantities with a value specified by each of the 8 integers.

(vector signed short)(int)

A list of 8 signed 16-bit quantities that all have the value of the single integer.

(vector signed short)(int, ...)

(vector signed short){int, ...}

A list of 8 signed 16-bit quantities with a value specified by each of the 8 integers.

(vector bool short)(unsigned int)

A list 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 list of 8 unsigned 16-bit quantities with a value specified by each of the 8 integers.

(vector unsigned int)(unsigned int)

A list 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 list of 4 unsigned 32-bit quantities with a value specified by each of the 4 integers.

(vector signed int)(int)

A list of 4 signed 32-bit quantities that all have the value of the single integer.

(vector signed int)(int, ...)

(vector signed int){int, ...}

A list of 4 signed 32-bit quantities with a value specified by each of the 4 integers.

(vector bool int)(unsigned int)

A list 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 list of 4 unsigned 32-bit quantities with a value specified by each of the 4 integers.

(vector unsigned long long)(unsigned long long)

A list 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 list 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)

A list 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 list of 2 signed 64-bit quantities with a value specified by each of the 2 long longs.

(vector bool long long)(unsigned long long)

A list 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 list of 2 boolean 64-bit quantities with a value specified by each of the 2 unsigned long longs.

(vector double)(double)

A list of 2 64-bit IEEE-754 double-precision floating-point quantities that both have the value of the single double.

(vector double)(double, double)

(vector double){double, double}

A list of 2 64-bit IEEE-754 double-precision floating-point quantities with a value specified by each of the 2 doubles.
Note: The value of an element in a vector bool is FALSE if each bit of the element is set to 0 and TRUE if each bit of the element is set to 1.
For example, for an unsigned integer vector type, the literal could be either of the following:
(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))