diff mbox series

iio: Fixed IIO_VAL_FRACTIONAL calcuation for negative values

Message ID 1597912282-31254-1-git-send-email-anand.ashok.dumbre@xilinx.com (mailing list archive)
State New, archived
Headers show
Series iio: Fixed IIO_VAL_FRACTIONAL calcuation for negative values | expand

Commit Message

Anand Ashok Dumbre Aug. 20, 2020, 8:31 a.m. UTC
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(-)

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

Comments

Andy Shevchenko Aug. 20, 2020, 1:09 p.m. UTC | #1
On Thu, Aug 20, 2020 at 11:32 AM Anand Ashok Dumbre
<anand.ashok.dumbre@xilinx.com> wrote:

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

This is not the footer that allows you to do open source.
diff mbox series

Patch

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