如何将 RDRAND 指令添加到使用 VS 2008 编译的 64 位代码中? [英] How to add RDRAND instruction into 64-bit code compiled with VS 2008?

查看:22
本文介绍了如何将 RDRAND 指令添加到使用 VS 2008 编译的 64 位代码中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Visual Studio 2008 IDE 中处理 C++ 项目,我需要在其中使用英特尔的新 RDRAND 指令.我做了一个快速搜索,MSDN 建议使用 _rdrand64_step 定义在 immintrin.h,我在 VS 2008 中没有.

I'm working on a C++ project in Visual Studio 2008 IDE, where I need to use Intel's new RDRAND instruction. I did a quick search, and MSDN recommends using _rdrand64_step intrinsic defined in immintrin.h, which I do not have in VS 2008.

在 32 位编译代码中,我可以像这样使用 asm 关键字:

In a 32-bit compiled code I can get away with using asm keyword as such:

    __asm
    {
        xor eax, eax

        ;RDRAND instruction = Set random value into EAX.
        ;Will set overflow [C] flag if success
        _emit 0x0F
        _emit 0xC7
        _emit 0xF0
    }

但在 x64 上不支持 asm.

But on x64 asm is not supported.

您能否建议我如何使用 RDRAND 指令为 64 位编译我的项目?

Can you suggest how can I compile my project for 64-bit with the RDRAND instruction?

推荐答案

您要么需要将编译器升级到支持 _rdrand64_step 内在函数(自 Visual Studio 2012 起支持)的编译器,要么使用普通编译器(外部)汇编来创建您自己的函数(因为 Visual C++ 不支持 x86-64 目标的内联汇编).

You either need to upgrade your compiler to one that does support the _rdrand64_step intrinsic (supported since Visual Studio 2012), or use normal (external) assembly to create your own functions (since Visual C++ does not support inline assembly for x86-64 targets).

例如:

_TEXT   SEGMENT

    PUBLIC rdrand32_step
    PUBLIC rdrand32_retry
    PUBLIC rdrand64_step
    PUBLIC rdrand64_retry

    ; int rdrand32_step(unsigned *p)
rdrand32_step PROC
    xor     eax, eax
    rdrand  edx
    ; DB    0fh, 0c7h, 0f2h
    setc    al
    mov     [rcx], edx
    ret
rdrand32_step ENDP

    ; unsigned rdrand32_retry()
rdrand32_retry PROC
retry:
    rdrand  eax
    ; DB    0fh, 0c7h, 0f0h
    jnc     retry
    ret
rdrand32_retry ENDP

    ; int rdrand64_step(unsigned long long *p)
rdrand64_step PROC
    xor     eax, eax
    rdrand  rdx
    ; DB    048h, 0fh, 0c7h, 0f2h
    setc    al
    mov     [rcx], edx
    ret
rdrand64_step ENDP

    ; unsigned long long rdrand64_retry()
rdrand64_retry PROC
retry:
    rdrand  rax
    ; DB    048h, 0fh, 0c7h, 0f0h
    jnc     retry
    ret
rdrand64_retry ENDP

_TEXT   ENDS

    END

如果您使用的是 Visual Studio 2008 的 MASM 版本,您可能需要注释掉 RDRAND 指令并取消注释它们后面的 DB 指令.

If you're using the version of MASM from Visual Studio 2008, you'll probably have to comment out the RDRAND instructions and uncomment the DB directives that follow them.

这篇关于如何将 RDRAND 指令添加到使用 VS 2008 编译的 64 位代码中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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