aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/main.c b/main.c
index 69c0e78..d1a66fc 100644
--- a/main.c
+++ b/main.c
@@ -221,19 +221,33 @@ char *sprint_time5_utf8(char *string, intmax_t time, int space) {
int64_t add_int64(int64_t x, int64_t y) {
int64_t result;
+#ifdef __GNUC__
bool overflow = __builtin_add_overflow(x, y, &result);
if (overflow) {
result = ((uint64_t)x >> 63) + INT64_MAX; // Equivalent to (x > 0 ? INT64_MAX : INT64_MIN)
}
+#else
+ result =
+ (y > 0 && x > INT64_MAX - y) ? INT64_MAX :
+ (y < 0 && x < INT64_MIN - y) ? INT64_MIN :
+ x + y;
+#endif
return result;
}
int64_t sub_int64(int64_t x, int64_t y) {
int64_t result;
+#ifdef __GNUC__
bool overflow = __builtin_sub_overflow(x, y, &result);
if (overflow) {
result = ((uint64_t)x >> 63) + INT64_MAX; // Equivalent to (x > 0 ? INT64_MAX : INT64_MIN)
}
+#else
+ result =
+ (y < 0 && x > INT64_MAX + y) ? INT64_MAX :
+ (y > 0 && x < INT64_MIN + y) ? INT64_MIN :
+ x - y;
+#endif
return result;
}