public inbox for linux-iio@vger.kernel.org
 help / color / mirror / Atom feed
From: Vasileios Amoiridis <vassilisamir@gmail.com>
To: Jonathan Cameron <jic23@kernel.org>
Cc: Vasileios Amoiridis <vassilisamir@gmail.com>,
	ak@it-klinger.de, phil@raspberrypi.com, lars@metafoo.de,
	andriy.shevchenko@linux.intel.com, ang.iglesiasg@gmail.com,
	mazziesaccount@gmail.com, petre.rodan@subdimension.ro,
	579lpy@gmail.com, linus.walleij@linaro.org,
	semen.protsenko@linaro.org, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org, Adam Rizkalla <ajarizzo@gmail.com>
Subject: Re: [PATCH v8 1/3] iio: pressure: bmp280: Generalize read_*() functions
Date: Wed, 26 Jun 2024 15:41:23 +0200	[thread overview]
Message-ID: <20240626134123.GB26894@vamoiridPC> (raw)
In-Reply-To: <20240625214439.245ae81a@jic23-huawei>

On Tue, Jun 25, 2024 at 09:44:39PM +0100, Jonathan Cameron wrote:
> On Sun, 23 Jun 2024 19:18:41 +0200
> Vasileios Amoiridis <vassilisamir@gmail.com> wrote:
> 
> > On Sun, Jun 23, 2024 at 05:23:30PM +0100, Jonathan Cameron wrote:
> > > On Sat, 22 Jun 2024 14:19:18 +0200
> > > Vasileios Amoiridis <vassilisamir@gmail.com> wrote:
> > >   
> > > > On Sat, Jun 22, 2024 at 10:28:26AM +0100, Jonathan Cameron wrote:  
> > > > > On Tue, 18 Jun 2024 01:05:38 +0200
> > > > > Vasileios Amoiridis <vassilisamir@gmail.com> wrote:
> > > > >     
> > > > > > Add the coefficients for the IIO standard units and the IIO value
> > > > > > inside the chip_info structure.
> > > > > > 
> > > > > > Move the calculations for the IIO unit compatibility from inside the
> > > > > > read_{temp,press,humid}() functions and move them to the general
> > > > > > read_raw() function.
> > > > > > 
> > > > > > In this way, all the data for the calculation of the value are
> > > > > > located in the chip_info structure of the respective sensor.
> > > > > > 
> > > > > > Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com>    
> > > > > Does this incorporate the fix?  I'm a little confused looking at
> > > > > what is visible here, so I'd like Adam to take a look.
> > > > > 
> > > > > Btw, you missed cc'ing Adam.
> > > > >     
> > > > 
> > > > Ah, I only used the output of get_maintainer...  
> > > 
> > > always be careful to sanity check that :)
> > >   
> > > > ...
> > > >     
> > > > > > @@ -518,11 +511,29 @@ static int bmp280_read_raw_impl(struct iio_dev *indio_dev,
> > > > > >  	case IIO_CHAN_INFO_PROCESSED:
> > > > > >  		switch (chan->type) {
> > > > > >  		case IIO_HUMIDITYRELATIVE:
> > > > > > -			return data->chip_info->read_humid(data, val, val2);
> > > > > > +			ret = data->chip_info->read_humid(data, &chan_value);
> > > > > > +			if (ret)
> > > > > > +				return ret;
> > > > > > +
> > > > > > +			*val = data->chip_info->humid_coeffs[0] * chan_value;
> > > > > > +			*val2 = data->chip_info->humid_coeffs[1];
> > > > > > +			return data->chip_info->humid_coeffs_type;
> > > > > >  		case IIO_PRESSURE:
> > > > > > -			return data->chip_info->read_press(data, val, val2);
> > > > > > +			ret = data->chip_info->read_press(data, &chan_value);
> > > > > > +			if (ret)
> > > > > > +				return ret;
> > > > > > +
> > > > > > +			*val = data->chip_info->press_coeffs[0] * chan_value;
> > > > > > +			*val2 = data->chip_info->press_coeffs[1];
> > > > > > +			return data->chip_info->press_coeffs_type;
> > > > > >  		case IIO_TEMP:
> > > > > > -			return data->chip_info->read_temp(data, val, val2);
> > > > > > +			ret = data->chip_info->read_temp(data, &chan_value);
> > > > > > +			if (ret)
> > > > > > +				return ret;
> > > > > > +
> > > > > > +			*val = data->chip_info->temp_coeffs[0] * (s64)chan_value;    
> > > > 
> > > > This is the first difference with the previous version where I incorporated
> > > > the typecasting to (s64).  
> > > 
> > > On a 32 bit platform that will then get pushed into a 32 bit int and overflow
> > > I think.  Back when IIO got started everything was 32 bit so it didn't make sense
> > > to make these 64 bit or indeed to worry about forcing the size.
> > > 
> > > Jonathan
> > >   
> > 
> > Well, I use a 32-bit platform, (BeagleBone Black) and the negative values are
> > handled gracefully with this code. Also, how is that different from the previous
> > code? Instead of doing the typecasting to (s64) in the bmp580_read_temp()
> > function, we do it here. I feel that it is the same piece of code, just in
> > different places.
> 
> The problem, I think, is the storage between those two places is too small.
> It's not about negatives, but rather use that in the extremely large value
> case it might not fit in the 32 bit int behind val so casting that back up
> to 64 bits later won't recover the lost most significant bits.
> 

Well, that is tricky, I didn't think about it. Thanks for noticing.

> > What I can do though, for your peace of mind is the following:
> > 
> > Since the problem with overflow comes when we multiply by 1000 in order to have
> > milli-Celsius, what I can do, is to "force" the division with 2^16 to happen
> > first. In this way, they divided value cannot be overflowed. The division will
> > happen inside the bmp580_read_temp() function and let the multiplication happen
> > later by the IIO code. Then we can drop the (s64) from here.
> 
> See Adam's reply - that should avoid overflow and preserve accuracy which you
> tend to loose with divide then multiply.
> 
> Jonathan
> 

I saw it, and I will apply it, thanks to both of you!

Cheers,
Vasilis

  reply	other threads:[~2024-06-26 13:41 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-17 23:05 [PATCH v8 0/3] Driver cleanup and triggered buffer support Vasileios Amoiridis
2024-06-17 23:05 ` [PATCH v8 1/3] iio: pressure: bmp280: Generalize read_*() functions Vasileios Amoiridis
2024-06-22  9:28   ` Jonathan Cameron
2024-06-22 12:19     ` Vasileios Amoiridis
2024-06-23 16:23       ` Jonathan Cameron
2024-06-23 17:18         ` Vasileios Amoiridis
2024-06-25 20:44           ` Jonathan Cameron
2024-06-26 13:41             ` Vasileios Amoiridis [this message]
2024-06-24  4:26       ` Adam Rizkalla
2024-06-26 13:38         ` Vasileios Amoiridis
2024-06-17 23:05 ` [PATCH v8 2/3] iio: pressure: bmp280: Add SCALE, RAW values in channels and refactorize them Vasileios Amoiridis
2024-06-17 23:05 ` [PATCH v8 3/3] iio: pressure: bmp280: Add triggered buffer support Vasileios Amoiridis
2024-06-22  9:40   ` Jonathan Cameron
2024-06-22 12:32     ` Vasileios Amoiridis
2024-06-22 14:09     ` Vasileios Amoiridis
2024-06-23 16:26       ` Jonathan Cameron
2024-06-23 17:19         ` Vasileios Amoiridis

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=20240626134123.GB26894@vamoiridPC \
    --to=vassilisamir@gmail.com \
    --cc=579lpy@gmail.com \
    --cc=ajarizzo@gmail.com \
    --cc=ak@it-klinger.de \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=ang.iglesiasg@gmail.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linus.walleij@linaro.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mazziesaccount@gmail.com \
    --cc=petre.rodan@subdimension.ro \
    --cc=phil@raspberrypi.com \
    --cc=semen.protsenko@linaro.org \
    /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