View previous topic :: View next topic |
Author |
Message |
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Optimization of wait for bit to be set |
Posted: Thu Jul 02, 2015 9:17 am |
|
|
I have written before about how CCS optimizes better if the code is all on one line. Here is another similar observation (using PCH C Compiler, Version 4.128):
Code: |
while(SSP1STAT.BF==0) {}
0292: BTFSC FC7.0
0294: BRA 0298
0296: BRA 0292 (3 instructions, 4 cycles per loop. Nice.)
do {} while(SSP1STAT.BF==0);
0292: BTFSS FC7.0
0294: BRA 0292 (2 instructions, 3 cycles per loop. Nicer.)
|
_________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1346
|
|
Posted: Thu Jul 02, 2015 9:56 am |
|
|
I think it has more to do with what information you are giving the parser. In the first example, you are starting a while loop and the compiler parser reaches the '{', it doesn't yet know your going to do nothing, so it goes into the portion that will process the scoping block (and has to start generated tokens for the while) at this point.
In the second example you have already provided it an empty set of brackets so it can optimize it better (it already know you are doing nothing).
If you try a third way:
Code: |
while(0==SSP1STAT.BF);
|
You'll see that it correctly optimizes is well. The reason for that is the ';'. When it gets there, it doesn't have a scope block, so it knows the while is done (and it doesn't have to change into processing a scoping block).
It's not to say it is impossible to optimize the first method better, but it is probably related to how their parser is designed. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Thu Jul 02, 2015 11:18 am |
|
|
The key is the difference between a 'while' which tests at the start of the loop, and a do..while, which tests at the end.
In the second case a single jump 'back' can be used. In the first there has to be a jump 'past' for the failed condition. |
|
|
|