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

CCS has impressive bit handling.

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



Joined: 17 Jun 2019
Posts: 540
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

CCS has impressive bit handling.
PostPosted: Tue Feb 25, 2020 1:35 pm     Reply with quote

I read about int1, but never used it. The compiler will use one byte for up to 8 of those:

Code:
int1 bit1;
int1 bit2;
int1 bit3;
int1 bit4;
int1 bit5;
int1 bit6;
int1 bit7;

   bit0 = 1;
   bit1 = 0;
   bit2 = 1;
   bit3 = 0;
   bit4 = 1;
   bit5 = 0;
   bit6 = 1;
   bit7 = 0;


Those turn into nice atomic assembly calls:

Code:
....................    bit0 = 1;
00228:  BSET.B  800.0
....................    bit1 = 0;
0022A:  BCLR.B  800.1
....................    bit2 = 1;
0022C:  BSET.B  800.2
....................    bit3 = 0;
0022E:  BCLR.B  800.3
....................    bit4 = 1;
00230:  BSET.B  800.4
....................    bit5 = 0;
00232:  BCLR.B  800.5
....................    bit6 = 1;
00234:  BSET.B  800.6
....................    bit7 = 0;
00236:  BCLR.B  800.7


So I wondered how different it was to doing it manually.

Just SETTING a bit like this is larger:

Code:
....................    byte = (1 << 4);
00238:  MOV.B   #10,W0L
0023A:  MOV.B   W0L,801


But ORing a bit generates the same nice code without needing to use non-standard int1:

Code:
....................    byte = byte | (1 << 4);
0023C:  BSET.B  801.4


Clearing a bit also generates one atomic instruction:

Code:
....................    byte = byte & ~(1 << 4);
0023E:  BCLR.B  801.4


Kudos to CCS. It's nice seeing int1 available, but nicer seeing the compiler is smart enough to do bit set/clear efficiently if you are manually doing it.

I created Arduino-compatible bit macros and worried they might generate bulky code, but I never checked until now:

Code:
....................    byte = bitSet(byte, 4);
0023C:  BSET.B  801.4

....................    byte = bitClear(byte, 4);
00242:  BCLR.B  801.4

_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
temtronic



Joined: 01 Jul 2010
Posts: 9204
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 25, 2020 5:19 pm     Reply with quote

Really has nothing to do with CCS, thank MICROCHIP ! They're the ones that embedded bit instructions in the PIC !!
allenhuffman



Joined: 17 Jun 2019
Posts: 540
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Feb 26, 2020 1:32 pm     Reply with quote

temtronic wrote:
Really has nothing to do with CCS, thank MICROCHIP ! They're the ones that embedded bit instructions in the PIC !!


I am glad to give credit to CCS for actually taking advantage of it Smile I've seem some terrible code generation over the years, so I'm always pleased when I see a compiler doing something smart.

I noticed that bit set/clear when looking at the I2C assembly. Nice.
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
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