next up previous contents index
Next: 8.1.12 Higher Order Byte Up: 8.1 Optimizations Previous: 8.1.10 Nibble and Byte   Contents   Index


8.1.11 Highest Order Bit / Any Order Bit

It is frequently required to obtain the highest order bit of an integral type (long, int, short or char types). Also obtaining any other order bit is not uncommon. SDCC recognizes the following expressions to yield the highest order bit and generates optimized code for it, e.g.:

unsigned int gint;  
 
foo () {  
  unsigned char hob1, aob1;  
  bit hob2, hob3, aob2, aob3;  
  ...  
  hob1 = (gint >> 15) & 1;  
  hob2 = (gint >> 15) & 1;  
  hob3 = gint & 0x8000;  
  aob1 = (gint >> 9) & 1;  
  aob2 = (gint >> 8) & 1;  
  aob3 = gint & 0x0800;  
  ..  
}
will generate the following code:

                          61 ;  hob.c 7  
000A E5*01                62         mov   a,(_gint + 1)  
000C 23                   63         rl    a  
000D 54 01                64         anl   a,#0x01  
000F F5*02                65         mov   _foo_hob1_1_1,a  
                          66 ;  hob.c 8  
0011 E5*01                67         mov   a,(_gint + 1)  
0013 33                   68         rlc   a  
0014 92*00                69         mov   _foo_hob2_1_1,c  
                          66 ;  hob.c 9  
0016 E5*01                67         mov   a,(_gint + 1)  
0018 33                   68         rlc   a  
0019 92*01                69         mov   _foo_hob3_1_1,c  
                          70 ;  hob.c 10  
001B E5*01                71         mov   a,(_gint + 1)  
001D 03                   72         rr    a  
001E 54 01                73         anl   a,#0x01  
0020 F5*03                74         mov   _foo_aob1_1_1,a  
                          75 ;  hob.c 11  
0022 E5*01                76         mov   a,(_gint + 1)  
0024 13                   77         rrc   a  
0025 92*02                78         mov   _foo_aob2_1_1,c  
                          79 ;  hob.c 12  
0027 E5*01                80         mov   a,(_gint + 1)  
0029 A2 E3                81         mov   c,acc[3]  
002B 92*03                82         mov   _foo_aob3_1_1,c
Other variations of these cases however will not be recognized. They are standard C expressions, so I heartily recommend these be the only way to get the highest order bit, (it is portable). Of course it will be recognized even if it is embedded in other expressions, e.g.:

xyz = gint + ((gint >> 15) & 1);
will still be recognized.


next up previous contents index
Next: 8.1.12 Higher Order Byte Up: 8.1 Optimizations Previous: 8.1.10 Nibble and Byte   Contents   Index
2008-12-05