title: Why use _mm_malloc? (as opposed to _aligned_malloc, alligned_alloc, or posix_memalign)
body: <p>There are a few options for acquiring an aligned block of memory but they're very similar and the issue mostly boils down to what language standard and platforms you're targeting.</p>
  
  <p>C11</p>
  
  <pre><code>void * aligned_alloc (size_t alignment, size_t size)
  </code></pre>
  
  <p>POSIX</p>
  
  <pre><code>int posix_memalign (void **memptr, size_t alignment, size_t size)
  </code></pre>
  
  <p>Windows</p>
  
  <pre><code>void * _aligned_malloc(size_t size, size_t alignment);
  </code></pre>
  
  <p>And of course it's also always an option to align by hand.</p>
  
  <p>Intel offers another option.</p>
  
  <p>Intel</p>
  
  <pre><code>void* _mm_malloc (int size, int align)
  void _mm_free (void *p)
  </code></pre>
  
  <p>Based on source code released by Intel, this seems to be the method of allocating aligned memory their engineers prefer but I can't find any documentation comparing it to other methods. The closest I found simply acknowledges that other aligned memory allocation routines exist.</p>
  
  <p><a href="https://software.intel.com/en-us/articles/memory-management-for-optimal-performance-on-intel-xeon-phi-coprocessor-alignment-and" rel="noreferrer">https://software.intel.com/en-us/articles/memory-management-for-optimal-performance-on-intel-xeon-phi-coprocessor-alignment-and</a></p>
  
  <blockquote>
    <p>To dynamically allocate a piece of aligned memory, use posix_memalign,
    which is supported by GCC as well as the Intel Compiler. The benefit
    of using it is that you don’t have to change the memory disposal API.
    You can use free() as you always do. But pay attention to the
    parameter profile:</p>
    
    <p>  int posix_memalign (void **memptr, size_t align, size_t size);</p>
    
    <p>The Intel Compiler also provides another set of memory allocation
    APIs. C/C++ programmers can use _mm_malloc and _mm_free to allocate
    and free aligned blocks of memory. For example, the following
    statement requests a 64-byte aligned memory block for 8 floating point
    elements.</p>
    
    <p>  farray = (float *)__mm_malloc(8*sizeof(float), 64);</p>
    
    <p>Memory that is allocated using _mm_malloc must be freed using
    _mm_free. Calling free on memory allocated with _mm_malloc or calling _mm_free on memory allocated with malloc will result in unpredictable behavior.</p>
  </blockquote>
  
  <p>The clear differences from a user perspective is that <code>_mm_malloc</code> requires direct CPU and compiler support and memory allocated with <code>_mm_malloc</code> must be freed with <code>_mm_free</code>. Given these drawbacks, what is the reason for ever using <code>_mm_malloc?</code> Can it have a slight performance advantage? Historical accident?</p>