'\" t .\" Title: Explicit conversions with convert_T() .\" Author: The Khronos Group .\" Generator: DocBook XSL Stylesheets vsnapshot .\" Date: 01/14/2021 .\" Manual: OpenCL Manual .\" Source: The Khronos Group .\" Language: English .\" .TH "EXPLICIT CONVERSIONS" "3clc" "01/14/2021" "The Khronos Group" "OpenCL Manual" .\" ----------------------------------------------------------------- .\" * Define some portability stuff .\" ----------------------------------------------------------------- .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .\" http://bugs.debian.org/507673 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" ----------------------------------------------------------------- .\" * set default formatting .\" ----------------------------------------------------------------- .\" disable hyphenation .nh .\" disable justification (adjust text to left margin only) .ad l .\" ----------------------------------------------------------------- .\" * MAIN CONTENT STARTS HERE * .\" ----------------------------------------------------------------- .SH "NAME" Explicit_conversions_with_convert_T() \- Explicit type conversions using convert_T() .SS "" .TS tab(:); l. T{ convert_\fIdestType\fR(\fIsourceType\fR) \fIdestType\fR convert_\fIdestType\fR<_sat><\fIroundingMode\fR> (\fIsourceType\fR) \fIdestTypen\fR convert_\fIdestTypen\fR<_sat><\fIroundingMode\fR> (\fIsourceType\fR) T} .TE .sp 1 .SH "DESCRIPTION" .PP 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 \fBscalarDataTypes\fR(3clc)) except for the following types: \fBbool\fR, \fBhalf\fR, \fBsize_t\fR, \fBptrdiff_t\fR, \fBintptr_t\fR, \fBuintptr_t\fR, and \fBvoid\fR\&. .PP The number of elements in the source and destination vectors must match\&. .PP 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\&. .PP The full form of the scalar convert function is: .sp .if n \{\ .RS 4 .\} .nf \fIdestType\fR convert_\fIdestType\fR<_sat><\fI_roundingMode\fR>(\fIsourceType\fR) .fi .if n \{\ .RE .\} .PP The full form of the vector convert function is: .sp .if n \{\ .RS 4 .\} .nf \fIdestTypen\fR convert_\fIdestTypen\fR<_sat><\fI_roundingMode\fR>(\fIsourceTypen\fR) .fi .if n \{\ .RE .\} .spData Types.PP Conversions are available for the following scalar types: \fBchar\fR, \fBuchar\fR, \fBshort\fR, \fBushort\fR, \fBint\fR, \fBuint\fR, \fBlong\fR, \fBulong\fR, \fBfloat\fR, 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\&. .PP Conversions between integer types follow the conversion rules specified in sections 6\&.3\&.1\&.1 and 6\&.3\&.1\&.3 of the C99 specification except for out\-of\-range behavior and saturated conversions which are described in section 6\&.2\&.3\&.3 below\&. Rounding Modes.PP 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\&. .PP .TS allbox tab(:); lB lB. T{ Modifier T}:T{ Rounding Mode Description T} .T& l l l l l l l l l l. T{ .PP _rte T}:T{ .PP Round to nearest even T} T{ .PP _rtz T}:T{ .PP Round towards zero T} T{ .PP _rtp T}:T{ .PP Round toward positive infinity T} T{ .PP _rtn T}:T{ .PP Round toward negative infinity T} T{ .PP no modifier specified T}:T{ .PP Use the default rounding mode for this destination type, _rtz for conversion to integers or the default rounding mode for conversion to floating\-point types\&. T} .TE .sp 1 .PP 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\&. .PP 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\&. .PP Out\-of\-Range Behavior and Saturated Conversions .PP 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\&. .PP 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)\&. .PP Conversions to floating\-point type shall conform to IEEE\-754 rounding rules\&. The _sat modifier may not be used for conversions to floating\-point formats\&. .SS "Examples" .PP In the following example, \fBconvert_int4\fR converts a \fBuchar4\fR vector u to an \fBint4\fR vector c: .TS tab(:); l. T{ uchar4 u; int4 c = convert_int4(u); T} .TE .sp 1 .PP In the following example, \fBconvert_int\fR converts a float scalar f to an int scalar i: .TS tab(:); l. T{ float f; int i = convert_int(f); T} .TE .sp 1 .SS "" .PP Example: .TS tab(:); l. T{ 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 ); T} .TE .sp 1 .SS "" .PP Example: .TS tab(:); l. T{ 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 ); T} .TE .sp 1 .SS "" .PP Example: .TS tab(:); l. T{ 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 ); T} .TE .sp 1 .SH "SPECIFICATION" .PP \m[blue]\fBOpenCL Specification\fR\m[]\&\s-2\u[1]\d\s+2 .SH "SEE ALSO" .PP \fBscalarDataTypes\fR(3clc), \fBvectorDataTypes\fR(3clc) .SH "AUTHORS" .PP \fBThe Khronos Group\fR .SH "COPYRIGHT" .br Copyright \(co 2007-2011 The Khronos Group Inc. .br Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the condition that this copyright notice and permission notice shall be included in all copies or substantial portions of the Materials. .sp .SH "NOTES" .IP " 1." 4 OpenCL Specification .RS 4 \%page 207, section 6.2.3 - Explicit Conversions .RE