|
@@ -100,16 +100,18 @@
|
|
)
|
|
)
|
|
|
|
|
|
/*
|
|
/*
|
|
- * 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.
|
|
|
|
|
|
+ * Divide positive or negative dividend by positive or negative divisor
|
|
|
|
+ * and round to closest integer. Result is undefined for negative
|
|
|
|
+ * divisors if he dividend variable type is unsigned and for negative
|
|
|
|
+ * dividends if the divisor variable type is unsigned.
|
|
*/
|
|
*/
|
|
#define DIV_ROUND_CLOSEST(x, divisor)( \
|
|
#define DIV_ROUND_CLOSEST(x, divisor)( \
|
|
{ \
|
|
{ \
|
|
typeof(x) __x = x; \
|
|
typeof(x) __x = x; \
|
|
typeof(divisor) __d = divisor; \
|
|
typeof(divisor) __d = divisor; \
|
|
(((typeof(x))-1) > 0 || \
|
|
(((typeof(x))-1) > 0 || \
|
|
- ((typeof(divisor))-1) > 0 || (__x) > 0) ? \
|
|
|
|
|
|
+ ((typeof(divisor))-1) > 0 || \
|
|
|
|
+ (((__x) > 0) == ((__d) > 0))) ? \
|
|
(((__x) + ((__d) / 2)) / (__d)) : \
|
|
(((__x) + ((__d) / 2)) / (__d)) : \
|
|
(((__x) - ((__d) / 2)) / (__d)); \
|
|
(((__x) - ((__d) / 2)) / (__d)); \
|
|
} \
|
|
} \
|