Jump to content

Recommended Posts

Hi,

 

To try and be consistent I have used the set_bit, clear_bit, toggle_bit and test_bit macros fairly extensively in most of my BoostC projects and apart from early versions which didn't work correctly above 8 bits I have had no problems.

 

While checking the code generated for something else I noticed that the code was bloated when using test_bit compared to the normal if(a.n) form when being used on 16 bit fields.

 

As an example the following shows the generated code for different ways of bit testing with 8 and 16 bit fields, although most would not be used in practice. I have commented the tests which generate more code than I would have expected.

 

Using SourceBoostC V6.91

 

	unsigned char a;
unsigned short b;

while( 1 )
003A		label1
009A  283A	  GOTO	label1

{
	// Testing for TRUE on 8-bit bitfield
	// ==================================

	if(a.2)	{nop();}
003A  1D23	  BTFSS main_1_a,2
003B  283D	  GOTO	label2
003C  0000	  NOP
003D		label2


	if(a.2 != 0) {nop();}
003D  1D23	  BTFSS main_1_a,2
003E  2840	  GOTO	label3
003F  0000	  NOP
0040		label3


	if(a.2 == 1) {nop();}
0040  1D23	  BTFSS main_1_a,2
0041  2843	  GOTO	label4
0042  0000	  NOP
0043		label4


	if(test_bit(a,2)) {nop();}
0043  1D23	  BTFSS main_1_a,2
0044  2846	  GOTO	label5
0045  0000	  NOP
0046		label5


	if(test_bit(a,2) != 0) {nop();}
0046  1D23	  BTFSS main_1_a,2
0047  2849	  GOTO	label6
0048  0000	  NOP
0049		label6


	if(test_bit(a,2) == 1) {nop();}		// Generates Bloated Code
0049  3004	  MOVLW 0x04
004A  0523	  ANDWF main_1_a, W
004B  00A6	  MOVWF CompTempVar552
004C  0326	  DECF CompTempVar552, W
004D  1D03	  BTFSS STATUS,Z
004E  2850	  GOTO	label7
004F  0000	  NOP
0050		label7


	// Testing for FALSE on 8-bit bitfield
	// ===================================

	if(!a.2) {nop();}
0050  1923	  BTFSC main_1_a,2
0051  2853	  GOTO	label8
0052  0000	  NOP
0053		label8


	if(a.2 == 0) {nop();}
0053  1923	  BTFSC main_1_a,2
0054  2856	  GOTO	label9
0055  0000	  NOP
0056		label9


	if(!test_bit(a,2)) {nop();}
0056  1923	  BTFSC main_1_a,2
0057  2859	  GOTO	label10
0058  0000	  NOP
0059		label10


	if(test_bit(a,2) == 0) {nop();}
0059  1923	  BTFSC main_1_a,2
005A  285C	  GOTO	label11
005B  0000	  NOP
005C		label11


	// Testing for TRUE on 16-bit bitfield
	// ===================================

	if(b.2) {nop();}
005C  1D24	  BTFSS main_1_b,2
005D  285F	  GOTO	label12
005E  0000	  NOP
005F		label12


	if(b.2 == 1) {nop();}
005F  1D24	  BTFSS main_1_b,2
0060  2862	  GOTO	label13
0061  0000	  NOP
0062		label13


	if(b.2 != 0) {nop();}
0062  1D24	  BTFSS main_1_b,2
0063  2865	  GOTO	label14
0064  0000	  NOP
0065		label14


	if(test_bit(b,2)) {nop();}
0065  1D24	  BTFSS main_1_b,2
0066  2868	  GOTO	label15
0067  0000	  NOP
0068		label15


	if(test_bit(b,2) != 0) {nop();}		// Generates Bloated Code
0068  3004	  MOVLW 0x04
0069  0524	  ANDWF main_1_b, W
006A  00A7	  MOVWF CompTempVar554
006B  01A8	  CLRF CompTempVar555
006C  08A7	  MOVF CompTempVar554, F
006D  1D03	  BTFSS STATUS,Z
006E  2872	  GOTO	label16
006F  08A8	  MOVF CompTempVar555, F
0070  1903	  BTFSC STATUS,Z
0071  2873	  GOTO	label17
0072		label16
0072  0000	  NOP
0073		label17


	if(test_bit(b,2) == 1) {nop();}		// Generates Bloated Code
0073  3004	  MOVLW 0x04
0074  0524	  ANDWF main_1_b, W
0075  00A9	  MOVWF CompTempVar556
0076  01AA	  CLRF CompTempVar557
0077  3001	  MOVLW 0x01
0078  0629	  XORWF CompTempVar556, W
0079  1903	  BTFSC STATUS,Z
007A  082A	  MOVF CompTempVar557, W
007B  1D03	  BTFSS STATUS,Z
007C  287E	  GOTO	label18
007D  0000	  NOP
007E		label18


	// Testing for FALSE on 16-bit bitfield
	// ====================================

	if(!b.2) {nop();}
007E  1924	  BTFSC main_1_b,2
007F  2881	  GOTO	label19
0080  0000	  NOP
0081		label19


	if(b.2 == 0) {nop();}
0081  1924	  BTFSC main_1_b,2
0082  2884	  GOTO	label20
0083  0000	  NOP
0084		label20


	if(!test_bit(b,2)) {nop();}		// Generates Bloated Code
0084  3004	  MOVLW 0x04
0085  0524	  ANDWF main_1_b, W
0086  00AB	  MOVWF CompTempVar560
0087  01AC	  CLRF CompTempVar561
0088  08AB	  MOVF CompTempVar560, F
0089  1D03	  BTFSS STATUS,Z
008A  288F	  GOTO	label21
008B  08AC	  MOVF CompTempVar561, F
008C  1D03	  BTFSS STATUS,Z
008D  288F	  GOTO	label21
008E  0000	  NOP
008F		label21


	if(test_bit(b,2) == 0) {nop();}		// Generates Bloated Code
008F  3004	  MOVLW 0x04
0090  0524	  ANDWF main_1_b, W
0091  00AD	  MOVWF CompTempVar562
0092  01AE	  CLRF CompTempVar563
0093  08AD	  MOVF CompTempVar562, F
0094  1D03	  BTFSS STATUS,Z
0095  283A	  GOTO	label1
0096  08AE	  MOVF CompTempVar563, F
0097  1D03	  BTFSS STATUS,Z
0098  283A	  GOTO	label1
0099  0000	  NOP

}
}

 

In an ideal world I would expect the test_bit macros to generate the same code as any test using the if(a.n) form.

 

Dave, Pavel, anyone else want to comment?

 

Many thanks

 

davidb

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...