Is there a faster way to do this with assembly than with normal C?
This is a prototype of the function in C:
uint8_t InsertBit(uint8_t input, uint16_t idx, uint8_t insbit)
The function should return the new value. I work on embedded arm cortex M. The C routine has around 18 instructions.
The C routine:
__attribute__((optimize("O1")))
uint8_t InsertBit(uint8_t input, uint16_t idx, uint8_t insbit)
{
//Split the input into two parts, one shifted and one not
uint8_t bottom = input;
uint8_t top = (input << 1);
//insert a '0' or '1' before the shifted part
if (insbit)
top |= (1 << idx);
else
top &= (~(1 << idx));
//keep the top bits of top
top &= (-1 << idx);
//keep the bottom bits of bottom
bottom &= ~(-1 << idx);
//combine the two parts.
return (bottom | top);
}