无忧启动论坛

标题: AVX2指令集的检测方法和汇编伪代码转Delphi [打印本页]

作者: chiannet    时间: 2013-6-8 12:05
标题: AVX2指令集的检测方法和汇编伪代码转Delphi
本帖最后由 chiannet 于 2013-6-8 15:12 编辑

应用程序如何检测AVX2

  在《Intel® Architecture Instruction Set Extensions Programming Reference》的“2.2.3 Detection of AVX2”中介绍了AVX2指令集的检测方法和汇编伪代码,摘录如下——

Hardware support for AVX2 is indicated by CPUID.(EAX=07H,ECX=0H):EBX.AVX2[bit 5]=1.
Application Software must identify that hardware supports AVX as explained in Section 2.2, after that it must also detect support for AVX2 by checking
CPUID.(EAX=07H, ECX=0H):EBX.AVX2[bit 5]. The recommended pseudocode sequence for detection of AVX2 is:
----------------------------------------------------------------------------------------
  1. INT supports_avx2()
  2. {    ; result in eax
  3.     mov eax, 1
  4.     cpuid
  5.     and ecx, 018000000H
  6.     cmp ecx, 018000000H; check both OSXSAVE and AVX feature flags
  7.     jne not_supported
  8.     ; processor supports AVX instructions and XGETBV is enabled by OS
  9.     mov eax, 7
  10.     mov ecx, 0
  11.     cpuid
  12.     and ebx, 20H
  13.     cmp ebx, 20H; check AVX2 feature flags
  14.     jne not_supported
  15.     mov ecx, 0; specify 0 for XFEATURE_ENABLED_MASK register
  16.     XGETBV; result in EDX:EAX
  17.     and eax, 06H
  18.     cmp eax, 06H; check OS has enabled both XMM and YMM state support
  19.     jne not_supported
  20.     mov eax, 1
  21.     jmp done
  22. NOT_SUPPORTED:
  23.     mov eax, 0
  24. done:
  25. }
复制代码
上面这个代码是intel官方提供的,先检测CPU是否具备AVX2支持能力,还检测了操作系统是否开启用了这个支持,我仅需要检测CPU是否具备AVX2支持就可以了,欲转化为如下delphi 7代码,能通过编译,但不知结果,因为手上木有Haswell 内核的CPU,估计暂时坛子里也没有几个人有此神器,请大家比照看看,代码是否正确。谢谢。

  1. function IsAVX2Support: Boolean; assembler;
  2. asm
  3.     cpuid
  4.     mov   @Result, True
  5.     and ecx, 018000000H
  6.     cmp ecx, 018000000H          // check both OSXSAVE and AVX feature flags
  7.     jbe   @not_supported         // processor supports AVX instructions and XGETBV is enabled by OS
  8.     mov eax, 7
  9.     mov ecx, 0
  10.     cpuid
  11.     and ebx, 20H
  12.     cmp ebx, 20H                 // check AVX2 feature flags
  13.     jbe   @not_supported
  14. //    mov ecx, 0                   // specify 0 for XFEATURE_ENABLED_MASK register
  15. //    XGETBV                     // result in EDX:EAX
  16. //    and eax, 06H
  17. //    cmp eax, 06H                 // check OS has enabled both XMM and YMM state support
  18. //    jbe   @not_supported
  19. //    mov eax, 1
  20.     jnz   @EXIT
  21. @not_supported:
  22.     mov   @Result, False
  23. @EXIT:
  24. end;
复制代码

作者: renchmin    时间: 2013-6-8 12:49
对汇编语言不大懂的,还是请论坛高手来解释吧。
作者: sgw888    时间: 2013-6-8 17:56
为什么把最开始的 mov eax, 1 这一句去掉??? 这是为什么?
作者: chiannet    时间: 2013-6-8 20:59
sgw888 发表于 2013-6-8 17:56
为什么把最开始的 mov eax, 1 这一句去掉??? 这是为什么?

它那是用 eax的状态保存检测结果,我这里使用boolean值直观显示。
作者: sgw888    时间: 2013-6-9 09:57
chiannet 发表于 2013-6-8 20:59
它那是用 eax的状态保存检测结果,我这里使用boolean值直观显示。

我还是不太明白.  mov eax,1 的目的是指定功能号!!! 先 mov eax,1 才能执行 cpuid 指令.




欢迎光临 无忧启动论坛 (http://wuyou.net/) Powered by Discuz! X3.3