如何从优化了忙等循环prevent GCC? [英] How to prevent GCC from optimizing out a busy wait loop?

查看:96
本文介绍了如何从优化了忙等循环prevent GCC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写爱特梅尔AVR微控制器是C code固件。我将使用GCC编译它。另外,我想启用编译器优化( -Os -O2 ),因为我看不出有什么理由不启用他们,他们可能会产生更好的装配方式比手动装配写速度更快。

I want to write a C code firmware for Atmel AVR microcontrollers. I will compile it using GCC. Also, I want to enable compiler optimizations (-Os or -O2), as I see no reason to not enable them, and they will probably generate a better assembly way faster than writing assembly manually.

不过,我想一小块code的未优化。我想通过一段时间的延迟函数的执行,因此我想写一个什么都不做只是循环浪费一些时间。没有必要precise,只是等待一段时间。

But I want a small piece of code not optimized. I want to delay the execution of a function by some time, and thus I wanted to write a do-nothing loop just to waste some time. No need to be precise, just wait some time.

/* How to NOT optimize this, while optimizing other code? */
unsigned char i, j;
j = 0;
while(--j) {
    i = 0;
    while(--i);
}

由于AVR内存访问是慢了很多,我想 I Ĵ被关在CPU寄存器

更新:我刚刚发现 UTIL / delay.h 和<一个href=\"http://www.nongnu.org/avr-libc/user-manual/group__util__delay__basic.html\">util/delay_basic.h从 AVR libc库。虽然大多数时候,它可能是一个更好的主意,使用这些功能,这个问题仍然有效,有趣。

Update: I just found util/delay.h and util/delay_basic.h from AVR Libc. Although most times it might be a better idea to use those functions, this question remains valid and interesting.

推荐答案

我从如下<一个链接后,开发了这个答案href=\"http://stackoverflow.com/questions/7083482/how-to-$p$pvent-compiler-optimization-on-a-small-piece-of-$c$c/7083880#7083880\">dmckee's回答,但它需要一个不同的方法比他/她的回答。

I developed this answer after following a link from dmckee's answer, but it takes a different approach than his/her answer.

功能属性从GCC 文档中提到:

noinline始终
  此功能属性prevents被认为是内联函数。如果函数没有副作用,还有比内联,导致函数调用被优化掉其他优化,虽然函数调用现场。为了使被优化掉,把这样的电话 ASM();

这给了我一个有趣的想法......而不是在内环添加 NOP 指令的,我想在里面添加一个空的组装code,像这样的:

This gave me an interesting idea... Instead of adding a nop instruction at the inner loop, I tried adding an empty assembly code in there, like this:

unsigned char i, j;
j = 0;
while(--j) {
    i = 0;
    while(--i)
        asm("");
}

和它的工作!这个循环没有优化出,并插入没有多余的 NOP 的说明。

And it worked! That loop has not been optimized-out, and no extra nop instructions were inserted.

更重要的是,如果你使用挥发性,GCC将存储在RAM中的变量,并添加了一堆 LDD 的和 STD 将它们复制到临时寄存器。这种做法,在另一方面,不使用挥发性并生成没有这样的开销。

What's more, if you use volatile, gcc will store those variables in RAM and add a bunch of ldd and std to copy them to temporary registers. This approach, on the other hand, doesn't use volatile and generates no such overhead.

更新:的如果你使用编译code -ansi -std ,则必须更换 __ ASM __ ASM 关键字>,为的described~~V在GCC文档

Update: If you are compiling code using -ansi or -std, you must replace the asm keyword with __asm__, as described in GCC documentation.

另外,你也可以使用 __ asm__ __volatile __()如果你的的 汇编语句必须执行,我们把它(即不得搬出一个循环为优化)

In addition, you can also use __asm__ __volatile__("") if your assembly statement must execute where we put it, (i.e. must not be moved out of a loop as an optimization).

这篇关于如何从优化了忙等循环prevent GCC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