Simvik

What QADD16 does

Add two pairs of packed 16-bit samples, each half saturating on its own. A medium embedded C problem (DSP instructions and fixed point on the Cortex-M33),…

medium · DSP instructions and fixed point on the Cortex-M33 · simd · saturation · q15

The Cortex-M33's QADD16 adds the two 16-bit halves of one register to the two halves of another, each sum saturated to the int16_t range, in one instruction. Firmware that must also build for a core without it needs the same result in plain C.

a and b each hold two signed 16-bit samples: bits 0-15 and bits 16-31. Return the packed result: the low halves' saturated sum in bits 0-15 and the high halves' in bits 16-31. A carry from the low half must never reach the high half.

Starter code

uint32_t qadd16(uint32_t a, uint32_t b) {
  // One 32-bit add: the halves wrap and the low half's carry spills into the high half.
  return a + b;
}

Example tests

Both halves in range

printf("%08lx\n", (unsigned long)qadd16(0x00030001, 0x00040002));

Expected output:

00070003

A half that overflows stops at the top

printf("%08lx\n", (unsigned long)qadd16(0x00017530, 0x00012710));

Expected output:

00027fff

Learn it first

More DSP instructions and fixed point on the Cortex-M33 problems