SDAA552 September 2026 MSPM0C1103 , MSPM0C1103-Q1 , MSPM0C1104 , MSPM0C1104-Q1
Multiply and divide operations take many cycles to perform and require more code to enable these operations. Therefore, finding ways to remove unnecessary multiplication or division from code can be a great way to save on both code space and execution time.
For multiplication or division by powers of 2 (for example, 2, 4, 8, 16, …), bit shifts can be used instead. To do a bit shift in C, use >> to right shift and << to left shift, then the number of bits to shift. This is much more efficient than a multiply or divide because there are assembly instructions and hardware in the CPU for bit shifts.
Another option is to determine if multiplications or divisions can be computed at compile-time rather than runtime. For example, when multiplying two constants with no variable, the compiler can calculate the result during compilation and use the computed value directly, eliminating the need for multiplication code that would otherwise consume both memory and execution time.
If a variable is part of a multiplication, such that it cannot all be calculated ahead, and that variable has a known range of values, a lookup table could be created to contain the possible results.