[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <0a9920f8-8546-1138-78d8-755f0a43c930@metafoo.de>
Date: Wed, 21 Aug 2019 22:09:21 +0200
From: Lars-Peter Clausen <lars@...afoo.de>
To: Alexander Stein <alexander.stein@...tec-electronic.com>,
Jonathan Cameron <jic23@...nel.org>,
Hartmut Knaack <knaack.h@....de>,
Peter Meerwald-Stadler <pmeerw@...erw.net>
Cc: linux-iio@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH 1/1] iio: core: Fix fractional format generation
On 8/21/19 5:50 PM, Alexander Stein wrote:
> In case the result is -0.3252 tmp0 is 0 after the div_s64_rem, so tmp0 is
> non-negative which results in an output of 0.3252.
> Fix this by explicitly handling the negative sign ourselves.
Hi,
Thanks for you patch. Some comments inline.
>
> Signed-off-by: Alexander Stein <alexander.stein@...tec-electronic.com>
> ---
> drivers/iio/industrialio-core.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index 245b5844028d..18350c1959ae 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -568,6 +568,7 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
> {
> unsigned long long tmp;
> int tmp0, tmp1;
> + const char *sign = vals[0] < 0 ? "-" : "";
> bool scale_db = false;
>
> switch (type) {
> @@ -593,11 +594,11 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
> 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 snprintf(buf, len, "%s%u.%09u", sign, abs(tmp0), abs(tmp1));
I think this breaks the case where vals[1] is negative, but vals[0] is
positive. Maybe we can use a similar approach as for
IIO_VAL_INT_PLUS_NANO. Maybe even put this into a small helper function.
> 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 snprintf(buf, len, "%s%u.%09u", sign, abs(tmp0), abs(tmp1));
> case IIO_VAL_INT_MULTIPLE:
> {
> int i;
>
Powered by blists - more mailing lists