I am using a loop to manipulate a number of variables using bit values as I go. I have noticed that I am using the expressions if (some_state & (1 << i))
, and some_var &= ~(1 << i)
dozens of times before i
is incremented.
This routine must be executed in on a regular interrupt, and my main clock is not all that fast, so I am wondering if this is the best I can do. In my head this is a lot of operations.
Maybe I could could store the shifted value and use it instead.
for (int i=0; i<MAX; ++i) {
uint32_t mask_value = (1 << i);
if (some_state & mask_value) {
if (! --some_array[i].timer_value) {
some_var |= mask_value;
another_var &= ~mask_value;
}
}
...
}
Does there appear to be any clear answer? Perhaps this is more of a situation where the compiler may optimize to some unknown level, so who can know until I run the routines 10,000 times, time them, and compare?
UPDATE:
I'm running GNU ARM Embedded Toolchain for my NXP LPC11 ARM (Cortex-M0) microcontroller.
Should be reasonable enough to review assembly in the IDE (LPCXpresso, Eclipse based) and to profile it.