You copied the Doc URL to your clipboard.

__swp intrinsic

The __swp intrinsic lets you use a SWP{size} instruction to swap data between registers and memory from your C or C++ code.

The SWP{size} instruction reads a value into a processor register and writes a value to a memory location as an atomic operation.


The use of SWP and SWPB is deprecated in ARMv6 and above.


unsigned int __swp(unsigned int val, volatile void *ptr)



Is the data value to be written to memory.


Points to the address of the data to be written to in memory. To specify the size of the data to be written, cast the parameter to an appropriate integral type.

Table 10-14 Access widths that the __swp intrinsic supports

Instruction Size of data Pointer type
SWPB byte char *
SWP word int *

Return value

The __swp intrinsic returns the data value in the memory address pointed to by ptr immediately before the SWP instruction overwrites it with val.


int foo(void)
    int loc=0xff;
    return(__swp(0x20, (volatile int *)loc));

Compiling this code produces

||foo|| PROC
    MOV      r1, #0xff
    MOV      r0, #0x20
    SWP      r0, r0, [r1]
    BX       lr

Related information