[PATCH next 01/14] overflow: Reduce expansion of __type_max()

david.laight.linux@gmail.com posted 14 patches 2 weeks, 3 days ago
[PATCH next 01/14] overflow: Reduce expansion of __type_max()
Posted by david.laight.linux@gmail.com 2 weeks, 3 days ago
From: David Laight <david.laight.linux@gmail.com>

Change '(x - 1) + x' to '2 * (x - 1) + 1' to avoid expanding the
non-trivial __type_half_max() twice.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
 include/linux/overflow.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index 736f633b2d5f..4f014d55ab25 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -31,7 +31,7 @@
  * credit to Christian Biere.
  */
 #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
-#define __type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
+#define __type_max(T) ((T)(2 * (__type_half_max(T) - 1) + 1))
 #define type_max(t)	__type_max(typeof(t))
 #define __type_min(T) ((T)((T)-type_max(T)-(T)1))
 #define type_min(t)	__type_min(typeof(t))
-- 
2.39.5
Re: [PATCH next 01/14] overflow: Reduce expansion of __type_max()
Posted by Yury Norov 5 days, 4 hours ago
On Wed, Jan 21, 2026 at 02:57:18PM +0000, david.laight.linux@gmail.com wrote:
> From: David Laight <david.laight.linux@gmail.com>
> 
> Change '(x - 1) + x' to '2 * (x - 1) + 1' to avoid expanding the
> non-trivial __type_half_max() twice.
> 
> Signed-off-by: David Laight <david.laight.linux@gmail.com>

Thanks!

Reviewed-by: Yury Norov <ynorov@nvidia.com>

> ---
>  include/linux/overflow.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> index 736f633b2d5f..4f014d55ab25 100644
> --- a/include/linux/overflow.h
> +++ b/include/linux/overflow.h
> @@ -31,7 +31,7 @@
>   * credit to Christian Biere.
>   */
>  #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
> -#define __type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
> +#define __type_max(T) ((T)(2 * (__type_half_max(T) - 1) + 1))
>  #define type_max(t)	__type_max(typeof(t))
>  #define __type_min(T) ((T)((T)-type_max(T)-(T)1))
>  #define type_min(t)	__type_min(typeof(t))
> -- 
> 2.39.5
Re: [PATCH next 01/14] overflow: Reduce expansion of __type_max()
Posted by Kees Cook 2 weeks, 3 days ago
On Wed, Jan 21, 2026 at 02:57:18PM +0000, david.laight.linux@gmail.com wrote:
> From: David Laight <david.laight.linux@gmail.com>
> 
> Change '(x - 1) + x' to '2 * (x - 1) + 1' to avoid expanding the
> non-trivial __type_half_max() twice.
> 
> Signed-off-by: David Laight <david.laight.linux@gmail.com>

Sure!

Reviewed-by: Kees Cook <kees@kernel.org>

-- 
Kees Cook