Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <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>

authored by

Cristian Ciocaltea and committed by
Daniel Stone
de9e2b3d 25854131

+19 -17
+1 -17
include/linux/math.h
··· 89 89 } \ 90 90 ) 91 91 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 109 93 /* 110 94 * Same as above but for u64 dividends. divisor must be a 32-bit 111 95 * number.
+18
include/uapi/linux/const.h
··· 50 50 51 51 #define __KERNEL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) 52 52 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 + 53 71 #endif /* _UAPI_LINUX_CONST_H */