如何告诉编译器使用增强(SSE / SIMD)指令编译一个函数,而没有(在同一个文件中)? [英] How to tell the compiler to compile one function with enhanced ( SSE / SIMD ) instructions, and other without ( in the same file ) ?

查看:136
本文介绍了如何告诉编译器使用增强(SSE / SIMD)指令编译一个函数,而没有(在同一个文件中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写两个函数副本,使用浮点函数:



一个带有现代处理器的增强/ SSE指令。

和其他只使用旧处理器(Pentium 1 / Pentium MMX)中的FPU指令。



I want to write two copies of function, that works with floating point:

One with enhanced / SSE instructions for modern processors.
And other using only FPU instruction in old processors ( Pentium 1 / Pentium MMX ).

typedef float( *MY_CALC_FUNK )( float a, float b, float c, float d );

// Which attribute / keyword to force SSE / enhanced instructions compilation ?
float MyCalcFunc_with_SSE( float a, float b, float c, float d ) { return a / b + c / d; }
// Which attribute / keyword to disable SSE / enhanced instructions compilation ?
float MyCalcFunc_Legacy__( float a, float b, float c, float d ) { return a / b + c / d; }

int CheckCPU_SSE( void ) { return 0; }

void main( void ) {
	MY_CALC_FUNK MyCalcFunc = CheckCPU_SSE( ) ? MyCalcFunc_with_SSE : MyCalcFunc_Legacy__;
	printf( "The number is %f\n", MyCalcFunc( 10, 20, 30, 40 ) );
}





我看到这个案例在GCC中有一个解决方案:



I saw that this case has a solution in GCC:

float MyCalcFunc_with_SSE( float a, float b, float c, float d ) __attribute__ ((__target__ ("sse")));
float MyCalcFunc_Legacy__( float a, float b, float c, float d ) __attribute__ ((__target__ ("no-sse")));





是否有 Visual Studio 的解决方案?

推荐答案

您可以将代码放入不同的文件并选择特定于文件的选项。



我会指定项目的默认设置(例如,支持SSE)并将遗留代码移动到一个或多个附加文件。对于这些文件,单击解决方案资源管理器中的名称右键,然后选择属性。在那里你可以改变编译器选项。



[更新]

您可以通过检查一些来检查源文件中的SSE特定编译器设置预定义的宏:

You can put the code into different files and select file specific options.

I would specify a default setting for the project (e.g. with SSE support) and move the legacy code to one or more additional files. For these files click right on the names in the Solution Explorer and choose 'Properties'. There you can change the compiler options.

[UPDATE]
You can check the SSE specific compiler settings in the source files by examining some pre-defined macros:
// Determine which min. SSE version is required by build options.
// Predefined macros:
// _M_IX86     Defined for x86 processors. Not defined for 64-bit processors.
// _MX_64      Defined for x64 processors when building 64-bit apps.
// _M_IX86_FP  Indicates which /arch compiler option is used 
//             (not defined for 64-bit builds):
//             0 = IA32
//             1 = SSE
//             2 = SSE2, AVX, or AVX2
// _AVX_       Defined when using /arch:AVX compiler option.
// _AVX2_      Defined when using /arch:AVX2 compiler option.


这篇关于如何告诉编译器使用增强(SSE / SIMD)指令编译一个函数,而没有(在同一个文件中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