Linux IIO development
 help / color / mirror / Atom feed
From: Antonio Quartulli <antonio@mandelbit.com>
To: David Lechner <dlechner@baylibre.com>,
	Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: linux-iio@vger.kernel.org,
	"Jean-Baptiste Maneyrol" <jean-baptiste.maneyrol@tdk.com>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>
Subject: Re: [PATCH] iio: imu: inv_icm42600: fix 64bit multiplication
Date: Wed, 16 Jul 2025 22:03:19 +0200	[thread overview]
Message-ID: <b27db733-4ad0-4533-a587-391d42ae2af1@mandelbit.com> (raw)
In-Reply-To: <c011f005-de79-4457-804a-d701f315b566@baylibre.com>

On 16/07/2025 21:52, David Lechner wrote:
> On 7/16/25 2:44 PM, Antonio Quartulli wrote:
>> On 16/07/2025 21:36, Andy Shevchenko wrote:
>>> On Wed, Jul 16, 2025 at 10:22 PM Antonio Quartulli
>>> <antonio@mandelbit.com> wrote:
>>>>
>>>> In inv_icm42600_accel_convert_wom_to_roc() multiplying
>>>> `threshold` by `convert` may result in a number requiring more
>>>> than 32bit.
>>>> In this case, although `value` is 64bit wide, the result is
>>>> truncated because the multiplication is performed in the
>>>> 32bit domain, due to both operands being 32bit long.
>>>>
>>>> Cast the first operand to u64 to ensure the multiplication is
>>>> performed in the expected domain.
>>>
>>> Is this a theoretical or practical issue?
>>
>> Can't say if it's practical because I don't know how large `threshold` can be.
> 
> 
> The code is clear that it is between 0 and 255 inclusive.
> 
> unsigned int inv_icm42600_accel_convert_roc_to_wom(...)
> {
> 	...
> 	if (roc == 0)
> 		return 0;
> 	...
> 	return clamp(value, 1, 255);
> }
> 
> threshold = inv_icm42600_accel_convert_roc_to_wom(value, accel_hz, accel_uhz);
> value = inv_icm42600_accel_convert_wom_to_roc(threshold, accel_hz, accel_uhz)

Thanks for pointing this out - I overlooked that.

> 
> So I would not call this a fix, just making the code more robust against
> any changes in the future.

It's the type being used that convinced me something could be off.
Maybe threshold could just be u8 and value be u32? (just to improve self 
documentation)
Also inv_icm42600_accel_convert_roc_to_wom() could return u8 for 
consistency.

I any case, this is a bit code cleanup and a bit bike shedding :-)
I agree there is nothing to "fix" at this point.

Regards,


-- 
Antonio Quartulli

CEO and Co-Founder
Mandelbit Srl
https://www.mandelbit.com


  reply	other threads:[~2025-07-16 20:03 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-16 19:22 [PATCH] iio: imu: inv_icm42600: fix 64bit multiplication Antonio Quartulli
2025-07-16 19:36 ` Andy Shevchenko
2025-07-16 19:44   ` Antonio Quartulli
2025-07-16 19:52     ` David Lechner
2025-07-16 20:03       ` Antonio Quartulli [this message]
2025-07-17  6:21     ` Andy Shevchenko
2025-07-17  7:50       ` Antonio Quartulli
2025-07-17 12:51         ` Andy Shevchenko
2025-07-16 19:38 ` Andy Shevchenko
2025-07-16 19:45   ` Antonio Quartulli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b27db733-4ad0-4533-a587-391d42ae2af1@mandelbit.com \
    --to=antonio@mandelbit.com \
    --cc=andy.shevchenko@gmail.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jean-baptiste.maneyrol@tdk.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox