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

Optimization High/Low byte

 
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 High/Low byte
PostPosted: Fri Feb 25, 2011 11:59 am     Reply with quote

In the listing below, I am trying to get the most optimum 16F1824 code for the following function:
Code:
.................... unsigned int16 GetTimer1(void)
.................... {
....................    unsigned int k;
....................    unsigned int16 retGetTimer1;
.................... #byte gt_low=retGetTimer1
.................... #byte gt_high=retGetTimer1+1
.................... 
....................    k = TMR1H;
*
00BB:  MOVF   17,W
00BC:  MOVWF  37
....................    gt_low = TMR1L;
00BD:  MOVF   16,W
00BE:  MOVWF  38
....................    if(k != (gt_high=TMR1H))      //..high byte changed..
00BF:  MOVF   17,W
00C0:  MOVWF  39
00C1:  SUBWF  37,W
00C2:  BTFSC  03.2
00C3:  GOTO   0C6
....................       gt_low = TMR1L;   //..then re-read low byte
00C4:  MOVF   16,W
00C5:  MOVWF  38
....................    return retGetTimer1;
00C6:  MOVF   38,W
00C7:  MOVWF  78
00C8:  MOVF   39,W
00C9:  MOVWF  79

As you can see this is very optimum. But why was it necessary for me to resort to the #byte directives to get it? I was hoping to get the same sort of code from the following:
Code:
.................... unsigned int16 GetTimer1(void)
.................... {
....................    unsigned int8 k,m,n;
....................    k = TMR1H;
*
00BB:  MOVF   17,W
00BC:  MOVWF  37
....................    m = TMR1L;
00BD:  MOVF   16,W
00BE:  MOVWF  38
....................    //n = TMR1H;
....................    if(k != (n=TMR1H))      //..high byte changed..
00BF:  MOVF   17,W
00C0:  MOVWF  39
00C1:  SUBWF  37,W
00C2:  BTFSC  03.2
00C3:  GOTO   0C6
....................       m = TMR1L;   //..then re-read low byte
00C4:  MOVF   16,W
00C5:  MOVWF  38
....................    return ((unsigned int16)n<<8) + m;
00C6:  CLRF   3B
00C7:  MOVF   39,W
00C8:  MOVWF  3A
00C9:  MOVWF  3B
00CA:  CLRF   3A
00CB:  MOVF   38,W
00CC:  ADDWF  3A,W
00CD:  MOVWF  78
00CE:  MOVLW  00
00CF:  ADDWFC 3B,W
00D0:  MOVWF  7A
00D1:  MOVF   7A,W
00D2:  MOVWF  79

As you can see, it is nowhere near as good as the first implementation. Is there some way I can coax the optimization of the first listing from an implementation that did not resort to such compiler-dependent constructs as #byte and and assumed endian order? (My build options are set for maximum optimization.)
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 25, 2011 1:07 pm     Reply with quote

You can use make16() to do it efficiently:
Code:

.................... return(make16(n,m));
000E:  MOVF   n,W
000F:  MOVWF  @7A
0010:  MOVF   m,W
0011:  MOVWF  @78
0012:  MOVF   n,W
0013:  MOVWF  @79

Code:

#include <16F1824.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT //, NOLVP
#use delay(clock=4000000)

#byte TMR1H = 0x17
#byte TMR1L = 0x16

unsigned int16 GetTimer1(void)
{
unsigned int8 k,m,n;
k = TMR1H;
m = TMR1L;
//n = TMR1H;
if(k != (n=TMR1H))      //..high byte changed..
   m = TMR1L;           //..then re-read low byte

return(make16(n,m));
}

//==========================================
void main()
{
int16 result;

result = GetTimer1();

while(1);
}
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

PostPosted: Fri Feb 25, 2011 1:45 pm     Reply with quote

PCM programmer wrote:
You can use make16() to do it efficiently:

Thanks. That is neater than my solution.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
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