Working of __asm__ __volatile__ (“” : : : “memory”)
来源:互联网 发布:思科路由器端口nat 编辑:程序博客网 时间:2024/05/24 06:47
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, r0 2: c805 ldmia r0, {r0, r2} 4: 3001 adds r0, #1 6: 3201 adds r2, #1 8: 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, r0 4: 3201 adds r2, #1 6: 6002 str r2, [r0, #0] 8: 6808 ldr r0, [r1, #0] a: 685a ldr r2, [r3, #4] c: 3201 adds r2, #1 e: 605a str r2, [r3, #4] 10: 4770 bx lr 12: 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 and memory addresses are on normal
type instead of strongly ordered
or device
(ref).
- Working of __asm__ __volatile__ (“” : : : “memory”)
- __asm__ __volatile__ ("" : : : "memory")
- __asm__ __volatile__("": : :"memory")
- __asm__ __volatile__("": : :"memory")
- __asm__ __volatile__("": : :"memory")
- __asm__ __volatile__("": : :"memory")含义
- __asm__ __volatile__("": : :"memory")
- __asm__ __volatile__("": : :"memory")
- __asm__ __volatile__("": : :"memory")
- __asm__ __volatile__("": : :"memory")
- __asm__ __volatile__("": : :"memory")
- 关于__asm__ __volatile__ (“” : : : “memory”)
- __asm__ __volatile__("": : :"memory")
- __asm__ __volatile__("": : :"memory")
- __asm__ __volatile__("": : :"memory")含义
- __asm__ __volatile__("": : :"memory")
- 内存屏障(__asm__ __volatile__("": : :"memory"))
- __asm__ __volatile__("":::"memory"),内存屏障(memory barrier)
- [问题解决]unhandled exception——VC6.0
- Android.mk 文件语法详解
- SpringMVC学习笔记上
- 缓存一致性和跨服务器查询的数据异构解决方案canal
- Android webview加载iframe标签的HTML
- Working of __asm__ __volatile__ (“” : : : “memory”)
- 自己总结的有关表单的一些基础知识....表单的类型和属性......
- 设置Github,使用https:// 来替换 git://
- Appium入门实例(Java)
- Kettle 中常用 控件一览
- 安装AX2012R3流程
- String、StringBuffer和StringBuilder的区别
- Oracle 12c数据库优化器统计信息收集的最佳实践(二)
- 工作中棋牌麻将的总结一些术语(2)