-
Notifications
You must be signed in to change notification settings - Fork 8.1k
include: util: fix division by zero in DIV_ROUND macros #95303
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
base: main
Are you sure you want to change the base?
include: util: fix division by zero in DIV_ROUND macros #95303
Conversation
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.
Fix the formatting issues.
Also fix DIV_ROUND_UP(n, d) in cases where at least one parameter is negative. Added unit tests to check for assserts and wrong calculation. Signed-off-by: Lucas Ribeiro <[email protected]>
3bc1061
to
787da83
Compare
|
*/ | ||
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) | ||
#define DIV_ROUND_UP(n, d) ((d == 0) \ | ||
? __ASSERT_NO_MSG(false) \ |
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.
Looking closer at this, this won't work. The ternary operator won't be able to expand the macro for asserts here.
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.
We will have to use a static inline
function then, but that needs a fixed type for return and parameters.
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.
You might need to resort to _Generic
to make it work.
* @return The result of @p n / @p d, rounded up. | ||
*/ | ||
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) | ||
#define DIV_ROUND_UP(n, d) ((d == 0) \ |
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.
How about:
#define DIV_ROUND_UP(n, d) ({ \
__typeof__(n) un = (n); \
__typeof__(d) ud = (d); \
__ASSERT_NO_MSG(ud != 0); \
((un + ud - 1) / ud); \
})
Inspired by #96478
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.
CC: @fabiobaltieri
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.
Nah :-(
include/zephyr/drivers/can.h: uint32_t data_32[DIV_ROUND_UP(CAN_MAX_DLEN, sizeof(uint32_t))];
include/zephyr/logging/log_msg.h: long long _ll_buf[DIV_ROUND_UP(len, sizeof(long long))]; \
include/zephyr/logging/log_msg.h: long double _ld_buf[DIV_ROUND_UP(len, sizeof(long double))]; \
and plenty more, I think we should have a DIV_ROUND_UP
and div_round_up
, once you do the ({
thing it effectively becomes a function
Also fix DIV_ROUND_UP(n, d) in cases where at least one parameter is negative.
Fixes #95302.