Message ID | 20211222034646.222189-11-liambeguin@gmail.com (mailing list archive) |
---|---|
State | Changes Requested |
Headers | show |
Series | iio: afe: add temperature rescaling support | expand |
On Wed, Dec 22, 2021 at 5:47 AM Liam Beguin <liambeguin@gmail.com> wrote: > > From: Liam Beguin <lvb@xiphos.com> > > 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
On Wed, Dec 22, 2021 at 02:33:52PM +0200, Andy Shevchenko wrote: > On Wed, Dec 22, 2021 at 5:47 AM Liam Beguin <liambeguin@gmail.com> wrote: > > > > From: Liam Beguin <lvb@xiphos.com> > > > > 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. I get the difference, but I guess I'm not sure I understand how you want me to use them. I was using NANO here as that made most sense for me. If we go by the positive vs. negative powers of ten, I should always use GIGA as we're multiplying by 10^9 and dividing by 10^9. Is that what you expected? IMO, that wouldn't be as clear, but I believe you know better. Cheers, Liam > ... > > > *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
On Wed, Dec 22, 2021 at 8:51 PM Liam Beguin <liambeguin@gmail.com> wrote: > On Wed, Dec 22, 2021 at 02:33:52PM +0200, Andy Shevchenko wrote: > > On Wed, Dec 22, 2021 at 5:47 AM Liam Beguin <liambeguin@gmail.com> wrote: ... > > 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. > > I get the difference, but I guess I'm not sure I understand how you want me to > use them. I was using NANO here as that made most sense for me. > > If we go by the positive vs. negative powers of ten, I should always use > GIGA as we're multiplying by 10^9 and dividing by 10^9. Is that what you > expected? You should get the proper power after the operation. Write a formula (mathematically speaking) and check each of them for this. 10^-5/10^-9 == 1*10^4 (Used NANO) 10^-5/10^9 == 1/10^-14 (Used GIGA) See the difference? In the similar way for values of e.g. 10^5.
diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 0000a58bab9d..27f99ce67b94 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -49,11 +49,11 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, } fallthrough; 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; if (!rem) @@ -69,7 +69,7 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, *val2 = rem / (int)tmp; if (rem2) - *val2 += div_s64((s64)rem2 * 1000000000LL, tmp); + *val2 += div_s64((s64)rem2 * NANO, tmp); return IIO_VAL_INT_PLUS_NANO; case IIO_VAL_INT_PLUS_NANO: @@ -330,8 +330,8 @@ static int rescale_current_sense_amplifier_props(struct device *dev, * gain_div / (gain_mult * sense), while trying to keep the * numerator/denominator from overflowing. */ - factor = gcd(sense, 1000000); - rescale->numerator = 1000000 / factor; + factor = gcd(sense, MICRO); + rescale->numerator = MICRO / factor; rescale->denominator = sense / factor; factor = gcd(rescale->numerator, gain_mult); @@ -359,8 +359,8 @@ static int rescale_current_sense_shunt_props(struct device *dev, return ret; } - factor = gcd(shunt, 1000000); - rescale->numerator = 1000000 / factor; + factor = gcd(shunt, MICRO); + rescale->numerator = MICRO / factor; rescale->denominator = shunt / factor; return 0;