2016-08-03 75 views
2

我正在玩Apple Metal API以及所谓的simd库。有一个头这样的代码:什么是__ext_vector_type__和simd?

typedef __attribute__((__ext_vector_type__(3))) float vector_float3; 

而且我很好奇它实际上做,为什么编译器不允许在一个函数的参数,或者在未来的方式vector_float3引用:

vector_float3 v; 
vector_float3& ref = v; 

回答

3

这似乎是一个铛扩展GNU C vector extensions,在这样的事情是正常的:

typedef int v4si __attribute__ ((vector_size (16))); 

谷歌搜索ext_vector_type没有前导/尾随下划线找到the Clang docs。 IDK为什么增加了一个下划线版本,因为它只在GNU C __attribute__内有效。

我最好的猜测是它的让编译器知道你只关心4个元素SIMD寄存器的前3个元素的值。因此,它可能会进行优化,使高级元素中的垃圾不同于源代码中的垃圾。


为什么编译器不允许vector_float3

作品引用了我clang3.8。尝试不同的编译器。

typedef __attribute__((__ext_vector_type__(3))) float vector_float3; 

int foo(vector_float3 &arg) { 
    vector_float3 v; 
    vector_float3& ref = v; // unused, but syntactically valid 
    return arg[0];   // return the low element of the vector, converted to an integer. GNU C vector-extensions syntax. 
} 

compiles to the following asm on the Godbolt compiler explorer

# targeting x86-64 SystemV ABI (so the first integer/pointer arg is in rdi) 
    cvttss2si  eax, dword ptr [rdi] 
    ret 
+0

感谢您的帮助!因为我看到它不是真正受欢迎的主题 –

+0

@AndrewLavq:这是否回答你的问题?如果是这样,请使用向上/向下投票箭头下方的复选框将其标记为已接受。如果不是,请编辑该问题以清楚说明你真正想要问什么。 –