我正在尝试使用英特尔的RDRAND
指令。根据英特尔® 64 和 IA-32 架构软件开发人员手册第 2 卷(第 4-298 页),RDRAND
默认情况下会生成 32 位随机值,即使在 64 位机器上也是如此:
在 64 位模式下,指令的默认操作大小为 32 位。使用 REX.B 形式的 REX 前缀允许访问其他寄存器 (R8-R15)。
我正在尝试使用 强制生成 64 位rdrandq
,但它会产生错误(/tmp/ccLxwW6S.s
由于使用了内联汇编):
$ g++ -Wall rdrand.cxx -o rdrand.exe
/tmp/ccLxwW6S.s: Assembler messages:
/tmp/ccLxwW6S.s:5141: Error: invalid instruction suffix for `rdrand'
如何在 GCC 下强制使用 64 位版本的 RDRAND 指令?在 GCC 下使用 RDRAND 时如何设置 REX 前缀?
提前致谢。
在下面的代码中,output
是byte[]
一个长度为size
. safety
是一个故障保险。两种不同的字长处理X86、X32 和 X64 平台。
#if BOOL_X86
word32 val;
#else // X32 and X64
word64 val;
#endif
while (size && safety)
{
char rc;
__asm__ volatile(
#if BOOL_X86
"rdrandl %0 ; setc %1"
#else
"rdrandq %0 ; setc %1"
#endif
: "=rm" (val), "=qm" (rc)
:
: "cc"
);
if (rc)
{
size_t count = (size < sizeof(val) ? size : sizeof(val));
memcpy(output, &val, count);
size =- count;
}
else
{
safety--;
}
}
如果我从 RDRAND 中删除显式操作数大小(即使用rdrand
而不是rdrandl
or rdrandq
),那么在尝试使用 时会出现错误word64
:
/tmp/ccbeXOvM.s: Assembler messages:
/tmp/ccbeXOvM.s:5167: Error: operand size mismatch for `rdrand'