-
Notifications
You must be signed in to change notification settings - Fork 2k
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: Add IS_CT_CONSTANT() #17273
core: Add IS_CT_CONSTANT() #17273
Conversation
I added a WIP because I haven't tested this yet for the motivating example stated (for using pin / port as immediate value in AVR assembly when the GPIO is a compile time constant). |
still WIP? |
No longer, I found a new user for it :) |
{ | ||
/* These test might fail on non-GCC-non-clang compilers. We don't support | ||
* any of those (yet), but this test might need adaption in the future */ | ||
int actual_constant = (int)(42 * 1337 / 3.14159); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int actual_constant = (int)(42 * 1337 / 3.14159); | |
unsigned actual_constant = (unsigned)(42 * 1337 / 3.14159); |
to make MSP430 happy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was not enough to make GCC 11.2 for the AVR happy, I changed the computation a bit to not overflow. In addition, I moved this into at static inline
function to make it a bit less trivial, so that we have a decent base-line on what we expect from compilers to detect as suitable for constant-folding.
7bb2f27
to
5e4785c
Compare
🎉 |
5e4785c
to
f2cd61c
Compare
This adds a simple macro to check (at C level) whether a given expression is proven to be compile time constant and suitable for constant folding. This allows writing code like this: ```C int gpio_read(gpio_t pin) { if (IS_CT_CONSTANT(pin)) { /* this implementation should even be able to use the port and * pin number as immediate in inline assembly */ } else { /* less efficient implementation that cannot use port and pin * number as immediate in inline assembly */ } } ```
f2cd61c
to
68424a9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK
Contribution description
This adds a simple macro to check (at C level) whether a given expression is proven to be compile time constant and suitable for constant folding. This allows writing code like this:
Testing procedure
A unit test was provided that should do the trick.
Issues/PRs references
None