[cfe-dev] Clang ext_vector_type sizeof()

Stephen Canon scanon at apple.com
Mon Aug 4 10:10:24 PDT 2014


> On Aug 1, 2014, at 4:35 PM, Robinson, Paul <Paul_Robinson at playstation.sony.com> wrote:
> 
>> Is there any way of obtaining the declared size of a vector defined as:
>> 	typedef float vec3 __attribute__((ext_vector_type(3)));
>> Using sizeof(vec3) gives 16, I assume because 4 is faster than 3, but I
>> can't find anything which will give "3" or "12" at compile-time or
>> runtime. Certainly sizeof(((vec3)0).x) gives me sizeof(float), but that's
>> as far as I got.
> 
> The Clang documentation is pretty sketchy, but googling
> "opencl vector data types example" got me to this page:
> http://www.informit.com/articles/article.aspx?p=1732873&seqNum=3
> which says:
> For 3-component vector data types, the size of the data type is 
> 4 x sizeof(component). This means that a 3-component vector data type will be 
> aligned to a 4 x sizeof(component) boundary.
> 
> So, the actual size of a vec3 is 16 presumably because OpenCL says so,
> and you won't find any way to get "12" as an answer. Also seems unlikely
> you'd find a way to get "3" out of it.

As Paul wrote, sizeof(vec3) really is 16; there’s no way to make it 12.  What are you really trying to do?  If you’re e.g. trying to get an element count for generic code, you can do something like the following:

	int __attribute__((overloadable)) count(vec3 x) { return 3; }
	int __attribute__((overloadable)) count(vec4 x) { return 4; }

– Steve



More information about the cfe-dev mailing list