Re: [PATCH v3 1/3] minmax: Add in_range_inclusive() for inclusive range checks
From: Andy Shevchenko
Date: Tue Sep 01 2026 - 03:28:34 EST
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