It is better to use common division or Shift and Soma operations on 8bit microcontrollers

Asked

Viewed 176 times

0

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

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

Therefore, it is better to use complex mathematical operations for the processor as a division, or to go directly to an optimization of the code and use logical operators for displacement (shift) and sums?

1 answer

1


I would have to know

a) the exact model of the processor, as well as the version of the same, because different versions with the same set Instruction may have quite different performance in the division instruction. And many architectures don’t even have division instruction (8051 has, AVR doesn’t have).

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

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

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

but this is worth it when it comes to a constant and small divider. The DIV of a processor is optimized for the "medium case", IE, it will have better performance than division by software if considering all possible splitters.

Another problem that appears when trying to do division (or multiplication) using more elementary instructions is the question of how to deal with the sign (positive or negative).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.