Skip to content

Commit

Permalink
core/macros: rewrite DIV_ROUND add DIV_ROUND_UINT
Browse files Browse the repository at this point in the history
  • Loading branch information
kfessel committed Nov 8, 2022
1 parent c354ab6 commit 23bcc18
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions core/include/macros/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ extern "C" {
unsigned int: 1, \
unsigned long: 1, \
unsigned long long: 1, \
default: (long long)(a) < 0 ? -1 : 1)
default: ((a) <= 0 ? ((a) == 0 ? 1L : -1L ): 1L))
/**
* @brief Calculates @p a/ @p b with arithmetic rounding
*/
#define DIV_ROUND(a, b) (((a) + SIGNOF(a) * (b) / 2) / (b))
#define DIV_ROUND(a, b) (((long long)(a) + SIGNOF(a) * SIGNOF(b) * (long long)(b) / 2) / (b))

/**
* @brief Calculates @p a/ @p b with arithmetic rounding for natural numbers (unsigned)
*/
#define DIV_ROUND_UINT(a, b) (((a) + (b) / 2) / (b))

/**
* @brief Calculates @p a/ @p b, always rounding up to the
Expand Down

0 comments on commit 23bcc18

Please sign in to comment.