From patchwork Wed Apr 6 17:19:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 8763861 Return-Path: X-Original-To: patchwork-linux-input@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id BAEB0C0553 for ; Wed, 6 Apr 2016 17:20:06 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D337520114 for ; Wed, 6 Apr 2016 17:20:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CE0B4201D3 for ; Wed, 6 Apr 2016 17:20:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751314AbcDFRUD (ORCPT ); Wed, 6 Apr 2016 13:20:03 -0400 Received: from mail-pf0-f177.google.com ([209.85.192.177]:35317 "EHLO mail-pf0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751310AbcDFRUC (ORCPT ); Wed, 6 Apr 2016 13:20:02 -0400 Received: by mail-pf0-f177.google.com with SMTP id n1so37597978pfn.2; Wed, 06 Apr 2016 10:20:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=7UySTsSHGKg/jw28DFOUJ/tTF6AH56ME40NmPOtw7Yo=; b=CjAhhaeg4y/l/XM4VXVhk11s3uhvjcYLQLtY1gnX8Gq0/kE16u1xOAx33PjsWjgFKO j7f8ybjcSjH9mAOS65ZdaQp9mQ8MqEN6FUoZWg7Ky9B47DP/8oP3WLiw+NklnPbmM8HH 1eg4J76TfkPHZfK8FQNy1jGf1wLq83WFf1ciiXiBewW5vmb70fMjkrY+lMFeA3flPgN+ +wXxQTtiMpnpRHzGRBID3YREJX1Iv9pv8bGgGQq/xRNf0lRzfJhlUQX4/Y6rmZO4Xze+ icnV4VSshrLsgNaKYy50QKfR4iWHq6aBuVH85nJ3LVmfXDRJMyhI6cBvbJYKjKtnOuAF 4z3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=7UySTsSHGKg/jw28DFOUJ/tTF6AH56ME40NmPOtw7Yo=; b=fq+n7H14w8Kd1WCHtdnO+ztr3c/UccVAr1rI8VoH4fqEYecFYUNLMrbRnFxQDOcmV1 VqA5D8mtcD+4JO15CzWOawvFWjuVbQzAP37w1Dmi9Ug9ctgU1PfizC9l9UVyML4FSaA/ BTwrNKhcSpQEshG/jiUh26iLtDLXDjVE8DzhOXkNR77P0tQtgUcuyVSBs0tl9FglLk4U V3jcW2RPs6rhHGN2wVdBdRnc6gxx+ofI/NqZrPcrmYcBzErqCxWZCz5ToXiE/UmHOBG6 zLB5/+sqhA0uxVbXtOnyQKG1+FjMQ8seSxwEvqZNG8z/mKOTqFn6k5ajFHM9A6DtXX9x o6Rg== X-Gm-Message-State: AD7BkJLVflxXOIHD4MHQcdVBXvvxzUVvSbs35F6yM8IyOHjinMIcetGFKuM4bA7rDDlNLw== X-Received: by 10.98.44.73 with SMTP id s70mr40146012pfs.2.1459963201462; Wed, 06 Apr 2016 10:20:01 -0700 (PDT) Received: from dtor-ws ([2620:0:1000:1311:88d:5784:88a8:44d]) by smtp.gmail.com with ESMTPSA id n11sm6301916pfb.74.2016.04.06.10.20.00 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Wed, 06 Apr 2016 10:20:00 -0700 (PDT) Date: Wed, 6 Apr 2016 10:19:58 -0700 From: Dmitry Torokhov To: Jiri Kosina Cc: Benjamin Tissoires , Doug Anderson , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] HID: simplify implement() a bit Message-ID: <20160406171958.GA39482@dtor-ws> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The 'size' variable is not really needed, and we can also shift constant in the loop body when masking off existing bits. Also we do not have to us 64 bit calculations if we take an extra branch. Suggested-by: Doug Anderson Signed-off-by: Dmitry Torokhov Reviewed-by: Douglas Anderson --- drivers/hid/hid-core.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 264cc49..9673dea 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -1129,49 +1129,46 @@ EXPORT_SYMBOL_GPL(hid_field_extract); static void __implement(u8 *report, unsigned offset, int n, u32 value) { unsigned int idx = offset / 8; - unsigned int size = offset + n; unsigned int bit_shift = offset % 8; int bits_to_set = 8 - bit_shift; - u8 bit_mask = 0xff << bit_shift; while (n - bits_to_set >= 0) { - report[idx] &= ~bit_mask; + report[idx] &= ~(0xff << bit_shift); report[idx] |= value << bit_shift; value >>= bits_to_set; n -= bits_to_set; bits_to_set = 8; - bit_mask = 0xff; bit_shift = 0; idx++; } /* last nibble */ if (n) { - if (size % 8) - bit_mask &= (1U << (size % 8)) - 1; - report[idx] &= ~bit_mask; - report[idx] |= (value << bit_shift) & bit_mask; + u8 bit_mask = ((1U << n) - 1); + report[idx] &= ~(bit_mask << bit_shift); + report[idx] |= value << bit_shift; } } static void implement(const struct hid_device *hid, u8 *report, unsigned offset, unsigned n, u32 value) { - u64 m; - - if (n > 32) { + if (unlikely(n > 32)) { hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n", __func__, n, current->comm); n = 32; + } else if (n < 32) { + u32 m = (1U << n) - 1; + + if (unlikely(value > m)) { + hid_warn(hid, + "%s() called with too large value %d (n: %d)! (%s)\n", + __func__, value, n, current->comm); + WARN_ON(1); + value &= m; + } } - m = (1ULL << n) - 1; - if (value > m) - hid_warn(hid, "%s() called with too large value %d! (%s)\n", - __func__, value, current->comm); - WARN_ON(value > m); - value &= m; - __implement(report, offset, n, value); }