Non-Confidential |
![]() |
ARM DUI0472J | ||
|
||||
Home > Compiler-specific Features > __ldrex intrinsic |
The __ldrex
intrinsic lets you load data from memory in your C or C++ code using an LDREX[size]
instruction.
size
in LDREX[size]
is B
for byte loads or H
for halfword loads. If no size
is specified, word loads are performed.
LDREX
instruction generated for the __ldrex
intrinsic and the
STREX
instruction generated for the __strex
intrinsic.
Because memory accesses can clear the exclusive monitor, code using the
__ldrex
and __strex
intrinsics can have unexpected
behavior. Where LDREX
and STREX
instructions are needed, ARM recommends using embedded assembly.
unsigned int __ldrex(volatile void *ptr)
Where:
ptr
points to the address of the data to be loaded from memory. To specify the type of the data to be loaded, cast the parameter to an appropriate pointer type.
Table 10-8 Access widths that the __ldrex intrinsic supports
Instruction | Size of data loaded | Pointer type |
---|---|---|
LDREXB
|
byte |
unsigned char *
|
LDREXB
|
byte |
signed char *
|
LDREXH
|
halfword |
unsigned short *
|
LDREXH
|
halfword |
signed short *
|
LDREX
|
word |
int *
|
The __ldrex
intrinsic returns the data loaded from the memory address
pointed to by ptr
.
The compiler does not recognize the __ldrex
intrinsic when compiling for a
target that does not support the LDREX
instruction. The compiler generates
either a warning or an error in this case, depending on the source language:
Warning: #223-D: function "__ldrex" declared implicitly
.Error: #20: identifier "__ldrex" is undefined
.The __ldrex
intrinsic does not support access to doubleword data. The
compiler generates an error if you specify an access width that is not supported.
int foo(void) { int loc = 0xff; return __ldrex((volatile char *)loc); }
Compiling this code with the command-line option --cpu=6k
produces
||foo|| PROC MOV r0,#0xff LDREXB r0,[r0] BX lr ENDP