[PATCH] iio: imu: use min() to improve code

Qianfeng Rong posted 1 patch 1 month, 2 weeks ago
There is a newer version of this series
drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c | 11 +++--------
1 file changed, 3 insertions(+), 8 deletions(-)
[PATCH] iio: imu: use min() to improve code
Posted by Qianfeng Rong 1 month, 2 weeks ago
Use min() to reduce code in inv_icm42600_buffer_update_fifo_period()
and inv_icm42600_buffer_update_watermark(), and improve readability.

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c b/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
index 7c4ed981db04..91d166de1231 100644
--- a/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
+++ b/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
@@ -9,6 +9,7 @@
 #include <linux/pm_runtime.h>
 #include <linux/regmap.h>
 #include <linux/delay.h>
+#include <linux/minmax.h>
 
 #include <linux/iio/buffer.h>
 #include <linux/iio/common/inv_sensors_timestamp.h>
@@ -112,10 +113,7 @@ void inv_icm42600_buffer_update_fifo_period(struct inv_icm42600_state *st)
 	else
 		period_accel = U32_MAX;
 
-	if (period_gyro <= period_accel)
-		period = period_gyro;
-	else
-		period = period_accel;
+	period = min(period_gyro, period_accel);
 
 	st->fifo.period = period;
 }
@@ -238,10 +236,7 @@ int inv_icm42600_buffer_update_watermark(struct inv_icm42600_state *st)
 		else
 			latency = latency_accel - (latency_gyro % latency_accel);
 		/* use the shortest period */
-		if (period_gyro <= period_accel)
-			period = period_gyro;
-		else
-			period = period_accel;
+		period = min(period_gyro, period_accel);
 		/* all this works because periods are multiple of each others */
 		watermark = latency / period;
 		if (watermark < 1)
-- 
2.34.1
Re: [PATCH] iio: imu: use min() to improve code
Posted by David Lechner 1 month, 2 weeks ago
On 8/15/25 3:06 AM, Qianfeng Rong wrote:
> Use min() to reduce code in inv_icm42600_buffer_update_fifo_period()
> and inv_icm42600_buffer_update_watermark(), and improve readability.
> 
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> ---
>  drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c | 11 +++--------
>  1 file changed, 3 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c b/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
> index 7c4ed981db04..91d166de1231 100644
> --- a/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
> +++ b/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
> @@ -9,6 +9,7 @@
>  #include <linux/pm_runtime.h>
>  #include <linux/regmap.h>
>  #include <linux/delay.h>
> +#include <linux/minmax.h>

Would be nice to put this in alphabetical order the best we can.

>  
>  #include <linux/iio/buffer.h>
>  #include <linux/iio/common/inv_sensors_timestamp.h>
> @@ -112,10 +113,7 @@ void inv_icm42600_buffer_update_fifo_period(struct inv_icm42600_state *st)
>  	else
>  		period_accel = U32_MAX;
>  
> -	if (period_gyro <= period_accel)
> -		period = period_gyro;
> -	else
> -		period = period_accel;
> +	period = min(period_gyro, period_accel);
>  
>  	st->fifo.period = period;

Might as well just drop the local variable in this one and save
another line (and a blank line).

>  }
> @@ -238,10 +236,7 @@ int inv_icm42600_buffer_update_watermark(struct inv_icm42600_state *st)
>  		else
>  			latency = latency_accel - (latency_gyro % latency_accel);
>  		/* use the shortest period */

We could remove the comment as well since it should be
obvious from the code.

> -		if (period_gyro <= period_accel)
> -			period = period_gyro;
> -		else
> -			period = period_accel;
> +		period = min(period_gyro, period_accel);
>  		/* all this works because periods are multiple of each others */
>  		watermark = latency / period;

Could probably also drop the local variable here and still fit everything on one line.

>  		if (watermark < 1)
Re: [PATCH] iio: imu: use min() to improve code
Posted by Qianfeng Rong 1 month, 2 weeks ago
在 2025/8/15 22:25, David Lechner 写道:
> On 8/15/25 3:06 AM, Qianfeng Rong wrote:
>> Use min() to reduce code in inv_icm42600_buffer_update_fifo_period()
>> and inv_icm42600_buffer_update_watermark(), and improve readability.
>>
>> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
>> ---
>>   drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c | 11 +++--------
>>   1 file changed, 3 insertions(+), 8 deletions(-)
>>
>> diff --git a/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c b/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
>> index 7c4ed981db04..91d166de1231 100644
>> --- a/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
>> +++ b/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
>> @@ -9,6 +9,7 @@
>>   #include <linux/pm_runtime.h>
>>   #include <linux/regmap.h>
>>   #include <linux/delay.h>
>> +#include <linux/minmax.h>
> Would be nice to put this in alphabetical order the best we can.
Thanks! Good to know this detail.
>
>>   
>>   #include <linux/iio/buffer.h>
>>   #include <linux/iio/common/inv_sensors_timestamp.h>
>> @@ -112,10 +113,7 @@ void inv_icm42600_buffer_update_fifo_period(struct inv_icm42600_state *st)
>>   	else
>>   		period_accel = U32_MAX;
>>   
>> -	if (period_gyro <= period_accel)
>> -		period = period_gyro;
>> -	else
>> -		period = period_accel;
>> +	period = min(period_gyro, period_accel);
>>   
>>   	st->fifo.period = period;
> Might as well just drop the local variable in this one and save
> another line (and a blank line).
It is indeed better to drop the local variable.
>
>>   }
>> @@ -238,10 +236,7 @@ int inv_icm42600_buffer_update_watermark(struct inv_icm42600_state *st)
>>   		else
>>   			latency = latency_accel - (latency_gyro % latency_accel);
>>   		/* use the shortest period */
> We could remove the comment as well since it should be
> obvious from the code.
ok :)
>
>> -		if (period_gyro <= period_accel)
>> -			period = period_gyro;
>> -		else
>> -			period = period_accel;
>> +		period = min(period_gyro, period_accel);
>>   		/* all this works because periods are multiple of each others */
>>   		watermark = latency / period;
> Could probably also drop the local variable here and still fit everything on one line.
I will try to do this in the next version.
>
>>   		if (watermark < 1)
Best regards,
Qianfeng