diff options
author | Yuval Peress <peress@google.com> | 2022-11-13 22:53:37 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-11-14 22:11:10 +0000 |
commit | fb5608cc9342c8e31a5f44ca980ae6324f5ce43a (patch) | |
tree | 8cf9e243cedcf41c36a2ef5ff6d40c594011bff2 | |
parent | e0c1fdf11777e198a4f08461eadcb3c8b09c50a6 (diff) | |
download | chrome-ec-fb5608cc9342c8e31a5f44ca980ae6324f5ce43a.tar.gz |
gpio: optimize the gpio_set_flags_by_mask loop
Make use of __builtin_ctz in order to loop through 'flags' instead
of checking each bit one by one.
BRANCH=none
BUG=none
TEST=twister
Signed-off-by: Yuval Peress <peress@google.com>
Change-Id: If202aa650326f52927effca9bd2f685bcb869ff0
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4025331
Reviewed-by: Aaron Massey <aaronmassey@google.com>
Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r-- | zephyr/shim/src/gpio.c | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/zephyr/shim/src/gpio.c b/zephyr/shim/src/gpio.c index c21a46c365..08f6d48742 100644 --- a/zephyr/shim/src/gpio.c +++ b/zephyr/shim/src/gpio.c @@ -339,12 +339,17 @@ void gpio_set_flags(enum gpio_signal signal, int flags) void gpio_set_flags_by_mask(uint32_t port, uint32_t mask, uint32_t flags) { - int pin; + const gpio_flags_t zephyr_flags = convert_to_zephyr_flags(flags); - for (pin = 0; pin < 8; pin++) - if (mask & BIT(pin)) - gpio_configure_port_pin(port, pin, - convert_to_zephyr_flags(flags)); + /* Using __builtin_ctz here will guarantee that this loop is as + * performant as the underlying architecture allows it to be. + */ + while (mask != 0) { + int pin = __builtin_ctz(mask); + + gpio_configure_port_pin(port, pin, zephyr_flags); + mask &= ~BIT(pin); + } } int signal_is_gpio(int signal) |