~/okanakyuz
← all posts

SIMD

A short look at SIMD on x86: MMX, SSE and AVX registers, checking CPU support with cpuid, and dividing vectors with inline asm and intrinsics.

11 April 2020updated 25 November 20264 min read

SIMD is a terminology for high-performance calculation inside the CPU. In this post we are going to look at it shortly.

Meaning Of SIMD

SIMD means Single Instruction Multiple Data. One instruction calculates several values at once, using special wide registers on the CPU. To do that you have to know the basic registers of the Intel x86 architecture.

Table of x86 registers

x86 registers

  • MMX is the first version of SIMD on x86. The Pentium MMX (1997) and above have this technology. The registers are 64 bits, MM0..MM7, and they share space with the x87 floating point registers.
  • SSE (Pentium III) brings new 128-bit registers, XMM0..XMM7, for single precision floats. On x86-64 there are 16 of them, XMM0..XMM15.
  • SSE2 adds double precision and integer operations on the same 128-bit XMM registers.
  • SSE3 and SSSE3 add more instructions, like horizontal add.
  • SSE4.1 and SSE4.2 add several more instructions (dot product, string compare, CRC32 etc.).
  • AVX extends the XMM registers to 256-bit YMM registers, and AVX2 adds integer operations on them.
  • AVX-512 extends them again to 512-bit ZMM registers and adds ZMM16 to ZMM31.

The 64-bit general registers such as RAX and RDI are not part of SIMD, they come with x86-64.

You can check what your CPU supports with the cpuid instruction:

#include <cstdint>
#include <cstdlib>
#include <iostream>

using namespace std;

void get_cpu_info(){
    uint32_t b, c, d;
    __asm(
        "movl $1, %%eax;"
        "cpuid;"
        "movl %%ebx, %0;"
        "movl %%edx, %1;"
        "movl %%ecx, %2"
        : "=m"(b), "=m"(d), "=m"(c)      // ebx -> b, edx -> d, ecx -> c
        :                                // no input
        : "eax", "ebx", "ecx", "edx"     // clobbered registers
    );

    if ((d & (1 << 23)) != 0){
        cout << "MMX support \n";
    }
    else
    {
        cerr << "You don't have MMX \n";
    }

    if ((d & (1 << 25)) != 0)
    {
        cout << "SSE support \n";
    }
    else
    {
        cerr << "You don't have SSE \n";
    }

    if ((d & (1 << 26)) != 0)
    {
        cout << "SSE2 support \n";
    }
    else
    {
        cerr << "You don't have SSE2 \n";
    }

    if ((c & 1) != 0)
    {
        cout << "SSE3 support \n";
    }
    else
    {
        cerr << "You don't have SSE3 \n";
    }

    if ((c & (1 << 9)) != 0)
    {
        cout << "SSSE3 support \n";
    }
    else
    {
        cerr << "You don't have SSSE3 \n";
    }

    if ((c & (1 << 19)) != 0)
    {
        cout << "SSE4.1 support \n";
    } else {
        cerr << "You don't have SSE4.1 \n";
    }

    if ((c & (1 << 20)) != 0)
    {
        cout << "SSE4.2 support \n";
    } else {
        cerr << "You don't have SSE4.2 \n";
    }
}

int main(int argc, char const *argv[])
{
    //read https://en.wikipedia.org/wiki/CPUID
    get_cpu_info();

    return EXIT_SUCCESS;
}

The feature bits for MMX and SSE/SSE2 are in EDX, the others are in ECX, so take care of the order of the output operands.

You don’t have to write this by hand anymore. GCC and Clang have <cpuid.h> with __get_cpuid(), and even simpler, __builtin_cpu_supports("avx2"). MSVC has __cpuid() in <intrin.h>.

How to do it?

To vectorise your calculation, you may use SSE or SSE2 registers and instructions. The most direct way is using inline assembly code.

#include <cstdlib>
#include <iostream>

using namespace std;

void div_sse_two_vector(){
    float f1[4] = {1.1f, 2.2f, 3.3f, 4.4f};
    float f2[4] = {5.1f, 6.2f, 7.3f, 8.4f};
    float re[4] = {0.f};

    // SSE SIMD code: re = f2 / f1
    __asm __volatile (  "movups %1, %%xmm1;"
                        "movups %2, %%xmm2;"
                        "divps %%xmm1, %%xmm2;"
                        "movups %%xmm2, %0"
                        : "=m" (re)
                        : "m" (f1), "m" (f2)
                        : "xmm1", "xmm2");

    for (int x = 0; x < 4; x++) {
        cout << f2[x] << "\t / \t" << f1[x] << "\t = \t" << re[x] << "\n";
    }
}

