aboutsummaryrefslogtreecommitdiff
path: root/unused.jai
blob: 3529a461315eb2acdbeb8ff935daa3b4a932d176 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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;
}