Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1.1k
Update math macros to avoid computing callable arguments more than once#391
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
| #defineradians(deg) ((deg)*DEG_TO_RAD) | ||
| #definedegrees(rad) ((rad)*RAD_TO_DEG) |
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.
These two lines (124, 125 for radians and degrees) expose the very same problem as doall macro calls. Why not define them as inline constexpr functions instead? Like so:
'template<typename T> inline constexpr T min(T a, T b) { return a > b ? b : a; }
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.
Because that doesn't work in C, and these macros are currently usable from C, so that would break compatibility.
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.
I was also concerned about binary size.
matthijskooijman commentedJan 28, 2021
Note that min/max have already been updated similarly to what you do in this PR (and with a template function for C++) in the As I said, just |
Keating950 commentedJan 29, 2021
That sounds like the best place to start. Thanks for the direction! |
Currently, if you pass an a callable argument to (e.g.) the
maxmacro, the argument will be computed more than once.max(expensive_function(a), expensive_function(b))will expand to:The compiler can't always optimize these calls away, as seen in theGodbolt disassembly here. This PR uses the GCC
typeofextension to avoid these unnecessary calls where possible, falling back to the existing macros should that feature be unavailable. (Testing for it with the latest Arduino SDK shows that the extension is available.)