Note that there are some explanatory texts on larger screens.

plurals
  1. POIn GCC-style extended inline asm, is it possible to output a "virtualized" boolean value, e.g. the carry flag?
    text
    copied!<p>If I have the following C++ code to compare two 128-bit unsigned integers, with inline amd-64 asm:</p> <pre><code>struct uint128_t { uint64_t lo, hi; }; inline bool operator&lt; (const uint128_t &amp;a, const uint128_t &amp;b) { uint64_t temp; bool result; __asm__( "cmpq %3, %2;" "sbbq %4, %1;" "setc %0;" : // outputs: /*0*/"=r,1,2"(result), /*1*/"=r,r,r"(temp) : // inputs: /*2*/"r,r,r"(a.lo), /*3*/"emr,emr,emr"(b.lo), /*4*/"emr,emr,emr"(b.hi), "1"(a.hi)); return result; } </code></pre> <p>Then it will be inlined quite efficiently, but with one flaw. The return value is done through the "interface" of a general register with a value of 0 or 1. This adds two or three unnecessary extra instructions and detracts from a compare operation that would otherwise be fully optimized. The generated code will look something like this:</p> <pre><code> mov r10, [r14] mov r11, [r14+8] cmp r10, [r15] sbb r11, [r15+8] setc al movzx eax, al test eax, eax jnz is_lessthan </code></pre> <p>If I use "sbb %0,%0" with an "int" return value instead of "setc %0" with a "bool" return value, there's still two extra instructions:</p> <pre><code> mov r10, [r14] mov r11, [r14+8] cmp r10, [r15] sbb r11, [r15+8] sbb eax, eax test eax, eax jnz is_lessthan </code></pre> <p>What I want is this:</p> <pre><code> mov r10, [r14] mov r11, [r14+8] cmp r10, [r15] sbb r11, [r15+8] jc is_lessthan </code></pre> <p>GCC extended inline asm is wonderful, otherwise. But I want it to be just as good as an intrinsic function would be, in every way. I want to be able to directly return a boolean value in the form of the state of a CPU flag or flags, without having to "render" it into a general register.</p> <p>Is this possible, or would GCC (and the Intel C++ compiler, which also allows this form of inline asm to be used) have to be modified or even refactored to make it possible?</p> <p>Also, while I'm at it — is there any other way my formulation of the compare operator could be improved?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload