Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!uwm.edu!spool.mu.edu!uunet!karln!karln!karln From: karln@uunet.uu.net Newsgroups: comp.lang.c Subject: Re: Divide and C Message-ID: <1991Mar29.163422.14672@uunet.uu.net> Date: 29 Mar 91 16:34:22 GMT References: <1991Mar27.185804.7221@uunet.uu.net> <639@taumet.com> Reply-To: karln!karln@uunet.uu.net Organization: Sam76 - Pennington NJ Lines: 40 > >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@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@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@uunet.uu.net PS. I wonder if the optimizer will recoqnize the Binary Divide routine and just do an Assembly Divide :-)