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

From: Andy Shevchenko

Date: Fri Oct 02 2026 - 05:26:30 EST


On Thu, Oct 01, 2026 at 03:16:16PM +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.

Reviewed-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxx>

...

> + /* Section 4.9.1 of the DPS310 spec, Horner form to avoid overflow */
> + if (!dps310_horner_step(&prs, data->c30, p, kp) ||
> + !dps310_horner_step(&prs, data->c20, p, kp) ||
> + !dps310_horner_step(&prs, data->c10, p, kp) ||
> + !dps310_horner_step(&tmp, data->c21, p, kp) ||
> + !dps310_horner_step(&tmp, data->c11, p, kp) ||
> + !dps310_horner_step(&tmp, data->c01, t, kt))
> + return -ERANGE;

Sorry for nit-picking, but since the above checks for overflow, perhaps makes
sense to use actually -EOVERFLOW here.

> - pressure += div_s64(rem, 1000000000LL);
> - if (pressure < 0LL)
> + pressure = data->c00 + div64_s64(prs + tmp, DPS310_CALC_ONE);
> + if (pressure < 0)
> return -ERANGE;

--
With Best Regards,
Andy Shevchenko