Carp/core/carp_safe_int.h

29 lines
786 B
C

#if defined __GNUC__
bool Int_safe_MINUS_add(int x, int y, int* res) {
return __builtin_add_overflow(x, y, res);
}
bool Int_safe_MINUS_sub(int x, int y, int* res) {
return __builtin_sub_overflow(x, y, res);
}
bool Int_safe_MINUS_mul(int x, int y, int* res) {
return __builtin_mul_overflow(x, y, res);
}
#else
bool Int_safe_MINUS_add(int x, int y, int* res) {
int r = x + y;
*res = r;
return (y > 0) && (x > (INT_MAX - y)) || (y < 0) && (x < (INT_MIN - y));
}
bool Int_safe_MINUS_sub(int x, int y, int* res) {
int r = x - y;
*res = r;
*res = x - y;
return (y > 0 && x < (INT_MIN + y)) || (y < 0 && x > (INT_MAX + y));
}
bool Int_safe_MINUS_mul(int x, int y, int* res) {
int r = x * y;
*res = r;
return y == 0 || (r / y) != x;
}
#endif