Re: [PATCH v3 5/5] iio: dac: ltc2664: Add driver for LTC2664 and LTC2672
From: David Lechner
Date: Tue Jun 18 2024 - 10:11:38 EST
On 6/18/24 5:32 AM, Paller, Kim Seer wrote:
>
>
>>> +}
>>> +
>>> +static int ltc2672_scale_get(const struct ltc2664_state *st, int c)
>>> +{
>>> + const struct ltc2664_chan *chan = &st->channels[c];
>>> + int span, fs;
>>> +
>>> + span = chan->span;
>>> + if (span < 0)
>>> + return span;
>>> +
>>> + fs = 1000 * st->vref / st->rfsadj;
>>> +
>>> + if (span == LTC2672_MAX_SPAN)
>>> + return 4800 * fs;
>>> +
>>> + return LTC2672_SCALE_MULTIPLIER(span) * fs;
>>
>> Are we losing accuracy by multiplying after dividing here as well?
>
> Hi,
>
> In the case of max span for ltc2672, I found that performing multiplication
> before division causes an integer overflow during testing. I was wondering
> how the upstream handles this case. Could you provide some advice?
>
> Thanks,
> Kim
>
>
In cases like this, we usually do 64-bit multiplication to avoid the
overflow. There are helper functions for this sort of thing in
linux/math64.h.
For example, if LTC2672_SCALE_MULTIPLIER(span) is unsigned, you
could probably do something like this:
mul_u64_u32_div(LTC2672_SCALE_MULTIPLIER(span), 1000 * st->vref, st->rfsadj);