mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs.git
synced 2026-10-30 05:42:46 +01:00
uapi: Provide DIV_ROUND_CLOSEST()
Currently DIV_ROUND_CLOSEST() is only available for the kernel via include/linux/math.h. Expose it to userland as well by adding __KERNEL_DIV_ROUND_CLOSEST() as a common definition in uapi. Additionally, ensure it allows building ISO C applications by switching from the 'typeof' GNU extension to the ISO-friendly __typeof__. Reviewed-by: Nícolas F. R. A. Prado <[email protected]> Tested-by: Diederik de Haas <[email protected]> Acked-by: Andy Shevchenko <[email protected]> Reviewed-by: AngeloGioacchino Del Regno <[email protected]> Signed-off-by: Cristian Ciocaltea <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Daniel Stone <[email protected]>
This commit is contained in:
1 parent
25854131c0
commit
de9e2b3d88
2 files changed
+19
-17
No files matched your search
+1
-17
@@ -89,23 +89,7 @@
|
||||
} \
|
||||
)
|
||||
|
||||
/*
|
||||
* Divide positive or negative dividend by positive or negative divisor
|
||||
* and round to closest integer. Result is undefined for negative
|
||||
* divisors if the dividend variable type is unsigned 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) == ((__d) > 0))) ? \
|
||||
(((__x) + ((__d) / 2)) / (__d)) : \
|
||||
(((__x) - ((__d) / 2)) / (__d)); \
|
||||
} \
|
||||
)
|
||||
#define DIV_ROUND_CLOSEST __KERNEL_DIV_ROUND_CLOSEST
|
||||
/*
|
||||
* Same as above but for u64 dividends. divisor must be a 32-bit
|
||||
* number.
|
||||
|
||||
@@ -50,4 +50,22 @@
|
||||
|
||||
#define __KERNEL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
|
||||
|
||||
/*
|
||||
* Divide positive or negative dividend by positive or negative divisor
|
||||
* and round to closest integer. Result is undefined for negative
|
||||
* divisors if the dividend variable type is unsigned and for negative
|
||||
* dividends if the divisor variable type is unsigned.
|
||||
*/
|
||||
#define __KERNEL_DIV_ROUND_CLOSEST(x, divisor) \
|
||||
({ \
|
||||
__typeof__(x) __x = x; \
|
||||
__typeof__(divisor) __d = divisor; \
|
||||
\
|
||||
(((__typeof__(x))-1) > 0 || \
|
||||
((__typeof__(divisor))-1) > 0 || \
|
||||
(((__x) > 0) == ((__d) > 0))) ? \
|
||||
(((__x) + ((__d) / 2)) / (__d)) : \
|
||||
(((__x) - ((__d) / 2)) / (__d)); \
|
||||
})
|
||||
|
||||
#endif /* _UAPI_LINUX_CONST_H */
|
||||
Reference in new issue
Block a user