int main(int argc, char const *argv[])
{
    //read https://en.wikipedia.org/wiki/Streaming_SIMD_Extensions
    div_sse_two_vector();

    return EXIT_SUCCESS;
}

One divps instruction divides all four floats at once.

Disadvantages of inline assembly for SIMD?

  • Assembly is a really hard language.
  • There are tons of CPU instructions.
  • It is not portable. MSVC doesn’t even support inline asm on x64.

The solution is using intrinsics.

Intrinsics

The intrinsics have an easy format. The types are:

  • __m128i : 128-bit integer
  • __m256d : 256-bit double
  • __m128 : 128-bit float

Even though intrinsics are easy, you can’t use simple operators like + or / on them (GCC and Clang allow it as an extension, MSVC doesn’t). So you need the intrinsic functions, such as:

  • _mm256_mul_ps() : 256-bit packed float multiplication
  • _mm_add_ps() : 128-bit packed float addition

To use intrinsics, include <immintrin.h> on GCC and Clang (or <x86intrin.h> for everything). On MSVC <intrin.h> includes all of them.

Header Purpose
x86intrin.h Everything, including non-vector x86 instructions like _rdtsc().
mmintrin.h MMX (Pentium MMX!)
mm3dnow.h 3DNow! (K6-2) (deprecated, AMD dropped it long ago)
xmmintrin.h SSE + MMX (Pentium 3, Athlon XP)
emmintrin.h SSE2 + SSE + MMX (Pentium 4, Athlon 64)
pmmintrin.h SSE3 + SSE2 + SSE + MMX (Pentium 4 Prescott, Athlon 64 San Diego)
tmmintrin.h SSSE3 + SSE3 + SSE2 + SSE + MMX (Core 2, Bulldozer)
popcntintrin.h POPCNT (Nehalem (Core i7), Phenom)
ammintrin.h SSE4A + SSE3 + SSE2 + SSE + MMX (AMD-only, starting with Phenom)
smmintrin.h SSE4_1 + SSSE3 + SSE3 + SSE2 + SSE + MMX (Penryn, Bulldozer)
nmmintrin.h SSE4_2 + SSE4_1 + SSSE3 + SSE3 + SSE2 + SSE + MMX (Nehalem (aka Core i7), Bulldozer)
wmmintrin.h AES (Core i7 Westmere, Bulldozer)
immintrin.h AVX, AVX2, AVX512, all SSE+MMX (except SSE4A and XOP), popcnt, BMI/BMI2, FMA

intrin headers

#include <cstdlib>
#include <iostream>
#include <immintrin.h>

void div_sse_two_vector(){
    __m128 a = _mm_set_ps(1.1f, 2.2f, 3.3f, 4.4f);
    __m128 b = _mm_set_ps(5.1f, 6.2f, 7.3f, 8.4f);
    __m128 c = _mm_div_ps(b, a);

    float fa[4], fb[4], fc[4];
    _mm_storeu_ps(fa, a);
    _mm_storeu_ps(fb, b);
    _mm_storeu_ps(fc, c);

    for (int x = 0; x < 4; x++) {
        std::cout << fb[x] << "\t / \t" << fa[x] << "\t = \t" << fc[x] << "\n";
    }
}

int main(int argc, char const *argv[])
{
    div_sse_two_vector();
    return EXIT_SUCCESS;
}

Be careful, _mm_set_ps takes the values from the highest element to the lowest, so fa[0] is 4.4 here. If you want them in memory order use _mm_setr_ps. I copy the results to normal arrays with _mm_storeu_ps because indexing a __m128 like a[x] only works on GCC and Clang.

Result

As a conclusion, SIMD programming has some advantages and disadvantages.

Advantages:

  • You are using just CPU instructions to level up your calculation speed.

Disadvantages:

  • Programming in assembly language is really hard.
  • Intrinsics code is tied to one instruction set. Code for AVX2 won’t run on a CPU without AVX2, and it won’t compile for ARM at all.
  • The instructions run on only one core of the CPU. For more speed you still need threads.
  • Compilers can auto-vectorize simple loops by themselves (GCC and Clang at -O2/-O3, MSVC at /O2), but more complex code usually isn’t vectorized without help.

If you need matrix math, libraries like Eigen and Armadillo already use SIMD under the hood, so you get it for free. If you want to write your own vectorized code without touching intrinsics, have a look at xsimd or Google’s Highway.

Update (2026): Portable SIMD is finally coming to the standard. std::experimental::simd from the Parallelism TS v2 has been in GCC’s libstdc++ since GCC 11, and std::simd was voted into C++26 in the <simd> header. With it you write std::simd<float> and normal operators, and the library picks SSE, AVX or NEON for you.