From nobody Thu Oct 2 02:15:07 2025 Received: from fra-out-004.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-004.esa.eu-central-1.outbound.mail-perimeter.amazon.com [3.74.81.189]) (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 058E030DD0A; Mon, 29 Sep 2025 17:18:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.74.81.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166286; cv=none; b=BY3LmE6a9sLd1KHUNoFXHnaZ8Pb3ERhgRqQU2uD0O7TaQnQYfXQY2Rx3H56V7fmESsFg1AECvZkymkTZGikFWa3daskLvciD6+4XMLXHH3ycWBAAOAtzg7PlNKCxsKh/VsIZqm/bVObGHlkOBvNsq1TbwAZV7kGcFxiwQvmBIRs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166286; c=relaxed/simple; bh=P0/5K4itgZIzRKJXbbY7AyT0CEFiidrkrNPidMshKPg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rTPGhdz3GNz6otmcKZYqxXDTegHLYRxSEiSuesRIpF0llR645Rqa+/mC/c49D6cWSIHJ/2OUV3hCviDDArYFNZujv0U/sZ0V592O8AwVE8D5vXLTZVACNuVazEi+1sXJT/7oO9iy51hThL0QkDxgsHgLVwj4dVCiRtkBNR1M1uI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=JK9w16Go; arc=none smtp.client-ip=3.74.81.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="JK9w16Go" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759166284; x=1790702284; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xkEPvUKyQPfYOtTnd+JXsEAKoNLEM/0U49oHM5nqIDU=; b=JK9w16GohTOrxYClYJeEjgTS+R/QTiddLZour8VfCAIMNH1AKe/Iusln eRqtGEXckndcJSC1jVIdyqLzMz+FrP1ahnTz5N3fP1gQCGCJtsdvZmVis TiWYK6gHjKDG00oZ86mjIbNNSK1TsFiWTMUk+GvLCWMIguVwHOeda8Xyd NZMe0afzbnVRF9SbdpJJEvgAd0DlqW2VBBQKsvtW9SmlJIMQ51+K/QwIm ULEgFPhgSNXrT8mcckyVS6O+CD2MdktcWJ/ki++iqeKMTrElkaTjMaMkM sH6j9RlKxXN9EU5tXCSA9dTXEgOc8gDJJwES8PzHuJyEtMQ3sxfytDWAu g==; X-CSE-ConnectionGUID: OCpi9bEnRAqnF/S8t9UOjw== X-CSE-MsgGUID: BOfPB8N6SceQEjpb8xh/Cw== X-IronPort-AV: E=Sophos;i="6.18,302,1751241600"; d="scan'208";a="2837752" Received: from ip-10-6-6-97.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.6.97]) by internal-fra-out-004.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2025 17:17:54 +0000 Received: from EX19MTAEUA001.ant.amazon.com [54.240.197.233:30559] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.44.161:2525] with esmtp (Farcaster) id 45410551-6ebb-4874-94fb-5ae3798b00c3; Mon, 29 Sep 2025 17:17:54 +0000 (UTC) X-Farcaster-Flow-ID: 45410551-6ebb-4874-94fb-5ae3798b00c3 Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUA001.ant.amazon.com (10.252.50.50) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:17:51 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:17:48 +0000 From: Eliav Farber To: , , , , , , , , CC: Linus Torvalds , David Laight , Lorenzo Stoakes Subject: [PATCH v2 02/12 6.6.y] minmax: simplify min()/max()/clamp() implementation Date: Mon, 29 Sep 2025 17:17:23 +0000 Message-ID: <20250929171733.20671-3-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250929171733.20671-1-farbere@amazon.com> References: <20250929171733.20671-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: EX19D040UWA003.ant.amazon.com (10.13.139.6) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" From: Linus Torvalds [ Upstream commit dc1c8034e31b14a2e5e212104ec508aec44ce1b9 ] Now that we no longer have any C constant expression contexts (ie array size declarations or static initializers) that use min() or max(), we can simpify the implementation by not having to worry about the result staying as a C constant expression. So now we can unconditionally just use temporary variables of the right type, and get rid of the excessive expansion that used to come from the use of __builtin_choose_expr(__is_constexpr(...), .. to pick the specialized code for constant expressions. Another expansion simplification is to pass the temporary variables (in addition to the original expression) to our __types_ok() macro. That may superficially look like it complicates the macro, but when we only want the type of the expression, expanding the temporary variable names is much simpler and smaller than expanding the potentially complicated original expression. As a result, on my machine, doing a $ time make drivers/staging/media/atomisp/pci/isp/kernels/ynr/ynr_1.0/ia_= css_ynr.host.i goes from real 0m16.621s user 0m15.360s sys 0m1.221s to real 0m2.532s user 0m2.091s sys 0m0.452s because the token expansion goes down dramatically. In particular, the longest line expansion (which was line 71 of that 'ia_css_ynr.host.c' file) shrinks from 23,338kB (yes, 23MB for one single line) to "just" 1,444kB (now "only" 1.4MB). And yes, that line is still the line from hell, because it's doing multiple levels of "min()/max()" expansion thanks to some of them being hidden inside the uDIGIT_FITTING() macro. Lorenzo has a nice cleanup patch that makes that driver use inline functions instead of macros for sDIGIT_FITTING() and uDIGIT_FITTING(), which will fix that line once and for all, but the 16-fold reduction in this case does show why we need to simplify these helpers. Cc: David Laight Cc: Lorenzo Stoakes Signed-off-by: Linus Torvalds Signed-off-by: Eliav Farber --- include/linux/minmax.h | 43 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index fc384714da45..e3e4353df983 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -35,10 +35,10 @@ #define __is_noneg_int(x) \ (__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >=3D 0) =20 -#define __types_ok(x, y) \ - (__is_signed(x) =3D=3D __is_signed(y) || \ - __is_signed((x) + 0) =3D=3D __is_signed((y) + 0) || \ - __is_noneg_int(x) || __is_noneg_int(y)) +#define __types_ok(x, y, ux, uy) \ + (__is_signed(ux) =3D=3D __is_signed(uy) || \ + __is_signed((ux) + 0) =3D=3D __is_signed((uy) + 0) || \ + __is_noneg_int(x) || __is_noneg_int(y)) =20 #define __cmp_op_min < #define __cmp_op_max > @@ -51,34 +51,31 @@ #define __cmp_once(op, type, x, y) \ __cmp_once_unique(op, type, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_)) =20 -#define __careful_cmp_once(op, x, y) ({ \ - static_assert(__types_ok(x, y), \ +#define __careful_cmp_once(op, x, y, ux, uy) ({ \ + __auto_type ux =3D (x); __auto_type uy =3D (y); \ + static_assert(__types_ok(x, y, ux, uy), \ #op "(" #x ", " #y ") signedness error, fix types or consider u" #op "()= before " #op "_t()"); \ - __cmp_once(op, __auto_type, x, y); }) + __cmp(op, ux, uy); }) =20 -#define __careful_cmp(op, x, y) \ - __builtin_choose_expr(__is_constexpr((x) - (y)), \ - __cmp(op, x, y), __careful_cmp_once(op, x, y)) +#define __careful_cmp(op, x, y) \ + __careful_cmp_once(op, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_)) =20 #define __clamp(val, lo, hi) \ ((val) >=3D (hi) ? (hi) : ((val) <=3D (lo) ? (lo) : (val))) =20 -#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \ - typeof(val) unique_val =3D (val); \ - typeof(lo) unique_lo =3D (lo); \ - typeof(hi) unique_hi =3D (hi); \ +#define __clamp_once(val, lo, hi, uval, ulo, uhi) ({ \ + __auto_type uval =3D (val); \ + __auto_type ulo =3D (lo); \ + __auto_type uhi =3D (hi); \ static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ (lo) <=3D (hi), true), \ "clamp() low limit " #lo " greater than high limit " #hi); \ - static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error"); \ - static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error"); \ - __clamp(unique_val, unique_lo, unique_hi); }) - -#define __careful_clamp(val, lo, hi) ({ \ - __builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)), \ - __clamp(val, lo, hi), \ - __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \ - __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); }) + static_assert(__types_ok(uval, lo, uval, ulo), "clamp() 'lo' signedness e= rror"); \ + static_assert(__types_ok(uval, hi, uval, uhi), "clamp() 'hi' signedness e= rror"); \ + __clamp(uval, ulo, uhi); }) + +#define __careful_clamp(val, lo, hi) \ + __clamp_once(val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h= _)) =20 /** * min - return minimum of two values of the same or compatible types --=20 2.47.3