_builtins = [
['__builtin_clz', '0', true],
['__builtin_types_compatible_p', 'int, int', true],
+ ['__builtin_uadd_overflow', '0U, 0U, (void*)0', false],
['__builtin_uaddl_overflow', '0UL, 0UL, (void*)0', false],
['__builtin_uaddll_overflow', '0ULL, 0ULL, (void*)0', false],
]
}
#define _cleanup_free_ _cleanup_(freep)
+static inline bool uadd32_overflow(uint32_t a, uint32_t b, uint32_t *res)
+{
+#if (HAVE___BUILTIN_UADD_OVERFLOW && __SIZEOF_INT__ == 4)
+ return __builtin_uadd_overflow(a, b, res);
+#else
+ *res = a + b;
+ return UINT32_MAX - a < b;
+#endif
+}
+
static inline bool uadd64_overflow(uint64_t a, uint64_t b, uint64_t *res)
{
#if (HAVE___BUILTIN_UADDL_OVERFLOW && __SIZEOF_LONG__ == 8)
},
});
+static int test_uadd32_overflow(const struct test *t)
+{
+ uint32_t res;
+ bool overflow;
+
+ overflow = uadd32_overflow(UINT32_MAX - 1, 1, &res);
+ assert_return(!overflow, EXIT_FAILURE);
+ assert_return(res == UINT32_MAX, EXIT_FAILURE);
+
+ overflow = uadd32_overflow(UINT32_MAX, 1, &res);
+ assert_return(overflow, EXIT_FAILURE);
+
+ return EXIT_SUCCESS;
+}
+DEFINE_TEST(test_uadd32_overflow,
+ .description = "check implementation of uadd32_overflow()")
+
static int test_uadd64_overflow(const struct test *t)
{
uint64_t res;