我正在做一个实验,第一步是展开一个循环(来自C/C++)十几次(例如:10,50等)并输出C/C++展开的代码.有没有可以用来自动化这种展开的工具?
换句话说,我需要的是:
C/C++ source/loop --->> TOOL (Unroll by X) ----> Unrolled C/C++ source/loop
Run Code Online (Sandbox Code Playgroud) 我有一个函数,我需要在C++ 11的紧密循环中内联
我希望函数在头文件的单独文件中实现,并且仍然强制它在所使用的任何地方内联.另外,我想用clang,GCC和英特尔编译器进行编译.
充实要求.我正在寻找一个允许我做类似的宏:
#define force_inline <something here>
Run Code Online (Sandbox Code Playgroud)
在标题中:
force_inline void foo();
Run Code Online (Sandbox Code Playgroud)
我应该能够在实现文件中执行此操作:
void foo() {... Code.. }
Run Code Online (Sandbox Code Playgroud)
为了清楚起见,我不想把代码放在我的标题中.我希望它们只包含函数的声明.
有没有办法使用适用于所有编译器的宏来实现内联?
我到目前为止最好的解决方案是这个宏:
#define forceinline inline __attribute__((always-inline))
Run Code Online (Sandbox Code Playgroud)
似乎ICC需要两者inline(与内联代码无关)和头部中的完整实现以保证函数的内联.
PS:是的,我已经测量了我的表现,而且我知道一个事实是内联函数比没有更快.不,编译器不会为我做这件事.
我在Linux上安装了Intel Compiler composer_xe_2013_sp1.3.174.我对icc警告感到困惑.使用简单的程序main.c提供icc,如下所示:
int main(int argc, char **argv) {
int a = 1;
unsigned int b = -22;
if (b = a) {
}
}
Run Code Online (Sandbox Code Playgroud)
我用命令编译了文件:icc -Wall main.c.令人惊讶的是,该命令在没有任何警告的情况下静默工作.我是否必须打开icc上的警告开关?谢谢
我正在尝试使用alignas作为类成员的指针,坦率地说,我不确定我应该把它放在哪里.
例如:
class A
{
private:
int n;
alignas(64) double* ptr;
public:
A(const int num) : n(num), ptr(new double[num])
{}
};
Run Code Online (Sandbox Code Playgroud)
我希望能确保ptr的数据在64字节块上对齐.使用英特尔编译器,它没有.
有人能指出我正确的方向吗?
我有一个关于C编译器优化的问题以及内联函数何时/如何循环展开.
我正在开发一个类似于下面例子的数字代码.基本上,my_for()会计算某种模板并调用每个op()数据.在这里,包装,创建参数并将函数指针发送给...谁的工作是修改每个()双数组的th double .my_type *argimy_func()my_for()my_op()iarg->narg->dest[j]
typedef struct my_type {
int const n;
double *dest[16];
double const *src[16];
} my_type;
static inline void my_for( void (*op)(my_type *,int), my_type *arg, int N ) {
int i;
for( i=0; i<N; ++i )
op( arg, i );
}
static inline void my_op( my_type *arg, int i ) {
int j;
int const n = arg->n;
for( j=0; j<n; ++j )
arg->dest[j][i] += …Run Code Online (Sandbox Code Playgroud) 我们使用英特尔C++编译器并检测到它错误编译(?)以下内容,从使用中减少了boost::function<Ponies()> f(unnamedNamespacedFunctor).
a1.cc:
template<typename T>
int f(T) { static int x = T::x; return x; }
namespace { struct A { static const int x = 1; }; }
int f1() {
return f(A());
}
Run Code Online (Sandbox Code Playgroud)
a2.cc:
template<typename T>
int f(T) { static int x = T::x; return x; }
namespace { struct A { static const int x = 0; }; }
int f2() {
return f(A());
}
Run Code Online (Sandbox Code Playgroud)
main.cc:
#include <cstdio>
int f1();
int f2();
int main() { …Run Code Online (Sandbox Code Playgroud) 静态断言声明可能出现在块作用域(作为块声明)和类体内(作为成员声明)
好的,现在我有以下代码:
struct foo_t
{
static constexpr std::size_t maxAlignment()
{
// This is just a sample; I removed real code from this method.
return std::max(alignof(__m128), __alignof(__m256));
}
static_assert(0 == ((maxAlignment() -1) & maxAlignment()), "some message");
};
Run Code Online (Sandbox Code Playgroud)
MSVC 2015和Intel C++ 16.0.2都没有编译此代码(前者显示"错误C2131:表达式未评估为常量",后者显示"函数调用必须在常量表达式中具有常量值"错误并指向调用maxAlignmentin static_assert).
但MSVC 2015 Update 1 确实编译了以下代码,而英特尔C++ 16.0.2 则没有:
template <typename T, std::size_t Alignment>
struct foo_t
{
static constexpr std::size_t maxAlignment()
{
return std::max(std::alignment_of<T>::value, Alignment);
}
static_assert(0 == ((maxAlignment() -1) & …Run Code Online (Sandbox Code Playgroud) movemask指令采用__m256i并返回int32,其中每个位(取决于输入向量元素类型的前4位,8位或所有32位)是相应向量元素的最高有效位.
我想做反过来:取一个32(其中只有4,8或32个最低有效位有意义),并获得__m256i,其中每个int8,int32或int64大小的块的最高有效位设置为原始位.
基本上,我想从压缩的位掩码转到可被其他AVX2指令(例如maskstore,maskload,mask_gather)用作掩码的位掩码.
我无法快速找到这样做的指令,所以我在这里问.如果没有一条具有该功能的指令,您是否可以想到一个聪明的黑客,只需很少的指令即可实现这一点?
我目前的方法是使用256元素查找表.我想在一个没有其他事情发生的循环中使用这个操作来加速它.注意,我对长多指令序列或实现此操作的小循环不太感兴趣.
我正在尝试从使用英特尔 OneAPI 工具包中的经典英特尔编译器切换到下一代 DPC/C++ 编译器,但处理浮点运算的默认行为似乎已损坏或不同,因为comparison with infinity always evaluates to false in fast floating point modes. 以上既是编译器警告,也是我现在在 ICX 中遇到的行为,但不是经典编译器中遇到的行为(对于使用相同的最小编译器标志集)。
#include <iostream>
#include <cmath>
int main()
{
double a = 1.0/0.0;
if (std::isinf(a))
std::cout << "is infinite";
else
std::cout << "is not infinite;";
}
Run Code Online (Sandbox Code Playgroud)
编译器标志:
-O3 -Wall -fp-model=fast
ICC 2021.5.0 输出:(
is infinite
也在几个旧版本上进行了测试)
ICX 2022.0.0 输出:(
is not infinite
也在 2022.0.1 上测试)
编译器浏览器上的现场演示: https: //godbolt.org/z/vzeYj1Wa3
默认情况下-fp-model=fast在两个编译器上都启用。如果我手动指定,-fp-model=precise我可以恢复行为,但不能恢复性能。
有谁知道使用下一代编译器保持快速浮点模型之前的行为和性能的潜在解决方案?
我希望使用一组用C++编写的库和英特尔编译器.我附上了演示问题的示例代码.库中有很多地方使用'using'指令和部分重载(例如,我想从基类中使用foo(void)方法,但在派生类中重新实现第二个版本fo foo) .gcc没有问题,但英特尔确实如此.
#include <iostream>
template <class F>
struct Interface
{
static const F f=10;
};
template <class F>
struct Base : public Interface<F>
{
void foo (void) { std::cout << "void" << std::endl; }
template <class FF>
void foo (Interface<FF> &ii) { std::cout << "F : " << ii.f << std::endl; }
};
template <class F,int i>
struct Derived : public Base<F>
{
// void foo (void) { Base<F>::foo(); } // works fine
using Base<F>::foo; // gives error
template …Run Code Online (Sandbox Code Playgroud) icc ×10
c++ ×7
gcc ×3
c ×2
c++11 ×2
alignas ×1
avx ×1
avx2 ×1
clang ×1
icx ×1
inline ×1
intel ×1
intrinsics ×1
llvm ×1
optimization ×1
overloading ×1
static ×1
templates ×1
visual-c++ ×1
x86 ×1