SPRUIG8J January 2018 – March 2024
The as_<destination type>(<source type object>) functions are provided to re-interpret the original type of an object as another vector type. The source type and destination type must be the same size in number of bits. An error is returned if the sizes are different.
Reinterpretations of Boolean vectors that do not result in each element containing exactly 0x0 or 0x1 are undefined. The following shows examples where the result is define or undefined:
ushort2 myshort2_0 = ushort2(0,1);
bool4 mybool4_0 = as_bool4(myshort2_0); // Defined
ushort2 myshort2_1 = ushort2(2,3);
bool4 mybool4_1 = as_bool4(myshort2_1); // Undefined
bool8 mybool8_0 = bool8(0,1,0,1,0,1,0,1);
float2 myfloat2_0 = as_float2(mybool8_0); // Defined
float2 myfloat2_1 = float2(1.0,2.0);
bool8 mybool8_1 = as_bool8(myfloat2_1); // Undefined
While arithmetic conversion is performed by the conversion functions described in the previous section, no arithmetic conversion is performed by the re-interpretation functions. For example, suppose a float value of 1.0 is re-interpreted as an int value. Since the float value of 1.0 is represented in hex as 0x3f800000, the value in the resulting int is 1,065,353,216.
The following example reinterprets a non-vector variable of the long type (64 bits) to a float2 vector (2 elements of 32 bits each). The least significant 32-bits of mylong are placed in fltvec2.s0 and the most significant 32-bits of mylong are placed in fltvec2.s1. No arithmetic conversion is performed.
extern long mylong;
float2 fltvec2 = as_float2(mylong);
If the sizes of the source and destination types are different, an error occurs.
If vector data types are enabled, you can also use the as_<type>() functions for scalar (non-vector) types. The types must have the same number of bits. The following example re-interprets a float value as an int value. Since the float value of 1.0 is represented in hex as 0x3f800000, the value in the resulting int is 1,065,353,216.
float myfloat = 1.0f;
myint = as_int(myfloat);