[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20210801194000.3646303-9-liambeguin@gmail.com>
Date: Sun, 1 Aug 2021 15:39:55 -0400
From: Liam Beguin <liambeguin@...il.com>
To: liambeguin@...il.com, peda@...ntia.se, jic23@...nel.org,
lars@...afoo.de, pmeerw@...erw.net
Cc: linux-kernel@...r.kernel.org, linux-iio@...r.kernel.org,
devicetree@...r.kernel.org, robh+dt@...nel.org
Subject: [PATCH v7 08/13] iio: afe: rescale: fix precision on fractional log scale
From: Liam Beguin <lvb@...hos.com>
The IIO_VAL_FRACTIONAL_LOG2 scale type doesn't return the expected
scale. Update the case so that the rescaler returns a fractional type
and a more precise scale.
Signed-off-by: Liam Beguin <lvb@...hos.com>
---
drivers/iio/afe/iio-rescale.c | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c
index abd7ad73d1ce..e37a9766080c 100644
--- a/drivers/iio/afe/iio-rescale.c
+++ b/drivers/iio/afe/iio-rescale.c
@@ -47,12 +47,17 @@ int rescale_process_scale(struct rescale *rescale, int scale_type,
*val2 = rescale->denominator;
return IIO_VAL_FRACTIONAL;
case IIO_VAL_FRACTIONAL_LOG2:
- tmp = *val * 1000000000LL;
- do_div(tmp, rescale->denominator);
- tmp *= rescale->numerator;
- do_div(tmp, 1000000000LL);
+ if (check_mul_overflow(*val, rescale->numerator, (s32 *)&tmp) ||
+ check_mul_overflow(rescale->denominator, (1 << *val2), (s32 *)&tmp2)) {
+ tmp = (s64)*val * rescale->numerator;
+ tmp2 = (s64)rescale->denominator * (1 << *val2);
+ factor = gcd(abs(tmp), abs(tmp2));
+ tmp = div_s64(tmp, factor);
+ tmp2 = div_s64(tmp2, factor);
+ }
*val = tmp;
- return scale_type;
+ *val2 = tmp2;
+ return IIO_VAL_FRACTIONAL;
case IIO_VAL_INT_PLUS_NANO:
case IIO_VAL_INT_PLUS_MICRO:
if (scale_type == IIO_VAL_INT_PLUS_NANO)
--
2.30.1.489.g328c10930387
Powered by blists - more mailing lists