From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753326AbbCJS1R (ORCPT ); Tue, 10 Mar 2015 14:27:17 -0400 Received: from smtprelay0070.hostedemail.com ([216.40.44.70]:52088 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753277AbbCJS1Q (ORCPT ); Tue, 10 Mar 2015 14:27:16 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::,RULES_HIT:41:355:379:541:599:960:973:982:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2198:2199:2393:2559:2562:2731:2828:3138:3139:3140:3141:3142:3353:3622:3865:3866:3867:3868:3870:3871:3872:3874:4321:5007:6261:7807:8603:10004:10400:10848:11026:11232:11473:11658:11914:12296:12517:12519:12740:13069:13161:13229:13311:13357:21080,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0 X-HE-Tag: shame01_4f492cac1b16 X-Filterd-Recvd-Size: 2407 Message-ID: <1426012032.18060.18.camel@perches.com> Subject: Re: [PATCH v2 1/4] kernel.h: add find_closest() macro From: Joe Perches To: Bartosz Golaszewski Cc: LKML , Guenter Roeck , lm-sensors , Andrew Morton , Steven Rostedt Date: Tue, 10 Mar 2015 11:27:12 -0700 In-Reply-To: <1426008442-26236-2-git-send-email-bgolaszewski@baylibre.com> References: <1426008442-26236-1-git-send-email-bgolaszewski@baylibre.com> <1426008442-26236-2-git-send-email-bgolaszewski@baylibre.com> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.12.10-0ubuntu1~14.10.1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2015-03-10 at 18:27 +0100, Bartosz Golaszewski wrote: > Searching for the member of an array closest to 'x' is > duplicated in several places. [] > diff --git a/include/linux/kernel.h b/include/linux/kernel.h [] > @@ -116,6 +116,29 @@ > } \ > ) > > +#define __find_closest(x, a, as, op)( \ > +{ \ > + typeof(as) _i, _as = (as) - 1; \ > + typeof(x) _x = (x); \ > + typeof(*a) *_a = (a); \ > + for (_i = 0; _i < _as; _i++) { \ > + if (_x op DIV_ROUND_CLOSEST(_a[_i] + _a[_i + 1], 2)) \ > + break; \ > + } \ > + (_i); \ > +} \ > +) Please use more descriptive variable names. Most kernel statement expression macros consolidate the "({" and "})" uses on single lines #define sem(args) {( \ etc... \ )} > + > +/* > + * Given an array 'a' (sorted in ascending order) of size 'as' return > + * the index of the element in that array closest to 'x'. > + */ It'd be nice to use kernel-doc comments here. > +#define find_closest(x, a, as) __find_closest(x, a, as, <=) > +/* > + * Similar to find_closest(), but 'a' is expected to be sorted > + * in descending order. > + */ And here. > +#define find_closest_desc(x, a, as) __find_closest(x, a, as, >) Shouldn't find_closest and find_closest_dest use equivalent comparison? >= ?