I have some doubts regarding the implementation of multiplications by shifts and adds only.

I have a set of coefficients that I’ve use to multiply some variable. For example, I need to multiply tmp by coefficient 15/36. The coefficient is always in the for of k/2^m, and I have a lot of different coefficients (99 in total, and k<2^m). Hence I have the next:

tmp*15/36 is equivalent to (tmp<<3 + tmp<<2 + tmp<<1 + tmp)>>5. It requires 11 shifts and 3 additions.

Now, with this coefficient it is more efficient to do the next: tmp – tmp*17/36 which is equivalent to tmp – (tmp<<4 + tmp)>>5. In this case the same result is obtained with 9 shifts and 1 addition and 1 subtraction, which is a bit more optimized than in the first case.

Contrary, when coefficient is 19/32 it is more efficient to keep it like that, than to use it as 1-13/32, since the number of shifts and adds will be the same and we will need one subtraction in addition.

In summary, my question is how to determinate when to use coefficient directly, and when to use it in the form of 1-(2^m-k)/2^m? Is there any way to conclude it automatically?

Thank you.