Skip to content
systemcoding.dev
Performance

Add eight floats at once with _mm256_add_ps

Add eight floats at once with _mm256_add_ps from AVX in C, loading two vectors with loadu, adding them and storing the result back to memory.

1 min read
Summarize in AI

AVX gives you 256-bit registers that hold eight single-precision floats. _mm256_add_ps adds two of them lane by lane in one instruction. The surrounding calls move data in and out of the register, and you build with -mavx on GCC 14 or Clang 19.

add8.cc
#include <immintrin.h>
#include <stdio.h>

int main(void) {
    float a[8] = {1, 2, 3, 4, 5, 6, 7, 8};
    float b[8] = {10, 10, 10, 10, 10, 10, 10, 10};
    float c[8];

    __m256 va = _mm256_loadu_ps(a);
    __m256 vb = _mm256_loadu_ps(b);
    _mm256_storeu_ps(c, _mm256_add_ps(va, vb));

    for (int i = 0; i < 8; i++)
        printf("%g ", c[i]);
    printf("\n");
}
Text
11 12 13 14 15 16 17 18

Each output lane is the sum of the lanes in the same position, so the result matches a scalar loop exactly: a single addition per lane gives the same answer, with the same rounding. __m256 is a register type, not an array, so you cannot index it; go through memory to read the lanes. Check that the CPU supports AVX before executing the code: on a machine without it, the program dies with an illegal instruction. The operation is listed in the Intel Intrinsics Guide.

Taggedcavx
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.