Inline assembly in Linux
I&#x27;ve been using a lot of inline assembly lately, and while the Stockholm syndrome might be in effect, I&#x27;m coming to like the GCC syntax.   For me, main thing that has helped has been to adopt a consistent syntax.   Here&#x27;s some examples of what I&#x27;m currently using for an AVX2 popcnt optimization, with some explanation.<p><pre><code>  #define ASM_VEC_BYTE_COUNT_SET(vec, sum, mask, shuf)                  \
    __asm volatile (&quot;vpsrld $4, %[VEC], %[SUM]\n&quot;                       \
                    &quot;vpand %[MASK], %[VEC], %[VEC]\n&quot;                   \
                    &quot;vpand %[MASK], %[SUM], %[SUM]\n&quot;                   \
                    &quot;vpshufb %[VEC], %[SHUF], %[VEC]\n&quot;                 \
                    &quot;vpshufb %[SUM], %[SHUF], %[SUM]\n&quot;                 \
                    &quot;vpaddb %[VEC], %[SUM], %[SUM]\n&quot; :                 \
                    &#x2F;* rd&#x2F;wr ymm *&#x2F; [VEC] &quot;+&amp;x&quot; (vec),                  \
                    &#x2F;* write ymm *&#x2F; [SUM] &quot;=&amp;x&quot; (sum) :                  \
        	    &#x2F;* read ymm  *&#x2F; [MASK] &quot;x&quot; (mask),                  \
                    &#x2F;* read ymm  *&#x2F; [SHUF] &quot;x&quot; (shuf))
</code></pre>
1) Try to use the %[symbolic] syntax rather than %[n] numeric.   It&#x27;s slightly longer to write, but usually clearer to read.  Use upper case for the symbolic name.  Put your inputs one per line, with a preceding comment.<p>2) If you are using the same assembly more than once in your program, declare your assembly within a #define macro, then use the macro in your code.<p>3) Use &quot;__asm volatile&quot;.  Declaring &quot;volatile&quot; is not required, but once you are writing inline assembly you usually know more than the compiler about where the block should go.<p>5) If you have multiple lines of assembly and output registers, you are almost always safer to use &quot;+&amp;&quot; and &quot;=&amp;&quot; for your constraint rather than just &quot;+&quot; or &quot;=&quot;.   Search for &quot;early clobber&quot; for details.<p>6) Strongly prefer single type constraints.  The more flexibility you give the compiler, the more likely it will defeat your efforts at optimization.   Use explicit memory addressing modes rather than &quot;m&quot;.   The modifier &quot;c&quot; is needed for the offset.<p><pre><code>  #define ASM_VEC_LOAD_OFFSET_MEM(off, mem, vec)                    \
    __asm volatile (&quot;vmovdqu %c[OFF](%[MEM]), %[VEC]\n&quot; :           \
                    &#x2F;* destination *&#x2F; [VEC] &quot;=x&quot; (vec) :            \
                    &#x2F;* byte offset *&#x2F; [OFF] &quot;i&quot; (off),              \
                    &#x2F;* mem address *&#x2F; [MEM] &quot;r&quot; (mem))
</code></pre>
7) The register constraints for vectors are tricky, because the &quot;x&quot; constraint is used for both XMM and YMM vectors.  There is no way to specify that one wants only one or the other.  This sort of makes sense, since in hardware they share  the same register.  You can use the &quot;q&quot; modifier when you need to specify XMM syntax in the output when you need both forms of the same vector.