How to understand this GNU C built-in macro for PowerPC stwbrx

This is basically a swap execution for buffers when passing a message buffer. This expression left me puzzled (due to my ignorance with the built-in build code in c). This is pc power pc command

#define ASMSWAP32(dest_addr,data) __asm__ volatile ("stwbrx %0, 0, %1" : : "r" (data), "r" (dest_addr))
+4
source share
2 answers

#define ASMSWAP32(dest_addr,data)...

This part should be clear.

__asm__ volatile (... : : "r" (data), "r" (dest_addr))

This is the actual built-in assembly:

Two values ​​are passed to the access code; no value is returned from the assembly code (these are colons after the actual assembly code).

("r"). %0 , data, %1 , dest_addr ( ).

volatile , .

, C:

ASMSWAP(&a, b);

... :

# write the address of a to register 5 (for example)
...
# write the value of b to register 6
...
stwbrx 6, 0, 5

, stwbrx b, a.

stwbrx x, 0, y

x , y; "reverse endian" ( "little endian".

:

uint32 a;
ASMSWAP32(&a, 0x12345678);

... a = 0x78563412.

+5

, - , , , .


stwbrx= . x .

GNU C asm, __builtin_bswap32 .

void swapstore_asm(int a, int *p) {
    ASMSWAP32(p, a);
}

void swapstore_c(int a, int *p) {
    *p = __builtin_bswap32(a);
}

gcc4.8.5 -O3 -mregnames, (Godbolt compiler explorer):

swapstore:
    stwbrx %r3, 0, %r4
    blr
swapstore_c:
    stwbrx %r3,0,%r4
    blr

( p[off], off arg), , , :

void swapstore_offset(int a, int *p, int off) {
     = __builtin_bswap32(a);
}

swapstore_offset:
    slwi %r5,%r5,2              # *4 = sizeof(int)
    stwbrx %r3,%r4,%r5          # use an indexed addressing mode, with both registers non-zero
    blr

swapstore_offset_asm:
    slwi %r5,%r5,2
    add %r4,%r4,%r5            # extra instruction forced by using the macro
    stwbrx %r3, 0, %r4
    blr

, asm asus GNU C, asm , . . "" GCC/clang? ASM .


, : "memory" clobber . , asm volatile. , *dest_addr , , *dest_addr insn , , , . (, , .)

"memory" clobber ( volatile), , =m" (*dest_addr), , reg+reg. (IDK PPC , , "=m" .)

, . .

https://gcc.gnu.org/wiki/DontUseInlineAsm

. fooobar.com/questions/tagged/....

+7

Source: https://habr.com/ru/post/1688921/


All Articles