无忧启动论坛

 找回密码
 注册
搜索
系统gho:最纯净好用系统下载站投放广告、加入VIP会员,请联系 微信:wuyouceo
查看: 3014|回复: 4
打印 上一主题 下一主题

[求助] AVX2指令集的检测方法和汇编伪代码转Delphi

[复制链接]
跳转到指定楼层
1#
发表于 2013-6-8 12:05:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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;
复制代码
2#
发表于 2013-6-8 12:49:30 | 只看该作者
对汇编语言不大懂的,还是请论坛高手来解释吧。
回复

使用道具 举报

3#
发表于 2013-6-8 17:56:18 | 只看该作者
为什么把最开始的 mov eax, 1 这一句去掉??? 这是为什么?

点评

它那是用 eax的状态保存检测结果,我这里使用boolean值直观显示。  详情 回复 发表于 2013-6-8 20:59
回复

使用道具 举报

4#
 楼主| 发表于 2013-6-8 20:59:06 | 只看该作者
sgw888 发表于 2013-6-8 17:56
为什么把最开始的 mov eax, 1 这一句去掉??? 这是为什么?

它那是用 eax的状态保存检测结果,我这里使用boolean值直观显示。

点评

我还是不太明白. mov eax,1 的目的是指定功能号!!! 先 mov eax,1 才能执行 cpuid 指令.  详情 回复 发表于 2013-6-9 09:57
回复

使用道具 举报

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

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

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|捐助支持|无忧启动 ( 闽ICP备05002490号-1 )

闽公网安备 35020302032614号

GMT+8, 2025-8-4 06:42

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表