Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1372681 > unrolled thread

[PATCH] HID: simplify implement() a bit

Started byDmitry Torokhov <dmitry.torokhov@gmail.com>
First post2016-04-06 19:30 +0200
Last post2016-04-07 15:50 +0200
Articles 3 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] HID: simplify implement() a bit Dmitry Torokhov <dmitry.torokhov@gmail.com> - 2016-04-06 19:30 +0200
    Re: [PATCH] HID: simplify implement() a bit Doug Anderson <dianders@chromium.org> - 2016-04-06 23:40 +0200
    Re: [PATCH] HID: simplify implement() a bit Jiri Kosina <jikos@kernel.org> - 2016-04-07 15:50 +0200

#1372681 — [PATCH] HID: simplify implement() a bit

FromDmitry Torokhov <dmitry.torokhov@gmail.com>
Date2016-04-06 19:30 +0200
Subject[PATCH] HID: simplify implement() a bit
Message-ID<rkZrc-7ns-15@gated-at.bofh.it>
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 <dianders@chromium.org>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 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);
 }
 
-- 
2.8.0.rc3.226.g39d4020


-- 
Dmitry

[toc] | [next] | [standalone]


#1372871

FromDoug Anderson <dianders@chromium.org>
Date2016-04-06 23:40 +0200
Message-ID<rl3l9-1Hl-55@gated-at.bofh.it>
In reply to#1372681
Dmitry,

On Wed, Apr 6, 2016 at 10:19 AM, Dmitry Torokhov
<dmitry.torokhov@gmail.com> wrote:
> 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 <dianders@chromium.org>
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>  drivers/hid/hid-core.c | 33 +++++++++++++++------------------
>  1 file changed, 15 insertions(+), 18 deletions(-)

Reviewed-by: Douglas Anderson <dianders@chromium.org>

[toc] | [prev] | [next] | [standalone]


#1373407

FromJiri Kosina <jikos@kernel.org>
Date2016-04-07 15:50 +0200
Message-ID<rlitQ-4AV-33@gated-at.bofh.it>
In reply to#1372681
On Wed, 6 Apr 2016, Dmitry Torokhov wrote:

> 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 <dianders@chromium.org>
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

Applied to for-4.7/upstream. Thanks!

-- 
Jiri Kosina
SUSE Labs

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web