2

我试图在 VS2010 中优化我的锻炼应用程序。基本上我在核心循环中有几个 sqrt、pow 和 memset。更具体地说,这就是我所做的:

// in a cpp file ...
#include <cmath>

#pragma intrinsic(sqrt, pow, memset)
void Simulator::calculate() 
{
  for( int i=0; i<NUM; i++ )
  {
    ...
    float len = std::sqrt(lenSq);
    distrib[0] = std::pow(baseVal, expVal);
    ...
    clearQuad(i); // invokes memset
  }
}

构建后,反汇编显示,例如,sqrt 调用仍编译为“call _CIsqrt(0x####)”,无论是否启用 /Oi 标志,都没有任何变化。

谁能解释一下我如何启用内在版本以及如何通过反汇编代码验证它? (我还在项目设置中启用了 /O2。)

谢谢

编辑: 通过添加 /fp:fast 解决了问题。以 sqrt 为例,内部版本使用单个“fsqrt”来替换标准版本“call __CIsqrt()”。可悲的是,就我而言,内在版本慢了 5%。

非常感谢 Zan Lynx 和 mch。

4

2 回答 2

2

使用 C++ std 命名空间可能会导致编译器不使用内在函数。尝试std::从您的sqrtpowmemset通话中删除。

MSDN 库文档#pragma intrinsic提供了一个测试内在函数是否真正被使用的示例:使用-FAs标志编译并查看生成的 .asm 文件。

正如您似乎已经在做的那样,查看调试器中的反汇编,也应该显示内在而不是call.

于 2010-10-29T03:58:04.410 回答
1

You are compiling to machine code and not to .NET CLR. Right?

If you compile to .NET then the code won't be optimized until it is run through JIT. At that point .NET has its own intrinsics and other things that will happen.

If you are compiling to native machine code, you might want to play with the /arch option and the /fp:fast option.

于 2010-10-29T04:33:48.110 回答