Re: [PATCH v3 5/5] iio: dac: ltc2664: Add driver for LTC2664 and LTC2672

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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);




[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Input]     [Linux Kernel]     [Linux SCSI]     [X.org]

  Powered by Linux