Re: [PATCH] bring sanity to div64.h and do_div usage

Maciej W. Rozycki
2002-02-08T18:54:14Z

On Fri, 8 Feb 2002, Troy Benjegerdes wrote:

> Several people I have talked to on the issue specifically asked for the
> panic(), as people using do_div() should really know better than to do 64
> bit divides in the kernel.

There are legitimate cases where you cannot avoid a double-precision
division and the inefficiency is negligible. For example for MIPS it's
used in do_*_gettimeoffset() and at most once a jiffy (actually we use
do_div64_32() to reduce work, as we know the quotient will *always* fit in
32 bits).

> The generic C algorithm only handles base < 65536.
>
> I can think of a couple ways around this..
>
> 1) Make the base argument be a 'u16 base', and people with too large a
> base would get compile warnings/errors.
>
> 2) run-time check on base, and panic if too large
>
> 3) run-time check on base, print dmesg warning if too large

4) Use a generic division algorithm using shifts and subtracts such as
one of these described in academic books. You may port the implementation
from include/asm-mips/div64.h. ;-)

Note that in do_*_gettimeoffset() the divisor is an arbitrary 32-bit
number, mostly depending on the uptime.

--
+ Maciej W. Rozycki, Technical University of Gdansk, Poland +
+--------------------------------------------------------------+
+ e-mail: macro@ds2.pg.gda.pl, PGP key available +