linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio: Fixed IIO_VAL_FRACTIONAL calcuation for negative values
@ 2020-08-20 18:09 Anand Ashok Dumbre
  2020-08-22 10:56 ` Jonathan Cameron
  0 siblings, 1 reply; 4+ messages in thread
From: Anand Ashok Dumbre @ 2020-08-20 18:09 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, michal.simek, git, linux-iio,
	linux-arm-kernel, linux-kernel
  Cc: anandash, Anand Ashok Dumbre

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.

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] iio: Fixed IIO_VAL_FRACTIONAL calcuation for negative values
  2020-08-20 18:09 [PATCH] iio: Fixed IIO_VAL_FRACTIONAL calcuation for negative values Anand Ashok Dumbre
@ 2020-08-22 10:56 ` Jonathan Cameron
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2020-08-22 10:56 UTC (permalink / raw)
  To: Anand Ashok Dumbre
  Cc: knaack.h, lars, pmeerw, michal.simek, git, linux-iio,
	linux-arm-kernel, linux-kernel, anandash

On Thu, 20 Aug 2020 11:09:44 -0700
Anand Ashok Dumbre <anand.ashok.dumbre@xilinx.com> wrote:

> 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>
Hi,

I would thrown in an example for this description. 
Also useful to know if there is a existing driver that is broken
and hence we need to backport this.
If that is the case, please try to figure out an appropriate fixes
tag.

Comments inline + the email footer issue needs sorting out.

Thanks,

Jonathan

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

Given tmp0 == 0, this might be clearer as
			  return snprintf(buf, len, "-0.%09u", 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.

As Andy said, this footer is a problem...  

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] iio: Fixed IIO_VAL_FRACTIONAL calcuation for negative values
  2020-08-20  8:31 Anand Ashok Dumbre
@ 2020-08-20 13:09 ` Andy Shevchenko
  0 siblings, 0 replies; 4+ messages in thread
From: Andy Shevchenko @ 2020-08-20 13:09 UTC (permalink / raw)
  To: Anand Ashok Dumbre
  Cc: Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald, Michal Simek, git, linux-iio,
	linux-arm Mailing List, Linux Kernel Mailing List

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.

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] iio: Fixed IIO_VAL_FRACTIONAL calcuation for negative values
@ 2020-08-20  8:31 Anand Ashok Dumbre
  2020-08-20 13:09 ` Andy Shevchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Anand Ashok Dumbre @ 2020-08-20  8:31 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, michal.simek, git, linux-iio,
	linux-arm-kernel, linux-kernel
  Cc: Anand Ashok Dumbre

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.

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-08-22 10:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-20 18:09 [PATCH] iio: Fixed IIO_VAL_FRACTIONAL calcuation for negative values Anand Ashok Dumbre
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

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