错误:在C中强制转换用户定义的数据类型 [英] Error : casting user defined data types in c
问题描述
这是我的问题的更简单视图,我想将浮点值转换为已定义的类型v4si(我想使用SIMD操作进行优化.)请帮助将浮点/双精度值转换为已定义的类型.
This is a simpler view of my Problem, I want to convert a float value into defined type v4si (I want to use SIMD Operation for optimization.) Please help to convert float/double value to a defined type.
#include<stdio.h>
typedef double v4si __attribute__ ((vector_size (16)));
int main()
{
double stoptime=36000;
float x =0.5*stoptime;
float * temp = &x;
v4si a = ((v4si)x); // Error: Incompatible data types
v4si b;
v4si *c;
c = ((v4si*)&temp); // Copies address of temp,
b = *(c);
printf("%f\n" , b); // but printing (*c) crashes program
}
推荐答案
You appear to be using GCC vector extensions. The following code shows how to do broadcasts, vector + scalar, vector*scalar, loads and stores using vector extensions. #include
#if defined(__clang__)
typedef float v4sf __attribute__((ext_vector_type(4)));
#else
typedef float v4sf __attribute__ ((vector_size (16)));
#endif
void print_v4sf(v4sf a) { for(int i=0; i<4; i++) printf("%f ", a[i]); puts(""); }
int main(void) {
v4sf a;
//broadcast a scalar
a = ((v4sf){} + 1)*3.14159f;
print_v4sf(a);
// vector + scalar
a += 3.14159f;
print_v4sf(a);
// vector*scalar
a *= 3.14159f;
print_v4sf(a);
//load from array
float data[] = {1, 2, 3, 4};
a = *(v4sf*)data;
//a = __builtin_ia32_loadups(data);
//store to array
float store[4];
*(v4sf*)store = a;
for(int i=0; i<4; i++) printf("%f ", store[i]); puts("");
}
Clang 4.0 和 ICC 17 支持GCC矢量扩展的子集.但是,它们都不支持GCC支持的vector + scalar
或vector*scalar
操作. Clang的一种解决方法是使用Clang的OpenCL矢量扩展名.我不了解ICC的解决方法. MSVC不支持我所知道的任何一种矢量扩展.
Clang 4.0 and ICC 17 support a subset of the GCC vector extensions. However, neither of them support vector + scalar
or vector*scalar
operations which GCC supports. A work around for Clang is to use Clang's OpenCL vector extensions. I don't know of a work around for ICC. MSVC does not support any kind of vector extension that I am aware of.
即使使用GCC支持vector + scalar
和vector*scalar
,您也不能执行vector = scalar
(但可以使用Clang的OpenCL矢量扩展名).相反,您可以使用此技巧.
With GCC even though it supports vector + scalar
and vector*scalar
you cannot do vector = scalar
(but you can with Clang's OpenCL vector extensions). Instead you can use this trick.
a = ((v4sf){} + 1)*3.14159f;
我会按照Paul R的建议做,并使用与四大主要C/C ++编译器(GCC,Clang,ICC和MSVC)兼容的内在函数.
I would do as Paul R suggests and use intrinsics which are mostly compatible with the four major C/C++ compilers: GCC, Clang, ICC, and MSVC.
这是每个使用GCC的矢量扩展名和Clang的OpenCL矢量扩展名的编译器支持的表.
Here is a table of what is supported by each compiler using GCC's vector extensions and Clang's OpenCL vector extensions.
gcc g++ clang icc OpenCL
unary operations
[] yes yes yes yes yes
+, – yes yes yes yes yes
++, -- yes yes no no no
~ yes yes yes yes yes
! no yes no no yes
binary vector op vector
+,–,*,/,% yes yes yes yes yes
&,|,^ yes yes yes yes yes
>>,<< yes yes yes yes yes
==, !=, >, <, >=, <= yes yes yes yes yes
&&, || no yes no no yes
binary vector op scalar
+,–,*,/,% yes yes no no yes
&,|,^ yes yes no no yes
>>,<< yes yes no no yes
==, !=, >, <, >=, <= yes yes no no yes
&&, || no yes no no yes
assignment
vector = vector yes yes yes yes yes
vector = scalar no no no no yes
ternary operator
?: no yes no no ?
我们看到Clang和ICC不支持GCC的vector operator scalar
操作. C ++模式下的GCC支持除vector = scalar
之外的所有内容. Clang的OpenCL矢量扩展支持除三元运算符以外的所有内容. Clang的文档声称可以,但是我无法正常工作. C模式下的GCC附加不支持二进制逻辑运算符或三元运算符.
We see that Clang and ICC do not support GCC's vector operator scalar
operations. GCC in C++ mode supports everything but vector = scalar
. Clang's OpenCL vector extensions support everything except maybe the ternary operator. Clang's documentation claims it does but I don't get it to work. GCC in C mode additional does not support binary logical operators or the ternary operator.
这篇关于错误:在C中强制转换用户定义的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!