Link Copied
Hi Denis,
C99 has its standard way to initialize structures by enclosing the initializing values in braces. OpenCL doesn't override this specification.
As for vector elements, the right way to initialize a float4 variable would be:
float4 f = (float4)(0.f,1.f,2.f,3.f);
as stated in the OpenCL 1.1 spec, section 6.1.6.
So the right way to do this whole initialization is:
__constant A d = {(float4)(0,1,2,3),(float2)(4,5),(float4)(6,7,8,9)};
Other forms of initialization might compile, but the result might not be as intended.
For example, __constant A a = {0,1,2,3,4,5,6,7,8,9}; will initialize the first vector (f) with 0 in all its elements, the second vector (g) will be initialized with the value (float2)(1,1), and the third (h) vector will hold (float4)(2,2,2,2). The rest of the initializer list will be thrown away.
Thanks
Guy
I decided to reproducethe casewith anMS C/C++ compiler ( VS 2005 ). A C structure lookslike:
typedef struct tagA
{
float f[4];
float g[2];
float h[4];
} A;
So,after initialization avariable 'v' of a type Alooked like:
v.f[0] = 0
v.f[1] = 1
v.f[2] = 2
v.f[3] = 3
v.g[0]= 4
v.g[1]= 5
v.h[0]= 6
v.h[1]= 7
v.h[2]= 8
v.h[3]= 9
in both cases ( described in the initial post ).
It doesn't matter if you declare a set of numbers for initialization with brackets or without brackets.
Best regards,
Sergey
For more complete information about compiler optimizations, see our Optimization Notice.