The multiply-by-constant as a fixed sequences of adds and shifts is,
of course, interesting (multiply-and-accumulate).
I've tried to find a similar inverse method for divide-by-constant, but
I think the fixed sequence turns out to be per the dividend, not the
divisor, that is, divide-a-constant-by rather than divide-by-constant.
I think.
I have also looked at division as multiply-by-inverse, but convinced
myself that inverting the divisor ends up being expensive enough
to negate any advantage over just doing the subtract-shift-count
binary long division.
Looking at this again, it occurs to me that, if the divisor is a
constant, it can be pre-computed. So, in the case of divide-by-ten,
the inverse of ten in binary is
.0001100110011001
to sixteen bits.
(bc is useful in calculating such things.)
Giving it a try with an arbitrary dividend of 65000 shows me that
there will be some tricky business with rounding, but it should
almost work:
bc -l<enter>
obase=2<enter>
65000<enter>
1111110111101000
65000/10<enter>
1100101100100.0000000000000000
0000000000000
1/10<enter>
.00011001100110011001100110011
ibase=2<enter>
1111110111101000*0001100110011
11001011000110110011110101000
Divide-by-ten can also be looked at as divide-by-five, then
divide-by-two, which might be of use in working out the rounding.
1/10 and 1/5 are repeating fractions in binary, of course, and that
could be used to effect in turning the long chain of add-and-shifts
into a loop, at the cost of adding loop overhead.
I'm going to come back to this when i have more time.
No comments:
Post a Comment