So I have a task to check for overflow when adding two int32_t numbers. In case of overflow my function must return maximum or minimum number of int32_t, depending on the sign of overflow, but using constants like UINT32_MAX is restricted. How do I do that? Here's the code if it will help you:
#include "inttypes.h"
int32_t
satsum(int32_t v1, int32_t v2) {
int32_t res = 0;
if(__builtin_sadd_overflow(v1, v2, &res)) {
if (res < 0) {
return ?
}
return ?
} else {
res = v1 + v2;
return res;
}
}