All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Guru Das Srinagesh <linux@gurudas.dev>
Cc: "Alex Lanzano" <lanzano.alex@gmail.com>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Matthew Wilcox" <willy@infradead.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Gustavo Silva" <gustavograzs@gmail.com>,
	linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org
Subject: Re: [PATCH v3 1/3] minmax: Add in_range_inclusive() for inclusive range checks
Date: Tue, 1 Sep 2026 10:26:15 +0300	[thread overview]
Message-ID: <apZ-FxfpHTso57XR@ashevche-desk.local> (raw)
In-Reply-To: <20260831-minmax-in-range-incl-v3-1-30444a2fd2a5@gurudas.dev>

On Mon, Aug 31, 2026 at 09:26:28AM -0700, Guru Das Srinagesh wrote:
> Extend the logic in in_range() to support checking for an inclusive
> range [min, max].
> 
> The condition in the check is derived as follows, starting from the
> in_range() macro with len = (max - min + 1):
> 
>     (val - min) <  (max - min + 1) // overflows for [0, U32/U64_MAX]
>     (val - min) <= (max - min)     // no overflow
> 
> The behaviour of the macro from the signedness perspective is documented
> in the kernel-doc and in the in_range_inclusive KUnit test suite.

...

> +/**
> + * in_range_inclusive - Determine if a value lies within an inclusive range.
> + * @val: Value to test.

> + * @min: First value in range.
> + * @max: Last value in range.

We have macros named min() and max(), since this is a macro as well it might
give an interesting outcome when two collide. Suggestion is to rename the
parameters to avoid potential collisions.

> + * This checks if a value lies within the closed range of [@min, @max]. Note that
> + * "range" refers to values counting up from @min with wraparound at
> + * unsigned-datatype max if encountered, continuing on till @max is reached.
> + *
> + * This macro is not a drop-in replacement for "if (val >= min && val <= max)".
> + * Unsigned arithmetic determines what the 'true' range exactly is depending on
> + * whether @min <= @max holds, and in which reading (signed vs unsigned) as follows::
> + *
> + *     Valid in reading   Example    'True' range is
> + *     Both readings      [5, 10]     interval as written in either reading
> + *     Signed only        [-10, 5]    signed interval
> + *     Unsigned only      [5, -10]    unsigned interval
> + *     Neither reading    [-5, -10]   neither; all values except [unsigned(-9), unsigned(-6)]
> + *
> + * The last two cases provide "surprising" results and are to be used carefully, if
> + * at all. Further, if @max = @min - 1, every @val is in range.
> + *
> + * Return: true or false as described above.
> + */

Imagine something like in_range_inclusive(value, min(A, B), max(C, D)) case
which I consider plausible to happen (in some form).

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2026-09-01  7:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-31 16:26 [PATCH v3 0/3] Introduce in_range_incl() inclusive range check macro Guru Das Srinagesh
2026-08-31 16:26 ` [PATCH v3 1/3] minmax: Add in_range_inclusive() for inclusive range checks Guru Das Srinagesh
2026-09-01  7:26   ` Andy Shevchenko [this message]
2026-08-31 16:26 ` [PATCH v3 2/3] lib/tests: Add in_range_inclusive() KUnit test Guru Das Srinagesh
2026-09-01  7:38   ` Andy Shevchenko
2026-08-31 16:26 ` [PATCH v3 3/3] iio: imu: bmi270: Use in_range_inclusive() in bmi270_write_event_value() Guru Das Srinagesh
2026-09-01  7:47   ` Andy Shevchenko

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=apZ-FxfpHTso57XR@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=gustavograzs@gmail.com \
    --cc=jic23@kernel.org \
    --cc=lanzano.alex@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@gurudas.dev \
    --cc=nuno.sa@analog.com \
    --cc=willy@infradead.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.