Key Takeaway

SIMD (Single Instruction, Multiple Data) applies the same operation to multiple data elements with a single instruction. Because one 128-bit register holds 16 eight-bit values, 16 scalar additions can be processed at once. In C++, compiler auto-vectorization is the easiest starting point, and in g++ you can immediately check whether a loop was vectorized with -O3 -fopt-info-vec-optimized.

A recent Hacker News discussion highlighted an article showing that the low-cost microcontroller ESP32-S3 includes 128-bit SIMD instructions and uses them to accelerate a computer-vision feature detector (FAST). In that post, the author reports it became faster than the reference implementation. Rather than reproducing the article’s assembly or algorithm, this post explains the SIMD concept and shows how to check C++ compiler auto-vectorization with g++ on a PC.

What is SIMD?

One-line answer: One instruction applies the same operation to several data elements at once.

In SISD (Single Instruction, Single Data), one instruction operates on one datum, such as a normal scalar addition. In SIMD (Single Instruction, Multiple Data), a single instruction operates on a vector register holding several values in parallel.

Typical use cases include image and audio processing as well as array math where the same operation is repeated across many elements.

SISD processes one datum per instruction; SIMD applies the same operation to multiple lanes in one vector register at once

How many values fit in a 128-bit register?

One-line answer: The count equals 128 divided by the bit width of each element.

Each element slot inside a vector register is called a lane. Depending on element width, a 128-bit register holds the following number of lanes:

Element WidthNumber of Lanes
8-bit×16
16-bit×8
32-bit×4

In the original article, the ESP32-S3 vector register is 128 bits wide, holding 16 eight-bit values at once. How bytes are ordered inside memory is a separate topic covered in our post on endianness (big-endian vs little-endian).

How can you use SIMD from C++?

One-line answer: There are three ways: compiler auto-vectorization, intrinsics, and assembly.

  • Auto-vectorization: Write plain C++ loops and let the compiler convert them into vector instructions. It is the easiest and most portable option, though not guaranteed for every loop.
  • Intrinsics: Call C/C++ functions that map directly to specific vector instructions through ISA-specific headers.
  • Assembly: Write target assembly instructions directly for full control, which is also the hardest to write and maintain. The original author wrote ESP32-S3 assembly.

A practical workflow is to start with auto-vectorization and measure before reaching for lower-level code.

How do you check whether g++ vectorized your loop?

One-line answer: Compile with -O3 -fopt-info-vec-optimized and look for loop vectorized in the compiler output.

Here is the example source file simd/add.cpp:

#include <cstdint>
#include <cstdio>
void add_u8(uint8_t* __restrict d,const uint8_t* __restrict a,const uint8_t* __restrict b,int n){for(int i=0;i<n;i++){int s=a[i]+b[i];d[i]=s>255?255:s;}}
int main(){uint8_t a[64],b[64],d[64];for(int i=0;i<64;i++){a[i]=i*4;b[i]=100;}add_u8(d,a,b,64);printf("%d %d %d\n",d[0],d[20],d[63]);}

Here is the result measured in this post’s environment (g++ 14.2.0, x86-64 Linux):

$ g++ -O3 -fopt-info-vec-optimized simd/add.cpp -o add && ./add
simd/add.cpp:3:111: optimized: loop vectorized using 16 byte vectors
simd/add.cpp:3:111: optimized: loop vectorized using 16 byte vectors
simd/add.cpp:4:51: optimized: loop vectorized using 16 byte vectors
100 180 255
$ g++ -O1 -fopt-info-vec-optimized simd/add.cpp -o add1 && ./add1
100 180 255

The function add_u8 adds two uint8_t arrays and clamps each result to 255 (a saturating add written in plain C++), while __restrict promises the compiler that the arrays do not overlap in memory.

At -O3, the messages report that the loop on line 3 (add_u8) and the loop on line 4 (the initialization loop in main) were vectorized using 16 byte vectors, meaning 16 bytes = 128 bits = 16 uint8_t lanes. Line 3 appears twice in this environment, which may happen when the same loop is reported twice as separate copies of it are vectorized.

The program prints 100 180 255 both at -O3 and at -O1: vectorization changes how the loop runs, not the computed result. At -O1 there is no vectorization message. In addition, the flag -fopt-info-vec-missed exists to see why a loop was not vectorized.

What prevents auto-vectorization?

One-line answer: Pointer aliasing, branches or function calls in the loop, loop-carried data dependencies, and alignment issues are the main blockers.

  • Pointer aliasing: If the compiler cannot prove that input and output arrays do not overlap, it cannot safely vectorize. Use __restrict or restructure the data.
  • Branches and calls inside the loop: A simple conditional like clamping to 255 can become a vector select operation, whereas opaque function calls usually block vectorization.
  • Data dependency between iterations: When a[i] depends on a[i-1] from the previous iteration, lanes cannot be evaluated simultaneously.
  • Alignment and unknown trip count: The compiler may need to add runtime checks or scalar remainder loops.

Why do saturating arithmetic and signedness cause trouble?

One-line answer: SIMD instruction sets often clamp results (saturate) instead of wrapping, and may offer operations only for certain signed or unsigned types, so data must be adapted.

Standard C++ uint8_t arithmetic wraps around when converted back to 8 bits, whereas saturating arithmetic stops at the maximum value of 255 (which is why d[63] in the reproduction printed 255).

In the original ESP32-S3 case, the author encountered several hardware constraints:

  • Comparisons are provided only for signed 8-bit values.
  • SIMD arithmetic is saturating, so subtracting 128 from unsigned pixel values clamps at the end of the range instead of shifting it.
  • Unaligned reads are not possible, which was worked around with an instruction that concatenates two registers and extracts a slice.
  • Using the identity x - 128 == x ^ 0x80 (flipping the top bit) moves unsigned 0~255 values into the signed -128~127 range so signed comparisons work properly.

For the basics of integer promotion and signedness in C++, see What Is C++ Integral Promotion? Why ~true Evaluates to true.

Comparison of uint8_t wrap-around versus saturating arithmetic that stops at 255

What are the frequently asked questions?

One-line answer: Here are quick answers on how SIMD differs from a GPU, whether auto-vectorization works on embedded MCUs, and how to inspect missed vectorizations.

How do SIMD and a GPU differ?

SIMD uses vector instructions inside a CPU core on data held in CPU registers, whereas a GPU is a separate processor with many cores running massive parallel work. Both are data-parallel; for a broader hardware comparison, see What is the difference between an NPU and a GPU? A simple comparison by structure, use case, and power.

Does auto-vectorization work on embedded MCUs?

It depends on the toolchain and ISA; check with your compiler’s report options. Some MCU SIMD is used through vendor libraries or assembly (as in the original article). For running firmware without physical hardware, see “What is an ESP32 emulator? Running firmware on a PC or in the browser without a board”.

How can you see why a loop was not vectorized?

Use the -fopt-info-vec-missed flag in g++ and check for pointer aliasing or loop-carried dependencies as described above.

One-line answer: These related posts cover C++ integral promotion, memory endianness, and parallel processor comparisons.

What are the sources?

One-line answer: Below are the Hacker News discussion link, the unlinked reference documents, and the measurement environment for this post.