title: cmpxchg example for 64 bit integer
body: <p>I am using cmpxchg (compare-and-exchange) in i686 architecture for 32 bit compare and swap as follows.</p>
  
  <p>(Editor's note: the original 32-bit example was buggy, but the question isn't about it.  I believe this version is safe, and as a bonus compiles correctly for x86-64 as well.  <strong>Also note that inline asm isn't needed or recommended for this; <a href="https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html" rel="nofollow noreferrer"><code>__atomic_compare_exchange_n</code></a> or the older <code>__sync_bool_compare_and_swap</code> work for <code>int32_t</code> or <code>int64_t</code> on i486 and x86-64</strong>.  But this question is about doing it with inline asm, in case you still want to.)</p>
  
  <pre><code>// note that this function doesn't return the updated oldVal
  static int CAS(int *ptr, int oldVal, int newVal)
  {
      unsigned char ret;
      __asm__ __volatile__ (
              "  lock\n"
              "  cmpxchgl %[newval], %[mem]\n"
              "  sete %0\n"
              : "=q" (ret), [mem] "+m" (*ptr), "+a" (oldVal)
              : [newval]"r" (newVal)
              : "memory");    // barrier for compiler reordering around this
  
      return ret;   // ZF result, 1 on success else 0
  }
  </code></pre>
  
  <hr>
  
  <p><strong>What is the equivalent for x86_64 architecture for 64 bit compare and swap</strong></p>
  
  <pre><code>static int CAS(long *ptr, long oldVal, long newVal)
  {
      unsigned char ret;
      // ?
      return ret;
  }
  </code></pre>