Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/macros: add math helper macros #17702

Merged
merged 2 commits into from
Sep 22, 2022
Merged

Conversation

benpicco
Copy link
Contributor

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 benpicco requested a review from maribu February 24, 2022 17:01
@github-actions github-actions bot added Area: core Area: RIOT kernel. Handle PRs marked with this with care! Area: tests Area: tests and testing framework labels Feb 24, 2022
@benpicco benpicco force-pushed the core/macros/math branch 2 times, most recently from e896b1c to e7d36e6 Compare February 24, 2022 17:04
@kaspar030
Copy link
Contributor

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

@benpicco
Copy link
Contributor Author

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 benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Sep 21, 2022
@maribu
Copy link
Member

maribu commented Sep 21, 2022

/**
* @brief Return the rounded result of x / y
* @param x Number to divide
* @param y @p x should be divided by this
* @return x/y, rounded to the nearest integer, using "round half up" rule
* @pre Both @p x and @p y are compile time constant integers and the
* expressions are evaluated without side-effects
*/
#define ROUNDED_DIV(x, y) (((x) + ((y) / 2)) / (y))

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.

core/include/macros/math.h Outdated Show resolved Hide resolved
@benpicco benpicco merged commit 4737d81 into RIOT-OS:master Sep 22, 2022
@benpicco benpicco deleted the core/macros/math branch September 22, 2022 12:14
@maribu maribu added this to the Release 2022.10 milestone Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: core Area: RIOT kernel. Handle PRs marked with this with care! Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants