title: Embedded broadcasts with intrinsics and assembly
body: <p>In section 2.5.3 "Broadcasts" of <a href="https://software.intel.com/sites/default/files/managed/07/b7/319433-023.pdf" rel="nofollow noreferrer">the Intel Architecture Instruction Set Extensions Programming Reference</a> the  we learn than 
  AVX512 (and Knights Corner) has</p>
  
  <blockquote>
    <p>a bit-field to encode data broadcast for some load-op instructions, i.e. instructions that 
    load data from memory and perform some computational 
    or data movement operation.</p>
  </blockquote>
  
  <p>For example using Intel assembly syntax we can broadcast the scalar at the address stored in <code>rax</code> and then multiplying with the 16 floats in <code>zmm2</code> and write the result to <code>zmm1</code> like this </p>
  
  <pre><code>vmulps zmm1, zmm2, [rax] {1to16}
  </code></pre>
  
  <p>However, there are no intrinsics which can do this.  Therefore, with intrinsics the compiler should be able to fold </p>
  
  <pre><code>__m512 bb = _mm512_set1_ps(b);
  __m512 ab = _mm512_mul_ps(a,bb);
  </code></pre>
  
  <p>to a single instruction</p>
  
  <pre><code>vmulps zmm1, zmm2, [rax] {1to16}
  </code></pre>
  
  <p>but I have not observed GCC doing this. I found a <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63351" rel="nofollow noreferrer">GCC bug report about this</a>. </p>
  
  <p>I have observed something similar with FMA with GCC. e.g. GCC 4.9 will not collapse <code>_mm256_add_ps(_mm256_mul_ps(areg0,breg0)</code> <a href="https://stackoverflow.com/questions/21001388/fma3-in-gcc-how-to-enable">to a single fma instruction with <code>-Ofast</code></a>. However, GCC 5.1 does collapse it to a single fma now. At least there are intrinsics to do this with FMA e.g. <code>_mm256_fmadd_ps</code>. But there is no e.g. <code>_mm512_mulbroad_ps(vector,scalar)</code> intrinsic.  </p>
  
  <p>GCC may fix this at some point but until then assembly is the only solution.</p>
  
  <p>So my question is how to do this with inline assembly in GCC?</p>
  
  <p>I think I may have come up with the correct syntax (but I am not sure) for GCC inline assembly for the example above.</p>
  
  <pre><code>"vmulps        (%%rax)%{1to16}, %%zmm1, %%zmm2\n\t"
  </code></pre>
  
  <p>I am really looking for a function like this</p>
  
  <pre><code>static inline __m512 mul_broad(__m512 a, float b) {
      return a*b;
  }
  </code></pre>
  
  <p>where if <code>b</code> is in memory point to in <code>rax</code> it produces </p>
  
  <pre><code>vmulps        (%rax){1to16}, %zmm0, %zmm0
  ret
  </code></pre>
  
  <p>and if <code>b</code> is in <code>xmm1</code> it produces</p>
  
  <pre><code>vbroadcastss    %xmm1, %zmm1
  vmulps          %zmm1, %zmm0, %zmm0
  ret
  </code></pre>
  
  <hr>
  
  <p>GCC will already do the <code>vbroadcastss</code>-from-register case with intrinsics, but if <code>b</code> is in memory, compiles this to a <code>vbroadcastss</code> from memory.</p>
  
  <pre><code>__m512 mul_broad(__m512 a, float b) {       
      __m512 bb = _mm512_set1_ps(b);
      __m512 ab = _mm512_mul_ps(a,bb);
      return ab;
  }
  </code></pre>
  
  <p><a href="http://goo.gl/gdrhXM" rel="nofollow noreferrer">clang will use a broadcast memory operand</a> if <code>b</code> is in memory.</p>