From patchwork Wed Jun 14 07:48:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 13279679 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96689C001DF for ; Wed, 14 Jun 2023 07:50:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243393AbjFNHuR (ORCPT ); Wed, 14 Jun 2023 03:50:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243398AbjFNHtm (ORCPT ); Wed, 14 Jun 2023 03:49:42 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::225]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 228C1E62; Wed, 14 Jun 2023 00:49:39 -0700 (PDT) X-GND-Sasl: herve.codina@bootlin.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1686728978; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+zfO40ZB12GKQyPAf5k5MdzEzUKB2vFqguLETpbhayQ=; b=fwD9kZ9FkVy9sF1xJtD5WfPxZjQi+LrBv8wjipQlt79coHq445SdLV974h3wcWoFLfwB8O sKkrF28/MJfobtc/bsbMy4lZpqg/0pq+aYk9rNaalKyYg+MUFtJhySFXlLhlN2s1BkRvXV 8jKghyBLSrHAxdROg3mcL2rVTJrKOlTtNtqjX3lHTFqwehm5JItrcZKYStpfFP3pNQ53eH 4qtoRB1vCqMFUj6F4B9VZp5HfhAnRlHMj85nYsjaSoMBCNTSdIknzNPYYoZ3Qwdr7ZiNM7 yt86VgfWii+dDquJCJYPIYuvK3YozyEdZUmrHkEZ1oLb1hbiK40E3JKxd3Fivw== X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPA id 6C4B01C0003; Wed, 14 Jun 2023 07:49:37 +0000 (UTC) From: Herve Codina To: Herve Codina , Liam Girdwood , Mark Brown , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Cameron , Lars-Peter Clausen , Jaroslav Kysela , Takashi Iwai , Kuninori Morimoto , Andy Shevchenko Cc: alsa-devel@alsa-project.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Christophe Leroy , Thomas Petazzoni Subject: [PATCH v4 07/13] minmax: Introduce {min,max}_array() Date: Wed, 14 Jun 2023 09:48:58 +0200 Message-Id: <20230614074904.29085-8-herve.codina@bootlin.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230614074904.29085-1-herve.codina@bootlin.com> References: <20230614074904.29085-1-herve.codina@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org 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 --- include/linux/minmax.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 396df1121bff..2cd0d34ce921 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -133,6 +133,42 @@ */ #define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >) +/* + * 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< --- + */ +#define __minmax_array(op, array, len) ({ \ + typeof(array) __array = (array); \ + typeof(len) __len = (len); \ + typeof(__array[0] + 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