title: Trying to implement 128 bit add in amd64 with inline assembly with multiple alternative constraints
body: <p>Trying to get usable 128 bit operations in GCC on amd64, I implemented some inline functions. Like add_128_128_128. I wanted to let the compiler decide, which registers to use as inputs and outputs for most flexibility. So, I used the multiple alternative constraints.</p>
  
  <pre><code>inline __uint128_t add_128_128_128(__uint128_t a, __uint128_t b) {
          uint64_t a_hi = a &gt;&gt; 64;
          uint64_t a_lo = a;
          uint64_t b_hi = b &gt;&gt; 64;
          uint64_t b_lo = b;
          uint64_t retval_hi;
          uint64_t retval_lo;
  
          asm (
                  "\n"
                  "       add     %2, %0\n"
                  "       adc     %3, %1\n"
                  : "=r,r,r,r" (retval_lo)
                  , "=r,r,r,r" (retval_hi)
                  : "r,0,r,0" (a_lo)
                  , "0,r,0,r" (b_lo)
                  , "r,1,1,r" (a_hi)
                  , "1,r,r,1" (b_hi)
          );
  
          return ((__uint128_t)retval_hi) &lt;&lt; 64 | retval_lo;
  }
  </code></pre>
  
  <p>Now, the generated assembler output is:</p>
  
  <pre><code>_Z11add_128_128oo:
          movq    %rdx, %rax
          movq    %rcx, %rdx
          add     %rdi, %rax
          adc     %rax, %rdx
          ret
  </code></pre>
  
  <p>What puzzles me is how to get the adc instruction fixed. From thinking about this, I came to the temporary conclusion, that even the matching constraints get "new" numbers, which would explain the %rax being %3 == %0 == %rax. So, is there a way to tell GCC to only count the "r" constraints? (I know that I can get this inline assembly to work by just giving up on multiple alternative constraints.)</p>
  
  <p>BTW: Is there any useful documentation of GCC'S inline assembly? The official manual with zero examples when it comes to the interesting stuff is nothing I would call useful in this context. Searching with Google didn't make me find any. All howtos and stuff just speak about the trivial basic things but completely omit more advanced stuff like multiple alternative constraints just completely.</p>