mirror of
https://github.com/carp-lang/Carp.git
synced 2024-11-05 04:44:12 +03:00
29 lines
786 B
C
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
|