Skip to content

Commit 954520d

Browse files
cristiccchewitt
authored andcommitted
FROMLIST(v3): 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__. Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>
1 parent 6c03fc3 commit 954520d

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,21 @@
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+
(((__typeof__(x))-1) > 0 || \
64+
((__typeof__(divisor))-1) > 0 || \
65+
(((__x) > 0) == ((__d) > 0))) ? \
66+
(((__x) + ((__d) / 2)) / (__d)) : \
67+
(((__x) - ((__d) / 2)) / (__d)); \
68+
} \
69+
)
5370
#endif /* _UAPI_LINUX_CONST_H */

0 commit comments

Comments
 (0)