title: Multiplying 64-Bit Integers using AVX2 and AVX-512 Performance
body: <p>I'm trying to determine if the _mm512_mullox_epi64 (AVX-512 foundation) sequence intrinsic is substantially slower than the _mm512_mullo_epi64 (AVX-512 Double-Word and Quad-Word ISA) hardware-implemented intrinsic.</p>
  <p>The _mm512_mullo_epi64 will throw an &quot;Invalid Instruction&quot; exception on hardware with AVX-512 but without the DWQW instruction set extensions.</p>
  <p>I don't have an AVX-512 capable CPU and trying to benchmark using godbolt provides very inconsistent results. My code also does not compile using quick bench as you can't currently pass in compiler options like -mavx512dq</p>
  <p>I'm also interested in knowing whether or not there is a good option for using AVX2 as there is no intrinsic for multiplying 64-Bit integers with AVX2.</p>
  <p>Using _mm256_mul_pd with a cast often produces incorrect results when the product is within the bounds of an int64_t but outside of the bounds of a 64-bit double.</p>
  <p>Here's my test code if you're interested:</p>
  <pre><code> #include &quot;immintrin.h&quot;
   #include &lt;cstdint&gt;
   #include &lt;array&gt;
   #include &lt;algorithm&gt;
   #include &lt;numeric&gt;
   #include &lt;iostream&gt;
   #include &lt;chrono&gt;
  
   std::array&lt;int64_t, 1000000&gt; arr1;
   std::array&lt;int64_t, 1000000&gt; arr2;
   std::array&lt;int64_t, 1000000&gt; arr3;
  
   class Timer
  {
  public:
      Timer()
      {
          start = std::chrono::high_resolution_clock::now();
      }//End of constructor
  
      Timer(Timer const&amp;) = delete;
      Timer&amp; operator=(Timer const&amp;) = delete;
      Timer(Timer&amp;&amp;) = delete;
      Timer&amp; operator=(Timer&amp;&amp;) = delete;
  
      ~Timer()
      {
          end = std::chrono::high_resolution_clock::now();
          std::chrono::high_resolution_clock::duration d = end - start;
          std::cout &lt;&lt; std::chrono::duration_cast&lt;std::chrono::nanoseconds&gt;(d).count() &lt;&lt;      &quot;ns\n&quot;;
      }//End of destructor
  
  private:
      std::chrono::high_resolution_clock::time_point start;
      std::chrono::high_resolution_clock::time_point end;
  };//End of class Timer
  
   template&lt;uint64_t SIZE1, uint64_t SIZE2, uint64_t SIZE3&gt;
   void mul_f(const std::array&lt;int64_t, SIZE1&gt;&amp; src, const std::array&lt;int64_t, SIZE2&gt;&amp; src2,           std::array&lt;int64_t, SIZE3&gt;&amp; dest)
   {
  __m512i _src1;
  __m512i _src2;
  __m512i _dest;
  
  for(uint64_t i = 0; i &lt; SIZE3; i+=8)
  {
      if((i + 8) &gt; SIZE3)
      {
          break;
      }
  
      _src1 = _mm512_load_epi64(&amp;src[i]);
      _src2 = _mm512_load_epi64(&amp;src2[i]);
  
      _dest = _mm512_mullox_epi64(_src1, _src2);
  
      _mm512_store_epi64(&amp;dest[i], _dest);        
       }
   }
  
   template&lt;uint64_t SIZE1, uint64_t SIZE2, uint64_t SIZE3&gt;
   void mul_dq(const std::array&lt;int64_t, SIZE1&gt;&amp; src, const std::array&lt;int64_t, SIZE2&gt;&amp; src2, std::array&lt;int64_t, SIZE3&gt;&amp; dest)
   {
  __m512i _src1;
  __m512i _src2;
  __m512i _dest;
  
  for(uint64_t i = 0; i &lt; SIZE3; i+=8)
  {
      if((i + 8) &gt; SIZE3)
      {
          break;
      }
  
      _src1 = _mm512_load_epi64(&amp;src[i]);
      _src2 = _mm512_load_epi64(&amp;src2[i]);
  
      _dest = _mm512_mullo_epi64(_src1, _src2);
  
      _mm512_store_epi64(&amp;dest[i], _dest);        
  }
   }
  
   template&lt;uint64_t SIZE1, uint64_t SIZE2, uint64_t SIZE3&gt;
   void mul_avx2(const std::array&lt;int64_t, SIZE1&gt;&amp; src, const std::array&lt;int64_t, SIZE2&gt;&amp; src2, std::array&lt;int64_t, SIZE3&gt;&amp; dest)
   {
  __m256i _src1;
  __m256i _src2;
  __m256i _dest;
  
  for(uint64_t i = 0; i &lt; SIZE3; i+=4)
  {
      if((i + 4) &gt; SIZE3)
      {
          break;
      }
  
      _src1 = _mm256_load_si256((__m256i*)&amp;src[i]);
      _src2 = _mm256_load_si256((__m256i*)&amp;src2[i]);
  
      int64_t d[4] = {};
      for (size_t x = 0; x != 4; ++x)
      {
   #ifdef _WIN32
          d[x] = _src1.m256i_i64[x] * _src2.m256i_i64[x];
   #else
          d[x] = _src1[x] * _src2[x];
   #endif
      }//End for
  
      _dest = _mm256_load_si256((__m256i*) &amp;d);
  
      _mm256_store_si256((__m256i*)&amp;dest[i], _dest);        
  }
   }
  
  
  
   int main()
   {
  std::iota(arr1.begin(), arr1.end(), 5);
  std::iota(arr2.begin(), arr2.end(), 2);
  
  {   
      Timer();
       mul_f(arr1, arr2, arr3);
  }
  
  {
      Timer();
      mul_dq(arr1, arr2, arr3);
  }
  
  {
      Timer();
      mul_avx2(arr1, arr2, arr3);
  }
  
  return static_cast&lt;int&gt;(arr3[0]);
   }
  </code></pre>
  <p>Thanks in advance for your assistance.</p>