Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2010198imm; Thu, 12 Jul 2018 11:33:08 -0700 (PDT) X-Google-Smtp-Source: AAOMgpe6oJvUBJG4aoX+w3TDcrb5qlcJkhDsvBGPnUn4BAM2PLXlJ4BG+iAA4g7E/xusHZKAvbp3 X-Received: by 2002:a63:e452:: with SMTP id i18-v6mr3054100pgk.185.1531420388902; Thu, 12 Jul 2018 11:33:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531420388; cv=none; d=google.com; s=arc-20160816; b=CtlljKyccAUZ8Qj8C0mF6A1EiRVuVOsbZ6eygbppNXLcZZeCCPYJL+ibThp2yah23z xhs8C2UnDCEe3P0ctX+NMveoKoHm6njpXXxrcff45GGeA6GSujuzUGpgNZYqH1nR7JNO 9GLENf0fXVO9lDmJCBNhimMP3/t5HrwA7pZMFInxETV214oPK4WdQ0VRG7/5h/sn7PjG GdWPMDzppX/qHs5EzWihDNucGibQk/uxdddMnmoEUkwJv43+wz4957bFSnKdxnXjA+K8 RpH5EKIag6D4I4tEVDHgdy8rpPvE5wOWLAuKfu5aJZ9TEHpejz1RrTlTEQPYVigDikjJ NA5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:arc-authentication-results; bh=ha9wtbQkMkvC6dN6QRMtL2Ff7eLsM5kF2OoA1ftq3Hs=; b=PtTZ7t2Whu04NDUcGFop2foWwG12EuUwUNgT5+dw9dZ8iLeXhyA9QBVflVl6lVhQYT SlsnK4NqnYPDWo5wUIzbOvlIJXmAqhj/WJYZEuLURxa9esrfgEJTDly/DuMHu9gS5H1J 9bUJJJs2hRsJSPIHVz9QCVCDJx44htGARyH6wCyVk6ccT3YGyt/HdxmJyIc+d3uosDFG JX9RokM5Jn8evzpS0uZV7RcUEmBtZ0y9arxIVjhk+W4P8z8U1rQiq2zIivhuo+m+dCXe vuGIGXvenW2O8nJo45R6h0Hinx/v7sYCp6Nu8tXFm9n7FAasLsob/yW3GwVRYhdUB5VZ fRPQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b13-v6si20493563pgw.478.2018.07.12.11.32.53; Thu, 12 Jul 2018 11:33:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727660AbeGLSmu (ORCPT + 99 others); Thu, 12 Jul 2018 14:42:50 -0400 Received: from www381.your-server.de ([78.46.137.84]:54707 "EHLO www381.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726543AbeGLSmu (ORCPT ); Thu, 12 Jul 2018 14:42:50 -0400 Received: from [78.46.172.2] (helo=sslproxy05.your-server.de) by www381.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.85_2) (envelope-from ) id 1fdgNk-0001mi-UQ; Thu, 12 Jul 2018 20:32:01 +0200 Received: from [188.174.121.149] (helo=[192.168.178.33]) by sslproxy05.your-server.de with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1fdgNk-000Dud-NU; Thu, 12 Jul 2018 20:32:00 +0200 Subject: Re: [PATCH 4/5] iio:adxl372: Add sampling frequency support To: Andy Shevchenko , Stefan Popa Cc: Jonathan Cameron , Michael Hennerich , Hartmut Knaack , Peter Meerwald , linux-iio , Linux Kernel Mailing List References: <1531409786-20180-1-git-send-email-stefan.popa@analog.com> From: Lars-Peter Clausen Message-ID: <4bfc67b7-b062-a8cb-8a30-ccd117d67db9@metafoo.de> Date: Thu, 12 Jul 2018 20:31:59 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: lars@metafoo.de X-Virus-Scanned: Clear (ClamAV 0.100.0/24746/Thu Jul 12 14:54:54 2018) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 07/12/2018 08:25 PM, Andy Shevchenko wrote: > On Thu, Jul 12, 2018 at 6:36 PM, Stefan Popa wrote: >> This patch adds the option for the user to select the sampling frequency. >> Also, the user can read the available frequencies and read the currently >> set frequency via the read_raw function. The frequency can be set via the >> write_raw function. >> >> When the frequency is set, the bandwidth is also checked and ensured >> that it is constrained to at most half of the sampling frequency. > >> +static int adxl372_find_closest_match(const int *array, >> + unsigned int size, int val) >> +{ >> + int i; >> + >> + for (i = 0; i < size; i++) { >> + if (val <= array[i]) >> + return i; >> + } >> + >> + return size - 1; >> +} > > Perhaps it's time to extend bsearch with something called > bsearch_closest(). > While it makes sense to have a helper function for this the generic bsearch is not a good solution for small arrays. The generated code is probably larger and the function call overhead of calling the cmp function completely outweighs the savings from doing a binary search. bsearch() is good if your array has 100s or 1000s of entries. If it only has 5, not so much.