Re: [PATCH v11 10/15] iio: afe: rescale: make use of units.h

From: Andy Shevchenko
Date: Wed Dec 22 2021 - 07:35:55 EST


On Wed, Dec 22, 2021 at 5:47 AM Liam Beguin <liambeguin@xxxxxxxxx> wrote:
>
> From: Liam Beguin <lvb@xxxxxxxxxx>
>
> Make use of well-defined SI metric prefixes to improve code readability.

...

> case IIO_VAL_FRACTIONAL_LOG2:
> - tmp = (s64)*val * 1000000000LL;
> + tmp = (s64)*val * NANO;
> tmp = div_s64(tmp, rescale->denominator);
> tmp *= rescale->numerator;
>
> - tmp = div_s64_rem(tmp, 1000000000LL, &rem);
> + tmp = div_s64_rem(tmp, NANO, &rem);
> *val = tmp;

Thanks! The important part of this conversion is to get one trick,
i.e. NANO and GIGA are both represented by 10^9. We need to be sure
that here we use the proper sign of the power of these numbers. So
please double check in all cases that the chosen SI prefixes are
correct from the power sign point of view, e.g. it is 10^-9 and not
10^9 or otherwise.

...

> *val2 = rem / (int)tmp;
> if (rem2)
> - *val2 += div_s64((s64)rem2 * 1000000000LL, tmp);
> + *val2 += div_s64((s64)rem2 * NANO, tmp);

Ditto here and for the rest

--
With Best Regards,
Andy Shevchenko