public inbox for linux-nvme@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH] nvme: replace max(a, min(b, c)) by clamp(b, a, c)
@ 2025-03-17  7:39 shao.mingyin
  2025-03-18  2:57 ` Chaitanya Kulkarni
  2025-03-19 21:00 ` David Laight
  0 siblings, 2 replies; 5+ messages in thread
From: shao.mingyin @ 2025-03-17  7:39 UTC (permalink / raw)
  To: hch; +Cc: sagi, kch, linux-nvme, linux-kernel, yang.tao172, yang.yang29,
	xu.xin16

From: LiHaoran <li.haoran7@zte.com.cn>

This patch replaces max(a, min(b, c)) by clamp(b, a, c) in the nvme
driver. This improves the readability.

Signed-off-by: LiHaoran <li.haoran7@zte.com.cn>
Cc: ShaoMingyin <shao.mingyin@zte.com.cn>
---
 drivers/nvme/target/nvmet.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index fcf4f460dc9a..30804b0ca66e 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -819,7 +819,7 @@ static inline u8 nvmet_cc_iocqes(u32 cc)
 /* Convert a 32-bit number to a 16-bit 0's based number */
 static inline __le16 to0based(u32 a)
 {
-	return cpu_to_le16(max(1U, min(1U << 16, a)) - 1);
+	return cpu_to_le16(clamp(1U << 16, 1U, a) - 1);
 }

 static inline bool nvmet_ns_has_pi(struct nvmet_ns *ns)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] nvme: replace max(a, min(b, c)) by clamp(b, a, c)
       [not found] <20250317170938569jgM2gAWy39rgQQbnOh0Vu@zte.com.cn>
@ 2025-03-17  9:31 ` Krzysztof Kozlowski
  0 siblings, 0 replies; 5+ messages in thread
From: Krzysztof Kozlowski @ 2025-03-17  9:31 UTC (permalink / raw)
  To: shao.mingyin, hch
  Cc: sagi, kch, linux-nvme, linux-kernel, yang.tao172, yang.yang29,
	xu.xin16

On 17/03/2025 10:09, shao.mingyin@zte.com.cn wrote:
> Dear best,
> Please ignore this patch. We will submit a v2 patch instead.
No. Instead wait, read feedback given to all of your 60 @zte.com.cn
patches and implement all this feedback. Then send 1, 2 or 3 patches for
entire zte.com.cn to see how maintainers will respond.

Best regards,
Krzysztof


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] nvme: replace max(a, min(b, c)) by clamp(b, a, c)
  2025-03-17  7:39 shao.mingyin
@ 2025-03-18  2:57 ` Chaitanya Kulkarni
  2025-03-19 21:00 ` David Laight
  1 sibling, 0 replies; 5+ messages in thread
From: Chaitanya Kulkarni @ 2025-03-18  2:57 UTC (permalink / raw)
  To: shao.mingyin@zte.com.cn
  Cc: sagi@grimberg.me, Chaitanya Kulkarni,
	linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org,
	yang.tao172@zte.com.cn, yang.yang29@zte.com.cn, hch@lst.de,
	xu.xin16@zte.com.cn

On 3/17/25 00:39, shao.mingyin@zte.com.cn wrote:
> From: LiHaoran <li.haoran7@zte.com.cn>
>
> This patch replaces max(a, min(b, c)) by clamp(b, a, c) in the nvme
> driver. This improves the readability.
>
> Signed-off-by: LiHaoran <li.haoran7@zte.com.cn>
> Cc: ShaoMingyin <shao.mingyin@zte.com.cn>
> ---
>   drivers/nvme/target/nvmet.h | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
> index fcf4f460dc9a..30804b0ca66e 100644
> --- a/drivers/nvme/target/nvmet.h
> +++ b/drivers/nvme/target/nvmet.h
> @@ -819,7 +819,7 @@ static inline u8 nvmet_cc_iocqes(u32 cc)
>   /* Convert a 32-bit number to a 16-bit 0's based number */
>   static inline __le16 to0based(u32 a)
>   {
> -	return cpu_to_le16(max(1U, min(1U << 16, a)) - 1);
> +	return cpu_to_le16(clamp(1U << 16, 1U, a) - 1);

I've gotten used to the min/max or max/min combination,
but if others prefer something else, sure go for it.

-ck



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] nvme: replace max(a, min(b, c)) by clamp(b, a, c)
  2025-03-17  7:39 shao.mingyin
  2025-03-18  2:57 ` Chaitanya Kulkarni
