diff options
| -rw-r--r-- | ttt.jai | 39 | ||||
| -rw-r--r-- | unused.jai | 50 |
2 files changed, 62 insertions, 27 deletions
@@ -271,39 +271,23 @@ int mvprintw_time(int y, int x, intmax_t time, int space) { return mvprintw(y, x, "%*s ∞ %*s", left_padding, "", right_padding, ""); } } +*/ -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 : +add_int64 :: (x :s64, y: s64) -> s64 { + return + ifx (y > 0 && x > S64_MAX - y) then S64_MAX else + ifx (y < 0 && x < S64_MIN - y) then S64_MIN else 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 : +sub_int64 :: (x :s64, y :s64) -> s64 { + return + ifx (y < 0 && x > S64_MAX + y) then S64_MAX else + ifx (y > 0 && x < S64_MIN + y) then S64_MIN else x - y; -#endif - return result; } +/* // Returns active task or NULL if none applies. task_st *get_active_task(database_st *db) { assert(db != NULL); @@ -1823,7 +1807,8 @@ int main(int argc, char *argv[]) { #import "Basic"; #import "System"; -#import "File_Utilities"; +#import "Math"; +// #import "File_Utilities"; homie : string; diff --git a/unused.jai b/unused.jai new file mode 100644 index 0000000..3529a46 --- /dev/null +++ b/unused.jai @@ -0,0 +1,50 @@ +checked_add :: (a: $T, b: T) -> result: T, overflow: bool +#modify { + if T.type == .INTEGER return; + T = null; +} +{ + overflow: bool; + result: T = a + b; + + info := type_info(T); + if info.signed { + // (+A) + (+B) = −C + // (−A) + (−B) = +C + if ((a > 0) && (b > 0) && (result < 0)) || ((a < 0) && (b < 0) && (result > 0)) { + overflow = true; + } + } else { + if result < a { + overflow = true; + } + } + + return result, overflow; +} + +checked_sub :: (a: $T, b: T) -> result: T, overflow: bool +#modify { + if T.type == .INTEGER return; + T = null; +} +{ + overflow: bool; + result: T = a - b; + + info := type_info(T); + if info.signed { + // (+A) − (−B) = −C + // (−A) − (+B) = +C + if ((a > 0) && (b < 0) && (result < 0)) || ((a < 0) && (b > 0) && (result > 0)) { + overflow = true; + } + } else { + if result > a { + overflow = true; + } + } + + return result, overflow; +} + |
