piclist 2017\09\05\153215a >
www.piclist.com/techref/microchip/math/index.htm?key=division
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

res = 0;

while (true)
{
x = x - y;

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

{        break;
}

x = x << 1;
}

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

return res;
}

Regards
Sergio Masci
-- http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive