From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751890AbbCSPPR (ORCPT ); Thu, 19 Mar 2015 11:15:17 -0400 Received: from smtprelay0028.hostedemail.com ([216.40.44.28]:47968 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750905AbbCSPPN (ORCPT ); Thu, 19 Mar 2015 11:15:13 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::,RULES_HIT:41:355:379:541:599:960: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:3354:3622:3865:3866:3867:3870:3871:4321:5007:6119:6261:7875:7903:8603:10004:10400:10848:11026:11232:11473:11658:11914:12438:12517:12519:12740:13019:13069:13311:13357:14096:14097: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: stamp39_1028952adcb61 X-Filterd-Recvd-Size: 2687 Message-ID: <1426778109.21142.9.camel@perches.com> Subject: Re: [PATCH v3 1/4] util_macros.h: add find_closest() macro From: Joe Perches To: Bartosz Golaszewski Cc: LKML , Guenter Roeck , lm-sensors , Andrew Morton , Steven Rostedt Date: Thu, 19 Mar 2015 08:15:09 -0700 In-Reply-To: <1426775420-31350-2-git-send-email-bgolaszewski@baylibre.com> References: <1426775420-31350-1-git-send-email-bgolaszewski@baylibre.com> <1426775420-31350-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 Thu, 2015-03-19 at 15:30 +0100, Bartosz Golaszewski wrote: > Searching for the member of an array closest to 'x' is > duplicated in several places. [] > diff --git a/include/linux/util_macros.h b/include/linux/util_macros.h [] > @@ -0,0 +1,39 @@ > +#ifndef _LINUX_HELPER_MACROS_H_ > +#define _LINUX_HELPER_MACROS_H_ > + > +#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 consistent statement expression start/top whitespace. > + > +/* This should be /** for proper kernel-doc style > + * find_closest - locate the closest element in a sorted array > + * @x: The reference value. > + * @a: The array in which to look for the closest element. Must be sorted > + * in ascending order. > + * @as: Size of 'a'. > + * > + * Returns the index of the element closest to 'x'. > + */ > +#define find_closest(x, a, as) __find_closest(x, a, as, <=) > + > +/* > + * find_closest_descending - locate the closest element in a sorted array > + * @x: The reference value. > + * @a: The array in which to look for the closest element. Must be sorted > + * in descending order. > + * @as: Size of 'a'. > + * > + * Similar to get_closest() but 'a' is expected to be sorted in descending > + * order. > + */ > +#define find_closest_descending(x, a, as) __find_closest(x, a, as, >) Again, why is this > not >= ?