Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: use for_each_set_bit() where appropriate

Instead of iterating over all bits in a bitmap and test them individually
let's siwtch to for_each_set_bit() which is more compact and is also
faster.

Also use bitmap_weight() when counting number of set bits.

This also fixes INPUT_DO_TOGGLE() implementation as it should have used
*_CNT as the upper boundary, not *_MAX.

Signed-off-by: Anshul Garg <aksgarg1989@gmail.com>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

authored by

Anshul Garg and committed by
Dmitry Torokhov
3e2b03da 3e30c11c

+9 -25
+9 -25
drivers/input/input.c
··· 677 677 int code; 678 678 679 679 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) { 680 - for (code = 0; code <= KEY_MAX; code++) { 681 - if (is_event_supported(code, dev->keybit, KEY_MAX) && 682 - __test_and_clear_bit(code, dev->key)) { 683 - input_pass_event(dev, EV_KEY, code, 0); 684 - } 685 - } 680 + for_each_set_bit(code, dev->key, KEY_CNT) 681 + input_pass_event(dev, EV_KEY, code, 0); 682 + memset(dev->key, 0, sizeof(dev->key)); 686 683 input_pass_event(dev, EV_SYN, SYN_REPORT, 1); 687 684 } 688 685 } ··· 1623 1626 if (!test_bit(EV_##type, dev->evbit)) \ 1624 1627 break; \ 1625 1628 \ 1626 - for (i = 0; i < type##_MAX; i++) { \ 1627 - if (!test_bit(i, dev->bits##bit)) \ 1628 - continue; \ 1629 - \ 1629 + for_each_set_bit(i, dev->bits##bit, type##_CNT) { \ 1630 1630 active = test_bit(i, dev->bits); \ 1631 1631 if (!active && !on) \ 1632 1632 continue; \ ··· 1974 1980 1975 1981 events = mt_slots + 1; /* count SYN_MT_REPORT and SYN_REPORT */ 1976 1982 1977 - if (test_bit(EV_ABS, dev->evbit)) { 1978 - for (i = 0; i < ABS_CNT; i++) { 1979 - if (test_bit(i, dev->absbit)) { 1980 - if (input_is_mt_axis(i)) 1981 - events += mt_slots; 1982 - else 1983 - events++; 1984 - } 1985 - } 1986 - } 1983 + if (test_bit(EV_ABS, dev->evbit)) 1984 + for_each_set_bit(i, dev->absbit, ABS_CNT) 1985 + events += input_is_mt_axis(i) ? mt_slots : 1; 1987 1986 1988 - if (test_bit(EV_REL, dev->evbit)) { 1989 - for (i = 0; i < REL_CNT; i++) 1990 - if (test_bit(i, dev->relbit)) 1991 - events++; 1992 - } 1987 + if (test_bit(EV_REL, dev->evbit)) 1988 + events += bitmap_weight(dev->relbit, REL_CNT); 1993 1989 1994 1990 /* Make room for KEY and MSC events */ 1995 1991 events += 7;