Module 9 · Performance
9.4SIMD
Do the same operation on eight floats with one instruction. See when the compiler vectorizes a loop for you and what stops it, then write a dot product with AVX2 intrinsics that matches the plain loop, handles leftovers and checks the CPU first.
On this page
- Key terms
- In 30 seconds
- One instruction, eight floats
- Auto-vectorization
- What stops it
- Help the compiler: separate sums
- Intrinsics
- The dot product with intrinsics
- How much faster?
- Checking the CPU at run time
- Which one should I use?
- Under the hood
- Common pitfalls
- Reading past the end
- Forgetting a flag
- Try it yourself
Key terms
- SIMD
- Single instruction, multiple data: one CPU instruction that does the same operation on several values at once, such as adding 8 pairs of
floats. - vector register (
xmm,ymm), lane - A wide CPU register that holds several values side by side: an
xmmregister is 128 bits (4floats), aymmregister 256 bits (8floats). Each value's slot is a lane. - AVX2
- The x86-64 instructions for 256-bit SIMD, in most x86-64 CPUs from the last ten years.
-mavx2lets the compiler use them. - auto-vectorization
- The compiler turning a loop into SIMD instructions by itself, usually at
-O2or-O3. -ffast-math- A flag that lets the compiler treat floating-point math as exact: reorder additions, and assume there are no NaN or infinity values. Faster, but results can change.
- intrinsic
- A function the compiler turns into one particular CPU instruction, such as
_mm256_add_psforvaddps. x86 intrinsics come from<immintrin.h>. - fused multiply-add (FMA)
- One instruction that computes
a * b + cfor every lane, rounding once at the end.-mfmaenables it. - horizontal sum
- Adding up the lanes of one vector register, as opposed to adding two registers lane by lane.
A modern CPU can add eight pairs of floats in the time it takes to add one pair, if you ask it the right way. That’s SIMD: single instruction, multiple data. Loops over pixels, scores and weight vectors are exactly the work it’s made for, and a dot product, the heart of every neural network layer, can run several times faster with it.
In 30 seconds
- A 256-bit
ymmregister holds 8floats. One AVX2 instruction adds, multiplies or compares all 8 lanes at once. - The compiler vectorizes simple loops itself at
-O3(and some at-O2) when you allow the instructions with-mavx2. Overlapping pointers, steps that depend on the previous one, and floating-point sums stop it. - When it can’t, intrinsics from
<immintrin.h>(_mm256_loadu_ps,_mm256_fmadd_ps, …) let you write the SIMD instructions yourself. - Handle the leftover elements, compare against the plain loop, and check that the CPU has AVX2 before using it.
One instruction, eight floats
Optimization levels and reading assembly showed xmm registers holding four ints. x86-64 CPUs have several generations of these vector registers:
| Registers | Width | floats per register | Instructions | Compiler flag |
|---|---|---|---|---|
xmm0–xmm15 | 128 bits | 4 | SSE, SSE2 (every x86-64 CPU) | none needed |
ymm0–ymm15 | 256 bits | 8 | AVX, AVX2, FMA | -mavx2 -mfma |
zmm0–zmm31 | 512 bits | 16 | AVX-512 (some server and desktop CPUs) | -mavx512f |
Each value’s slot is a lane. An addition works lane by lane: lane 0 of a plus lane 0 of b goes into lane 0 of the result, and so on, all in one instruction.
| Value | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | Size |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
a | 14 bytes | 24 bytes | 34 bytes | 44 bytes | 54 bytes | 64 bytes | 74 bytes | 84 bytes | 32 bytes | ||||||||||||||||||||||||
b | 104 bytes | 204 bytes | 304 bytes | 404 bytes | 504 bytes | 604 bytes | 704 bytes | 804 bytes | 32 bytes | ||||||||||||||||||||||||
a + b | 114 bytes | 224 bytes | 334 bytes | 444 bytes | 554 bytes | 664 bytes | 774 bytes | 884 bytes | 32 bytes | ||||||||||||||||||||||||
Three 32-byte rows. a holds the floats 1 to 8, one per 4-byte lane. b holds 10, 20 and so on up to 80. a + b holds 11, 22 and so on up to 88: each lane is the sum of the lanes above it.
Every x86-64 CPU has SSE2, so the compiler uses xmm registers without being asked. AVX2 and FMA are newer, so a program built with -mavx2 -mfma doesn’t run on older CPUs; “Checking the CPU at run time” below shows how to handle that. This lesson uses AVX2: it’s what most x86-64 machines have, and the checking machine for this site has no AVX-512. Arm CPUs, including phones and Apple’s M-series, have their own 128-bit SIMD called NEON, with different intrinsics.
Auto-vectorization
You often don’t need to write SIMD yourself. Here GCC turns a 16-step loop into two 8-lane additions:
void add16(float* __restrict out, const float* __restrict a,
const float* __restrict b) {
for (int i = 0; i < 16; ++i) {
out[i] = a[i] + b[i];
}
}Assembly (GCC 15, -O3 -mavx2)
add16(float*, float const*, float const*):
vmovups ymm0, YMMWORD PTR [rdx]
vaddps ymm0, ymm0, YMMWORD PTR [rsi]
vmovups YMMWORD PTR [rdi], ymm0
vmovups ymm0, YMMWORD PTR 32[rsi]
vaddps ymm0, ymm0, YMMWORD PTR 32[rdx]
vmovups YMMWORD PTR 32[rdi], ymm0
vzeroupper
retvmovups ymm0, YMMWORD PTR [rdx]loads 32 bytes, eightfloats ofb, intoymm0(“ups”: unaligned, packed, single precision).vaddpsadds eightfloats ofato them, and the nextvmovupsstores the eight sums toout. Then the same again, 32 bytes further on.vzeroupperclears the upper halves of theymmregisters before returning, because mixing 256-bit code with older 128-bit code is slow on some CPUs.
That is auto-vectorization. With an unknown count n, GCC also vectorizes the loop, and adds code for the elements left over when n isn’t a multiple of 8.
What stops it
Overlapping memory. __restrict promises the compiler that the three pointers don’t overlap. It’s an extension that GCC, Clang and MSVC all accept, not standard C++. Without it, out might point into a, so writing out[0] could change a[1]. GCC then adds a check for overlap at the start, plus a second copy of the loop that works one float at a time for when they do overlap: 67 lines of assembly instead of 9. Pointers that may point to the same memory are said to alias. Passing separate std::vectors doesn’t help on its own: the function only sees pointers or spans.
Each step needing the previous one. A running total, values[i] += values[i - 1], can’t be computed eight at a time, because each element needs the sum before it. GCC compiles it to one vaddss per element: the “scalar single” form, which works on lane 0 only.
Floating-point sums. This one surprises people. Summing eight floats with SIMD means adding them in a different order: lane by lane, then the lanes together. With floats, (a + b) + c can differ from a + (b + c) in the last bits, because every addition rounds. The compiler isn’t allowed to change your result, so it keeps your order, even at -O3:
float sum8(const float* values) {
float total = 0.0f;
for (int i = 0; i < 8; ++i) {
total += values[i];
}
return total;
}Assembly (GCC 15, -O3 -mavx2)
sum8(float const*):
vxorps xmm0, xmm0, xmm0
vaddss xmm0, xmm0, DWORD PTR [rdi]
vaddss xmm0, xmm0, DWORD PTR 4[rdi]
vaddss xmm0, xmm0, DWORD PTR 8[rdi]
vaddss xmm0, xmm0, DWORD PTR 12[rdi]
vaddss xmm0, xmm0, DWORD PTR 16[rdi]
vaddss xmm0, xmm0, DWORD PTR 20[rdi]
vaddss xmm0, xmm0, DWORD PTR 24[rdi]
vaddss xmm0, xmm0, DWORD PTR 28[rdi]
retEight scalar additions, each waiting for the one before. -ffast-math gives the compiler permission to reorder, and the same loop becomes 8-lane vaddps (open it in Compiler Explorer to see). The price is a different answer. Here are a million copies of 0.1f, added in order, built without sanitizers because their checks stop GCC from vectorizing this loop:
#include <print>
#include <vector>
std::vector<float> values(1'000'000, 0.1f);
float total = 0.0f;
for (float v : values) {
total += v;
}
std::println("{}", total);Output
100958.34
The same code with -ffast-math:
#include <print>
#include <vector>
std::vector<float> values(1'000'000, 0.1f);
float total = 0.0f;
for (float v : values) {
total += v;
}
std::println("{}", total);Output
99910.33
The exact sum is just over 100000. The reordered sum happens to be closer, because each of its 8 partial sums stays smaller and loses less to rounding, but -ffast-math promises only a different answer, not a better one. It also lets the compiler assume no value is NaN or infinity, so a check like std::isnan(x) can be removed. Use it only for a whole program you’ve tested with it, never as a quick fix for one loop. The next section shows a way to get a vectorized sum without it.
Help the compiler: separate sums
The order of additions is yours to choose. Keep eight separate running sums, one per lane, and the compiler can put them in one ymm register without changing any result. Here is a dot product, the sum of a[i] * b[i], written both ways:
#include <cstddef>
#include <print>
#include <span>
#include <vector>
float dot_plain(std::span<const float> a, std::span<const float> b) {
float total = 0.0f;
for (std::size_t i = 0; i < a.size(); ++i) {
total += a[i] * b[i];
}
return total;
}
float dot_lanes(std::span<const float> a, std::span<const float> b) {
float lanes[8] = {};
std::size_t i = 0;
for (; i + 8 <= a.size(); i += 8) {
for (std::size_t j = 0; j < 8; ++j) {
lanes[j] += a[i + j] * b[i + j];
}
}
for (; i < a.size(); ++i) { // the 0 to 7 elements left over
lanes[0] += a[i] * b[i];
}
float total = 0.0f;
for (float lane : lanes) {
total += lane;
}
return total;
}
int main() {
std::vector<float> a;
std::vector<float> b;
for (int i = 0; i < 1003; ++i) {
a.push_back(static_cast<float>(i % 8) * 0.5f);
b.push_back(static_cast<float>(i % 5) * 0.25f);
}
std::println("plain: {}", dot_plain(a, b));
std::println("lanes: {}", dot_lanes(a, b));
}Output
plain: 875.625 lanes: 875.625
dot_lanes processes the elements in blocks of 8. Its inner loop has no dependency between lanes, so GCC turns it into one 8-lane multiply and one 8-lane add per block. The second loop handles the leftover elements: 1003 isn’t a multiple of 8, so 3 are left after the last full block. The two results match exactly here because these values are multiples of 0.125, so every sum is exact. With arbitrary values they’d differ in the last digits, and neither would be more correct.
Intrinsics
When the compiler won’t vectorize, you can write the instructions yourself. Intrinsics look like functions but each one becomes one instruction. They come from <immintrin.h>, and the AVX ones follow one naming pattern: _mm256_ (256 bits), the operation, then _ps (packed single: 8 floats). The type __m256 is one ymm register’s worth of 8 floats:
#include <immintrin.h>
#include <print>
float x[8] = {1, 2, 3, 4, 5, 6, 7, 8};
float w[8] = {0.5f, 0.5f, 0.5f, 0.5f, 2, 2, 2, 2};
float out[8];
__m256 vx = _mm256_loadu_ps(x); // load 8 floats
__m256 vw = _mm256_loadu_ps(w);
__m256 ten = _mm256_set1_ps(10.0f); // 10 in every lane
_mm256_storeu_ps(out, _mm256_add_ps(vx, vw)); // store 8 floats
std::println("x + w: {}", out);
_mm256_storeu_ps(out, _mm256_mul_ps(vx, vw));
std::println("x * w: {}", out);
_mm256_storeu_ps(out, _mm256_fmadd_ps(vx, vw, ten));
std::println("x * w + 10: {}", out);Output
x + w: [1.5, 2.5, 3.5, 4.5, 7, 8, 9, 10] x * w: [0.5, 1, 1.5, 2, 10, 12, 14, 16] x * w + 10: [10.5, 11, 11.5, 12, 20, 22, 24, 26]
_mm256_loadu_ps(p) loads 8 floats from p and _mm256_storeu_ps(p, v) stores them; p needn’t be aligned. _mm256_set1_ps(x) puts x in every lane, _mm256_setzero_ps() makes all lanes 0, and _mm256_add_ps and _mm256_mul_ps work lane by lane. _mm256_fmadd_ps(a, b, c) computes a * b + c in every lane with one fused multiply-add instruction, which rounds once, at the end. The flags -mavx2 -mfma allow those instructions; without them, the intrinsics don’t compile (“Common pitfalls” shows the error).
The dot product with intrinsics
#include <cstddef>
#include <immintrin.h>
#include <print>
#include <span>
#include <vector>
float horizontal_sum(__m256 v) {
float lanes[8];
_mm256_storeu_ps(lanes, v);
float total = 0.0f;
for (float lane : lanes) {
total += lane;
}
return total;
}
float dot_avx(std::span<const float> a, std::span<const float> b) {
__m256 sums = _mm256_setzero_ps();
std::size_t i = 0;
for (; i + 8 <= a.size(); i += 8) {
__m256 va = _mm256_loadu_ps(a.data() + i);
__m256 vb = _mm256_loadu_ps(b.data() + i);
sums = _mm256_fmadd_ps(va, vb, sums); // sums += va * vb
}
float total = horizontal_sum(sums);
for (; i < a.size(); ++i) {
total += a[i] * b[i];
}
return total;
}
int main() {
std::vector<float> a; // the same 1003 values as before
std::vector<float> b;
std::println("avx: {}", dot_avx(a, b));
}Output
avx: 875.625
The same answer as both loops. The structure is the one every SIMD loop has: a main loop over full blocks of 8, a horizontal sum that adds the 8 lanes of the result together, and a plain loop for the leftovers. horizontal_sum stores the register to an array and adds the lanes one by one. Faster versions shuffle the lanes inside registers, but this runs once per call, so it hardly matters.
How much faster?
Here are the three versions, plus a fourth that keeps two __m256 sums, timed on 4096 floats (16 KB each, so the data stays in the cache). best_ms is the helper from Caches and data-oriented design: the best of five runs. The input starts at a different offset each time, so the compiler can’t compute one dot product and reuse it:
#include <cstddef>
#include <immintrin.h>
#include <print>
#include <span>
#include <vector>
float dot_avx_two_sums(std::span<const float> a, std::span<const float> b) {
__m256 sums0 = _mm256_setzero_ps();
__m256 sums1 = _mm256_setzero_ps();
std::size_t i = 0;
for (; i + 16 <= a.size(); i += 16) {
__m256 a0 = _mm256_loadu_ps(a.data() + i);
__m256 a1 = _mm256_loadu_ps(a.data() + i + 8);
sums0 = _mm256_fmadd_ps(a0, _mm256_loadu_ps(b.data() + i), sums0);
sums1 = _mm256_fmadd_ps(a1, _mm256_loadu_ps(b.data() + i + 8), sums1);
}
float total = horizontal_sum(_mm256_add_ps(sums0, sums1));
for (; i < a.size(); ++i) {
total += a[i] * b[i];
}
return total;
}
int main() {
std::vector<float> a;
std::vector<float> b;
for (int i = 0; i < 4096 + 8; ++i) {
a.push_back(static_cast<float>(i % 8) * 0.5f);
b.push_back(static_cast<float>(i % 5) * 0.25f);
}
auto time = [&](const char* name, auto dot) {
double total = 0.0;
double ms = best_ms([&] {
for (int rep = 0; rep < 1000; ++rep) { // 1000 dot products
auto shifted = std::span(a).subspan(rep % 8, 4096);
total += dot(shifted, std::span(b).first(4096));
}
});
std::println("{:<9} {:5.2f} ms (total {})", name, ms, total);
};
time("plain", dot_plain);
time("lanes", dot_lanes);
time("avx", dot_avx);
time("two sums", dot_avx_two_sums);
}Output from one run (it varies)
plain 1.65 ms (total 17915625) lanes 0.20 ms (total 17915625) avx 0.38 ms (total 17915625) two sums 0.19 ms (total 17915625)
Your times will differ with your machine; on the checking machine, the ratios held from run to run:
dot_lanes, plain C++ that the compiler vectorized, is about 8 times faster thandot_plain.dot_avxis only about 4 times faster. Every_mm256_fmadd_psadds into the samesums, so each one waits for the one before to finish, and on this CPU an FMA takes longer to finish than the addition GCC used indot_lanes.- With two separate sums, two FMAs are under way at once, and the intrinsics catch up.
A chain of steps that each wait for the last limits SIMD code just as it limits a plain loop. Intrinsics give you control, not speed for free: measure.
Checking the CPU at run time
A program built with -mavx2 may use AVX2 instructions anywhere, even in code you didn’t vectorize. On a CPU without AVX2, it stops with “illegal instruction”. To ship one program that runs everywhere, build it without -mavx2, mark only the SIMD functions with [[gnu::target("avx2")]], and choose at run time with __builtin_cpu_supports. Here, scaling pixel brightness from 0–1 to 0–255:
#include <cstddef>
#include <immintrin.h>
#include <print>
#include <span>
#include <vector>
[[gnu::target("avx2")]] void scale_avx(std::span<float> values, float factor) {
__m256 f = _mm256_set1_ps(factor);
std::size_t i = 0;
for (; i + 8 <= values.size(); i += 8) {
__m256 v = _mm256_loadu_ps(values.data() + i);
_mm256_storeu_ps(values.data() + i, _mm256_mul_ps(v, f));
}
for (; i < values.size(); ++i) {
values[i] *= factor;
}
}
void scale(std::span<float> values, float factor) {
if (__builtin_cpu_supports("avx2")) {
scale_avx(values, factor);
return;
}
for (float& v : values) {
v *= factor;
}
}
int main() {
std::vector<float> pixels = {0, 0.5f, 1, 0.25f, 0.75f, 1, 0, 0.5f, 0.125f};
scale(pixels, 255.0f);
std::println("{}", pixels);
}Output
[0, 127.5, 255, 63.75, 191.25, 255, 0, 127.5, 31.875]
[[gnu::target]] lets the compiler use AVX2 in that one function, so the intrinsics compile without the flag. __builtin_cpu_supports("avx2") asks the CPU what it has. Both work in GCC and Clang. The output is the same either way; only the speed differs.
Which one should I use?
| What | When | Why | How |
|---|---|---|---|
| A plain loop | First, always. | The compiler vectorizes simple loops over contiguous data at -O3. | -O3 -mavx2 (or -march=native for your own machine) |
| Help the compiler | A loop that should vectorize but doesn’t. | Remove what stops it: overlap, dependencies, a single running sum. | __restrict, separate sums per lane |
-ffast-math | Numeric code where small changes in results are acceptable, for the whole program. | Lets the compiler reorder sums and ignore NaN. | -ffast-math, tested with it |
| Intrinsics | A hot loop, measured, that the compiler won’t vectorize well. | Full control of the instructions. | <immintrin.h>, -mavx2 -mfma or [[gnu::target]] |
| A library | Matrix multiplication, FFTs, image processing. | Tuned for every CPU by people who do nothing else. | BLAS, Eigen, oneDNN, … |
Rule of thumb: write the plain loop, check the assembly or measure, and reach for intrinsics only for the few loops where it pays. Keep the plain version and test against it, with a tolerance: SIMD sums add in a different order and FMA rounds once, so results can differ in the last digits.
Under the hood
The registers overlap: xmm0 is the lower half of ymm0, which is the lower half of zmm0 on CPUs with AVX-512. That’s why vzeroupper exists, and why __m256 values passed to a function arrive in ymm0, ymm1, …, exactly like floats in xmm0.
SIMD needs contiguous data: one vmovups loads 8 neighbors, while 8 values from 8 scattered places cost 8 loads. That’s another reason for the structs of arrays from Caches and data-oriented design. _mm256_load_ps (no u) requires an address that’s a multiple of 32 and crashes otherwise; on current CPUs it’s no faster than loadu on aligned data, so loadu is the safe default.
Intrinsics tie your code to one CPU family: AVX2 code doesn’t compile for Arm, and NEON code doesn’t compile for x86-64. GCC ships std::experimental::simd (in <experimental/simd>), a portable type whose operations map to whichever SIMD instructions the target has, and C++26 standardizes it as std::simd. Once your compiler ships it, it will be the portable way to write the loops in this lesson.
Common pitfalls
Reading past the end
A SIMD loop that steps by 8 with i < size instead of i + 8 <= size reads a full block past the end when the size isn’t a multiple of 8. AddressSanitizer catches the 32-byte read:
#include <immintrin.h>
#include <print>
#include <vector>
std::vector<float> values(12, 1.0f);
__m256 total = _mm256_setzero_ps();
for (std::size_t i = 0; i < values.size(); i += 8) {
total = _mm256_add_ps(total, _mm256_loadu_ps(values.data() + i));
}
float lanes[8];
_mm256_storeu_ps(lanes, total);
std::println("{}", lanes[0]);This fails when it runs
ERROR: AddressSanitizer: unknown-crash on address 0x…
READ of size 32 at 0x… thread T0
#0 _mm256_loadu_ps(float const*)
#1 main main.cpp:8
0x… is located 0 bytes after 48-byte region [0x…,0x…)
allocated by thread T0 here:
#0 operator new(unsigned long)
… 7 frames inside the standard library
#8 main main.cpp:5
SUMMARY: AddressSanitizer: unknown-crash avxintrin.h:849 in _mm256_loadu_ps(float const*)Loop while i + 8 <= values.size(), then handle the rest one element at a time.
Forgetting a flag
Each intrinsic needs the instruction set it belongs to. _mm256_fmadd_ps is FMA, which -mavx2 doesn’t include:
#include <immintrin.h>
#include <print>
__m256 two = _mm256_set1_ps(2.0f);
__m256 six = _mm256_fmadd_ps(two, two, two);
std::println("{}", _mm256_cvtss_f32(six));The compiler rejects this
In file included from /usr/lib/gcc/x86_64-linux-gnu/15/include/immintrin.h:109,
from main.cpp:1:
/usr/lib/gcc/x86_64-linux-gnu/15/include/fmaintrin.h: In function ‘int main()’:
/usr/lib/gcc/x86_64-linux-gnu/15/include/fmaintrin.h:63:1: error: inlining failed in call to ‘always_inline’ ‘__m256 _mm256_fmadd_ps(__m256, __m256, __m256)’: target specific option mismatch
| _mm256_fmadd_ps (__m256 __A, __m256 __B, __m256 __C)
| ^~~~~~~~~~~~~~~
main.cpp:5:29: note: called from here
5 | __m256 six = _mm256_fmadd_ps(two, two, two);
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
compilation terminated due to -fmax-errors=1.(_mm256_cvtss_f32 reads lane 0 as a plain float.) GCC’s message says inlining failed because of a “target specific option mismatch”: every intrinsic is a function that must be inlined, and it’s marked with the instructions it needs. Add -mfma, or fma to the function’s [[gnu::target]].
Try it yourself
Try it yourself
Write scale_add(y, factor, x), which computes y[i] += factor * x[i] for every element, a step in many machine-learning updates, with AVX2 intrinsics: blocks of 8 with _mm256_fmadd_ps, then the leftovers one by one. With x = 1 to 11, y = eleven 0.5s and factor = 2, it must print the same as the plain loop for (std::size_t i = 0; i < y.size(); ++i) { y[i] += factor * x[i]; }.
Expected output
[2.5, 4.5, 6.5, 8.5, 10.5, 12.5, 14.5, 16.5, 18.5, 20.5, 22.5]
Show a hint
Broadcast factor with _mm256_set1_ps. In blocks of 8, load x and y, compute _mm256_fmadd_ps(f, vx, vy) and store it back to y. Then finish the last elements with the plain loop.
Show a solution
#include <cstddef>
#include <immintrin.h>
#include <print>
#include <span>
#include <vector>
void scale_add(std::span<float> y, float factor, std::span<const float> x) {
__m256 f = _mm256_set1_ps(factor);
std::size_t i = 0;
for (; i + 8 <= y.size(); i += 8) {
__m256 vx = _mm256_loadu_ps(x.data() + i);
__m256 vy = _mm256_loadu_ps(y.data() + i);
_mm256_storeu_ps(y.data() + i, _mm256_fmadd_ps(f, vx, vy));
}
for (; i < y.size(); ++i) {
y[i] += factor * x[i];
}
}
int main() {
std::vector<float> x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
std::vector<float> y(11, 0.5f);
scale_add(y, 2.0f, x);
std::println("{}", y);
}Each block of 8 computes factor * x + y in all lanes with one FMA and writes it back over y. 11 elements make one full block and 3 leftovers, which the plain loop handles. Every value here is exact, so the result matches the plain loop exactly.
Saved in this browser only.