Re: [PATCH] iio: pressure: dps310: fix overflow in pressure compensation

From: Andy Shevchenko

Date: Mon Sep 28 2026 - 18:00:55 EST


On Mon, Sep 28, 2026 at 01:18:03PM +0300, Rupesh Majhi wrote:
> p^3 * c30 and t * p^2 * c21 overflow s64 at most oversampling ratios,
> and pressure comes out about 150 Pa low on a DPS310 here.
>
> Use Horner form, dividing by scale factor at each step. Overflow then
> needs a raw value far outside sensor range and returns -ERANGE.
>
> Checked against datasheet formula at all eight ratios.

...

> +/* Fixed point one for the compensation fractions */
> +#define DPS310_CALC_ONE ((s64)BIT_ULL(20))

What's the point in having _ULL in this case?

...

> - *val = (int)min_t(s64, pressure, INT_MAX);
> + *val = min_t(s64, pressure, INT_MAX);

Avoid using min_t(). It's prone to errors that will be hard to debug.

--
With Best Regards,
Andy Shevchenko