Re: [PATCH v7 2/3] iio: (bma400) add driver for the BMA400

From: Andy Shevchenko
Date: Fri Dec 20 2019 - 04:32:58 EST


On Fri, Dec 20, 2019 at 11:27 AM Andy Shevchenko
<andy.shevchenko@xxxxxxxxx> wrote:
> On Fri, Dec 20, 2019 at 6:48 AM Dan Robertson <dan@xxxxxxxxxxxxxxx> wrote:
> > On Thu, Dec 19, 2019 at 01:02:28PM +0200, Andy Shevchenko wrote:
> > > On Thu, Dec 19, 2019 at 6:27 AM Dan Robertson <dan@xxxxxxxxxxxxxxx> wrote:
>
> > > > +static int bma400_set_accel_output_data_rate(struct bma400_data *data,
> > > > + int hz, int uhz)
> > > > +{
> > > > + unsigned int idx;
> > > > + unsigned int odr;
> > > > + unsigned int val;
> > > > + int ret;
> > > > +
> > > > + if (hz >= BMA400_ACC_ODR_MIN_WHOLE_HZ) {
> > > > + if (uhz || hz % BMA400_ACC_ODR_MIN_WHOLE_HZ)
> > > > + return -EINVAL;
> > > > +
> > > > + val = hz / BMA400_ACC_ODR_MIN_WHOLE_HZ;
> > >
> > > Again, AFAICS division may be avoided in both cases (% and / above)
> > > because of is_power_of_2() check below.
> > > Can you revisit this?
> >
> > Yeah I can update this in the next patchset, but I don't know if it is much more
> > readable this way.
>
> You may describe the algo in the comment.
>
> Let's see how it might look like
>
> if (uhz)
> return -EINVAL;
> idx = __ffs(val);
> /* We're expecting value to be 2^n * ODR_MIN_WHOLE_HZ */
> if ((val >> idx) != BMA400_ACC_ODR_MIN_WHOLE_HZ)

Okay, this would require trickier conditional for the cases when
MIN_WHOLE_HZ can be divided by 2^k...
Still from performance point of view it might be much faster than division.

> retutn -EINVAL;
> idx += BMA400_ACC_ODR_MIN_RAW + 1;
>
> Would it work?
>
> > > > + if (!is_power_of_2(val))
> > > > + return -EINVAL;
> > > > +
> > > > + idx = __ffs(val) + BMA400_ACC_ODR_MIN_RAW + 1;

--
With Best Regards,
Andy Shevchenko