It is better to use common division or Shift and Sum operations in 8bits microcontrollers

0
Considering programming for 8bits microcontrollers, we know that even for this microcontrollers, the compilers have reached a unique optimization level and that allows us to program focused only on the quality of the code reading.

But when programming for microcontrollers it is crucial that we always care about the performance of our code and its size.

In view of this is it better to use complex math operations for the processor as the division, or go directly to code optimization and use logical operators for shift and sums?

    
asked by anonymous 09.07.2015 / 19:08

1 answer

1

I would have to know

a) the exact model of the processor, as well as the version of it, because different versions with the same instruction set can perform very differently in the division statement. And many architectures do not even have split statement (8051 has, AVR does not).

b) What kind of division your code will face most. Division by power of 2? Division by a constant? The constant is small, type 3, 5, 7?

Even in "fat" architectures like x86, a good compiler replaces the DIV instruction with multiplications, sums, and shifts, such as the famous 3:

return (uint32_t)(((uint64_t)0xAAAAAAABULL * divideMe) >> 33);

But this is worth it when it comes to a constant small divider. The DIV of a processor is optimized for the "middle case", that is, it will perform better than software splitting if you consider all possible divisors.

Another problem that appears when trying to divide (or multiply) using more elementary instructions is the question of how to deal with the signal (positive or negative).

    
28.08.2015 / 19:01