CONVERT_T(3clc)

πŸ“¦
version 1.0~svn33624 Β· 3 versions
, in ubuntu jammy, ubuntu noble

πŸ’– NAME πŸ’–

Explicit_conversions_with_convert_T() β€” explicit type conversions using convert_T(), bestie. That's it, that's the whole vibe.


πŸŽ€ SYNOPSIS πŸŽ€

convert_destType(sourceType) destType
convert_destType<_sat><roundingMode>
(sourceType) destTypen
convert_destTypen<_sat><roundingMode>
(sourceType)

✨ DESCRIPTION ✨

okay so, when you need to deliberately change one type into another, you reach for the convert_destType(sourceType) suite of functions. They give you a full set of type conversions between all the supported types (peep scalarDataTypes(3clc) for the receipts) β€” with a few exceptions that are simply not invited to the party: bool, half, size_t, ptrdiff_t, intptr_t, uintptr_t, and void.

🚨 the number of elements in the source and destination vectors must match. no notes. they have to match.

The behavior of the conversion can be tweaked by one or two optional modifiers that say how to handle out-of-range inputs (saturation) and how to round.

The full form of the scalar convert function is:

destType
convert_destType<_sat><_roundingMode>(sourceType)

The full form of the vector convert function is:

destTypen
convert_destTypen<_sat><_roundingMode>(sourceTypen)

Conversions are available for the following scalar types: char, uchar, short, ushort, int, uint, long, ulong, float, and the built-in vector types derived from those. The operand and result type must have the same number of elements. And lowkey β€” the operand and result type are allowed to be the same type, in which case the conversion has literally no effect on the type or value of the expression. pointless? yes. legal? also yes. πŸ’…

Conversions between integer types follow the conversion rules from 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:

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. πŸ“‹

  • _rte β€” Round to nearest even
  • _rtz β€” Round towards zero
  • _rtp β€” Round toward positive infinity
  • _rtn β€” Round toward negative infinity
  • no modifier specified β€” 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.

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. 😀

πŸ”₯ Out-of-Range Behavior and Saturated Conversions πŸ”₯

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 an 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 β€” so like, don't count on it being the same everywhere, fr fr.

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. The _sat modifier may not be used for conversions to floating-point formats. periodt.

🌸 Examples 🌸

In the following example, convert_int4 converts a uchar4 vector u to an int4 vector c:

uchar4 u; int4   c = convert_int4(u);

In the following example, convert_int converts a float scalar f to an int scalar i:

float   f; int    i = convert_int(f);

Example:

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 );

Example:

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 );

Example:

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 );

πŸ¦‹ SPECIFICATION πŸ¦‹

OpenCL Specification[1]


πŸ‘‘ SEE ALSO πŸ‘‘

scalarDataTypes(3clc), vectorDataTypes(3clc)


πŸ’œ AUTHORS πŸ’œ

The Khronos Group


Copyright Β© 2007-2011 The Khronos Group Inc.

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.


πŸ’– NOTES πŸ’–

  1. OpenCL Specification β€” page 207, section 6.2.3 - Explicit Conversions

and that's the whole page bestie!! 🫢 brought to you by The Khronos Group, dated 01/14/2021, filed under EXPLICIT CONVERSIONS(3clc) β€” go convert something cute βœ¨πŸ’…

πŸ“¦ where this page comes from