linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anand Ashok Dumbre <anand.ashok.dumbre@xilinx.com>
To: jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de,
	pmeerw@pmeerw.net, michal.simek@xilinx.com, git@xilinx.com,
	linux-iio@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org
Cc: anandash@xilinx.com, Anand Ashok Dumbre <anand.ashok.dumbre@xilinx.com>
Subject: [PATCH] iio: Fixed IIO_VAL_FRACTIONAL calcuation for negative values
Date: Thu, 20 Aug 2020 11:09:44 -0700	[thread overview]
Message-ID: <1597946984-25844-1-git-send-email-anand.ashok.dumbre@xilinx.com> (raw)

This patch fixes IIO_VAL_FRACTIONAL calculation for negative
values where the exponent is 0.

Signed-off-by: Anand Ashok Dumbre <anand.ashok.dumbre@xilinx.com>
---
 drivers/iio/industrialio-core.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index f72c2dc..cd43b17 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -554,6 +554,7 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
 {
        unsigned long long tmp;
        int tmp0, tmp1;
+       s64 tmp2;
        bool scale_db = false;

        switch (type) {
@@ -576,10 +577,13 @@ 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]);
+               tmp2 = 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));
+               tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1);
+               if ((tmp2 < 0) && (tmp0 == 0))
+                       return snprintf(buf, len, "-%d.%09u", tmp0, abs(tmp1));
+               else
+                       return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
        case IIO_VAL_FRACTIONAL_LOG2:
                tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
                tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
--
2.7.4

This email and any attachments are intended for the sole use of the named recipient(s) and contain(s) confidential information that may be proprietary, privileged or copyrighted under applicable law. If you are not the intended recipient, do not read, copy, or forward this email message or any attachments. Delete this email message and any attachments immediately.

             reply	other threads:[~2020-08-20 18:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-20 18:09 Anand Ashok Dumbre [this message]
2020-08-22 10:56 ` Jonathan Cameron
  -- strict thread matches above, loose matches on Subject: below --
2020-08-20  8:31 Anand Ashok Dumbre
2020-08-20 13:09 ` Andy Shevchenko

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=1597946984-25844-1-git-send-email-anand.ashok.dumbre@xilinx.com \
    --to=anand.ashok.dumbre@xilinx.com \
    --cc=anandash@xilinx.com \
    --cc=git@xilinx.com \
    --cc=jic23@kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michal.simek@xilinx.com \
    --cc=pmeerw@pmeerw.net \
    --subject='Re: [PATCH] iio: Fixed IIO_VAL_FRACTIONAL calcuation for negative values' \
    /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

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).