Explicit type conversions using convert_`T`()
convert_destType(sourceType) destType convert_destType<_sat><roundingMode> (sourceType) destTypen convert_destTypen<_sat><roundingMode> (sourceType)
Explicit conversions may be performed using the
convert_destType(sourceType) suite of functions.
These provide a full set of type conversions between supported types (see Scalar Data Types) except for the following types:
The number of elements in the source and destination vectors must match.
The behavior of the conversion may be modified by one or two optional modifiers that specify saturation for out-of-range inputs and rounding behavior.
The full form of the scalar convert function is:
The full form of the vector convert function is:
Conversions are available for the following scalar types:
float, and built-in vector types derived therefrom.
The operand and result type must have the same number of elements.
The operand and result type may be the same type in which case the conversion has no effect on the type or value of an expression.
Conversions between integer types follow the conversion rules specified in sections 188.8.131.52 and 184.108.40.206 of the C99 specification except for out-of-range behavior and saturated conversions which are described in section 220.127.116.11 below.
Conversions to and from floating-point type shall conform to IEEE-754 rounding rules. Conversions may have an optional rounding mode modifier described in the table below.
|Modifier||Rounding Mode Description|
Round to nearest even
Round towards zero
Round toward positive infinity
Round toward negative infinity
no modifier specified
Use the default rounding mode for this destination type,
By default, conversions to integer type use the
_rtz (round toward zero) rounding mode and conversions to floating-point type use the default rounding mode.
The only default floating-point rounding mode supported is round to nearest even, i.e the default rounding mode will be
_rte for floating-point types.
For conversions to floating-point format, when a finite source value exceeds the maximum representable finite floating-point destination value, the rounding mode will affect whether the result is the maximum finite floating point value or infinity of same sign as the source value, per IEEE-754 rules for rounding.
When the conversion operand is either greater than the greatest representable destination value or less than the least representable destination value, it is said to be out-of-range. The result of out-of-range conversion is determined by the conversion rules specified by the C99 specification in section 6.3. When converting from a floating-point type to integer type, the behavior is implementation-defined.
Conversions to integer type may opt to convert using the optional saturated mode by appending the
_sat modifier to the conversion function name.
When in saturated mode, values that are outside the representable range shall clamp to the nearest representable value in the destination format. (NaN should be converted to 0).
Conversions to floating-point type shall conform to IEEE-754 rounding rules.
_sat modifier may not be used for conversions to floating-point formats.
convert_ operators for conversion between all types.
C99 does not define what happens when a floating-point type is converted to integer type and the floating-point value lies outside the representable range of the integer type after rounding.
sat variant of the conversion is used, the float shall be converted to the nearest representable integer value.
Similarly, OpenCL also makes recommendations about what should happen with NaN.
Hardware manufacturers that provide the saturated conversion in hardware may use the saturated conversion hardware for both the saturated and non-saturated versions of the OpenCL
OpenCL does not define what happens for the non-saturated conversions when floating-point operands are outside the range representable integers after rounding.
In the following example,
convert_int4 converts a
u to an
uchar4 u; int4 c = convert_int4(u);
In the following example,
convert_int converts a float scalar
f to an int scalar
float f; int i = convert_int(f);
short4 s; // negative values clamped to 0 ushort4 u = convert_ushort4_sat( s ); // values > CHAR_MAX converted to CHAR_MAX // values < CHAR_MIN converted to CHAR_MIN char4 c = convert_char4_sat( s );
float4 f; // values implementation defined for // f > INT_MAX, f < INT_MIN or NaN int4 i = convert_int4( f ); // values > INT_MAX clamp to INT_MAX, values < INT_MIN clamp // to INT_MIN. NaN should produce 0. // The _rtz rounding mode is // used to produce the integer values. int4 i2 = convert_int4_sat( f ); // similar to convert_int4, except that // floating-point values are rounded to the nearest // integer instead of truncated int4 i3 = convert_int4_rte( f ); // similar to convert_int4_sat, except that // floating-point values are rounded to the // nearest integer instead of truncated int4 i4 = convert_int4_sat_rte( f );
int4 i; // convert ints to floats using the default rounding mode. float4 f = convert_float4( i ); // convert ints to floats. integer values that cannot // be exactly represented as floats should round up to the // next representable float. float4 f = convert_float4_rtp( i );