From patchwork Fri Jun 23 08:58:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 111989 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp5629623vqr; Fri, 23 Jun 2023 02:00:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4+QAFQvvCaODMFYIfZRaV94om00bNXkRiN2OkFMhACZMbPAU8K03BtaJJFS/YTywWUe2m2 X-Received: by 2002:a05:6a00:244a:b0:666:8cbb:6e02 with SMTP id d10-20020a056a00244a00b006668cbb6e02mr25329921pfj.8.1687510851701; Fri, 23 Jun 2023 02:00:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687510851; cv=none; d=google.com; s=arc-20160816; b=w3seKS4mMFzDFIMDEQ77d3LgKPyuuQ/GeRFS6Rhf/2+dQpHK4Aw19vKQ4qk+KMk8gb 60MD0fN/CMuCixp/zIPiSQYKZ7dRrL8n15EMw8VbYi7PZHdjozgpT6sgGdoki6+JCFJw sTgiT3HmMKxW3A/nDpNvQdkB6OWclLwQNL1S9HZBGhA57zzyfy8n/DQA3PwuDACvnaA6 yB+6R74CWzhORrlOJjRwB/3q9wQfgPFxfTjJHDqFaxa9D85gucLoce6Fh3VqFOhpmhu8 zbEcbPe+HyoyH0BqlYa9CsgcvwLYxgs6+tdOf8XwJxfDTQjTEz4TuwkEVs+EpkzNf4ba b/7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=WPVv4brVsiUHGeag/hBIO6Z/lcdFpqC+U3IbirtBNKo=; b=Ex5f+q6iWF4w7pkde40cuXIFml9jm++jjF7GcZbImZn1qjDiuBktiUH44c0EV5ro1T 4gzoeIu8+lsT+bPUt/lbj8AQngyMQs4dRpNx+o0k4ojz0ZbyVWDkpo4I2CJHKzXbj50N Dzc4TWI0RQ5pyCh7zpUnfbfo1Pa0loqBhTpCeCfbwPZzgDr7vKqhVWrl6NPOBzOQhnLs tqyisvOnFRx/Xr9x/+Ny5S7Tsj5LkP3ymPAh6Gru7OAfbu5dLc1J9juE51efrOcZy6wT SejiY5u/cfIRPWfOhgubVIfYpBU0uRhpKTs2knSBrstN7XmRZeu3WOauSs7brFJZwBtB d8tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=LVrM9sWw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a25-20020aa79719000000b00646672f2be3si5698570pfg.282.2023.06.23.02.00.35; Fri, 23 Jun 2023 02:00:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=LVrM9sWw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231609AbjFWI7S (ORCPT + 99 others); Fri, 23 Jun 2023 04:59:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231829AbjFWI65 (ORCPT ); Fri, 23 Jun 2023 04:58:57 -0400 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::224]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 131052694; Fri, 23 Jun 2023 01:58:54 -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=1687510733; 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=WPVv4brVsiUHGeag/hBIO6Z/lcdFpqC+U3IbirtBNKo=; b=LVrM9sWwFLLdhXrM05i0UtyEQDJk6FeX3NPv10J51SP2lmGWJbawmPWtBziQwGdIg17o4D LNiBnVjUN0u8VQpy1uO9C9Z7dbO/rqZielSfAES1/L18Dg7yknoK5pPFoXiwkhl29CpCpr fM0LNC86R0tdXOVD17zDKN4ysH+RjzZ0e1BRcDJum0ItlWY9Kavf2UW9eVe8g9X5M7HkrN IMG1nIXLU04hbRVKBUUKu//uLO52Kf3R71TtPf8LSdooyrSwtoCt8eePtR9N2Pv67lZvxT MJxbovHNMRHw2LkSFuCLwqrLFPUdgWw6bs/GE8RncdIrA9mKkD+DCyGN4KieTA== 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 X-GND-Sasl: herve.codina@bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPA id E1C0BE0009; Fri, 23 Jun 2023 08:58:51 +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, Wojciech Ziemba , Christophe Leroy , Thomas Petazzoni Subject: [PATCH v6 07/13] minmax: Introduce {min,max}_array() Date: Fri, 23 Jun 2023 10:58:24 +0200 Message-Id: <20230623085830.749991-8-herve.codina@bootlin.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230623085830.749991-1-herve.codina@bootlin.com> References: <20230623085830.749991-1-herve.codina@bootlin.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769483378801542033?= X-GMAIL-MSGID: =?utf-8?q?1769483378801542033?= 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 --- include/linux/minmax.h | 64 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 396df1121bff..798c6963909f 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -133,6 +133,70 @@ */ #define max_t(type, x, y) __careful_cmp((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