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

Note

The compiler does not guarantee that it will preserve the state of the exclusive monitor. It may generate load and store instructions between the 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.

Syntax

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 *

Return value

The __ldrex intrinsic returns the data loaded from the memory address pointed to by ptr.

Errors

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:

  • In C code: Warning: #223-D: function "__ldrex" declared implicitly.
  • In C++ code: 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.

Examples

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
Related reference
10.118 __ldrexd intrinsic
10.139 __strex intrinsic
10.140 __strexd intrinsic
Related information
LDREX
Non-Confidential PDF file icon PDF version ARM DUI0472J
Copyright © 2010-2013 ARM. All rights reserved.