Divide and C
karln at uunet.uu.net
karln at uunet.uu.net
Sat Mar 30 02:34:22 AEST 1991
>
>If your compiler is not this smart and you know that the time spent in
>the extra division is making your program unacceptably slow, you can
>try the Standard C library function div(), which takes a numerator
>and denominator and returns a structure containing the quotient and
>remainder. Use of this function might turn out to be slower than
>the extra divide.
>
>Steve Clamage, TauMetric Corp, steve at taumet.com
This shows a good question someone pointed out
about the speed of an assembly divide plus a multiply
verses doing the real binary divide in C with subtracts and
shifts. Here is the posting that did not make into the
summary.
int twodivide(val,divisor,&result,&remain)
{
int r;
r=val/divisor;
*result=r;
*remain=val-divisor*r;
}
that's with a multiply (divisor*r) - but that's probably a lot faster
than looping, subtracting divisor each time, adding 1 onto r, you know.
Russell Schulz ersys!rschulz at nro.cs.athabascau.ca
Edmonton Remote Systems: Serving Northern Alberta since 1982
Do you suppose that is true? I do have that 'Binary Divide' code around
somewhere. I think I will find it and do a benchmark. Stay tuned.
Karl Nicholas
karln!karln at uunet.uu.net
PS. I wonder if the optimizer will recoqnize the Binary Divide
routine and just do an Assembly Divide :-)
More information about the Comp.lang.c
mailing list