在Visual Studio 2015中检测要与C ++宏一起使用的SIMD指令集 [英] Detecting SIMD instruction sets to be used with C++ Macros in Visual Studio 2015

查看:333
本文介绍了在Visual Studio 2015中检测要与C ++宏一起使用的SIMD指令集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这就是我要完成的工作.在必须使用Microsoft Visual Studio 2015或更高版本进行编译的C ++项目中,根据用户CPU中可用的最新SIMD指令集,我需要使一些代码具有不同的版本,其中包括: SSESSE2SSE3SSSE3SSE4.1SSE4.2AVXAVX2AVX512.

So, here is what I am trying to accomplish. In my C++ project that has to be compiled with Microsoft Visual Studio 2015 or above, I need to have some code have different versions depending on the newest SIMD instrunction set available in the CPU of the user, among: SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2, AVX, AVX2 and AVX512.

由于此时我要查找的是编译时CPU调度,所以我的第一个猜测是,可以使用编译器宏轻松地完成此任务.但是,令我惊讶的是,很难找到有关如何在VS2015中使用宏实现此类CPU调度的信息.

Since what I am look for at this point is compile-time CPU dispatching, my first guess was that it could be easily accomplished using compiler macros. However, to my astonishment, it has been quite hard to find information on how to achieve such CPU dispatching with macros in VS2015.

例如,前一个问题"检测SSE/在Visual Studio中设置的SSE2指令"包含有关如何为x86代码而不是x64代码检测SSE和SSE2的信息.虽然,他们引用了此Microsoft文档: http://msdn.microsoft .com/en-us/library/b0084kay.aspx

For instance, the former question "Detect the availability of SSE/SSE2 instruction set in Visual Studio" has information on how to detect SSE and SSE2 for x86 code, but not for x64 code. Although, they make a reference to this Microsoft's document: http://msdn.microsoft.com/en-us/library/b0084kay.aspx

那里,我们仅具有有关如何检测编译器中的启用的SSE,SSE2,AVX和AVX2的信息-并不完全是它们是否受CPU支持.另外,其他指令集也一无所有,例如SSE3,SSSE3,SSE4.1,SSE4.2和AVX512.

There, we only have information on how to detect whether SSE, SSE2, AVX and AVX2 are enabled in the compiler - not exactly whether they are supported by CPU. Also, there is nothing at all about the other instrunction sets, like SSE3, SSSE3, SSE4.1, SSE4.2 and AVX512.

所以,我的问题变成了:如何像其他编译器一样,使用Microsoft Visual Studio 2015,如何通过宏检测用户的CPU是否支持这些指令集?

So, my question becomes: how can I detect whether the user's CPU supports those instrunction sets via macro, just like other compilers do, but with Microsoft Visual Studio 2015?

推荐答案

您面临的问题是,Visual Studio历来是为软件供应商设计的.编译自己的软件的想法根本不在Microsoft的头脑中.

The problem you're facing is that Visual Studio historically is intended for software vendors. The idea that you compile your own software simply isn't in Microsoft's DNA.

实际结果是Microsoft几乎不关心构建机器的处理器.不太可能是用于运行该软件的处理器.

The practical result is that Microsoft hardly cares about the processor of the build machine. That's unlikely to be the processor used to run the software.

从好的方面来说,这还意味着Microsoft不会受到长期存在的Linux问题的困扰,因为Linux假定存在目标计算机上会存在构建系统库.在适用于Windows 7的Windows 10上构建就可以了.

On the upside, this also means that Microsoft doesn't suffer from the perennial Linux problem that the build system libraries are assumed to be present on the target machine. Building on Windows 10 for Windows 7 just works.

例如,编译器还不允许您启用SSE4.1.您只能使用/arch:avx或不使用任何内容.另外,该选项仅定义__AVX__,而不定义gcc/clang/icc定义的普通宏(如__SSSE3__),以指示对AVX隐含的先前指令集的目标支持.

The compiler also doesn't allow you to enable up to SSE4.1, for example. You can only use /arch:avx or nothing. Also, that option only defines __AVX__, not the usual macros like __SSSE3__ that gcc/clang/icc define to indicate target support for previous instruction sets implied by AVX.

这篇关于在Visual Studio 2015中检测要与C ++宏一起使用的SIMD指令集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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