[cfe-dev] Clang ext_vector_type sizeof()

Steven Joubanian sjoubani at gmail.com
Tue Aug 5 22:09:25 PDT 2014


On Aug 4, 2014, at 1:10 PM, Stephen Canon <scanon at apple.com> wrote:

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

That would work. As you guessed, the goal was to get the element count after defining a struct elsewhere, so data of the appropriate size could be read into it (also to make sure W wasn't used where it was uninitialized). Since ext_vector_type is great for composition, arithmetic, and initialization, I was hoping there was a trick to retrieving the definition. Or something I could use everywhere vectors were used, which would reduce to a constant, like (pseudo code):
#define countof(x) typeof(x) == typeof(vec3) ? 12 : sizeof(x)

Thanks,
S



More information about the cfe-dev mailing list