-
Notifications
You must be signed in to change notification settings - Fork 4
/
func7.fj
249 lines (189 loc) · 3.88 KB
/
func7.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Calculate the squared distance between each pair of points (between the 3 points)
// Tests a complicated call stack, with many push/pop n, that's repeated 3 times.
stl.startup_and_init_all 100
N = 2
STACK_N = (N+1)/2
hex.set N, p1_x, 9
hex.set N, p1_y, 0-54
hex.set N, p2_x, 0-22
hex.set N, p2_y, 69
hex.set N, p3_x, 87
hex.set N, p3_y, 0-127
calc_dist_squared p1_x, p1_y, p2_x, p2_y, "dist[p1, p2] ** 2 = 0x"
calc_dist_squared p1_x, p1_y, p3_x, p3_y, "dist[p1, p3] ** 2 = 0x"
calc_dist_squared p2_x, p2_y, p3_x, p3_y, "dist[p2, p3] ** 2 = 0x"
stl.loop
p1_x: hex.vec N
p1_y: hex.vec N
p2_x: hex.vec N
p2_y: hex.vec N
p3_x: hex.vec N
p3_y: hex.vec N
def calc_dist_squared x1, y1, x2, y2, prefix_distance_string @ end, res < math.square_distance {
stl.output 'A'
pushN x1
pushN y1
pushN x2
pushN y2
stl.output 'B'
stl.call math.square_distance, 2*STACK_N
stl.output 'I'
pop2N res
stl.output "J\n"
stl.output prefix_distance_string
hex.print_as_digit 2*N, res, 1
stl.output "\n\n"
;end
res: hex.vec 2*N
end:
}
ns math {
sub:
ns sub {
hex.sp_dec
popN .res
popN .temp
stl.output "-1"
hex.sub N, .res, .temp
stl.output '2'
pushN .res
hex.sp_add STACK_N+1
stl.output "3-"
stl.return
res: hex.vec N
temp: hex.vec N
}
add_2N:
ns add_2N {
hex.sp_dec
pop2N .res
pop2N .temp
stl.output "-1"
hex.add 2*N, .res, .temp
stl.output '2'
push2N .res
hex.sp_add 2*STACK_N+1
stl.output "3-"
stl.return
res: hex.vec 2*N
temp: hex.vec 2*N
}
mul:
ns mul {
stl.output "-1"
hex.sp_dec
stl.output '2'
popN .temp1
hex.sign_extend 2*N, N, .temp1
stl.output '3'
popN .temp2
hex.sign_extend 2*N, N, .temp2
stl.output '4'
hex.mul 2*N, .res, .temp1, .temp2
stl.output '5'
push2N .res
hex.sp_inc
stl.output "6-"
stl.return
temp1: hex.vec 2*N
temp2: hex.vec 2*N
res: hex.vec 2*N
}
square_sub:
ns square_sub {
hex.sp_dec
popN .b
popN .a
hex.sp_add 2*STACK_N+1
stl.output 'a'
pushN .a
pushN .b
stl.call ..sub, STACK_N
popN .res
stl.output 'b'
pushN .res
pushN .res
stl.call ..mul
pop2N .res
stl.output 'c'
hex.sp_sub 2*STACK_N+1
push2N .res
hex.sp_inc
stl.output "d\n"
stl.return
a: hex.vec 2*N
b: hex.vec 2*N
res: hex.vec 2*N
}
square_distance:
ns square_distance {
stl.output 'C'
hex.sp_dec
popN .y2
popN .x2
popN .y1
popN .x1
hex.sp_add 4*STACK_N+1
stl.output 'D'
// (x1-x2)**2
pushN .x1
pushN .x2
stl.call ..square_sub
pop2N .res1
stl.output 'E'
// (y1-y2)**2
pushN .y1
pushN .y2
stl.call ..square_sub
pop2N .res2
stl.output 'F'
// (x1-x2)**2 + (y1-y2)**2
push2N .res1
push2N .res2
stl.call ..add_2N, 2*STACK_N
pop2N .res1
stl.output 'G'
hex.sp_sub 4*STACK_N+1
push2N .res1
hex.sp_add 2*STACK_N+1
stl.output 'H'
stl.return
x1: hex.vec N
y1: hex.vec N
x2: hex.vec N
y2: hex.vec N
res1: hex.vec 2*N
res2: hex.vec 2*N
}
}
// Push+Pop fast-functions
def pushN variable < pushN, stack_variable, pushed_ret {
hex.mov N, stack_variable, variable
stl.fcall pushN, pushed_ret
}
def push2N variable < push2N, stack_variable, pushed_ret {
hex.mov 2*N, stack_variable, variable
stl.fcall push2N, pushed_ret
}
def popN variable < popN, stack_variable, pushed_ret {
stl.fcall popN, pushed_ret
hex.mov N, variable, stack_variable
}
def pop2N variable < pop2N, stack_variable, pushed_ret {
stl.fcall pop2N, pushed_ret
hex.mov 2*N, variable, stack_variable
}
stack_variable: hex.vec 2*N
pushed_ret: bit.bit
pushN:
hex.push N, stack_variable
stl.fret pushed_ret
push2N:
hex.push 2*N, stack_variable
stl.fret pushed_ret
popN:
hex.pop N, stack_variable
stl.fret pushed_ret
pop2N:
hex.pop 2*N, stack_variable
stl.fret pushed_ret