Write ARM NEON Intrinsics With arm_neon.h
Use arm_neon.h to write NEON vector loops in C for AArch64: float32x4_t, vld1q_f32, vaddq_f32, vfmaq_f32 and vaddvq_f32, with a full dot product and its output.

NEON is Arm's 128-bit SIMD instruction set, and on AArch64 it is part of the base architecture: every 64-bit Arm core that runs a normal operating system has it, from Raspberry Pi boards to phones and Apple silicon laptops. You program it from C and C++ with the intrinsics in <arm_neon.h>, which are standardized by Arm in the C Language Extensions and documented in the Arm Intrinsics reference. This post writes a float dot product with NEON intrinsics for AArch64, shows the load, fused multiply-add and horizontal-add pattern, covers the type system and naming scheme, and notes what changes on 32-bit Arm. The compilers are GCC 15 and Clang 20 targeting aarch64, which enable NEON with no extra flags.
Types and naming
NEON vector types are named by element type and count. float32x4_t is four 32-bit floats in a 128-bit register, int32x4_t is four 32-bit signed integers, uint8x16_t is sixteen unsigned bytes and float64x2_t is two doubles, which exists on AArch64 only. A 64-bit form exists too: float32x2_t is two floats in a half-width register, and the 128-bit forms are called quad registers.
Intrinsic names follow a pattern: v for vector, the operation, an optional q that means the 128-bit quad width, and a suffix for the element type. So vaddq_f32 is a vector add on quad registers of 32-bit floats, vld1q_f32 loads one vector of four floats, and vst1q_f32 stores one. vmulq_f32 multiplies, and vfmaq_f32(acc, a, b) computes acc + a * b with a single rounding, a fused multiply-add, available on AArch64. The older vmlaq_f32 is a multiply-accumulate that the compiler may or may not fuse, so prefer vfmaq_f32 on AArch64 when you want the fused form.
A dot product
A dot product multiplies two arrays element-wise and sums the products. The vector version keeps four partial sums in one register and combines them at the end:
#include <arm_neon.h>
#include <stddef.h>
#include <stdio.h>
float dot_neon(const float *a, const float *b, size_t n)
{
float32x4_t acc = vdupq_n_f32(0.0f); // four zeros
size_t i = 0;
for (; i + 4 <= n; i += 4) {
float32x4_t va = vld1q_f32(a + i);
float32x4_t vb = vld1q_f32(b + i);
acc = vfmaq_f32(acc, va, vb); // acc += va * vb, per lane
}
float sum = vaddvq_f32(acc); // add the four lanes (AArch64)
for (; i < n; ++i) // scalar tail
sum += a[i] * b[i];
return sum;
}
int main(void)
{
float a[8] = {1, 2, 3, 4, 5, 6, 7, 8};
float b[8] = {2, 2, 2, 2, 2, 2, 2, 2};
printf("dot = %.1f\n", dot_neon(a, b, 8));
return 0;
}dot = 72.0Each element of a is multiplied by 2, so the dot product is twice the sum of 1 through 8, which is 36, giving 72. Eight elements are two full iterations of the vector loop, so the tail does nothing here. The partial sums are exact because every intermediate value is a small integer well inside float precision.
Build on an AArch64 machine with gcc -O2 dot_neon.c -o dot_neon, or cross-compile with aarch64-linux-gnu-gcc -O2. No -mfpu flag is needed on AArch64, because NEON is always present.
What each step does
vdupq_n_f32(0.0f) broadcasts a scalar into all four lanes, the NEON counterpart of _mm_set1_ps. vld1q_f32(ptr) loads four consecutive floats, with no alignment requirement beyond that of the element type, which is 4 bytes. That is one difference from x86: there is no aligned and unaligned pair, because NEON loads handle any element-aligned address efficiently. vfmaq_f32(acc, va, vb) performs the fused multiply-add on each lane independently, which in a loop forms four independent accumulation chains.
vaddvq_f32(acc) is a horizontal add across lanes that returns a scalar. It exists only on AArch64. On 32-bit Arm, you reduce with pairwise adds: vadd_f32(vget_low_f32(acc), vget_high_f32(acc)) followed by vpadd_f32 and vget_lane_f32. Horizontal reductions cost more than ordinary adds, so do them once after the loop, as above.
Floating-point order and tolerance
The vector dot product adds in a different order than a scalar loop: it sums elements 0, 4, 8 and so on into lane 0, elements 1, 5, 9 into lane 1, and so on, then adds the four lane sums. Floating-point addition is not associative, so the result can differ from the scalar loop in the last bits. The fused multiply-add also rounds once instead of twice, so it can differ from a separate multiply and add. When you test, compare with a tolerance relative to the magnitude of the terms, not with ==. Compilers will not make this transformation themselves for a plain scalar loop unless you allow it with -ffast-math or -fassociative-math, which is why hand-written intrinsics, or an explicit vectorization pragma, are how you get a vectorized float reduction.
Unrolling for throughput
A single accumulator makes a dependency chain: each vfmaq_f32 waits for the previous one. A fused multiply-add has a latency of several cycles on current Arm cores, and the cores can start more than one per cycle, so the loop runs at the latency of one instruction per iteration unless you break the chain. Use several accumulators:
float32x4_t acc0 = vdupq_n_f32(0.0f), acc1 = vdupq_n_f32(0.0f);
for (; i + 8 <= n; i += 8) {
acc0 = vfmaq_f32(acc0, vld1q_f32(a + i), vld1q_f32(b + i));
acc1 = vfmaq_f32(acc1, vld1q_f32(a + i + 4), vld1q_f32(b + i + 4));
}
float sum = vaddvq_f32(vaddq_f32(acc0, acc1));The two accumulators are independent, so the processor overlaps their fused multiply-adds. The latencies and throughputs of these instructions are published for each core in Arm's software optimization guides, such as those for the Cortex-A and Neoverse families, and they tell you how many accumulators to use. Four is a common choice for modern cores.
Other useful intrinsics
Beyond arithmetic, a handful of intrinsics cover most beginner needs. vmaxq_f32 and vminq_f32 take lane-wise maximum and minimum. vcgtq_f32(a, b) compares lanes and returns a mask vector of all ones or all zeros per lane, and vbslq_f32(mask, a, b) selects a where the mask is set and b elsewhere, which is how branchy code becomes branchless in NEON. vcvtq_f32_s32 converts integers to floats and vcvtq_s32_f32 goes the other way, truncating. vget_lane_f32 extracts one lane, and vsetq_lane_f32 inserts one, both taking a constant lane index.
Structure loads deserve a mention. vld3q_u8 loads 48 bytes and deinterleaves them into three vectors, so an array of RGB pixels becomes separate red, green and blue vectors in one instruction, and vst3q_u8 interleaves them on the way back. The same family, vld2 and vld4, handles pairs and quads. This is one place where NEON's instruction set is more convenient than SSE.
Dispatch and portability
On AArch64 there is no runtime check needed for base NEON. Optional extensions such as the dot product instructions (SDOT), SVE and SVE2 do need one. On Linux you test them with getauxval(AT_HWCAP) and the HWCAP_ASIMDDP bit, or with __ARM_FEATURE_DOTPROD at compile time. Apple platforms expose features through sysctlbyname.
To share code between x86 and Arm, wrap the two implementations behind one function name and select with the predefined macros __aarch64__ and __x86_64__, and keep a scalar reference version for tests. Libraries such as SIMDe provide portable wrappers that implement x86 intrinsics in terms of NEON, which is convenient when porting existing code, and Google's Highway library offers a portable abstraction.
32-bit Arm
On 32-bit ARMv7, NEON is optional and requires -mfpu=neon along with a hard-float ABI, as in -mfloat-abi=hard. The fused multiply-add vfmaq_f32 needs the VFPv4 variant, selected by -mfpu=neon-vfpv4. Instructions such as vaddvq_f32 and the 64-bit float64x2_t type do not exist. If you must support ARMv7, write the horizontal reduction with the pairwise form shown above, and test on real hardware or an emulator.
Testing
Compare your vector loop against a scalar reference for lengths 0 to 17, which covers every remainder class and several full-vector counts, using a relative tolerance such as 1e-5 for float data. Run the test on real AArch64 hardware or under an emulator such as QEMU user mode for CI. Because the intrinsics map almost directly to instructions, objdump -d on the binary shows fmla for the fused multiply-add and ld1 for the loads, which is the quickest check that you got what you wrote.


