c - How to prevent GCC from optimizing out a busy wait loop? -
I want to write C code firmware for Atmel AVR microcontrollers. I will compile it through GCC. Also, I want to enable compiler optimization ( But I do not want to customize a small piece of code. I have been delaying execution of a function for some time, and in this way I wanted to write a do-not-loop for a while. There is no need to be accurate, just wait for some time. Since memory access to AVR is very slow, so I want the UPDATE: I could get a better idea to use those functions, however, this question became valid and interesting even though I got more from it. The document mentions the GCC: I got an interesting idea from this ... And it worked! That loop has not been optimized, and no additional What's more, if you use Update: If you are compiling the code by using In addition, you can also use -Os or
-O2 ), because I do not see any reason not to enable them, and they Possibly a better assembly method will generate faster than manually writing the assembly
/ * How to optimize other code while not optimizing? * / Unsigned char i, j; j = 0; While (-J) {i = 0; while I); }
i and
j to be placed in the CPU registers.
noinline This function attributes prevent a function from being considered for inlining. If there are no side-effects of the function, there are other optimizations other than inlineing, so that the function calls can be customized, although the function call is live. In order to keep away from optimizing such calls,
asm ("");
NOP Instructions on the internal loop, I tried to add empty assembly code there, Like:
Unsigned four I, J; j = 0; While (-J) {i = 0; While (- I) ASM (""); }
nop instructions have been inserted.
instability , then the GCC will store those variables in RAM and
ldd and
std Add a group of to copy them to temporary registers. On the other hand, this approach does not use
unstable and does not produce such an upper part.
-ansi or
-std , you With the
asm keyword, replace
__ asm __ , such as.
__ asm__ __volatile __ ("") .
Comments
Post a Comment