-
Notifications
You must be signed in to change notification settings - Fork 11
/
examples13.txt
298 lines (235 loc) · 7.63 KB
/
examples13.txt
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
****************************************************************
Explicit deallocation in a pure OO language.
// Returns a value of x that satisfies the equation
// 0 = a x^2 + b x + c
//
// Strategy: quadratic formula
double root (double a, double b, double c) {
return (- b + Math.sqrt (b * b - 4 * a * c)) / (2 * a);
}
****************************************************************
// Returns a value of x that satisfies the equation
// 0 = a x^2 + b x + c
//
// The caller is responsible for deallocating that value of x.
//
// Strategy: quadratic formula
double root (double a, double b, double c) {
double bsquared = b * b;
double ac = a * c;
double fourAC = 4 * ac;
double diff = bsquared - fourAC;
double sqrtDiff = Math.sqrt (diff);
double minusB = - b;
double numerator = minusB + sqrtDiff;
double denominator = 2 * a;
double result = numerator / denominator;
delete bsquared;
delete ac;
delete fourAC;
delete diff;
delete sqrtDiff;
delete minusB;
delete numerator;
delete denominator;
return result;
}
****************************************************************
fib (n)
if n < 2
then n
else fib (n - 1) + fib (n - 2)
core 0 core 1 core 2 core 3
fib (10)
fib (9) fib (8)
fib (8) fib (7) fib (7) fib (6)
****************************************************************
fib (n)
if n < 2
then n
else fibLoop (2, n, 0, 1)
# Given i <= n and the values of fib(i-2) and fib(i-1),
# returns fib(n).
fibLoop (i, n, x2, x1)
if i = n
then x2 + x1
else fibLoop (i + 1, n, x1, x2 + x1)
****************************************************************
// Returns the inner product of the given arrays.
double ip (double[] a, double[] b) {
int n = a.length;
double result = 0.0;
for (int i = 0; i < n; i = i + 1)
result = result + a[i] * b[i];
return result;
}
****************************************************************
double ip (register double *a, register double *b, register int n) {
register double result = 0.0;
while (n-- > 0)
result += *(a++) * *(b++);
return result;
}
****************************************************************
while (m > 0) {
m = m - 1;
result = ip (a, b);
}
System.out.println (ip (a, b));
****************************************************************
double ip (double[] a, double[] b) {
int n = a.length;
if (n != b.length)
throw new RuntimeException();
double result = 0.0;
for (int i = 0; i < n; i = i + 1)
result = result + a[i] * b[i];
return result;
}
// a is in r1
// b is in r2
// n is in r3
// result is in f0
sub r4,r4,r4 // i is in r4
L1:
cmp r4,r3,r5
bge L2
cmpi r1,0,r5 // null check for a
trapeq r5
cmpi r4,0,r5 // range check for a[i]
traplt r5
ldi -4(r1),r5
cmp r4,r5,r5
trapge r5
muli r4,8,r5 // fetch a[i] into f1
fld r5(r1),f1
cmpi r2,0,r5 // null check for b
trapeq r5
cmpi r4,0,r5 // range check for b[i]
traplt r5
ldi -4(r2),r5
cmp r4,r5,r5
trapge r5
muli r4,8,r5 // fetch b[i] into f2
fld r5(r2),f2
fmul f1,f2,f1 // floating point multiplication
fadd f0,f1,f0 // floating point addition
addi r4,1,r4 // i = i + 1;
b L1
L2:
****************************************************************
L1:
cmp r4,r3,r5
bge L2
muli r4,8,r5 // fetch a[i] into f1
fld r5(r1),f1
muli r4,8,r5 // fetch b[i] into f2
fld r5(r2),f2
fmul f1,f2,f1 // floating point multiplication
fadd f0,f1,f0 // floating point addition
addi r4,1,r4 // i = i + 1;
b L1
L2:
****************************************************************
L1:
cmp r4,r3,r5
bge L2
L1b:
muli r4,8,r5 // fetch a[i] into f1
fld r5(r1),f1
muli r4,8,r5 // fetch b[i] into f2
fld r5(r2),f2
fmul f1,f2,f1 // floating point multiplication
fadd f0,f1,f0 // floating point addition
addi r4,1,r4 // i = i + 1;
cmp r4,r3,r5
blt L1b
L2:
****************************************************************
L1:
cmp r4,r3,r5
bge L2
L1b:
fld r4(r1),f1 // fetch a[i] into f1
fld r4(r2),f2 // fetch b[i] into f2
fmul f1,f2,f1 // floating point multiplication
fadd f0,f1,f0 // floating point addition
addi r4,8,r4 // i8 = i8 + 8;
cmp r4,r3,r5 // is i8 < n8 ?
blt L1b
L2:
****************************************************************
# Returns a tree of height n.
makeTree(n)
if n = 0
then emptyTree
else makeNode (makeTree (n - 1),
makeTree (n - 1))
# Returns a tree of height n.
makeTree(n)
if n = 0
then emptyTree
else let tmp1 = makeTree (n - 1)
in makeNode (tmp1, tmp1)
****************************************************************
(define-syntax
(syntax-rules (else)
((cond)
(if #f #f))
((cond (else <expr>) clause ...)
<expr>)
((cond (<test> <result>) clause ...)
(if <test> <result> (cond clause ...)))))
(cond ((empty? y) #f)
((equal? x (first y))
#t)
(else
(member? x (rest y))))
macro-expands into
(if (empty? y)
#f
(if (equal? x (first y))
#t
(member? x (rest y))))
****************************************************************
(define-syntax
(syntax-rules ()
((let ((<var> <rhs>) ...) <expr>)
((lambda (<var> ...) <expr>)
<rhs> ...))))
(let ((x 1)
(y 2)
(z 3))
(+ x y z))
macro-expands into
((lambda (x y z) (+ x y z))
1 2 3)
****************************************************************
(define-syntax while
(syntax-rules ()
((while <test> <stmt1> ...)
(let ((loop (lambda (loop)
(if <test>
(begin <stmt1> ... (loop loop))))))
(loop loop)))))
****************************************************************
(let ((if 1000.0) ; intermediate frequency of transceiver
(loop 3) ; we want to loop 3 times
(result 0)) ; will become the 3rd harmonic of if
(while (> loop 0)
(set! loop (- loop 1))
(set! result (+ result if)))
result)
With non-hygienic macro expansion, that example would expand
into
((lambda (if loop result)
((lambda (loop) (loop loop))
(lambda (loop)
(if (> loop 0)
(begin (set! loop (- loop 1))
(set! result (+ result if))
(loop loop))))))
1000.0
3
0)
****************************************************************