aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Math_Ext.jai53
1 files changed, 32 insertions, 21 deletions
diff --git a/Math_Ext.jai b/Math_Ext.jai
index 5a02756..e2bba35 100644
--- a/Math_Ext.jai
+++ b/Math_Ext.jai
@@ -57,28 +57,39 @@ sub_int64 :: (x :s64, y :s64) -> s64 {
x - y;
}
-add :: (value_a: s64, value_b: s64) -> result: s64, saturated: bool #dump { // TODO Comparing implementaitons using dump
- result: s64 = ---;
- flag: bool = ---;
- #asm {
- mov d: gpr === d, 9223372036854775807;
- mov a: gpr === a, value_a;
- mov b: gpr === b, value_b;
- add a, b;
- seto flag; // Flag 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;
+add :: (x: s64, y: s64) -> result: s64, saturated: bool #dump { // TODO Comparing implementaitons using dump
+ #if CPU == .X64 {
+ saturated := false;
+ ...test this case...
+ if (y > 0 && x > S64_MAX - y) then return S64_MAX, true;
+ if (y < 0 && x < S64_MIN - y) then return S64_MIN, true;
+ return x + y, false;
+ // if (y > 0 && x > S64_MAX - y) then return S64_MAX, true;
+ // if (y < 0 && x < S64_MIN - y) then return S64_MIN, true;
+ // return x + y, false;
+ } else {
+ result: s64 = ---;
+ flag: bool = ---;
+ #asm {
+ mov d: gpr === d, 9223372036854775807;
+ mov a: gpr === a, x;
+ mov b: gpr === b, y;
+ add a, b;
+ seto flag; // Flag overflow.
+ mov result, a;
+ mov a, x;
+ shr a, 63;
+ add a, d;
+ mov c: gpr, x;
+ xor c, b;
+ xor b, result;
+ not b;
+ or c, b;
+ test c, c;
+ cmovns result, a;
+ }
+ return result, flag;
}
- return result, flag;
}
/*