mirror of
https://github.com/fail0verflow/switch-coreboot.git
synced 2025-05-04 01:39:18 -04:00
Add DIV_ROUND_CLOSEST
Bring in this useful function from Linux 4.7. BUG=chrome-os-partner:56556 BRANCH=none TEST=build on gru Change-Id: I37617e35b4784d6cdc51e6910aa91f566caf971d Signed-off-by: Simon Glass <sjg@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/382320 Reviewed-by: Julius Werner <jwerner@chromium.org>
This commit is contained in:
parent
e09cdfde26
commit
99f22182c2
1 changed files with 16 additions and 0 deletions
|
@ -16,6 +16,22 @@ static inline unsigned long div_round_up(unsigned int n, unsigned int d)
|
|||
{
|
||||
return (n + d - 1) / d;
|
||||
}
|
||||
|
||||
/*
|
||||
* Divide positive or negative dividend by positive divisor and round
|
||||
* to closest integer. Result is undefined for negative divisors and
|
||||
* for negative dividends if the divisor variable type is unsigned.
|
||||
*/
|
||||
#define DIV_ROUND_CLOSEST(x, divisor)( \
|
||||
{ \
|
||||
typeof(x) __x = x; \
|
||||
typeof(divisor) __d = divisor; \
|
||||
(((typeof(x))-1) > 0 || \
|
||||
((typeof(divisor))-1) > 0 || (__x) > 0) ? \
|
||||
(((__x) + ((__d) / 2)) / (__d)) : \
|
||||
(((__x) - ((__d) / 2)) / (__d)); \
|
||||
} \
|
||||
)
|
||||
#endif
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue