Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp20901rdb; Sun, 28 Jan 2024 11:28:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IHXD4IOdM+t6RiQKeMbknWMOaHpsqHHld8AZCGu3yUmGPpIOcsQ5UZSEdh79POtp9Xk6rk0 X-Received: by 2002:a17:906:d111:b0:a35:74f2:b9c9 with SMTP id b17-20020a170906d11100b00a3574f2b9c9mr1680964ejz.29.1706470108714; Sun, 28 Jan 2024 11:28:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706470108; cv=pass; d=google.com; s=arc-20160816; b=N/j3sKCfEf9Gg1lUIsj3VIw3NZHpZh7Z8HGKK6yK5V3wK3v3wrjqoNJ/uYtJCEZjH/ BiEe/GSk5XxAToypQ1t6xE1GB3XTPKPf67kRDztdbOU1KxIQleKNwUzdFEdmKA4ChppY ZxB7tbc56/+gMnCU4o7EpEoV1Sm2rKRhEYxHY2Wy8l27ahexmypS3Q4y0KtORPAAWpUQ IP7o7udfkb/+H2zsC9C7DrAx3KOQ6EE8mR0H18HmUEWRmpueU1PlbwTJGhgZ4FFuAjUX 8GbsMWYyT8ykdt+Eaaks/8GABdXeT7qPxXT8j5KBzxvtzFIgE9j57qA5EVZkLBRtwCme U3dA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:accept-language :in-reply-to:references:message-id:date:thread-index:thread-topic :subject:cc:to:from; bh=kEKJY0u4pHs7Zoau1E3WuqR5u8eF2jCI+x+xlkNWEEg=; fh=jAOu/RGlQxfhPk7PcGNzOfYLHKZO+STAaG1daOB/TAU=; b=G9BRimHzBVSaVoN8txr3fniDXe8b4qqft3pOPNFFd11QVEaXxtVabpfudKQzBEiPF8 REV81TYZUiBJ9qL+JhhQndmLkgKfhkDd27GbaF9UrOidV+3Lcp1HVpKosSAwKgHAAM20 RJMCL7uZ4/STYxWLdYJ4c74xKvWOW6eaWOfjvNnlbqEWd6DucWCK7IdTk+2UDjv5g3a+ cjB1sStZSHR2CXVdu7z5C5D9MwES6eY6DkNDIkX0bmSLXVZPtccAiCTtag0scxa5IpQu y1ewgh392/RNUtTbUKSZenZPa2ZnvozJi5CaRVdgQaKztStRF4BfjHjIdRl3/pb8Z7Hl J9KA== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=aculab.com dmarc=pass fromdomain=aculab.com); spf=pass (google.com: domain of linux-kernel+bounces-41900-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41900-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id d15-20020a170906040f00b00a318449f138si2637740eja.1021.2024.01.28.11.28.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jan 2024 11:28:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41900-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=aculab.com dmarc=pass fromdomain=aculab.com); spf=pass (google.com: domain of linux-kernel+bounces-41900-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41900-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 75D4B1F220BA for ; Sun, 28 Jan 2024 19:28:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9262633CD6; Sun, 28 Jan 2024 19:28:20 +0000 (UTC) Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 722EF31A89 for ; Sun, 28 Jan 2024 19:28:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.85.151 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706470100; cv=none; b=jjLiK+dK2EIfX2fQKNKpq1D8a4ts8acmo1nOecIkdakNtYs1pYDAtv/Lw2ZbZXvko4F/VQXkWiwlWS8mQjdvwUNIfrTco0fHR+Hzt0YYHAcjUfEWw0SP8lADPchKJTqQgxUCDWT2Wu/St5m1My+vtIYi5a/8kED8hszS5x+xDTE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706470100; c=relaxed/simple; bh=b47avTJTpbz8SUvSEAE7S2ReZNJws3zaCv2HopfJALc=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=o6z0F1Xzt2ledklJeyICvaqEnAVdbrL3c8WlYAci1+DdRmqAUEC/vUfdMXO1TIfjb82lb3om5wLMA+ThvXZri5nlcd83fuBZtklNe6go+t6/yp0PQ+4pEb5DdK+K/hWwXj93mjK8BK6A23719GD8C/e2MIWpKMoQdBO8QNMu4No= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM; spf=pass smtp.mailfrom=aculab.com; arc=none smtp.client-ip=185.58.85.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-318-8QfP3wKZN0SExZdtO-dIDg-1; Sun, 28 Jan 2024 19:28:15 +0000 X-MC-Unique: 8QfP3wKZN0SExZdtO-dIDg-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 28 Jan 2024 19:27:51 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jan 2024 19:27:51 +0000 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" , 'Netdev' , "'dri-devel@lists.freedesktop.org'" CC: 'Andy Shevchenko' , 'Andrew Morton' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , "'Dan Carpenter'" , 'Linus Walleij' , "'David S . Miller'" , "'linux-btrfs@vger.kernel.org'" , 'Jens Axboe' Subject: RE: [PATCH next 03/11] minmax: Simplify signedness check Thread-Topic: [PATCH next 03/11] minmax: Simplify signedness check Thread-Index: AdpSIB/Zp5Jfu0DYRYSLz+RLoj6R6Q== Date: Sun, 28 Jan 2024 19:27:51 +0000 Message-ID: <760fed5020ac42419cb0c3579ba1ed6c@AcuMS.aculab.com> References: <0ca26166dd2a4ff5a674b84704ff1517@AcuMS.aculab.com> In-Reply-To: <0ca26166dd2a4ff5a674b84704ff1517@AcuMS.aculab.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable It is enough to check that both 'x' and 'y' are valid for either a signed compare or an unsigned compare. For unsigned they must be an unsigned type or a positive constant. For signed they must be signed after unsigned char/short are promoted. The predicate for _Static_assert() only needs to be a compile-time constant not a constant integeger expression. In particular the short-circuit evaluation of || && ?: can be used to avoid the non-constantness of (pointer_type)1 in is_signed_type(). The '+ 0' in '(x) + 0 > =3D 0' is there to convert 'bool' to 'int' and avoid a compiler warning because max() gets used for 'bool' in one place (a very expensive 'or'). (The code is optimised away by two earlier checks - but the compiler still bleats.) Signed-off-by: David Laight --- include/linux/minmax.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 900eec7a28e5..c32b4b40ce01 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -8,7 +8,7 @@ #include =20 /* - * min()/max()/clamp() macros must accomplish three things: + * min()/max()/clamp() macros must accomplish several things: * * - Avoid multiple evaluations of the arguments (so side-effects like * "x++" happen only once) when non-constant. @@ -26,19 +26,17 @@ #define __typecheck(x, y) \ =09(!!(sizeof((typeof(x) *)1 =3D=3D (typeof(y) *)1))) =20 -/* is_signed_type() isn't a constexpr for pointer types */ -#define __is_signed(x) =09=09=09=09=09=09=09=09\ -=09__builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))),=09\ -=09=09is_signed_type(typeof(x)), 0) +/* Allow unsigned compares against non-negative signed constants. */ +#define __is_ok_unsigned(x) \ +=09(is_unsigned_type(typeof(x)) || (__is_constexpr(x) ? (x) + 0 >=3D 0 : 0= )) =20 -/* True for a non-negative signed int constant */ -#define __is_noneg_int(x)=09\ -=09(__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >=3D= 0) +/* Check for signed after promoting unsigned char/short to int */ +#define __is_ok_signed(x) is_signed_type(typeof((x) + 0)) =20 -#define __types_ok(x, y) =09=09=09=09=09\ -=09(__is_signed(x) =3D=3D __is_signed(y) ||=09=09=09\ -=09=09__is_signed((x) + 0) =3D=3D __is_signed((y) + 0) ||=09\ -=09=09__is_noneg_int(x) || __is_noneg_int(y)) +/* Allow if both x and y are valid for either signed or unsigned compares.= */ +#define __types_ok(x, y)=09=09=09=09\ +=09((__is_ok_signed(x) && __is_ok_signed(y)) ||=09\ +=09 (__is_ok_unsigned(x) && __is_ok_unsigned(y))) =20 #define __cmp_op_min < #define __cmp_op_max > --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)