All of lore.kernel.org
 help / color / mirror / Atom feed
From: Phil Reid <preid@electromag.com.au>
To: jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de,
	pmeerw@pmeerw.net, preid@electromag.com.au,
	linux-iio@vger.kernel.org
Subject: [PATCH 1/1] iio: core: Improve precision of __iio_format_value for FRACTIONAL values
Date: Tue, 29 Jan 2019 14:26:52 +0800	[thread overview]
Message-ID: <1548743212-83787-1-git-send-email-preid@electromag.com.au> (raw)

Currently FRACTIONAL values are outputed with 9 digits after the decimal
place. This is not always sufficient to resolve the raw value with 1 bit.
Output FRACTIONAL values to 15 decimal places of precision, regardless
of the number of leading zeros.

Currently for a 2.5V ref with 24 bits of precision the code outputs only
to 9 decimal places.

Cur: 0.00014901100000000000 * 16777216 = 2499.989733
New: 0.00014901161193847600 * 16777216 = 2500.000000
Signed-off-by: Phil Reid <preid@electromag.com.au>
---

Notes:
    Alternatively I could add additonal FRACTIONAL types that select the new
    behaviour to prevent any possible regressions.

 drivers/iio/industrialio-core.c | 55 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 46 insertions(+), 9 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index a062cfd..bd9da64 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -571,11 +571,53 @@ int of_iio_read_mount_matrix(const struct device *dev,
 #endif
 EXPORT_SYMBOL(of_iio_read_mount_matrix);
 
+static ssize_t __iio_format_div_prec(char *buf, unsigned int len, s64 x, s32 y)
+{
+	unsigned int prec = 0;
+	unsigned int idx = 0;
+	s64 d;
+
+	if (!len)
+		return 0;
+
+	if (!y)
+		return snprintf(buf, len, "inf");
+
+	if (!x)
+		return snprintf(buf, len, "0");
+
+	if (((x > 0) && (y < 0)) || ((x < 0) && (y > 0))) {
+		buf[idx++] = '-';
+		x = x > 0 ? x : -x;
+		y = y > 0 ? y : -y;
+	}
+
+	d = div64_s64(x, y);
+	idx += snprintf(buf+idx, len-idx, "%d", (int)d);
+	x = x - (y * d);
+	if ((x != 0) && (idx < len-1)) {
+		buf[idx++] = '.';
+		x = x * 10;
+		d = div64_s64(x, y);
+
+		while ((idx < len-1) && (prec < 15)) {
+			if (d || prec)
+				prec++;
+			buf[idx++] = '0' + (char)d;
+			x = x - (y * d);
+			if (!x)
+				break;
+			x = x * 10;
+			d = div64_s64(x, y);
+		}
+		buf[idx] = 0;
+	}
+	return idx;
+}
+
 static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
 				  int size, const int *vals)
 {
-	unsigned long long tmp;
-	int tmp0, tmp1;
 	bool scale_db = false;
 
 	switch (type) {
@@ -598,14 +640,9 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
 		else
 			return snprintf(buf, len, "%d.%09u", vals[0], vals[1]);
 	case IIO_VAL_FRACTIONAL:
-		tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
-		tmp1 = vals[1];
-		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
-		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
+		return __iio_format_div_prec(buf, len, vals[0], vals[1]);
 	case IIO_VAL_FRACTIONAL_LOG2:
-		tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
-		tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
-		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
+		return __iio_format_div_prec(buf, len, vals[0], 1 << vals[1]);
 	case IIO_VAL_INT_MULTIPLE:
 	{
 		int i;
-- 
1.8.3.1


             reply	other threads:[~2019-01-29  6:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-29  6:26 Phil Reid [this message]
2019-01-29  8:32 ` [PATCH 1/1] iio: core: Improve precision of __iio_format_value for FRACTIONAL values Alexandru Ardelean
2019-01-29  9:11   ` Phil Reid
2019-01-31 13:35     ` Jonathan Cameron
2019-02-01  5:47       ` Phil Reid
2019-02-01 11:22         ` 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=1548743212-83787-1-git-send-email-preid@electromag.com.au \
    --to=preid@electromag.com.au \
    --cc=jic23@kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=pmeerw@pmeerw.net \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.