SPRUIG8J January 2018 – March 2024
The programming model implementation supports the following "swizzle" operators. These operators are used as suffixes to a variable name. They can be used on either side (left or right) of an assignment operator. When used on the left hand side of an assignment, each component must be uniquely identifiable.
char4 my_c4 = char4(1, 2, 3, 4);
char tmp = my_c4.y() * my_c4.w();
/* ".y()" accesses 2nd element; ".w()" accesses 4th element
* tmp = 2 * 4 = 8; */
uchar16 ucvec16 = uchar16(1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16 );
uchar8 ucvec8 = uchar8(2, 4, 6, 8, 10, 12, 14, 16);
int tmp = ucvec16.sa() * ucvec8.s7();
/* ".sa()" is 11th element of ucvec16;
* ".s7()" is 8th element of ucvec8
* tmp = 11 * 16 = 176; */
uchar16 ucvec16 = uchar16(1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16 );
uchar8 ucvec8 = uchar8(2, 4, 6, 8, 10, 12, 14, 16);
int tmp = ucvec16.s[10] * ucvec8.s[7];
/* ".s[10]" is 11th element of ucvec16;
* ".s[7]" is 8th element of ucvec8
* tmp = 11 * 16 = 176; */
ushort4 usvec4 = ushort4(1, 2, 3, 4);
ushort2 usvecodd = usvec4.odd(); /* usvecodd = ushort2(2, 4); */
ushort2 usveceven = usvec4.even(); /* usveceven = ushort2(1, 3); */
ushort8 usvec8 = ushort8(1, 2, 3, 4, 5, 6, 7, 8);
ushort4 usvechi = usvec8.hi(); /* usvechi = ushort4(5, 6, 7, 8);*/
ushort4 usveclo = usvec8.lo(); /* usveclo = ushort4(1, 2, 3, 4); */
cfloat2 cfa = cfloat2(1.0, -2.0, 3.0, -4.0);
float2 rfa = cfa.r(); /* rfa = float2(1.0, 3.0); */
cfloat2 cfa = cfloat2(1.0, -2.0, 3.0, -4.0);
float2 ifa = cfa.i(); /* ifa = float2(-2.0, -4.0); */
Swizzle operators can be combined to access a subset of the subset of elements. The result of the combination must be well-defined. For example, after the following code runs, usvec4 contains (1, 2, 5, 4).
ushort4 usvec4 = ushort4(1, 2, 3, 4);
usvec4.hi().even() = 5;