RIOT icon indicating copy to clipboard operation
RIOT copied to clipboard

core/macros: add math helper macros

Open benpicco opened this issue 3 years ago • 3 comments

Contribution description

This adds a few commonly useful math helper macros to core/macros:

  • SIGNOF() returns the sign of an integer
  • DIV_ROUND() performs arithmetic integer division
  • DIV_ROUND_UP() performs arithmetic integer division, always rounding to the next integer larger in magnitude.

Testing procedure

unittests/tests-core was extended with test cases for the new macros

Issues/PRs references

benpicco avatar Feb 24 '22 17:02 benpicco

... and they are supposed to be used in compile-time situations, I guess?

kaspar030 avatar Feb 24 '22 20:02 kaspar030

Also at run-time. It's just that I've found myself open-coding those way too often, so I thought providing a macro would be appropriate.

benpicco avatar Feb 24 '22 21:02 benpicco

https://github.com/RIOT-OS/RIOT/blob/6db9960741cc93acbc903f482a93391333f8b6e8/cpu/stm32/periph/ptp.c#L63-L71

It's just that I've found myself open-coding those way too often, so I thought providing a macro would be appropriate.

Yes, indeed. Is just one of the cases where this would have been useful.

maribu avatar Sep 21 '22 13:09 maribu