From: Jonathan Cameron <jic23@kernel.org>
To: Peter Rosin <peda@axentia.se>
Cc: Jonathan.Cameron@huawei.com, lars@metafoo.de,
linux-iio@vger.kernel.org, michael.hennerich@analog.com,
vincent.whitchurch@axis.com
Subject: Re: [PATCH 01/17] iio: core: Increase precision of IIO_VAL_FRACTIONAL_LOG2
Date: Thu, 28 Apr 2022 19:49:57 +0100 [thread overview]
Message-ID: <20220428194957.4d51edfa@jic23-huawei> (raw)
In-Reply-To: <a684c794-3f28-7ce5-92fe-0aed3ae52d91@axentia.se>
On Tue, 26 Apr 2022 14:00:49 +0200
Peter Rosin <peda@axentia.se> wrote:
> Hi!
>
> > From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> >
> > With some high resolution sensors such as the ad7746 the
> > build up of error when multiplying the _raw and _scale
> > values together can be significant. Reduce this affect by
> > providing additional resolution in both calculation and
> > formatting of result.
> >
> > Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> > ---
> > drivers/iio/industrialio-core.c | 23 +++++++++++++++--------
> > 1 file changed, 15 insertions(+), 8 deletions(-)
> >
> > diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> > index 2f48e9a97274..d831835770da 100644
> > --- a/drivers/iio/industrialio-core.c
> > +++ b/drivers/iio/industrialio-core.c
> > @@ -683,14 +683,21 @@ static ssize_t __iio_format_value(char *buf, size_t offset, unsigned int type,
> > else
> > return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
> > abs(tmp1));
> > - case IIO_VAL_FRACTIONAL_LOG2:
> > - tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
> > - tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1);
> > - if (tmp0 == 0 && tmp2 < 0)
> > - return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
> > - else
> > - return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
> > - abs(tmp1));
> > + case IIO_VAL_FRACTIONAL_LOG2: {
> > + u64 t1, t2;
> > + int integer;
> > + bool neg = vals[0] < 0;
> > +
> > + t1 = shift_right((u64)abs(vals[0]) * 1000000000000ULL, vals[1]);
>
> While the multiplication is safe if val[0] is 24 bits or less, I
> wonder if that's OK for *all* the existing stuff? I would have guessed
> that something somewhere needs more bits than that? Did you consider the
> rescaler? And if everything currently really does fit in 24 bits, do we
> really want to make it difficult to add something beyond 24 bits later
> on?
Good point. Perhaps we can do something a bit nefarious and check that
val[0] is sufficiently small and if not fallback to lower precision
as we had before? Or if adventurous adjust the precision automatically
so we can get as many digits (at least 9) as can be computed without
overflow... For now, I think 2 steps is enough though.
Jonathan
>
> Cheers,
> Peter
>
> > + integer = (int)div64_u64_rem(t1, 1000000000000ULL, &t2);
> > + if (integer == 0 && neg)
> > + return sysfs_emit_at(buf, offset, "-0.%012llu", abs(t2));
> > + if (neg)
> > + integer *= -1;
> > + return sysfs_emit_at(buf, offset, "%d.%012llu", integer,
> > + abs(t2));
> > + }
> > + }
> > case IIO_VAL_INT_MULTIPLE:
> > {
> > int i;
> > --
> > 2.35.3
> >
>
next prev parent reply other threads:[~2022-04-28 18:41 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-18 19:28 [PATCH 00/17] staging/iio: Clean up AD7746 CDC driver and move from staging Jonathan Cameron
2022-04-18 19:28 ` [PATCH 01/17] iio: core: Increase precision of IIO_VAL_FRACTIONAL_LOG2 Jonathan Cameron
2022-04-26 12:00 ` Peter Rosin
2022-04-28 18:49 ` Jonathan Cameron [this message]
2022-04-18 19:28 ` [PATCH 02/17] iio: ABI: Fix wrong format of differential capacitance channel ABI Jonathan Cameron
2022-04-18 19:28 ` [PATCH 03/17] staging: iio: cdc: ad7746: Use explicit be24 handling Jonathan Cameron
2022-04-26 10:58 ` Andy Shevchenko
2022-04-18 19:28 ` [PATCH 04/17] staging: iio: cdc: ad7746: Push handling of supply voltage scale to userspace Jonathan Cameron
2022-04-18 19:28 ` [PATCH 05/17] staging: iio: cdc: ad7746: Use local buffer for multi byte reads Jonathan Cameron
2022-04-18 19:28 ` [PATCH 06/17] staging: iio: cdc: ad7746: Factor out ad7746_read_channel() Jonathan Cameron
2022-04-18 19:28 ` [PATCH 07/17] staging: iio: cdc: ad7764: Push locking down into case statements in read/write_raw Jonathan Cameron
2022-04-18 19:28 ` [PATCH 08/17] staging: iio: cdc: ad7746: Break up use of chan->address and use FIELD_PREP etc Jonathan Cameron
2022-04-18 19:28 ` [PATCH 09/17] staging: iio: cdc: ad7746: Drop usused i2c_set_clientdata() Jonathan Cameron
2022-04-18 19:29 ` [PATCH 10/17] staging: iio: cdc: ad7746: Use _raw and _scale for temperature channels Jonathan Cameron
2022-04-18 19:29 ` [PATCH 11/17] iio: core: Introduce _inputoffset for differential channels Jonathan Cameron
2022-04-18 19:29 ` [PATCH 12/17] staging: iio: cdc: ad7746: Switch from _offset to " Jonathan Cameron
2022-04-18 19:29 ` [PATCH 13/17] staging: iio: cdc: ad7746: Use read_avail() rather than opencoding Jonathan Cameron
2022-04-18 19:29 ` [PATCH 14/17] staging: iio: ad7746: White space cleanup Jonathan Cameron
2022-04-18 19:29 ` [PATCH 15/17] iio: cdc: ad7746: Add device specific ABI documentation Jonathan Cameron
2022-04-18 19:29 ` [PATCH 16/17] iio: cdc: ad7746: Move driver out of staging Jonathan Cameron
2022-04-18 19:29 ` [PATCH 17/17] RFC: iio: cdc: ad7746: Add roadtest Jonathan Cameron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220428194957.4d51edfa@jic23-huawei \
--to=jic23@kernel.org \
--cc=Jonathan.Cameron@huawei.com \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=michael.hennerich@analog.com \
--cc=peda@axentia.se \
--cc=vincent.whitchurch@axis.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).