@ 2025-03-19 21:00 ` David Laight
  2025-03-19 21:33   ` David Laight
  1 sibling, 1 reply; 5+ messages in thread
From: David Laight @ 2025-03-19 21:00 UTC (permalink / raw)
  To: shao.mingyin
  Cc: hch, sagi, kch, linux-nvme, linux-kernel, yang.tao172,
	yang.yang29, xu.xin16

On Mon, 17 Mar 2025 15:39:09 +0800 (CST)
<shao.mingyin@zte.com.cn> wrote:

> From: LiHaoran <li.haoran7@zte.com.cn>
> 
> This patch replaces max(a, min(b, c)) by clamp(b, a, c) in the nvme
> driver. This improves the readability.
> 
> Signed-off-by: LiHaoran <li.haoran7@zte.com.cn>
> Cc: ShaoMingyin <shao.mingyin@zte.com.cn>
> ---
>  drivers/nvme/target/nvmet.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
> index fcf4f460dc9a..30804b0ca66e 100644
> --- a/drivers/nvme/target/nvmet.h
> +++ b/drivers/nvme/target/nvmet.h
> @@ -819,7 +819,7 @@ static inline u8 nvmet_cc_iocqes(u32 cc)
>  /* Convert a 32-bit number to a 16-bit 0's based number */
>  static inline __le16 to0based(u32 a)
>  {
> -	return cpu_to_le16(max(1U, min(1U << 16, a)) - 1);
> +	return cpu_to_le16(clamp(1U << 16, 1U, a) - 1);

Swap the arguments into a sane order - clamp(a, 1, 1 << 16)

	David

>  }
> 
>  static inline bool nvmet_ns_has_pi(struct nvmet_ns *ns)



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] nvme: replace max(a, min(b, c)) by clamp(b, a, c)
  2025-03-19 21:00 ` David Laight
@ 2025-03-19 21:33   ` David Laight
  0 siblings, 0 replies; 5+ messages in thread
From: David Laight @ 2025-03-19 21:33 UTC (permalink / raw)
  To: shao.mingyin
  Cc: hch, sagi, kch, linux-nvme, linux-kernel, yang.tao172,
	yang.yang29, xu.xin16

On Wed, 19 Mar 2025 21:00:32 +0000
David Laight <david.laight.linux@gmail.com> wrote:

> On Mon, 17 Mar 2025 15:39:09 +0800 (CST)
> <shao.mingyin@zte.com.cn> wrote:
> 
> > From: LiHaoran <li.haoran7@zte.com.cn>
> > 
> > This patch replaces max(a, min(b, c)) by clamp(b, a, c) in the nvme
> > driver. This improves the readability.
> > 
> > Signed-off-by: LiHaoran <li.haoran7@zte.com.cn>
> > Cc: ShaoMingyin <shao.mingyin@zte.com.cn>
> > ---
> >  drivers/nvme/target/nvmet.h | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
> > index fcf4f460dc9a..30804b0ca66e 100644
> > --- a/drivers/nvme/target/nvmet.h
> > +++ b/drivers/nvme/target/nvmet.h
> > @@ -819,7 +819,7 @@ static inline u8 nvmet_cc_iocqes(u32 cc)
> >  /* Convert a 32-bit number to a 16-bit 0's based number */
> >  static inline __le16 to0based(u32 a)
> >  {
> > -	return cpu_to_le16(max(1U, min(1U << 16, a)) - 1);
> > +	return cpu_to_le16(clamp(1U << 16, 1U, a) - 1);  
> 
> Swap the arguments into a sane order - clamp(a, 1, 1 << 16)

To clarify further it is clamp(val, lo, hi) and is only well defined if 'lo <= hi'.
In particular the order of the comparisons is not defined.
It might be val > hi ? hi : val < lo ? lo : val.
Which won't give the value you expect when 'a' is zero.

> 
> 	David
> 
> >  }
> > 
> >  static inline bool nvmet_ns_has_pi(struct nvmet_ns *ns)  
> 



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-03-19 21:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20250317170938569jgM2gAWy39rgQQbnOh0Vu@zte.com.cn>
2025-03-17  9:31 ` [PATCH] nvme: replace max(a, min(b, c)) by clamp(b, a, c) Krzysztof Kozlowski
2025-03-17  7:39 shao.mingyin
2025-03-18  2:57 ` Chaitanya Kulkarni
2025-03-19 21:00 ` David Laight
2025-03-19 21:33   ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox