piclist 2017\09\05\153215a >
Thread: Division routines.
face BY : smplx email (remove spam text)

On Tue, 5 Sep 2017, David C Brown wrote:

> I am trying to find some good 8 and 16 bit division routines for mid range
> PICs.   Most of the app notes I have found give algorithms which result in
> a quotient and remainder.
> However in my case the divisor is always greater than the dividend so I
> want a result that is conceptually between 0 and nearly 1 with an implied
> binary point to the left.
> I have written my own routines but I suspect that they are far from optimum.

I'll give an example in C and I'll leave it to you to convert:

int frac_div(int x, int y)
       // divide x by y where x is always <= y
       // x and y are always positive

       unsigned int        res, mask;

       mask = 0x8000;
       res = 0;

       while (true)
               x = x - y;

               if (x < 0)
               {        // on a PIC simply check the sign bit of x
                       x = x + y;
                       res = res | mask;

               mask = mask >> 1;

               if (mask == 0)
               {        break;

               x = x << 1;

       // BEWARE: MSB of res is int 0 or 1,
       // the remaining 15 bits are the binary fraction

       return res;

Sergio Masci
-- http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at

<alpine.LNX.2.00.1709052121550.17067@tin.allotrope.net> quoted-printable

In reply to: <CAFo_nNrrP-pkzd4t+65LLLrq7hj=EB8z6yKgLKyEGnjBaHtjtw@mail.gmail.com>
See also: www.piclist.com/techref/microchip/math/index.htm?key=division
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Division routines.

month overview.

new search...