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 support@ccsinfo.com

Printf code size in V4.039

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



Joined: 01 Jun 2007
Posts: 7

View user's profile Send private message

Printf code size in V4.039
PostPosted: Fri Jun 01, 2007 1:35 pm     Reply with quote

Just starting to use the latest compiler Version 4.039 after my faithfull 3.249. I'm trying some existing 16F877 projects on this new version. What I notice is that the code size is something 5-10% larger than before. Why, shouldn't the 4.xxx be more optimized in code generation?

Also, it seems that the largerst increase is in the printf function calls. I see 4X longer code at simple statements like:

printf("\f VA 4000\n Version 1.5");

What is the problem? Why does the 3.xxx behave so much better on printf? Is this a bug or something else I haven't figured out yet? Am I the only one on this problem?

Any insight appreciated...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 01, 2007 2:02 pm     Reply with quote

I made a test program and looked at the .LST files for vs. 3.249 and
vs. 4.039. In the the later version, the compiler is doing some kind
of totally unnecessary optimization, where it breaks up the string
into three pieces and creates separate "fetch and send" loops for each
piece. I think it's done entirely to optimize sending the three 0's in
the middle of the string. But the net result is that the size of the
generated code is larger. It's no optimization at all. It's the opposite.

I suggest that you email CCS support and ask them how to turn this off.
I tried playing around with different #opt levels and with #device CCS3
and they didn't have any effect. There are so many different controls
now that it's tough to know what options will affect what behavior.
Code:

#include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT 
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//===================================
void main()
{

printf("\f VA 4000\n Version 1.5");

while(1);
}
picport



Joined: 01 Jun 2007
Posts: 7

View user's profile Send private message

PostPosted: Sat Jun 02, 2007 12:13 am     Reply with quote

That's precisely the same conclusion as I got. The printf is optimized for some strange reason when there are 3 or more repeated letters. In the code below the two printf statements produce totally different style and amount of code (using CCS PCM C Compiler, Version 4.039).

Code:
#include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT 
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//===================================
void main()
{

printf("\f VA 4000\n Version 1.5");
printf("\f VA 4012\n Version 1.5");

while(1);
}


The 1st printf produces 35 lines of code and the 2nd produces only 13 lines (the way it should be). The const lookup table part (for the actual strings) for the 1st printf is 24 lines opposed to 27 lines for the 2nd one. The reason for the difference is that the 3 x "0"s on the 1st printf get treated separately and are not part of the const string. The net difference is that the new printf optimization produces 19 lines more code and no other real benefit.
Ttelmah
Guest







PostPosted: Sat Jun 02, 2007 4:29 am     Reply with quote

The 'optimisation', wold make 'perfect sense', if it turned 'on' for strings of more than 23 identical characters (it 'costs' 22 bytes in the code memory, and saves 1-number of identical characters, in the constant string), but currently is totally 'silly'....
You can avoid it, using named constant strings though:
Code:

#include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

const char prompt[]="\f VA 4000\n Version 1.5";

//===================================
void main()
{

printf(prompt);
printf("\f VA 4012\n Version 1.5");

while(1);
}


Best Wishes
picport



Joined: 01 Jun 2007
Posts: 7

View user's profile Send private message

PostPosted: Sat Jun 02, 2007 8:49 am     Reply with quote

Yes, I could code the constant strings that way. The only annoyance is that it makes the code hard to read, since the const strings would be at the top of the file and the respective printf statements somewhere in the middle of the code. I have a lot of these printf statements implementing a menu structure.

Your analysis on the 'cost' of optimization is quite accurate. I wonder though if the compiler does these kinds of cost evaluations? At least in the Version 3.249 all the printf statements were treated equal, irrespective of the number of repeated same chars.

Hope we see a different optimization strategy in a future compiler version.
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