-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -339,20 +339,30 @@ extern "C" { | |
| #define WB_DN(x) ROUND_DOWN(x, sizeof(void *)) | ||
|
|
||
| /** | ||
| * @brief Divide and round up. | ||
| * @brief Divide and round up towards infinity, akin to ceil(). | ||
| * | ||
| * @note Works with negative operands, asserts if denominator is zero. | ||
| * | ||
| * Example: | ||
| * @code{.c} | ||
| * DIV_ROUND_UP(3, 0); // Asserts | ||
| * DIV_ROUND_UP(1, 2); // 1 | ||
| * DIV_ROUND_UP(3, 2); // 2 | ||
| * DIV_ROUND_UP(-3, -2); // 2 | ||
| * DIV_ROUND_UP(3, -2); // 1 | ||
| * DIV_ROUND_UP(-3, 2); // 1 | ||
| * @endcode | ||
| * | ||
| * @param n Numerator. | ||
| * @param d Denominator. | ||
| * | ||
| * @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) \ | ||
| ? __ASSERT_NO_MSG(false) \ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. We will have to use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might need to resort to |
||
| : ((((n % d) == 0) || ((n < 0) ^ (d < 0))) \ | ||
| ? (n / d) \ | ||
| : (n / d) + 1)) | ||
|
|
||
| /** | ||
| * @brief Divide and round to the nearest integer. | ||
|
|
@@ -369,10 +379,11 @@ extern "C" { | |
| * | ||
| * @return The result of @p n / @p d, rounded to the nearest integer. | ||
| */ | ||
| #define DIV_ROUND_CLOSEST(n, d) \ | ||
| (((((__typeof__(n))-1) < 0) && (((__typeof__(d))-1) < 0) && ((n) < 0) ^ ((d) < 0)) \ | ||
| ? ((n) - ((d) / 2)) / (d) \ | ||
| : ((n) + ((d) / 2)) / (d)) | ||
| #define DIV_ROUND_CLOSEST(n, d) ((d == 0) \ | ||
| ? __ASSERT_NO_MSG(false) \ | ||
| : (((((__typeof__(n))-1) < 0) && (((__typeof__(d))-1) < 0) && ((n) < 0) ^ ((d) < 0)) \ | ||
| ? ((n) - ((d) / 2)) / (d) \ | ||
| : ((n) + ((d) / 2)) / (d))) | ||
|
|
||
| #ifndef MAX | ||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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:
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
Uh oh!
There was an error while loading. Please reload this page.
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 :-(
and plenty more, I think we should have a
DIV_ROUND_UPanddiv_round_up, once you do the({thing it effectively becomes a function