-
Notifications
You must be signed in to change notification settings - Fork 4
/
cond_jumps.fj
97 lines (87 loc) · 2.19 KB
/
cond_jumps.fj
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// ---------- Conditional Jumps
ns bit {
// Complexity: @+2
// if x == 0 jump to l0, else jump to l1
// x is a bit, l0,l1 are addresses.
def if x, l0, l1 @ label_ptr, base_jump_label {
.xor label_ptr, x
label_ptr:
;base_jump_label
pad 2
base_jump_label:
;l0
label_ptr + dbit;l1
}
// Complexity: n(@+2)
// if x[:n] == 0 jump to l0, else jump to l1
// x is bit[:n], l0,l1 are addresses.
def if n, x, l0, l1 {
rep(n-1, i) .if1 x+i*dw, l1
.if x+(n-1)*dw, l0, l1
}
// Complexity: @+2
// if x == 1 jump to l1
// x is a bit, l1 is an address.
def if1 x, l1 @ end {
.if x, end, l1
end:
}
// Complexity: n(@+2)
// if the x[:n] != 0 jump to l1
// x is bit[:n], l1 is an address.
def if1 n, x, l1 @ end {
.if n, x, end, l1
end:
}
// Complexity: @+2
// if x == 0 jump to l0
// x is a bit, l0 is an address.
def if0 x, l0 @ end {
.if x, l0, end
end:
}
// Complexity: n(@+2)
// if x[:n] == 0 jump to l0
// x is bit[:n], l0 is an address.
def if0 n, x, l0 @ end {
.if n, x, l0, end
end:
}
// Complexity: 2@+4
// Space: 3@+6
// jump to:
// a < b: lt
// a = b: eq
// a > b: gt
// a,b are bits, lt,eq,gt are addresses.
def cmp a, b, lt, eq, gt @ a_is1_label {
.if1 a, a_is1_label
.if b, eq, lt
a_is1_label:
.if b, gt, eq
}
// TIme Complexity: n(2@+4)
// Space Complexity: n(3@+6)
// jump to:
// a[:n] < b[:n]: lt
// a[:n] = b[:n]: eq
// a[:n] > b[:n]: gt
// a,b are bit[:n], lt,eq,gt are addresses.
def cmp n, a, b, lt, eq, gt {
rep(n-1, i) ._.cmp_next_eq a+(n-1-i)*dw, b+(n-1-i)*dw, lt, gt
.cmp a, b, lt, eq, gt
}
ns _ {
// Complexity: 2@+4
// Space: 3@+6
// jump to:
// a < b: lt
// a = b: continue
// a > b: gt
// a,b are bits, lt,gt are addresses.
def cmp_next_eq a, b, lt, gt @ eq {
..cmp a, b, lt, eq, gt
eq:
}
}
}