> -----Original Message----- > From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] On Behalf > Of William "Chops" Westfield > Sent: 10 May 2008 01:15 > To: Microcontroller discussion list - Public. > Subject: Re: [PIC] - Need fast divide by 14 constant routine? ( Random > access8-bit data in PIC16F flash ) > > > On May 9, 2008, at 12:39 PM, Harold Hallikainen wrote: > > There's a nice code generator for multiply and divide by a constant at > > http://www.piclist.com/techref/piclist/codegen/constdivmul.htm . > > The algorithm it produces does not seem to work very well in this > case, even if I use extra bits to produce less error in intermediate > steps. > I'm having trouble grasping why not :-( > > It says: > > ; ALGORITHM: > ; Clear accumulator > ; Add input / 16 to accumulator > ; Add input / 128 to accumulator > ; Add input / 1024 to accumulator > ; Add input / 8192 to accumulator > ; Add input / 65536 to accumulator > > The first error is at 14. (ALL the factors are obviously zero.) > Even if I do: > > (i/2 + i/16 + i/128 + i/1024 + i/8192) / 8 > > The first error is still at 14. > > Going at it backwards isn't going to work either: > x/14 = x/8 - x/32 - x/64 - x/256... > This problem very often occurs with small input values when using shift/add type multiplications. Often you can add a fudge factor which gives acceptable performance with small inputs (but will give the odd off-by-one error with large values). ((i/2 + i/16 + i/128 + i/1024 + i/8192) + 1 ) / 8 Mike ======================================================================= This e-mail is intended for the person it is addressed to only. The information contained in it may be confidential and/or protected by law. If you are not the intended recipient of this message, you must not make any use of this information, or copy or show it to any person. Please contact us immediately to tell us that you have received this e-mail, and return the original to us. Any use, forwarding, printing or copying of this message is strictly prohibited. No part of this message can be considered a request for goods or services. ======================================================================= -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist