diff options
| author | dam <dam@gudinoff> | 2022-11-25 00:19:51 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-11-25 00:19:51 +0000 |
| commit | d808a8c3af7a048d3d186cf10a8122e8d3ffc6bc (patch) | |
| tree | 0f2e699443cab08cbbf5ed3ef74b6b827c767e87 | |
| parent | 28eb0211471eda8e651aa8a7fffd48b611581dc1 (diff) | |
| download | task-time-tracker-d808a8c3af7a048d3d186cf10a8122e8d3ffc6bc.tar.zst task-time-tracker-d808a8c3af7a048d3d186cf10a8122e8d3ffc6bc.zip | |
Add fallback method for non-GNUC compilers on add_int64 and sub_int64 functions.
| -rw-r--r-- | main.c | 14 |
1 files changed, 14 insertions, 0 deletions
@@ -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; } |
