Re: [PATCH 07/18] iio: adc: ad4170-4: Fix overflow in sample rate fractional part
From: Andy Shevchenko
Date: Thu Dec 03 2026 - 12:13:32 EST
On Thu, Dec 03, 2026 at 04:19:47PM +0100, Janani Sunil wrote:
> ad4170_fill_sps_tbl() computes the fractional part of each sample rate
> with mult_frac(), which works in the type of its first argument. The
> remainder times MICRO overflows unsigned int for remainders above 4294,
> so the fractional part reported for FILTER_FS 65532 at the 16 MHz main
> clock is 7.001085 instead of 7.629860. A slower main clock makes more
> entries overflow.
>
> Compute the fractional part with 64-bit arithmetic.
...
> tmp0 = div_u64_rem(st->mclk_hz, 32 * ad4170_sinc3_filt_fs_tbl[i],
> &tmp1);
> - tmp1 = mult_frac(tmp1, MICRO, 32 * ad4170_sinc3_filt_fs_tbl[i]);
> + tmp1 = div_u64((u64)tmp1 * MICRO,
> + 32 * ad4170_sinc3_filt_fs_tbl[i]);
A single line is okay in this case.
tmp1 = div_u64((u64)tmp1 * MICRO, 32 * ad4170_sinc3_filt_fs_tbl[i]);
...
> for (i = 0; i < ARRAY_SIZE(ad4170_sinc5_filt_fs_tbl); i++) {
> tmp0 = div_u64_rem(st->mclk_hz, 32 * ad4170_sinc5_filt_fs_tbl[i],
> &tmp1);
> - tmp1 = mult_frac(tmp1, MICRO, 32 * ad4170_sinc5_filt_fs_tbl[i]);
> + tmp1 = div_u64((u64)tmp1 * MICRO,
> + 32 * ad4170_sinc5_filt_fs_tbl[i]);
Ditto.
--
With Best Regards,
Andy Shevchenko