我想为将在 gcc 中编译的 C 代码指定编译器选项。由于代码的部署方式,我需要在代码中执行此操作。这是当前似乎成功指定优化标志的代码。
#pragma GCC optimize ("-O3,-ffast-math")
typedef float v4sf __attribute__ ((vector_size (16)));
typedef union {
v4sf v;
float e[4];
} float4;
typedef struct {
float4 x;
float4 y;
} complex4;
static complex4 complex4_mul(complex4 a, complex4 b) {
return (complex4){a.x.v*b.x.v -a.y.v*b.y.v, a.y.v*b.x.v + a.x.v*b.y.v};
}
complex4 f4(complex4 x[], int n) {
v4sf one = {1,1,1,1};
complex4 p = {one,one};
for (int i = 0; i < n; i++) p = complex4_mul(p, x[i]);
return p;
}
Run Code Online (Sandbox Code Playgroud)
但是,我也想指定
-march=native. 这可能从代码中以某种方式实现吗?
我确实尝试过#pragma GCC optimize ("-O3,-ffast-math, -march=native"),但该-march=native部分似乎被忽略了。请参阅:https : //godbolt.org/g/FjbRcV。
现在我明白了。据我所知,除了使用别无他法#pragma GCC target
例如:
#pragma GCC target ("arch=skylake-avx512")
但它不接受native作为参数。我已将它添加到你的神马
IMO 这是错误的方法,因为它们应该使用命令行参数和正确编写的 makefile 传递给编译器