Message ID | 20230615152631.224529-8-herve.codina@bootlin.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp723504vqr; Thu, 15 Jun 2023 08:31:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4RvqAKZ/67kMC/JA0jSF8F33/7g+FDh7YeeVcqyxc/U7rqS0TXhT99TiMeTVHDN/X3wL5Z X-Received: by 2002:a92:dc05:0:b0:33d:3b69:2d23 with SMTP id t5-20020a92dc05000000b0033d3b692d23mr16584992iln.19.1686843114404; Thu, 15 Jun 2023 08:31:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686843114; cv=none; d=google.com; s=arc-20160816; b=x7vbt78wC+DEcuTL/gUt/Ftu4BdGy7hFuofIZ4OX78p5ihJ8Jva7+7rNlc1yupUpUy hgcMbH7swLhA7y/ASTgCMREFybT/wCrbrDDBKsFYRQ8lYul9amyu97ZGWFcA4fMxg2nh aFaYDLJ8Omqxfue8Bd+ehvCl/4H+f06JlPYGI9sRD558tXQ4E/4ja0wP3iWPGg1P6qmc ccYaVzpBWTDAFVhcaGpumqvVlngiaOkaIU97pr54sBNO79tbJBLLsHJNPfW5dn8W15ZB SOB8paJutaeIG5Wyogcc0QcfhdZpDQJemgVz86d0DO0XjSr6pvR/oLEma8gTCrYlZwAH MF0w== 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=KvU8ZFxyfZKV+6P7AuATlkHtw3Ni6ol9j6yJo+Wq6as=; b=QPHqPB+ruZaRgAYZHxmrVfkvoqG5dk/hzCxGv23wpES2Yfvzm0Il7HKPMkn0KoHtZ8 eYs3l+1WQrxyYCHnNfo6kAf2Y89dwXLB1UCaukbzrkbZ1TxX5HhSyinNz3mxbV7sJWkv +C71rLFuOK4n6fxofOLakHeE8I/1PL5PgUn2DJ5cegMPTVR9aN7DPdWOibfD87HtAfjK Z5+a22BcgNibyOqEMXbHxDE7gE8cPQwIcSEws22aKONNoymigxdF/LLMDH8mlTWBcogy oDfZsKl3lQeiO0E6Ymr5Guc3jkGlI4LWU2beRN3P98EDNQkUbGQCdOEbYftCv1vp4a1p a34w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=gSVlOJoX; 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 o22-20020a637316000000b00543d2ba66casi3273229pgc.621.2023.06.15.08.31.40; Thu, 15 Jun 2023 08:31:54 -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=gSVlOJoX; 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 S1345227AbjFOP1a (ORCPT <rfc822;n2h9z4@gmail.com> + 99 others); Thu, 15 Jun 2023 11:27:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345178AbjFOP0u (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 15 Jun 2023 11:26:50 -0400 Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8376E69; Thu, 15 Jun 2023 08:26:48 -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=1686842807; 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=KvU8ZFxyfZKV+6P7AuATlkHtw3Ni6ol9j6yJo+Wq6as=; b=gSVlOJoXENawBNZO9Y85VfX5SMHoBy0URfunSBWmGPz24SHA3JnQmvTXbEBf4m36Zi0iNu D9KjdJ5IvwftgPQNnmQCnZ/u2dZSjxfC8lFQiVdkqTHUBwYKPVFJTt8++Z6m/FEpy8AIeH 9LJh3ilhX09pffu02QobJRaVH3nBJMGzdVxXAdgmCQS5RgfRK4y36ZG/nbSc+YGk183Tl1 YOegPKxjPMaEUJHi++5RHuiKDr6UFpCh26Te0TzF/yRo4azFD+Aln1ddAP2La3WI6lK2Ql wrfZe/YrT0ZXoDcfdM1UeX3GQro2Lm78vPmlMgVS5YoFUrnTVy6im48toe8g8Q== 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 0391BFF814; Thu, 15 Jun 2023 15:26:45 +0000 (UTC) From: Herve Codina <herve.codina@bootlin.com> To: Herve Codina <herve.codina@bootlin.com>, Liam Girdwood <lgirdwood@gmail.com>, Mark Brown <broonie@kernel.org>, Rob Herring <robh+dt@kernel.org>, Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>, Conor Dooley <conor+dt@kernel.org>, Jonathan Cameron <jic23@kernel.org>, Lars-Peter Clausen <lars@metafoo.de>, Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>, Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>, Andy Shevchenko <andy.shevchenko@gmail.com> Cc: alsa-devel@alsa-project.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Christophe Leroy <christophe.leroy@csgroup.eu>, Thomas Petazzoni <thomas.petazzoni@bootlin.com> Subject: [PATCH v5 07/13] minmax: Introduce {min,max}_array() Date: Thu, 15 Jun 2023 17:26:25 +0200 Message-Id: <20230615152631.224529-8-herve.codina@bootlin.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230615152631.224529-1-herve.codina@bootlin.com> References: <20230615152631.224529-1-herve.codina@bootlin.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768783205346578725?= X-GMAIL-MSGID: =?utf-8?q?1768783205346578725?= |
Series |
Add support for IIO devices in ASoC
|
|
Commit Message
Herve Codina
June 15, 2023, 3:26 p.m. UTC
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 <herve.codina@bootlin.com> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> --- include/linux/minmax.h | 64 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)
Comments
On Thu, Jun 15, 2023 at 6:26 PM Herve Codina <herve.codina@bootlin.com> wrote: > > Introduce min_array() (resp max_array()) in order to get the > minimal (resp maximum) of values present in an array. Only in the case if you need to send a new version (otherwise a follow up will be okay)... ... > + * The first typeof(&(array)[0]) is needed in order to support arrays of both > + * 'int *buff' and 'int buf[N]' types. int buff[N] ? > + * The array can be an array of const items. > + * typeof() keeps the const qualifier. Use __unconst_typeof() in order to + _integer > + * discard the const qualifier for the __element variable.
From: Herve Codina > Sent: 15 June 2023 16:26 > > 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 <herve.codina@bootlin.com> > Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> > --- > include/linux/minmax.h | 64 ++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 64 insertions(+) > > diff --git a/include/linux/minmax.h b/include/linux/minmax.h > index 396df1121bff..1672985b02a3 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))) Those are probably more generally useful and belong elsewhere. > + > +/* > + * 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< --- Is that needed in the .h file? > + * > + * The first typeof(&(array)[0]) is needed in order to support arrays of both > + * 'int *buff' and 'int buf[N]' types. > + * > + * The array can be an array of const items. > + * typeof() keeps the const qualifier. Use __unconst_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]; \ s/__element/__bound/ > + while (__len--) \ > + __element = op(__element, __array[__len]); \ > + __element; }) I'm not all sure that all the shenanigans required to use min() is really needed here. It would also be generally better to process the array forwards. So something like: typeof (&array[0]) __ptr = array, __limit = array + len; typeof (array[0] + 0) __element, __bound = *__ptr++; while (ptr < __limit) { __element = *__ptr++; if (__element > __bound) __bound = __element; } (typeof (array[0]))__bound; }) seems fine to me. The final cast is there to convert 'int' back to un/signed char|short. Not really needed and might generate worse code. But if you insist on using min/max ignore this bit. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Hi David, Andy On Tue, 20 Jun 2023 11:45:01 +0000 David Laight <David.Laight@ACULAB.COM> wrote: > From: Herve Codina > > Sent: 15 June 2023 16:26 > > > > 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 <herve.codina@bootlin.com> > > Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> > > --- > > include/linux/minmax.h | 64 ++++++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 64 insertions(+) > > > > diff --git a/include/linux/minmax.h b/include/linux/minmax.h > > index 396df1121bff..1672985b02a3 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))) > > Those are probably more generally useful and belong elsewhere. Yes but it is only used here. It can be move somewhere, in a common place, when necessary. > > > + > > +/* > > + * 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< --- > > Is that needed in the .h file? > > > + * > > + * The first typeof(&(array)[0]) is needed in order to support arrays of both > > + * 'int *buff' and 'int buf[N]' types. > > + * > > + * The array can be an array of const items. > > + * typeof() keeps the const qualifier. Use __unconst_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]; \ > > s/__element/__bound/ > > > + while (__len--) \ > > + __element = op(__element, __array[__len]); \ > > + __element; }) > > I'm not all sure that all the shenanigans required to use min() > is really needed here. > > It would also be generally better to process the array forwards. > So something like: > typeof (&array[0]) __ptr = array, __limit = array + len; > typeof (array[0] + 0) __element, __bound = *__ptr++; > while (ptr < __limit) { > __element = *__ptr++; > if (__element > __bound) > __bound = __element; > } > (typeof (array[0]))__bound; }) > seems fine to me. > The final cast is there to convert 'int' back to un/signed char|short. > Not really needed and might generate worse code. > > But if you insist on using min/max ignore this bit. I didn't plan to change the {min,max}_array() macros in this series as you suggest. Maybe min()/max() is too strict but it's a way to be sure about the type used. Also the current version doesn't need any extra cast to get rid of the integer promotion as the integer promotion doesn't occur. Is it ok for you if we keep as it ? Thanks for your feedback, Hervé
On Thu, Jun 22, 2023 at 02:32:33PM +0200, Herve Codina wrote:
> Is it ok for you if we keep as it ?
I think any issues here can be addressed incrementally rather than
holding up the rest of the series.
On Thu, Jun 22, 2023 at 3:32 PM Herve Codina <herve.codina@bootlin.com> wrote: > On Tue, 20 Jun 2023 11:45:01 +0000 > David Laight <David.Laight@ACULAB.COM> wrote: > > From: Herve Codina > > > Sent: 15 June 2023 16:26 > I didn't plan to change the {min,max}_array() macros in this series as you > suggest. > > Maybe min()/max() is too strict but it's a way to be sure about the type > used. Also the current version doesn't need any extra cast to get rid of > the integer promotion as the integer promotion doesn't occur. > > Is it ok for you if we keep as it ? I think the current verison is good enough, and as Mark said we may update incrementally if ever needed.
diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 396df1121bff..1672985b02a3 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 buf[N]' types. + * + * The array can be an array of const items. + * typeof() keeps the const qualifier. Use __unconst_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