How to set REX prefix when using RDRAND?

Jeffrey Walton noloader@gmail.com
Mon Oct 12 17:42:00 GMT 2015


On Mon, Oct 12, 2015 at 5:29 AM, Mason <slash.tmp@free.fr> wrote:
> On 12/10/2015 10:34, Jeffrey Walton wrote:
>> I'm not able to compile a program that uses `rdrand` and a word64 due
>> to "Error: operand size mismatch for `rdrand'". If I try to explicitly
>> use `rdrandq` and the word64, then it results in
>> "Error: invalid instruction suffix for `rdrand'"
>
> (I'm not a gcc developer.)
>
> I think it would be a good idea to provide a minimal example of
> what you are compiling,

In the code below, output is a byte[] with a length of size. safety is
a failsafe.

The two different word sizes handle the X86, X32, and X64 platforms.
BOOL_X86 is set by __i386__ and friends.

Jeff

*****

This code produces "Error: invalid instruction suffix for `rdrand'":

#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--;
        }
    }

*****

This code produces "Error: operand size mismatch for `rdrand'"

        __asm__ volatile(
#if BOOL_X86
          "rdrand %0 ; setc %1"
#else
          "rdrand %0 ; setc %1"
#endif
          : "=rm" (val), "=qm" (rc)
          :
          : "cc"
        );

Jeff



More information about the Gcc-help mailing list