DIV in inline assembly: volatile needed due to "divide error" side effect?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I am attempting to access the x86_64 DIV instruction from inline
assembly as follows:

inline void div128(uint64_t* q, uint64_t *r,
                   uint64_t n1, uint64_t n0, uint64_t d)
{
    __asm__("divq %4"
            : "=a" (*q), "=d" (*r)
            : "1" (n1), "0" (n0), "rm" (d)
            : "cc");
}

I declare input and output operands, but is that sufficient? The DIV
instruction can produce a divide error for certain inputs.  The GCC
documentation says that inline assembly may be discarded or moved out
of loops.  Can it also be speculatively executed?  I have a case in
GCC 11.2 where a single DIV is duplicated and executed on two different
inputs coming from two sides of a conditional.  One of the inputs (the
one not chosen by the conditional) produces a divide error.

Is my code at fault?  Do I need the volatile qualifier?  Or is GCC not
supposed to speculatively execute inline assembly?

Here is a reproducible case: https://godbolt.org/z/37z3foTff



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux