[riot-notifications] [RIOT-OS/RIOT] core/bitarithm: introduce bitarithm_get/set_masked(), convert pca9685 (#12238)

benpicco notifications at github.com
Mon Sep 16 02:05:33 CEST 2019


### Contribution description
@gschorcht posted a collection of sensor drivers that all follow a common pattern. One neat helper function is `_set_reg_bit(uint8_t *byte, uint8_t mask, uint8_t bit)` that sets the masked bits in `byte` to the given value.

At first glance the function looks a bit odd as uses a loop to determine the lowest set bit of a compile-time constant. However, if `mask` is a compile-time constant, the compiler will [resolve the loop](https://godbolt.org/z/N_8UZD) and leave just a few instructions.

Now since this pattern is quite useful for other drivers and to keep people from trying to optimize that loop that will be optimized away - for some reason, when replacing the loop with `bitarithm_lsb()`, the compiler will *not* optimize it away (only verified by increased binary size), it's best to put it into common code. 

### Testing procedure

See if `tests/driver_pca9685` still produces the same binary.

### Issues/PRs references
#10083, #10082, #10092, #10420, #10462 all come with their own copy of this function.
You can view, comment on, or merge this pull request online at:

  https://github.com/RIOT-OS/RIOT/pull/12238

-- Commit Summary --

  * core/bitarithm: add bitarithm_get/set_masked()
  * drivers/pca9685: use bitarithm_set_masked()

-- File Changes --

    M core/include/bitarithm.h (41)
    M drivers/pca9685/pca9685.c (30)
    M tests/unittests/tests-core/tests-core-bitarithm.c (79)

-- Patch Links --

https://github.com/RIOT-OS/RIOT/pull/12238.patch
https://github.com/RIOT-OS/RIOT/pull/12238.diff

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/12238
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20190915/600e280a/attachment.htm>


More information about the notifications mailing list