Skip to content

Commit 337c7d2

Browse files
cristicc6by9
authored andcommitted
uapi: Provide DIV_ROUND_CLOSEST()
Commit de9e2b3d88af upstream. 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 <nfraprado@collabora.com> Tested-by: Diederik de Haas <diederik@cknow-tech.com> Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@collabora.com> Link: https://patch.msgid.link/20260303-rk3588-bgcolor-v8-1-fee377037ad1@collabora.com Signed-off-by: Daniel Stone <daniels@collabora.com>
1 parent 314efcc commit 337c7d2

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

include/linux/math.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,7 @@
8989
} \
9090
)
9191

92-
/*
93-
* Divide positive or negative dividend by positive or negative divisor
94-
* and round to closest integer. Result is undefined for negative
95-
* divisors if the dividend variable type is unsigned and for negative
96-
* dividends if the divisor variable type is unsigned.
97-
*/
98-
#define DIV_ROUND_CLOSEST(x, divisor)( \
99-
{ \
100-
typeof(x) __x = x; \
101-
typeof(divisor) __d = divisor; \
102-
(((typeof(x))-1) > 0 || \
103-
((typeof(divisor))-1) > 0 || \
104-
(((__x) > 0) == ((__d) > 0))) ? \
105-
(((__x) + ((__d) / 2)) / (__d)) : \
106-
(((__x) - ((__d) / 2)) / (__d)); \
107-
} \
108-
)
92+
#define DIV_ROUND_CLOSEST __KERNEL_DIV_ROUND_CLOSEST
10993
/*
11094
* Same as above but for u64 dividends. divisor must be a 32-bit
11195
* number.

include/uapi/linux/const.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,22 @@
5050

5151
#define __KERNEL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
5252

53+
/*
54+
* Divide positive or negative dividend by positive or negative divisor
55+
* and round to closest integer. Result is undefined for negative
56+
* divisors if the dividend variable type is unsigned and for negative
57+
* dividends if the divisor variable type is unsigned.
58+
*/
59+
#define __KERNEL_DIV_ROUND_CLOSEST(x, divisor) \
60+
({ \
61+
__typeof__(x) __x = x; \
62+
__typeof__(divisor) __d = divisor; \
63+
\
64+
(((__typeof__(x))-1) > 0 || \
65+
((__typeof__(divisor))-1) > 0 || \
66+
(((__x) > 0) == ((__d) > 0))) ? \
67+
(((__x) + ((__d) / 2)) / (__d)) : \
68+
(((__x) - ((__d) / 2)) / (__d)); \
69+
})
70+
5371
#endif /* _UAPI_LINUX_CONST_H */

0 commit comments

Comments
 (0)