linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Gregor Boirie <gregor.boirie@parrot.com>,
	linux-iio@vger.kernel.org, Hartmut Knaack <knaack.h@gmx.de>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Peter Meerwald-Stadler <pmeerw@pmeerw.net>
Subject: Re: [PATCH v1 1/1] iio:core: fix IIO_VAL_FRACTIONAL sign handling
Date: Sun, 4 Sep 2016 17:23:00 +0100	[thread overview]
Message-ID: <bbf0670a-fe1c-17f0-1f08-d9fd236df449@kernel.org> (raw)
In-Reply-To: <20160904161128.GA2016@geburah.sephiroth>

On 04/09/16 17:11, Gregor Boirie wrote:
> On Sat, Sep 03, 2016 at 04:13:30PM +0100, Jonathan Cameron wrote:
>> On 02/09/16 19:27, Gregor Boirie wrote:
>>> 7985e7c100 ("iio: Introduce a new fractional value type") introduced a
>>> new IIO_VAL_FRACTIONAL value type meant to represent rational type numbers
>>> expressed by a numerator and denominator combination.
>>>
>>> Formating of IIO_VAL_FRACTIONAL values relies upon do_div() usage. This
>>> fails handling negative values properly since parameters are reevaluated
>>> as unsigned values.
>>> Fix this by using div_s64_rem() instead. Computed integer part will carry
>>> properly signed value. Formatted fractional part will always be positive.
>>>
>>> Fixes: 7985e7c100 ("iio: Introduce a new fractional value type")
>>> Signed-off-by: Gregor Boirie <gregor.boirie@parrot.com>
>>
>> Hi Gregor,
> Hi,
> 
>>
>> While this looks sensible to me, I always gain an almighty headache when
>> I hit the various divide functions.
> So am I.
>>
>> Lars, the fractional code was yours in the first place.
>> If you have time can you sanity check this please.
> This patch may break a lot things indeed. I'd feel much more comfortable if
> someone else could perform additional testing too.
> 
How about adding a patch that adds a case of this to the iio_dummy driver?

That way we can all easily test it.

Jonathan
> Grégor.
>>
>> Jonathan
>>
>>> ---
>>>  drivers/iio/industrialio-core.c | 5 ++---
>>>  1 file changed, 2 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
>>> index f914d5d..d2b8899 100644
>>> --- a/drivers/iio/industrialio-core.c
>>> +++ b/drivers/iio/industrialio-core.c
>>> @@ -613,9 +613,8 @@ ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
>>>  			return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
>>>  	case IIO_VAL_FRACTIONAL:
>>>  		tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
>>> -		vals[1] = do_div(tmp, 1000000000LL);
>>> -		vals[0] = tmp;
>>> -		return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
>>> +		vals[0] = (int)div_s64_rem(tmp, 1000000000, &vals[1]);
>>> +		return sprintf(buf, "%d.%09u\n", vals[0], abs(vals[1]));
>>>  	case IIO_VAL_FRACTIONAL_LOG2:
>>>  		tmp = (s64)vals[0] * 1000000000LL >> vals[1];
>>>  		vals[1] = do_div(tmp, 1000000000LL);
>>>
>>


  reply	other threads:[~2016-09-04 16:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-02 18:27 [PATCH v1 1/1] iio:core: fix IIO_VAL_FRACTIONAL sign handling Gregor Boirie
2016-09-03 15:13 ` Jonathan Cameron
2016-09-04 16:11   ` Gregor Boirie
2016-09-04 16:23     ` Jonathan Cameron [this message]
2016-09-05  7:59   ` Lars-Peter Clausen
2016-09-05 20:24     ` Jonathan Cameron
2016-09-07 18:11       ` Gregor Boirie
2016-09-07 18:54         ` Lars-Peter Clausen
2016-09-08  8:21           ` Gregor Boirie

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=bbf0670a-fe1c-17f0-1f08-d9fd236df449@kernel.org \
    --to=jic23@kernel.org \
    --cc=gregor.boirie@parrot.com \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --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 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).