1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook MathML Module V1.1b1//EN"
3
"http://www.oasis-open.org/docbook/xml/mathml/1.1CR1/dbmathml.dtd">
9
Explicit conversions with convert_T()
15
<refentrytitle>Explicit conversions with convert_T()</refentrytitle>
19
<year>2007-2011</year>
20
<holder>The Khronos Group Inc.
21
Permission is hereby granted, free of charge, to any person obtaining a
22
copy of this software and/or associated documentation files (the
23
"Materials"), to deal in the Materials without restriction, including
24
without limitation the rights to use, copy, modify, merge, publish,
25
distribute, sublicense, and/or sell copies of the Materials, and to
26
permit persons to whom the Materials are furnished to do so, subject to
27
the condition that this copyright notice and permission notice shall be included
28
in all copies or substantial portions of the Materials.</holder>
31
<manvolnum>3</manvolnum>
34
<!-- ================================ SYNOPSIS -->
36
<refnamediv id="convert_T">
37
<refname>Explicit conversions with convert_T()</refname>
40
Explicit type conversions using convert_<varname>T</varname>()
44
<!-- ALTERNATIVE SYNTAX SYNOPSIS (NON-FUNCTION) -->
46
<refsect2 id="synopsis">
50
<informaltable frame="none">
51
<tgroup cols="1" align="left" colsep="0" rowsep="0">
52
<colspec colname="col1" colnum="1" />
56
convert_<replaceable>destType</replaceable>(<replaceable>sourceType</replaceable>)
57
<replaceable>destType</replaceable> convert_<replaceable>destType</replaceable><_sat><<replaceable>roundingMode</replaceable>> (<replaceable>sourceType</replaceable>)
58
<replaceable>destTypen</replaceable> convert_<replaceable>destTypen</replaceable><_sat><<replaceable>roundingMode</replaceable>> (<replaceable>sourceType</replaceable>)
66
<!-- ================================ DESCRIPTION -->
68
<refsect1 id="description"><title>Description</title>
70
Explicit conversions may be performed using the <code>convert_destType(sourceType)</code>
71
suite of functions. These provide a full set of type conversions between
72
supported types (see <citerefentry href="scalarDataTypes"><refentrytitle>Scalar Data
73
Types</refentrytitle></citerefentry>)<!--section 6.1.1--> except for the following types:
74
<type>bool</type>, <type>half</type>, <type>size_t</type>, <type>ptrdiff_t</type>,
75
<type>intptr_t</type>, <type>uintptr_t</type>, and <type>void</type>.
79
The number of elements in the source and destination vectors must match.
83
The behavior of the conversion may be modified by one or two optional modifiers that
84
specify saturation for out-of-range inputs and rounding behavior.
88
The full form of the scalar convert function is:
89
<literallayout> <code><replaceable>destType</replaceable>
90
convert_<replaceable>destType</replaceable><_sat><<replaceable>_roundingMode</replaceable>>(<replaceable>sourceType</replaceable>)</code></literallayout>
94
The full form of the vector convert function is:
95
<literallayout> <code><replaceable>destTypen</replaceable>
96
convert_<replaceable>destTypen</replaceable><_sat><<replaceable>_roundingMode</replaceable>>(<replaceable>sourceTypen</replaceable>)</code></literallayout>
99
<bridgehead>Data Types</bridgehead>
102
Conversions are available for the following scalar types: <type>char</type>,
103
<type>uchar</type>, <type>short</type>, <type>ushort</type>, <type>int</type>,
104
<type>uint</type>, <type>long</type>, <type>ulong</type>, <type>float</type>, and
105
built-in vector types derived therefrom. The operand and result type must have the
106
same number of elements. The operand and result type may be the same type in which
107
case the conversion has no effect on the type or value of an expression.
111
Conversions between integer types follow the conversion rules specified in sections
112
6.3.1.1 and 6.3.1.3 of the C99 specification except for out-of-range behavior and
113
saturated conversions which are described in section 6.2.3.3 below.
116
<bridgehead>Rounding Modes</bridgehead>
119
Conversions to and from floating-point type shall conform to IEEE-754 rounding rules.
120
Conversions may have an optional rounding mode modifier described in the table below.
124
<informaltable frame="all">
125
<tgroup cols="2" align="left" colsep="1" rowsep="1">
126
<colspec colname="col1" colnum="1" />
127
<colspec colname="col2" colnum="2" />
131
<entry>Modifier</entry>
132
<entry>Rounding Mode Description</entry>
138
<entry><para><code>_rte</code></para></entry>
139
<entry><para>Round to nearest even</para></entry>
143
<entry><para><code>_rtz</code></para></entry>
144
<entry><para>Round towards zero</para></entry>
148
<entry><para><code>_rtp</code></para></entry>
149
<entry><para>Round toward positive infinity</para></entry>
153
<entry><para><code>_rtn</code></para></entry>
154
<entry><para>Round toward negative infinity</para></entry>
158
<entry><para>no modifier specified</para></entry>
161
Use the default rounding mode for this destination type,
162
<code>_rtz</code> for conversion to integers or the default
163
rounding mode for conversion to floating-point types.
173
By default, conversions to integer type use the <code>_rtz</code> (round toward zero)
174
rounding mode and conversions to floating-point type use the default rounding mode. The
175
only default floating-point rounding mode supported is round to nearest even, i.e
176
the default rounding mode will be <code>_rte</code> for floating-point types.
180
For conversions to floating-point format, when a finite source value exceeds the
181
maximum representable finite floating-point destination value, the rounding mode will
182
affect whether the result is the maximum finite floating point value or infinity of
183
same sign as the source value, per IEEE-754 rules for rounding.
187
<bridgehead>Out-of-Range Behavior and Saturated Conversions</bridgehead>
191
When the conversion operand is either greater than the greatest representable
192
destination value or less than the least representable destination value, it is
193
said to be out-of-range. The result of out-of-range conversion is determined by the
194
conversion rules specified by the C99 specification in section 6.3. When converting
195
from a floating-point type to integer type, the behavior is implementation-defined.
199
Conversions to integer type may opt to convert using the optional saturated mode by
200
appending the <code>_sat</code> modifier to the conversion function name. When in
201
saturated mode, values that are outside the representable range shall clamp to the
202
nearest representable value in the destination format. (NaN should be converted to 0).
206
Conversions to floating-point type shall conform to IEEE-754 rounding rules. The
207
<code>_sat</code> modifier may not be used for conversions to floating-point formats.
211
<!-- ================================ NOTES -->
214
<refsect1 id="notes"><title>Notes</title>
218
<!-- ================================ EXAMPLE -->
220
<refsect2 id="example0">
226
In the following example, <function>convert_int4</function> converts a
227
<type>uchar4</type> vector <code>u</code> to an <type>int4</type> vector <code>c</code>:
230
<informaltable frame="none">
231
<tgroup cols="1" align="left" colsep="0" rowsep="0">
232
<colspec colname="col1" colnum="1" />
237
int4 c = convert_int4(u);
245
In the following example, <function>convert_int</function> converts a float scalar
246
<code>f</code> to an int scalar <code>i</code>:
249
<informaltable frame="none">
250
<tgroup cols="1" align="left" colsep="0" rowsep="0">
251
<colspec colname="col1" colnum="1" />
256
int i = convert_int(f);
264
<refsect2 id="example1">
272
<informaltable frame="none">
273
<tgroup cols="1" align="left" colsep="0" rowsep="0">
274
<colspec colname="col1" colnum="1" />
280
// negative values clamped to 0
281
ushort4 u = convert_ushort4_sat( s );
283
// values > CHAR_MAX converted to CHAR_MAX
284
// values < CHAR_MIN converted to CHAR_MIN
285
char4 c = convert_char4_sat( s );
293
<refsect2 id="example2">
301
<informaltable frame="none">
302
<tgroup cols="1" align="left" colsep="0" rowsep="0">
303
<colspec colname="col1" colnum="1" />
309
// values implementation defined for
310
// f > INT_MAX, f < INT_MIN or NaN
311
int4 i = convert_int4( f );
313
// values > INT_MAX clamp to INT_MAX, values < INT_MIN clamp
314
// to INT_MIN. NaN should produce 0.
316
// The _rtz rounding mode is
317
// used to produce the integer values.
318
int4 i2 = convert_int4_sat( f );
320
// similar to convert_int4, except that
321
// floating-point values are rounded to the nearest
322
// integer instead of truncated
323
int4 i3 = convert_int4_rte( f );
325
// similar to convert_int4_sat, except that
326
// floating-point values are rounded to the
327
// nearest integer instead of truncated
328
int4 i4 = convert_int4_sat_rte( f );
336
<refsect2 id="example3">
344
<informaltable frame="none">
345
<tgroup cols="1" align="left" colsep="0" rowsep="0">
346
<colspec colname="col1" colnum="1" />
352
// convert ints to floats using the default rounding mode.
353
float4 f = convert_float4( i );
355
// convert ints to floats. integer values that cannot
356
// be exactly represented as floats should round up to the
357
// next representable float.
358
float4 f = convert_float4_rtp( i );
366
<!-- ================================ SPECIFICATION -->
367
<!-- Set the "uri" attribute in the <olink /> element to the "named destination" for the PDF page
369
<refsect1 id="specification"><title>Specification</title>
372
<imagedata fileref="pdficon_small1.gif" format="gif" />
375
<olink uri="convert_T">OpenCL Specification</olink>
379
<!-- ================================ ALSO SEE -->
381
<refsect1 id="seealso"><title>Also see</title>
383
<citerefentry href="scalarDataTypes"><refentrytitle>Scalar Data Types</refentrytitle></citerefentry>,
384
<citerefentry href="vectorDataTypes"><refentrytitle>Vector Data Types</refentrytitle></citerefentry>
388
<!-- ============================== COPYRIGHT -->
389
<!-- Content included from copyright.inc.xsl -->
391
<refsect3 id="Copyright"><title></title>
393
<imagedata fileref="KhronosLogo.jpg" format="jpg" />