All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lars-Peter Clausen <lars@metafoo.de>
To: Jonathan Cameron <jic23@kernel.org>
Cc: Peter Meerwald-Stadler <pmeerw@pmeerw.net>,
	linux-iio@vger.kernel.org, Lars-Peter Clausen <lars@metafoo.de>
Subject: [PATCH 2/3] iio: iio_format_value(): Fix IIO_VAL_FRACTIONAL_LOG2 values between -1.0 and 0.0
Date: Tue, 15 Dec 2020 20:17:42 +0100	[thread overview]
Message-ID: <20201215191743.2725-2-lars@metafoo.de> (raw)
In-Reply-To: <20201215191743.2725-1-lars@metafoo.de>

When formatting a value using IIO_VAL_FRACTIONAL_LOG2 and the values is
between -1 and 0 the sign is omitted.

We need the same trick as for IIO_VAL_FRACTIONAL to make sure this gets
formatted correctly.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
Found while writing a unit test for this.

Considering nobody complained we probably do not have an driver that will
run into this issue at the moment. So this doesn't need to got to stable.
---
 drivers/iio/industrialio-core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index d84b74cc544b..4b30c042e5f6 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -626,7 +626,10 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
 	case IIO_VAL_FRACTIONAL_LOG2:
 		tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
 		tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1);
-		return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
+		if (tmp0 == 0 && tmp2 < 0)
+			return snprintf(buf, len, "-0.%09u", abs(tmp1));
+		else
+			return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
 	case IIO_VAL_INT_MULTIPLE:
 	{
 		int i;
-- 
2.20.1


  reply	other threads:[~2020-12-15 19:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-15 19:17 [PATCH 1/3] iio: iio_format_value(): Use signed temporary for IIO_VAL_FRACTIONAL_LOG2 Lars-Peter Clausen
2020-12-15 19:17 ` Lars-Peter Clausen [this message]
2020-12-15 19:17 ` [PATCH 3/3] iio: Add basic unit test for iio_format_value() Lars-Peter Clausen
2020-12-29 18:16   ` Jonathan Cameron
2021-01-02 14:59     ` Lars-Peter Clausen
2021-02-07 12:45       ` Lars-Peter Clausen
2021-02-14 12:50         ` 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=20201215191743.2725-2-lars@metafoo.de \
    --to=lars@metafoo.de \
    --cc=jic23@kernel.org \
    --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.