Re: [PATCH] staging: iio: ad5933: Use div64_ul instead of do_div

From: Deepak R Varma
Date: Fri Nov 04 2022 - 14:48:26 EST


On Sat, Oct 29, 2022 at 11:23:06AM +0000, David Laight wrote:
> > > -----Original Message-----
> > > From: Deepak R Varma <drv@xxxxxxxxx>
> > >
> > > [External]
> > >
> > > do_div() does a 64-by-32 division. Here the divisor is an unsigned long
> > > which on some platforms is 64 bit wide. So use div64_ul instead of do_div
> > > to avoid a possible truncation. Issue was identified using the
> > > coccicheck tool.
>
> These changes should all get nacked unless the domain of the values
> can be shown to be out of range.

Hello David,
I looked through the data sheets for these hardware [ad983*] and believe the
divisor max can attain 75MHz which fits well in a 32 bit size. Hence the
proposed changes for these drivers to switch to div64_ul may actually slowdown
the division. Please correct if I said something incorrect.

Requesting to kindly ignore my proposed changes.

>
> The entire point of do_div() is that because division is expensive
> using a limited range division is significantly faster.
>
> Even on Intel 64 bit cpu the 64 by 32 divide is significantly
> faster then a full 64 bit divide for the same input values.
>
> One might also question why the divisor is actually 'unsigned long'
> at all. The code is almost certainly expected to compile for 32bit
> so the domain of the value should fit in 32 bits.
> So either the type could be unsigned int, or it really doesn't matter
> that the value is truncated to 32bit because it can never be larger.

Thank you for the detailed explanation. This is very helpful.



Thank you for the detailed explanation. This is very helpful.

./drv

>
> David
>
>
> > >
> > > Signed-off-by: Deepak R Varma <drv@xxxxxxxxx>
> > > ---
> >
> > Reviewed-by: Nuno Sá <nuno.sa@xxxxxxxxxx>
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)
>