Skip to content

Conversation

LucasRibeiro-Zuhlke
Copy link

Also fix DIV_ROUND_UP(n, d) in cases where at least one parameter is negative.

Fixes #95302.

Copy link
Contributor

@pdgendt pdgendt left a 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]>
Copy link

sonarqubecloud bot commented Sep 2, 2025

*/
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
#define DIV_ROUND_UP(n, d) ((d == 0) \
? __ASSERT_NO_MSG(false) \
Copy link
Contributor

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.

Copy link
Author

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.

Copy link
Contributor

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) \
Copy link
Contributor

@pdgendt pdgendt Sep 25, 2025

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@fabiobaltieri fabiobaltieri Sep 25, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Base OS Base OS Library (lib/os) area: Testsuite Testsuite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

include: util: DIV_ROUND_UP and DIV_ROUND_CLOSEST accept division by zero

6 participants