Latest News
|NewsletterAaron Lager, Panamax Furman, Santa Rosa, CA; Edited by Charles H Small and Fran Granville -- EDN, 5/29/2008
This Design Idea presents a method for fast integer multiplying and multiplying by fractions.
What can you do when you lack access to a hardware multiplier or MAC (multiply/accumulate) function and you need to multiply by something other than a power of two?
One option is to include the math.h function and just sling around the multiplication operator and watch your code bloat and slow to a crawl. Option two is to get fancy with bit shifting.
The general idea is to find powers of two, including zero, that you can add to achieve the multiplier you need. This method works because of the distributive properties of multiplication.
Using the distributive properties of multiplication, you can, for example, rearrange the problem of: 12×12=144?(4+8)×12=144?(12×4)+(12×8)=144.
This version is amenable to implementation in C code because four and eight are powers of two. To implement the multiplications, you use the exponent of the power-of-two representation for your code as an integer shift. Because 4=22 and 8=23, you use two and three as your shift factors.
Using this same theory of distribution, you can also perform fractional multiplication or division. This method creates rounding errors just like dividing integers by values that are not powers of two does with math.h functions and the division operator.
All of these examples take up less space and are faster than calling the standard 8×8-multiply function or division function from most standard math libraries.
Also, you should note that, if the result of the variable you are multiplying can ever exceed 8 bits, then you should use a word function that can store 16 bits of your result, and you should use casting on the outer parentheses.
The result is (word)((foo<<1)+(foo>>1)+(foo>>3)).
Aaron Lager, Panamax Furman, Santa Rosa, CA; Edited by Charles H Small and Fran Granville -- EDN, 5/29/2008