From patchwork Fri Sep 8 16:33:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Meyer X-Patchwork-Id: 9944953 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C80856035D for ; Fri, 8 Sep 2017 19:47:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BEA36203B9 for ; Fri, 8 Sep 2017 19:47:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B31B527BFF; Fri, 8 Sep 2017 19:47:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=2.0 tests=BAYES_00, DATE_IN_PAST_03_06, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D09B7285EB for ; Fri, 8 Sep 2017 19:47:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756969AbdIHTq3 (ORCPT ); Fri, 8 Sep 2017 15:46:29 -0400 Received: from www17.your-server.de ([213.133.104.17]:40520 "EHLO www17.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756962AbdIHTq1 (ORCPT ); Fri, 8 Sep 2017 15:46:27 -0400 Received: from [95.222.27.54] (helo=localhost.localdomain) by www17.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES128-GCM-SHA256:128) (Exim 4.85_2) (envelope-from ) id 1dqPEP-0002ZJ-IY; Fri, 08 Sep 2017 21:46:25 +0200 From: Thomas Meyer To: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, mchehab@kernel.org Cc: Thomas Meyer Subject: [PATCH] media: rc: Use bsearch library function Date: Fri, 8 Sep 2017 18:33:36 +0200 Message-Id: <20170908163336.2438-1-thomas@m3y3r.de> X-Mailer: git-send-email 2.11.0 X-Authenticated-Sender: thomas@m3y3r.de X-Virus-Scanned: Clear (ClamAV 0.99.2/23795/Fri Sep 8 18:37:59 2017) Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Replace self coded binary search, by existing library version. Signed-off-by: Thomas Meyer --- drivers/media/rc/rc-main.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/drivers/media/rc/rc-main.c b/drivers/media/rc/rc-main.c index 981cccd6b988..d3d6537867fb 100644 --- a/drivers/media/rc/rc-main.c +++ b/drivers/media/rc/rc-main.c @@ -15,6 +15,7 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include +#include #include #include #include @@ -460,6 +461,18 @@ static int ir_setkeytable(struct rc_dev *dev, return rc; } +static int rc_map_cmp(const void *key, const void *elt) +{ + unsigned int scancode = *(unsigned int *) key; + struct rc_map_table *e = (struct rc_map_table *) elt; + + if (e->scancode > scancode) + return -1; + else if (e->scancode < scancode) + return 1; + return 0; +} + /** * ir_lookup_by_scancode() - locate mapping by scancode * @rc_map: the struct rc_map to search @@ -472,21 +485,14 @@ static int ir_setkeytable(struct rc_dev *dev, static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map, unsigned int scancode) { - int start = 0; - int end = rc_map->len - 1; - int mid; - - while (start <= end) { - mid = (start + end) / 2; - if (rc_map->scan[mid].scancode < scancode) - start = mid + 1; - else if (rc_map->scan[mid].scancode > scancode) - end = mid - 1; - else - return mid; - } + struct rc_map_table *res; - return -1U; + res = bsearch(&scancode, rc_map->scan, rc_map->len, + sizeof(struct rc_map_table), rc_map_cmp); + if (res == NULL) + return -1U; + else + return res - rc_map->scan; } /**