ShiftRegister-PWM-Library icon indicating copy to clipboard operation
ShiftRegister-PWM-Library copied to clipboard

Faster `shiftOut` function

Open knothed opened this issue 1 year ago • 0 comments

Your optimized version of the shiftOut function uses 8 conditional branches. It should be possible to write a faster version without if-branches. Something like this should work (not tested):

inline void shiftOut(uint8_t data) const
{
  // unrolled for loop
  // bit 0 (LSB)
  ShiftRegisterPWM_setDataPin(data & 0B00000001);
  ShiftRegisterPWM_toggleClockPinTwice();
  ...
}

where

// val must be 0 or 1
#define ShiftRegisterPWM_setDataPin(val)
  ShiftRegisterPWM_DATA_PORT = (ShiftRegisterPWM_DATA_PORT | ((-(val)) & ShiftRegisterPWM_DATA_MASK)) & ((-(val)) | ~ShiftRegisterPWM_DATA_MASK)

knothed avatar Sep 10 '24 07:09 knothed