Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/20/03 16:38
Read: times


 
#46138 - RE: bitfields
Responding to: ???'s previous message
"I wonder how would the c compiler treat the bitfields? would it >> and & or would it do it more efficiently."

My experience suggests that the compiler generates code to shift and mask, much like you would explicitly do it, and does not necessarily do a "better" job. In fact, sometimes you can generate "better" code by hand by taking advantage of your knowledge about shift "distances". For example, here's some portable configurable code that performs part of a CRC calculation. Notice that, for some 8-bitters (e.g., 8051, PIC) setting FCS_USE_SHORT_SHIFT takes one 12-bit shift down to using a specific byte, a nibble swap instruction and a single shift.

/** Configuration: HDLC FCS Calculation Method
 *
 *  Set FCS_METHOD to one of:
 *   - FCS_USE_TABLE is the fastest method, but uses a 512-byte
 *     feedback table that may be too large for some small
 *     systems.
 *   - FCS_USE_SHIFT uses a byte-wise shift and XOR calculation
 *     method, but some shift counts (e.g., 5-bit and 12-bit
 *     shifts) may not be optimally coded in some architectures.
 *   - FCS_USE_SHORT_SHIFT uses the same byte-wise shift and XOR
 *     calculation method as FCS_USE_SHIFT, but has been
 *     optimized for most 8-bitters, especially those with a
 *     nibble swap instruction.
 */
#define FCS_USE_TABLE           1
#define FCS_USE_SHIFT           2
#define FCS_USE_SHORT_SHIFT     3
#define FCS_METHOD              FCS_USE_SHORT_SHIFT

/*  FCS_UPD() implements the FCS calculation selected by FCS_METHOD.
 */
#if   FCS_METHOD == FCS_USE_TABLE
#define FCS_UPD(u16, b) 
    ((u16).w = ((u16).w >> 8) ^ feedback[((u16).w ^ (b)) & 0xFF])
#elif FCS_METHOD == FCS_USE_SHIFT
#define FCS_UPD(u16, b)                                  
do {                                                     
    (u16).w ^= (b);                                      
    (u16).w  = (U8)((u16).w >> 8) | (U16)((u16).w << 8); 
    (u16).w ^= (U16)(((u16).w & 0xFF00) << 4);           
    (u16).w ^= (u16).w >> 12;                            
    (u16).w ^= (U16)(((u16).w & 0xFF00) >> 5);           
} while (0)
#elif FCS_METHOD == FCS_USE_SHORT_SHIFT
#define FCS_UPD(u16, b)                                        
do {                                                           
    U8  u8;                                                    
    U16_LSB(u16) ^= (b);                                       
    u8            = U16_MSB(u16);                              
    U16_MSB(u16)  = U16_LSB(u16);                              
    U16_LSB(u16)  = u8;                                        
    U16_MSB(u16) ^= (U8)(U16_MSB(u16) << 4);                   
    U16_LSB(u16) ^= U16_MSB(u16) >> 4;                         
    u8            = U16_MSB(u16) ^ ((U16_MSB(u16) >> 4) >> 1); 
    U16_LSB(u16) ^= (U8)(U16_MSB(u16) << 3);                   
    U16_MSB(u16)  = u8;                                        
} while (0)
#else
#   error Unsupported FCS_METHOD.
#endif


"It is exciting to see the output of the compiler in assembly and see how it is done."

Yes, and that's how we "milk" the most out of our constrained 8-bitters!


List of 15 messages in thread
TopicAuthorDate
bitfields            01/01/70 00:00      
   RE: bitfields            01/01/70 00:00      
   RE: bitfields            01/01/70 00:00      
      RE: bitfields            01/01/70 00:00      
         RTFM!            01/01/70 00:00      
   RE: bitfields            01/01/70 00:00      
      RE: bitfields            01/01/70 00:00      
      RE: bitfields            01/01/70 00:00      
         RE: bitfields            01/01/70 00:00      
            RE: bitfields            01/01/70 00:00      
               RE: bitfields            01/01/70 00:00      
                  RE: bitfields            01/01/70 00:00      
   See the Keil site.            01/01/70 00:00      
      RE: See the Keil site.            01/01/70 00:00      
         RE: See the Keil site.            01/01/70 00:00      

Back to Subject List