What are compiler intrinsics, and when should you use them instead of inline assembly?
Short answer: Intrinsics are functions provided by the compiler (or by headers such as CMSIS) that map directly to specific machine instructions: __WFI(), __DSB(), __REV(), __CLZ(), __LDREXW(), saturating and SIMD operations. The compiler understands them, so it can still optimise around them and check types. Use intrinsics whenever one exists; use inline assembly only for instructions or sequences that have no intrinsic.
Common Cortex-M intrinsics (CMSIS)
| Intrinsic | Instruction | Use |
|---|---|---|
__enable_irq(), __disable_irq() |
CPSIE i, CPSID i |
critical sections |
__get_PRIMASK(), __set_BASEPRI() |
MRS/MSR |
interrupt masking |
__WFI(), __WFE(), __SEV() |
WFI, WFE, SEV |
sleep and event signalling |
__DMB(), __DSB(), __ISB() |
barriers | ordering |
__REV(), __REV16(), __RBIT() |
byte/bit reverse | endianness, CRCs |
__CLZ() |
count leading zeros | priority bitmaps, log2, normalisation |
__LDREXW(), __STREXW(), __CLREX() |
exclusive access | lock-free atomics |
__SSAT(), __USAT() |
saturate | DSP clipping |
__SADD16(), __SMLAD(), __QADD() |
DSP/SIMD (Cortex-M4/M7/M33) | fixed-point filters, CMSIS-DSP |
GCC/Clang built-ins: __builtin_clz, __builtin_ctz, __builtin_popcount, __builtin_bswap32, __builtin_expect (branch hints), __builtin_unreachable, and __atomic_*/__sync_* for atomics. Arm's ACLE (<arm_acle.h>) and Neon/Helium intrinsics (<arm_neon.h>, <arm_mve.h>) give SIMD operations on Cortex-A and Cortex-M55/M85.
Why intrinsics beat inline assembly
- The compiler knows exactly what the instruction reads, writes and clobbers, so it allocates registers and schedules code around it.
- Portable across compilers that implement CMSIS/ACLE (GCC, Clang, Arm Compiler, IAR).
- Type-checked, readable, and they do not block optimisation of the surrounding function.
When you still need inline assembly: special register access with no wrapper, exact instruction sequences (context switching, fault handler entry that must read the stack pointer before the compiler touches it), and timing-critical code. Use the extended GCC syntax with correct constraints and clobbers:
static inline uint32_t get_ipsr(void)
{
uint32_t r;
__asm volatile ("mrs %0, ipsr" : "=r" (r)); /* output in any register */
return r;
}
Write whole functions that need full control (PendSV context switch) as __attribute__((naked)) or in a separate .S file.