9.139 __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.

Note

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

Syntax

unsigned int __swp(unsigned int val, volatile void *ptr)
Where:
val
Is the data value to be written to memory.
ptr
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 9-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.

Example

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
    ENDP
Related information
SWP and SWPB
Non-ConfidentialPDF file icon PDF versionARM DUI0375F
Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved.