浏览代码

HID: simplify implement() a bit

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 use 64 bit calculations if we take an extra
branch.

[jkosina@suse.cz: fix a small error in changelog]
Suggested-by: Doug Anderson <dianders@chromium.org>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
Dmitry Torokhov 9 年之前
父节点
当前提交
95d1c8951e
共有 1 个文件被更改,包括 15 次插入18 次删除
  1. 15 18
      drivers/hid/hid-core.c

+ 15 - 18
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)
 static void __implement(u8 *report, unsigned offset, int n, u32 value)
 {
 {
 	unsigned int idx = offset / 8;
 	unsigned int idx = offset / 8;
-	unsigned int size = offset + n;
 	unsigned int bit_shift = offset % 8;
 	unsigned int bit_shift = offset % 8;
 	int bits_to_set = 8 - bit_shift;
 	int bits_to_set = 8 - bit_shift;
-	u8 bit_mask = 0xff << bit_shift;
 
 
 	while (n - bits_to_set >= 0) {
 	while (n - bits_to_set >= 0) {
-		report[idx] &= ~bit_mask;
+		report[idx] &= ~(0xff << bit_shift);
 		report[idx] |= value << bit_shift;
 		report[idx] |= value << bit_shift;
 		value >>= bits_to_set;
 		value >>= bits_to_set;
 		n -= bits_to_set;
 		n -= bits_to_set;
 		bits_to_set = 8;
 		bits_to_set = 8;
-		bit_mask = 0xff;
 		bit_shift = 0;
 		bit_shift = 0;
 		idx++;
 		idx++;
 	}
 	}
 
 
 	/* last nibble */
 	/* last nibble */
 	if (n) {
 	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,
 static void implement(const struct hid_device *hid, u8 *report,
 		      unsigned offset, unsigned n, u32 value)
 		      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",
 		hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
 			 __func__, n, current->comm);
 			 __func__, n, current->comm);
 		n = 32;
 		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);
 	__implement(report, offset, n, value);
 }
 }