99#include <bfdev/config.h>
1010#include <bfdev/types.h>
1111#include <bfdev/stddef.h>
12+ #include <bfdev/limits.h>
1213
1314BFDEV_BEGIN_DECLS
1415
@@ -18,7 +19,7 @@ BFDEV_BEGIN_DECLS
1819 * @b: second addend.
1920 * @d: pointer to store sum.
2021 *
21- * Returns 0 on success .
22+ * Returns true on wrap-around, false otherwise .
2223 */
2324#define bfdev_overflow_check_add (a , b , d ) \
2425bfdev_overflow_check(({ \
@@ -36,7 +37,7 @@ bfdev_overflow_check(({ \
3637 * @b: subtrahend; value to subtract from @a.
3738 * @d: pointer to store difference.
3839 *
39- * Returns 0 on success .
40+ * Returns true on wrap-around, false otherwise .
4041 */
4142#define bfdev_overflow_check_sub (a , b , d ) \
4243bfdev_overflow_check(({ \
@@ -54,7 +55,7 @@ bfdev_overflow_check(({ \
5455 * @b: second factor.
5556 * @d: pointer to store product.
5657 *
57- * Returns 0 on success .
58+ * Returns true on wrap-around, false otherwise .
5859 */
5960#define bfdev_overflow_check_mul (a , b , d ) \
6061bfdev_overflow_check(({ \
@@ -71,23 +72,23 @@ bfdev_overflow_check(({ \
7172 type __b = (type)(b); \
7273 type __d; \
7374 bfdev_overflow_check_add(__a, __b, &__d) \
74- ? (type)~0ULL : __d; \
75+ ? bfdev_type_max (type) : __d; \
7576})
7677
7778#define bfdev_overflow_sub_type (type , a , b ) ({ \
7879 type __a = (type)(a); \
7980 type __b = (type)(b); \
8081 type __d; \
8182 bfdev_overflow_check_sub(__a, __b, &__d) \
82- ? (type)~0ULL : __d; \
83+ ? bfdev_type_min (type) : __d; \
8384})
8485
8586#define bfdev_overflow_mul_type (type , a , b ) ({ \
8687 type __a = (type)(a); \
8788 type __b = (type)(b); \
8889 type __d; \
8990 bfdev_overflow_check_mul(__a, __b, &__d) \
90- ? (type)~0ULL : __d; \
91+ ? bfdev_type_max (type) : __d; \
9192})
9293
9394static inline __bfdev_must_check bfdev_bool
@@ -101,7 +102,8 @@ bfdev_overflow_check(bfdev_bool overflow)
101102 * @a: first addend.
102103 * @b: second addend.
103104 *
104- * Returns (type)~0ULL on failed.
105+ * Returns: calculate @a + @b, any overflow causing the
106+ * return value to be bfdev_type_max(type).
105107 */
106108#define bfdev_overflow_add (a , b ) \
107109 bfdev_overflow_add_type(typeof(a), a, b)
@@ -111,7 +113,8 @@ bfdev_overflow_check(bfdev_bool overflow)
111113 * @a: minuend; value to subtract from.
112114 * @b: subtrahend; value to subtract from @a.
113115 *
114- * Returns (type)~0ULL on failed.
116+ * Returns: calculate @a - @b, any overflow causing the
117+ * return value to be bfdev_type_min(type).
115118 */
116119#define bfdev_overflow_sub (a , b ) \
117120 bfdev_overflow_sub_type(typeof(a), a, b)
@@ -121,7 +124,8 @@ bfdev_overflow_check(bfdev_bool overflow)
121124 * @a: first factor.
122125 * @b: second factor.
123126 *
124- * Returns (type)~0ULL on failed.
127+ * Returns: calculate @a * @b, any overflow causing the
128+ * return value to be bfdev_type_max(type).
125129 */
126130#define bfdev_overflow_mul (a , b ) \
127131 bfdev_overflow_mul_type(typeof(a), a, b)
0 commit comments