Re: [PATCH v2] hwmon: (corsair-psu) Fix linear11 calculation

Guenter Roeck <[email protected]>
Newsgroups org.kernel.vger.linux-hwmon
Message-ID <[email protected]>
On 8/6/26 08:56, Wilken Gottwalt wrote:
...
>>>> +	s64 result = mant * scale;
>>>
>>> Uhm, this is a 32bit multiplicaiton, actully a C gotcha I explain the beginners
>>> in our company. https://godbolt.org/z/eM6TbGG5E
>>>
>>
>> It is, but that is ok and intentional: both mant and exp are guaranteed to be
>> no larger than s16, meaning the result is never larger than s32 and will never
>> overflow.
>>
>>>> -	return (exp >= 0) ? (result << exp) : (result >> -exp);
>>>> +	if (exp >= 0)
>>>> +		result *= (int)(1UL << exp);
>>
>> This is the calculation that can overflow, making it necessary for result to be s64.
> 
> Yeah, it was just funny to see in the wild. It made my day. :D
> 

Guess I lost you there. Do you want me to change it ? I could add a comment, or just
type cast mant to s64. Please let me know.

Thanks,
Guenter
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.