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

short-circuit evaluation

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



Joined: 10 May 2011
Posts: 15

View user's profile Send private message

short-circuit evaluation
PostPosted: Tue May 10, 2011 1:12 pm     Reply with quote

Hello!

I am new to CCS compiler and am struggling with the attempt to disable the short-circuit evaluation (some call it short boolean evaluation) in the CCS compiler. So far my attempts to do so gave no result. I tried the #OPT 0 directive but it doesn't help.
Does anyone know how to disable it?

Thank you in advance for all your help.

Cheers!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 10, 2011 1:17 pm     Reply with quote

Post a short, compilable program that shows what you want to do.
We should be able to copy-and-paste the program into MPLAB, without
modifications or additions, and have it compile with no errors.
PLEASE try to limit the whole test program to 10-15 lines, including any
pre-processor statements. Don't post any "CCS Wizard" code in main().

Indicate the lines of code that you're interested in discussing.

Tell us the result that you are currently getting, and tell us the
result that you want to see.

And finally, post your compiler version.
Nepersky



Joined: 10 May 2011
Posts: 15

View user's profile Send private message

PostPosted: Tue May 10, 2011 1:33 pm     Reply with quote

Before writing such a program I will try to explain another way and if things will still be unclear, I will take the time and write a program.

Let's say I have this line in my code:

if ((count==1)||(CFGPLLEN==1)) count++;

The disassembly listing looks like this (ox5 is the 'count' variable):
193: if ((count==1)||(CFGPLLEN==1)) count++;
0080 2C05 DECFSZ 0x5, W, ACCESS
0082 D001 BRA 0x86
0084 D001 BRA 0x88
0086 B800 BTFSC 0, 0x4, ACCESS
0088 2A05 INCF 0x5, F, ACCESS

The program will first evaluate count==1. If this turns out to be true, the second evaluation (CFGPLLEN==1) will never be carried out, which makes the code undeterministic, as far as time is concerned. The same line of code will have two different execution times. If count==1 the time will be shorter as if only CFGPLLEN equals one.

I would like the compiler to compile the code in such a way, that it would take the same time execute the if statement, regardless of the values of count and CFGPLLEN.

I hope this explanation helps.
As for the compiler version:
PCH 4.121
everything else 4.112


Cheers!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 10, 2011 1:45 pm     Reply with quote

There's no time required. It's just 4 lines for the #include, #fuses, and
#use delay() and a #define for the constant. Then a main(), and the
variable declaration(s) and the line in question, and you're done.
Compile it to see that it has no errors and post it.
Nepersky



Joined: 10 May 2011
Posts: 15

View user's profile Send private message

PostPosted: Tue May 10, 2011 1:51 pm     Reply with quote

Code:

#include "18F24J11.h"
#FUSES INTRC_IO
#FUSES NOXINST

#BYTE CONFIG1H = 0x300001   
 #BIT CPDIV0 = CONFIG1H.0
 #BIT CPDIV1 = CONFIG1H.1
 #BIT CP0 = CONFIG1H.2

int count;
void main()
{
   if ((count==1)||(CPDIV1==1)) count++;
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 10, 2011 5:23 pm     Reply with quote

If you do a bitwise OR operation, then it will always evaluate both sides.
However, the code is longer. But within each evaluation the execution
time will be slightly different, depending upon whether 'count' is 1 or 0.
To get exactly the same execution times, you would have to write it in
in ASM embedded in the C, by using #asm and #endasm, and you would
have to hand-tune it.

If you want to see examples of hand-tuned ASM code, here is the routine
for the pbpro COUNT command for an 18F PIC:
http://www.ccsinfo.com/forum/viewtopic.php?t=41877&start=8

Here's another example, for the pbpro PULSIN command:
http://www.ccsinfo.com/forum/viewtopic.php?t=42353

But I have a question. I don't see anything about your sample code that
requires equal execution time. It doesn't look time-critical to me.

--------------
Here is a test program. Look at the .LST file for this. You can see how the
bitwise OR doesn't do short-circuiting.
Code:

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

//==================================
void main()
{
int8 count, value, result;

count = 0;
value = 0;

if( (count==1) | (value==1) )  // Bitwise OR doesn't short-circuit
   result = 1;
else
   result = 0;


if( (count==1) || (value==1) )  // Logical OR does
   result = 1;
else
   result = 0;
 
while(1);
}


Also, the #byte directive is intended for RAM addresses, not ROM. The
code below won't work. If you make a test program and look at the .LST
file, you can see that it trys to read RAM address 0x01. That's not what
you want.
Quote:

#BYTE CONFIG1H = 0x300001
#BIT CPDIV0 = CONFIG1H.0
#BIT CPDIV1 = CONFIG1H.1
#BIT CP0 = CONFIG1H.2
Nepersky



Joined: 10 May 2011
Posts: 15

View user's profile Send private message

PostPosted: Wed May 11, 2011 1:14 am     Reply with quote

Thank you for the reply. I was quite impatient yesterday when I wrote the post so after there was no reply for about 15min I did exactly as you suggested, wrote it in ASM. I added an extra variable and the time of execution is always exactly the same. After that I went straight to bed, as it was already in the middle of the night over here. I apologise for not updating my post. If I did, you wouldn't have to write yours. I will make sure this doesn't happen in the future.

But the difference in bitwise and logical or you pointed out might come in handy one day, so thank you again for your reply.

As for the #BYTE CONFIG1H 0x300001 :
It was obviously a bad example. In the application I am building I will be using a byte from RAM instead anyway.

Cheers!
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Thu May 12, 2011 10:05 pm     Reply with quote

Nepersky wrote:


As for the #BYTE CONFIG1H 0x300001 :
It was obviously a bad example. In the application I am building I will be using a byte from RAM instead anyway.


I was going to ask if you expected that to work since you can't just write out there... and I IIRC, if you did write out there, the effects aren't realtime.

I think the CONFIG's are copied to out there at boot and then used once. (would have to revisit)

Anyway... cheers,

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
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