next up previous contents index
Next: 8.1.13 Peephole Optimizer Up: 8.1 Optimizations Previous: 8.1.11 Highest Order Bit   Contents   Index


8.1.12 Higher Order Byte / Higher Order Word

It is also frequently required to obtain a higher order byte or word of a larger integral type (long, int or short types). SDCC recognizes the following expressions to yield the higher order byte or word and generates optimized code for it, e.g.:

unsigned int gint;  
unsigned long int glong;  
 
foo () {  
  unsigned char hob1, hob2;  
  unsigned int how1, how2;  
  ...  
  hob1 = (gint >> 8) & 0xFF;  
  hob2 = glong >> 24;  
  how1 = (glong >> 16) & 0xFFFF;  
  how2 = glong >> 8;  
  ..  
}
will generate the following code:

                          91 ;  hob.c 15  
0037 85*01*06             92         mov   _foo_hob1_1_1,(_gint + 1)  
                          93 ;  hob.c 16  
003A 85*05*07             94         mov   _foo_hob2_1_1,(_glong + 3)  
                          95 ;  hob.c 17  
003D 85*04*08             96         mov   _foo_how1_1_1,(_glong + 2)  
0040 85*05*09             97         mov   (_foo_how1_1_1 + 1),(_glong + 3)  
0043 85*03*0A             98         mov   _foo_how2_1_1,(_glong + 1)  
0046 85*04*0B             99         mov   (_foo_how2_1_1 + 1),(_glong + 2)
Again, variations of these cases may not be recognized. They are standard C expressions, so I heartily recommend these be the only way to get the higher order byte/word, (it is portable). Of course it will be recognized even if it is embedded in other expressions, e.g.:

xyz = gint + ((gint >> 8) & 0xFF);
will still be recognized.


next up previous contents index
Next: 8.1.13 Peephole Optimizer Up: 8.1 Optimizations Previous: 8.1.11 Highest Order Bit   Contents   Index
2008-12-05