日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

__asm__ __volatile__(: : :memory);

發布時間:2023/12/31 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 __asm__ __volatile__(: : :memory); 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考:http://stackoverflow.com/questions/14950614/working-of-asm-volatile-memory

?

asmvolatile("":::"memory");

creates a compiler level memory barrier forcing optimizer to not re-order memory accesses across the barrier.

For example, if you need to access some address in a specific order (probably because that memory area is actually backed by a different device rather than a memory) you need to be able tell this to the compiler otherwise it may just optimize your steps for the sake of efficiency.

Assume in this scenario you must increment a value in address, read something and increment another value in an adjacent address.

int c(int *d, int *e) {int r;d[0] += 1;r = e[0];d[1] += 1;return r; }

Problem is compiler (gcc in this case) can rearrange your memory access to get better performance if you ask for it (-O). Probably leading to a sequence of instructions like below:

00000000 <c>:0: 4603 mov r3, r02: c805 ldmia r0, {r0, r2}4: 3001 adds r0, #16: 3201 adds r2, #18: 6018 str r0, [r3, #0]a: 6808 ldr r0, [r1, #0]c: 605a str r2, [r3, #4]e: 4770 bx lr

Above values for d[0] and d[1] are loaded at the same time. Lets assume this is something you want to avoid then you need to tell compiler not to reorder memory accesses and that is to use asm volatile("" ::: "memory").

int c(int *d, int *e) {int r;d[0] += 1;r = e[0];asm volatile("" ::: "memory");d[1] += 1;return r; }

So you'll get your instruction sequence as you want it to be:

00000000 <c>:0: 6802 ldr r2, [r0, #0]2: 4603 mov r3, r04: 3201 adds r2, #16: 6002 str r2, [r0, #0]8: 6808 ldr r0, [r1, #0]a: 685a ldr r2, [r3, #4]c: 3201 adds r2, #1e: 605a str r2, [r3, #4]10: 4770 bx lr12: bf00 nop

It should be noted that this is only compile time memory barrier to avoid compiler to reorder memory accesses, as it puts no extra hardware level instructions to flush memories or wait for load or stores to be completed. CPUs can still reorder memory accesses if they have the architectural capabilities.

?

?

?

This sequence is a compiler memory access scheduling barrier, as noted in the article referenced by Udo. This one is GCC specific - other compilers have other ways of describing them, some of them with more explicit (and less esoteric) statements.

__asm__ is a gcc extension of permitting assembly language statements to be entered nested within your C code - used here for its property of being able to specify side effects that prevent the compiler from performing certain types of optimisations (which in this case might end up generating incorrect code).

__volatile__ is required to ensure that the asm statement itself is not reordered with any other volatile accesses any (a guarantee in the C language).

memory is an instruction to GCC that (sort of) says that the inline asm sequence has side effects on global memory, and hence not just effects on local variables need to be taken into account.

?

總結

以上是生活随笔為你收集整理的__asm__ __volatile__(: : :memory);的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。