aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordam <dam@gudinoff>2023-01-25 00:26:24 +0000
committerdam <dam@gudinoff>2023-01-25 00:26:24 +0000
commite7cdf6fe804e1c01179afc3e40b761144cd0b057 (patch)
tree122cd35ee730bbcdb89ff252e1ce65f4dc92e06c
parentbb62e6e9f46f2009a9b09d8b3f106a7470ffb0f2 (diff)
downloadtask-time-tracker-e7cdf6fe804e1c01179afc3e40b761144cd0b057.tar.zst
task-time-tracker-e7cdf6fe804e1c01179afc3e40b761144cd0b057.zip
Ported add_int64 and sub_int64 to jai.
-rw-r--r--ttt.jai39
-rw-r--r--unused.jai50
2 files changed, 62 insertions, 27 deletions
diff --git a/ttt.jai b/ttt.jai
index fd39bb3..ea999c8 100644
--- a/ttt.jai
+++ b/ttt.jai
@@ -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;
+}
+