From patchwork Thu Oct 2 20:47:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eliav Farber X-Patchwork-Id: 907 Received: from fra-out-014.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-014.esa.eu-central-1.outbound.mail-perimeter.amazon.com [18.199.210.3]) (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 2F1651B424F; Thu, 2 Oct 2025 20:49:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=18.199.210.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759438145; cv=none; b=SFf+ZM2Mi2b/7dVyWjAq2YWqfkjthbkuv0eX0ovAD1uk2Rw8qYDsWzfpefMolAQl1UTchEDLW3AdV+BmZXi3t3tjh9kiRLzCdrL0sPxodkQMtmtovu55u5dAGqH7Zkbqg0/EWOVrcAdMexLWqtdiLNo5mxg/cnoYK55RRLLuvfU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759438145; c=relaxed/simple; bh=MPbFimTJUFx75fJugZt/UsCD1KUboxKM93LqL2VEi5M=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mxnU03JtrzRjVv8Zi0kPwfdjsyc3RasBZTF4uHQdL25HMfwYS/GoTQivq1NkMhxguHNDk/3JFNG1LPgz213+dlujcdme1iBJnMyikD1Hsv4jH7W1SAJ4xvyu4XjROoFMxGRlIPV/TT/ofBFybn9qMO8UCalagI34rJwiv0JB0ek= 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=YSZswaoQ; arc=none smtp.client-ip=18.199.210.3 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="YSZswaoQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759438143; x=1790974143; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CCU0o8wSJQJX56U4+pnGKdTopFZ/0WcuCtYzGhhpTrQ=; b=YSZswaoQG6SQPyCPqsNHwwzPXgk4aNF8fcCyzKuz/XQWqFRXb4/wviQs PweVqKaoU3ry+f2iV2atrhlJMSirfOJ8XoP4tBq9JmbNsxQ4J80/vRpkN 135X8XdmkMlsTlc/6k3FR2GXDRKz3fOGwKomI0UfZwFBA2JyXHzGUknOC gqNE8IiUKebqT05YN21Ha9mtQWN2dlapQFRIdfEq/UTBZxucNkV/8wAgp LKbqxGbZwtjy0GAGyxKvCYqxaY/yUnat0w3Q98SzkA2l48OeMW3rrqhOf tt2GWaz3FGukEnXz3XWyrqqTUr+cT7YY8cBmIxCeAX+2tGCHO+pgk8pfi Q==; X-CSE-ConnectionGUID: YFe8jPo1QUeEW67O7ndAHA== X-CSE-MsgGUID: tTAQC90ERwGyVX6J55647A== X-IronPort-AV: E=Sophos;i="6.18,310,1751241600"; d="scan'208";a="2924497" Received: from ip-10-6-3-216.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.3.216]) by internal-fra-out-014.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Oct 2025 20:49:01 +0000 Received: from EX19MTAEUA001.ant.amazon.com [54.240.197.233:11827] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.40.83:2525] with esmtp (Farcaster) id 6508bead-2ec3-4a32-addc-2cd3f144bb5d; Thu, 2 Oct 2025 20:49:01 +0000 (UTC) X-Farcaster-Flow-ID: 6508bead-2ec3-4a32-addc-2cd3f144bb5d Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUA001.ant.amazon.com (10.252.50.223) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Thu, 2 Oct 2025 20:48:55 +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; Thu, 2 Oct 2025 20:48:30 +0000 From: Eliav Farber To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: Andy Shevchenko , Christophe Leroy Subject: [PATCH 02/19 5.15.y] minmax: Introduce {min,max}_array() Date: Thu, 2 Oct 2025 20:47:16 +0000 Message-ID: <20251002204733.35652-3-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20251002204733.35652-1-farbere@amazon.com> References: <20251002204733.35652-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-sunxi@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D035UWA004.ant.amazon.com (10.13.139.109) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Status: O From: Herve Codina [ Upstream commit c952c748c7a983a8bda9112984e6f2c1f6e441a5 ] Introduce min_array() (resp max_array()) in order to get the minimal (resp maximum) of values present in an array. Signed-off-by: Herve Codina Reviewed-by: Andy Shevchenko Reviewed-by: Christophe Leroy Link: https://lore.kernel.org/r/20230623085830.749991-8-herve.codina@bootlin.com Signed-off-by: Mark Brown Signed-off-by: Eliav Farber --- include/linux/minmax.h | 64 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index d4bc394b449e..aac0b7d23768 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -168,6 +168,70 @@ */ #define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y)) +/* + * Remove a const qualifier from integer types + * _Generic(foo, type-name: association, ..., default: association) performs a + * comparison against the foo type (not the qualified type). + * Do not use the const keyword in the type-name as it will not match the + * unqualified type of foo. + */ +#define __unconst_integer_type_cases(type) \ + unsigned type: (unsigned type)0, \ + signed type: (signed type)0 + +#define __unconst_integer_typeof(x) typeof( \ + _Generic((x), \ + char: (char)0, \ + __unconst_integer_type_cases(char), \ + __unconst_integer_type_cases(short), \ + __unconst_integer_type_cases(int), \ + __unconst_integer_type_cases(long), \ + __unconst_integer_type_cases(long long), \ + default: (x))) + +/* + * Do not check the array parameter using __must_be_array(). + * In the following legit use-case where the "array" passed is a simple pointer, + * __must_be_array() will return a failure. + * --- 8< --- + * int *buff + * ... + * min = min_array(buff, nb_items); + * --- 8< --- + * + * The first typeof(&(array)[0]) is needed in order to support arrays of both + * 'int *buff' and 'int buff[N]' types. + * + * The array can be an array of const items. + * typeof() keeps the const qualifier. Use __unconst_integer_typeof() in order + * to discard the const qualifier for the __element variable. + */ +#define __minmax_array(op, array, len) ({ \ + typeof(&(array)[0]) __array = (array); \ + typeof(len) __len = (len); \ + __unconst_integer_typeof(__array[0]) __element = __array[--__len]; \ + while (__len--) \ + __element = op(__element, __array[__len]); \ + __element; }) + +/** + * min_array - return minimum of values present in an array + * @array: array + * @len: array length + * + * Note that @len must not be zero (empty array). + */ +#define min_array(array, len) __minmax_array(min, array, len) + +/** + * max_array - return maximum of values present in an array + * @array: array + * @len: array length + * + * Note that @len must not be zero (empty array). + */ +#define max_array(array, len) __minmax_array(max, array, len) + /** * clamp_t - return a value clamped to a given range using a given type * @type: the type of variable to use