CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Optimization of wait for bit to be set

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Optimization of wait for bit to be set
PostPosted: Thu Jul 02, 2015 9:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jul 02, 2015 9:56 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jul 02, 2015 11:18 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group