aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordam <dam@gudinoff>2023-04-29 03:48:10 +0100
committerdam <dam@gudinoff>2023-04-29 03:48:10 +0100
commit8b5c33cd6f2ff1f0a073a773a27987b995ea8bc1 (patch)
tree658e2ea42d0e878c4fd4607847cc2df2a3c42133
parent1d0c89ead1dc18ade2051e80b194b9de290106d5 (diff)
downloadtask-time-tracker-8b5c33cd6f2ff1f0a073a773a27987b995ea8bc1.tar.zst
task-time-tracker-8b5c33cd6f2ff1f0a073a773a27987b995ea8bc1.zip
Prototyping sat_s64 add/sub procedures.
-rw-r--r--ttt.jai89
1 files changed, 89 insertions, 0 deletions
diff --git a/ttt.jai b/ttt.jai
index 9c7f948..60db8b7 100644
--- a/ttt.jai
+++ b/ttt.jai
@@ -1163,6 +1163,95 @@ read_enter_confirmation :: inline (row: int, style: int, message: string) -> boo
}
main :: () {
+
+ // value_a: s64 = 2;
+ // value_b: s64 = S64_MAX-1;
+ value_a: s64 = -2;
+ value_b: s64 = S64_MIN+1;
+ print(">%\n", S64_MAX);
+ argx := get_command_line_arguments();
+ if argx.count > 1
+ value_a = parse_int(*argx[1]);
+ if argx.count > 2
+ value_b = parse_int(*argx[2]);
+
+ result: s64 = ---;
+ flags: s64;// = ---;
+ #asm LAHF_SAHF { // TODO Remove LAHF_SAHF it not required.
+
+ // Code from https://locklessinc.com/articles/sat_arithmetic/
+
+
+ // value_a === b;
+ // value_b === c;
+ // add value_a, value_b;
+ // mov result, value_a;
+
+ // Version 1
+ // mov b: gpr === b, value_a;
+ // mov c: gpr === c, value_b;
+ // add b, c;
+ // mov result, b;
+ // seto flags;
+ // cmovns b, c;
+
+// s64b sat_adds64b(s64b x, s64b y)
+// {
+// u64b ux = x;
+// u64b uy = y;
+// u64b res = ux + uy;
+//
+// ux = (ux >> 63) + LONG_MAX;
+//
+// /* Force compiler to use cmovns instruction */
+// if ((s64b) ((ux ^ uy) | ~(uy ^ res)) >= 0)
+// {
+// res = ux;
+// }
+//
+// return res;
+// }
+ // Version 2 - WORKS
+ mov d: gpr === d, 9223372036854775807;
+ mov a: gpr === a, value_a;
+ mov b: gpr === b, value_b;
+ add a, b;
+ seto flags; // Signal overflow.
+ mov result, a;
+ mov a, value_a;
+ shr a, 63;
+ add a, d;
+ mov c: gpr, value_a;
+ xor c, b;
+ xor b, result;
+ not b;
+ or c, b;
+ test c, c;
+ cmovns result, a;
+
+// s64b sat_subs64b(s64b x, s64b y)
+// {
+// u64b ux = x;
+// u64b uy = y;
+// u64b res = ux - uy;
+//
+// ux = (ux >> 63) + LONG_MAX;
+//
+// /* Force compiler to use cmovns instruction */
+// if ((s64b)((ux ^ uy) & (ux ^ res)) < 0)
+// {
+// res = ux;
+// }
+//
+// return res;
+// }
+ // TODO Use https://godbolt.org/ to help
+
+ }
+ print("% + % = %\n", value_a, value_b, result);
+ print("flag: %\n", flags);
+ return;
+
// TODO Implement signal handling and see modules/Debug.jai for examples.