Microchip C30 Bitwise Operation

Discussion in 'Embedded Systems and Microcontrollers' started by Art, May 13, 2015.

  1. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    Hi Guys,
    I’m used to assembler, or a BASIC compiler with access to inline assembler,
    so porting my reusable code to C30 has been a bit hit & miss where speed with
    bitwise operations are concerned.

    The most prohibitive thing at the moment, porting an LCD graphics routine,
    I was using this to bitwise rotate an array to the right for speed
    (well I'm actually rotating a 50 byte array to the left, but this is an example)
    Code (Text):
    1.  
    2. RotateRight ;bitwise rotate array
    3. rrf HMK+0 ,F ;a faster way
    4. rrf HMK+1 ,F ;
    5. rrf HMK+2 ,F ;
    6. rrf HMK+3 ,F ;
    7. rrf HMK+4 ,F ;
    8. rrf HMK+5 ,F ;
    9. rrf HMK+6 ,F ;
    10. rrf HMK+7 ,F ;
    11. rrf HMK+8 ,F ;
    12. rrf HMK+9 ,F ;
    13. bcf HMK+0 ,7 ;preclear MSB
    14. btfsc status ,C ;check carry bit
    15. bsf HMK+0 ,7 ;set MSB
    16. return ;or return
    17.  
    Now it seems the best option for the same with C30 is this:
    Code (Text):
    1.  
    2. void rotateRight() {
    3. temp = (HMK[9] << 7); // rotate HMK right once
    4. HMK[9] = (HMK[8] << 7) | (HMK[9] >> 1);
    5. HMK[8] = (HMK[7] << 7) | (HMK[8] >> 1);
    6. HMK[7] = (HMK[6] << 7) | (HMK[7] >> 1);
    7. HMK[6] = (HMK[5] << 7) | (HMK[6] >> 1);
    8. HMK[5] = (HMK[4] << 7) | (HMK[5] >> 1);
    9. HMK[4] = (HMK[3] << 7) | (HMK[4] >> 1);
    10. HMK[3] = (HMK[2] << 7) | (HMK[3] >> 1);
    11. HMK[2] = (HMK[1] << 7) | (HMK[2] >> 1);
    12. HMK[1] = (HMK[0] << 7) | (HMK[1] >> 1);
    13. HMK[0] = temp | (HMK[0] >> 1);
    14. }
    15.  
    It occurs to me this is a lot slower, even on a much faster micro.
    It could be that I need to check delays in other routines (such as the LCD delays),
    but is there a better way?
    Cheers, Art.
     
  2. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,908
    2,169
Loading...