Try Embedded

DSP instructions on the Cortex-M33

SIMD and saturating instructions, Q15 mixing and FIR filters from C.

DSP instructions on the Cortex-M33

Signal-processing code (audio, filters, motor control, sensor fusion) is mostly the same few operations done many times: multiply, add, and keep the result in range. The Cortex-M33 cores of the Raspberry Pi Pico 2 have the Armv8-M DSP extension, a set of instructions that do these on 8-bit and 16-bit values packed into 32-bit registers, with saturation built in. The Pico's Cortex-M0+ has none of them. This lesson shows what they do, how to use them from C, and how to keep plain-C code that gives the same results.

Q15 and saturation

Audio and filter coefficients are usually Q15: an int16_t whose value is the integer divided by 32768, so -32768 to 32767 stands for -1.0 to just under +1.0 (the fixed-point lesson covers the formats). Two Q15 samples add to a value up to twice as large. In plain C the sum wraps when cast back to 16 bits, and a loud sample becomes a large negative one: a click. Saturating arithmetic stops at the limit instead:

#include <stdio.h>
#include <stdint.h>

int16_t wrap_add(int16_t a, int16_t b) { return (int16_t)(a + b); }

int16_t sat_add(int16_t a, int16_t b) {
    int32_t s = (int32_t)a + b;
    return s > 32767 ? 32767 : s < -32768 ? -32768 : (int16_t)s;
}

int main(void) {
    printf("wrapped:   %d\n", wrap_add(30000, 10000));
    printf("saturated: %d\n", sat_add(30000, 10000));
    return 0;
}

The compare-and-clamp costs a few instructions a sample. The DSP extension does it inside the add.

SIMD: two samples an instruction

SIMD (single instruction, multiple data) instructions treat a 32-bit register as two 16-bit lanes or four 8-bit lanes, and work on every lane at once:

InstructionWhat it does
QADD16, QSUB16Two signed 16-bit adds or subtracts, each saturated
SADD16, SSUB16The same, wrapping; they set the GE flags per lane
UQADD8, UQSUB8Four unsigned 8-bit adds or subtracts, saturated (pixels)
SMLADTwo 16 × 16 multiplies, both products added to a 32-bit sum
SMLALDThe same into a 64-bit sum
SMUAD, SMUSDThe sum or difference of two products
SSAT, USATSaturate a value to n bits, signed or unsigned
PKHBT, PKHTBPack two halves into one register
SXTB16, UXTB16Unpack two bytes to two halfwords

With two samples in one register, a mix loop does half the loads, half the adds and half the stores, and no compares. A 16-tap FIR filter needs 8 SMLADs a sample, not 16 multiply-adds.

Using them from C

There is no need for assembly. GCC's arm_acle.h (the Arm C Language Extensions) gives each instruction as a function the compiler turns into that one instruction, when the core has it (__ARM_FEATURE_SIMD32 is defined):

#include <arm_acle.h>

int32_t mixed = __qadd16(a_pair, b_pair);        // two saturated adds
int32_t sum = __smlad(x_pair, h_pair, sum);      // sum += x.lo*h.lo + x.hi*h.hi
int16_t out = __ssat(sum >> 15, 16);             // back to a saturated Q15 sample

CMSIS's cmsis_gcc.h gives the same instructions under capitalised names (__QADD16), and the CMSIS-DSP library builds whole filters, FFTs and matrix operations on them (arm_fir_q15, arm_rfft_q15).

Two samples become one word with memcpy from an int16_t array (the compiler makes it a single load) or with a pointer to uint32_t on aligned data. The lower-addressed sample is in the low half on the little-endian Cortex-M33.

Keeping a plain-C version

Code that must also build for a core without the DSP extension, or for a unit test on a PC, keeps a portable version that gives the same results bit for bit. It is also the reference the fast version is tested against:

#include <stdio.h>
#include <stdint.h>

static int16_t sat16(int32_t v) { return v > 32767 ? 32767 : v < -32768 ? -32768 : (int16_t)v; }

// What QADD16 does: each 16-bit half added on its own, saturated, with no carry between them.
uint32_t qadd16(uint32_t a, uint32_t b) {
    int32_t lo = (int16_t)(a & 0xffff) + (int16_t)(b & 0xffff);
    int32_t hi = (int16_t)(a >> 16) + (int16_t)(b >> 16);
    return ((uint32_t)(uint16_t)sat16(hi) << 16) | (uint16_t)sat16(lo);
}

// What SMLAD does: two products added to the sum.
int32_t smlad(uint32_t x, uint32_t y, int32_t acc) {
    return acc + (int16_t)(x & 0xffff) * (int16_t)(y & 0xffff) + (int16_t)(x >> 16) * (int16_t)(y >> 16);
}

int main(void) {
    printf("%08lx\n", (unsigned long)qadd16(0x7fff0001, 0x00010001));   // high half saturates, low adds
    printf("%ld\n", (long)smlad(0x00020003, 0x00040005, 10));           // 10 + 3*5 + 2*4
    return 0;
}

The example project runs both versions of a mix and a FIR filter on the same block of samples, checks that they agree, and prints the speed-up: about three times for the mix, two for the filter.

Where the time goes

SIMD speeds up the arithmetic, not the loop around it. The FIR gains less than the mix because each tap pair still needs its loads and the loop's counter and branch. Real DSP code unrolls the loop, keeps the coefficients in registers, and processes several outputs at once so each loaded sample is used more than once. Measure with micros() or the DWT cycle counter rather than guess.

Interview question: Why does audio code saturate instead of letting integer arithmetic wrap?

Answer: A wrapped sum turns the largest positive value into a large negative one, a full-scale jump that is heard as a loud click or crackle, and it can drive a filter or amplifier into instability. Saturation clips at the limit, which sounds much better and is easy to detect. On DSP cores it costs nothing: QADD16, SSAT and similar instructions saturate as part of the operation, and the Q flag records that it happened.

Interview question: How would you make sure a SIMD-optimised filter is correct?

Answer: Keep a plain-C reference implementation with the same fixed-point rules (the same shifts, rounding and saturation), run both on the same input blocks, including full-scale, silent and random signals and the edges of the range, and compare the outputs sample for sample, not just by eye. Run it in the unit tests on the host and on the target, and measure the speed-up with a cycle counter.

Going deeper

Interview question: What is the Q flag on a Cortex-M with the DSP extension?

Answer: A sticky bit in the APSR that saturating instructions such as QADD, SSAT and SMLAD's overflow set when they had to clamp. It stays set until software clears it with MSR, so a block of DSP code can be checked once at the end to see whether anything clipped, rather than testing every result.

Interview question: Why are the DSP instructions in 16-bit lanes rather than full 32-bit operations?

Answer: Audio, sensor and filter data is mostly 16 bits or less, and two 16-bit values fit in one 32-bit register, so packing them doubles the work per instruction without wider registers or datapaths. The products of two 16-bit values are 32 bits, which the accumulate forms add into a 32-bit or 64-bit sum without losing precision.

Explain it: In your own words, explain how SIMD instructions make a mix of two audio tracks faster.

Answer: Each register holds two 16-bit samples, so one load brings in two samples of a track, one QADD16 adds both pairs and saturates each result, and one store writes two mixed samples. The plain loop does a load, an add, two compares and a store for every single sample. So the SIMD loop does about half the memory accesses and far fewer instructions for the same block.