AS_TYPEN(3clc)
π REINTERPRETING TYPES (3clc) π
okay bestie, so this page is from the OpenCL Manual and it's literally all about the as_type() and as_typen() operators. it's giving "same bits, new identity" and honestly? iconic. β¨
πΈ NAME πΈ
Reinterpreting_Types_Using_as_type()_and_as_typen()
as_type()as_typen()
π DESCRIPTION π
so. every data type described in scalarDataTypes(3clc) and vectorDataTypes(3clc) can get reinterpreted as another data type of the same size β using as_type() for scalar data types and as_typen() for vector data types.
but notbool, nothalf(unless thecl_khr_fp16(3clc)extension is supported), and notvoid. those three are sitting this one out, no cap. π«
when the operand and the result type contain the same number of elements, the bits in the operand shall be returned directly, without modification, as the new type. and listen β the usual type promotion for function arguments shall not be performed. none of that. π βοΈ
for example, as_float(0x3f800000) returns 1.0f, which is the value that the bit pattern 0x3f800000 has if you view it as an IEEE-754 single precision value. cute, right? π
when the operand and result type contain a different number of elements, the result shall be implementation-defined β EXCEPT if the operand is a 4-component vector and the result is a 3-component vector. in that one specific case, the bits in the operand shall be returned directly without modification as the new type.
that is: a conforming implementation shall explicitly define a behavior, but two conforming implementations need not have the same behavior when the number of elements in the result and operand types does not match. so like, everyone has to pick a lane, but y'all don't have to pick the same lane. π£οΈ
the implementation may define the result to contain all, some or none of the original bits in whatever order it chooses. chaotic? yes. legal? also yes.
β οΈ it is an error to use theas_type()oras_typen()operators to reinterpret data to a type of a different number of bytes. don't do it. it's not the vibe. π
π NOTES π
okay so while the union is intended to reflect the organization of data in memory, the as_type() and as_typen() constructs are intended to reflect the organization of data in register. π§
the as_type() and as_typen() constructs are intended to compile to no instructions on devices that use a shared register file designed to operate on both the operand and result types. free real estate, literally. π₯
note that while differences in memory organization are expected to largely be limited to those arising from endianness, the register based representation may also differ due to size of the element in register. (for example, an architecture may load a char into a 32-bit register, or a char vector into a SIMD vector register with fixed 32-bit element size.) π
if the element count does not match, then the implementation should pick a data representation that most closely matches what would happen if an appropriate result type operator was applied to a register containing data of the source type.
if the number of elements matches, then the as_typen() should faithfully reproduce the behavior expected from a similar data type reinterpretation using memory/unions. so, for example, if an implementation stores all single precision data as double in register, it should implement as_int( float ) by first downconverting the double to single precision and then (if necessary) moving the single precision bits to a register suitable for operating on integer data. slay, but do it correctly. π
if data stored in different address spaces do not have the same endianness, then the "dominant endianness" of the device should prevail. π
β¨ Example β¨
float f = 1.0f; uint u = as_uint(f);
// Legal. Contains: 0x3f800000
float4 f = (float4)(1.0f, 2.0f, 3.0f, 4.0f); // Legal. Contains: (int4)(0x3f800000, 0x40000000, 0x40400000, 0x40800000)
int4 i = as_int4(f);
float4 f, g; int4 is_less = f < g;
// Legal. f[i] = f[i] < g[i] ? f[i] : 0.0f
f = as_float4(as_int4(f) & is_less);
int i; // Legal. Result is implementation-defined.
short2 j = as_short2(i);
int4 i; // Legal. Result is implementation-defined.
short8 j = as_short8(i);
float4 f; //Error. result and operand have different size
double4 g = as_double4(f); // Only if double is supported.
float4 f; // Legal. g.xyz will have same values as f.xyz. g.w is undefined
float3 g = as_float3(f);
π SPECIFICATION π
OpenCL Specification[1]
π¦ SEE ALSO π¦
scalarDataTypes(3clc), vectorDataTypes(3clc)
π AUTHORS π
The Khronos Group
π COPYRIGHT π
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 π
- OpenCL Specification page 212, section 6.2.4.2 - Reinterpreting Types Using as_typen()
okay that's the whole thing, bestie, we did it π this page was brought to you by The Khronos Group, dated 01/14/2021, and it lives under the very glamorous title REINTERPRETING TYPES(3clc). go forth and reinterpret responsibly, mwah π«Άβ¨
π¦ where this page comes from
- ubuntu jammy: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- ubuntu noble: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms