From e7cdf6fe804e1c01179afc3e40b761144cd0b057 Mon Sep 17 00:00:00 2001 From: dam Date: Wed, 25 Jan 2023 00:26:24 +0000 Subject: Ported add_int64 and sub_int64 to jai. --- unused.jai | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 unused.jai (limited to 'unused.jai') 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; +} + -- cgit v1.2.3