Debian Codegraph @

benchmarks.larceny/src/takl.java

raw
1 // TAKL -- The TAKeuchi function using lists as counters.
2
3 class Pair {
4
5 // A Java compiler ought to generate inline code for these.
6
7 public static Pair cons (int n, Pair y) { return new Pair(n, y); }
8 public static int car (Pair x) { return x.hd; }
9 public static Pair cdr (Pair x) { return x.tl; }
10
11 // If it doesn't, then we'll inline them by hand.
12 // That's why the following are public.
13 // (But Sun's Java 1.2 does the inlining ok.)
14
15 public Pair (int n, Pair y) { hd = n; tl = y; }
16 public int hd;
17 public Pair tl;
18
19 }
20
21 class takl {
22
23 static Pair listn (int n) {
24 if (n != 0)
25 return Pair.cons (n, listn (n - 1));
26 else
27 return null;
28 }
29
30 // The boolean expression below comes from the original TAKL
31 // benchmark, and remains because it is fun to see which compilers
32 // can generate good code from it. See NTAKL for a version in
33 // which this mess has been cleaned up.
34
35 static boolean shorterp (Pair x, Pair y) {
36 return (y != null) &&
37 ((x == null) ||
38 shorterp (Pair.cdr(x), Pair.cdr(y)));
39 }
40
41 static Pair mas (Pair x, Pair y, Pair z) {
42 if (! shorterp (y, x))
43 return z;
44 else
45 return mas( mas( Pair.cdr(x), y, z ),
46 mas( Pair.cdr(y), z, x ),
47 mas( Pair.cdr(z), x, y ) );
48 }
49
50 static Pair l18 = listn(18);
51 static Pair l12 = listn(12);
52 static Pair l6 = listn(6);
53
54 static int result;
55
56 static void test_takl() {
57 result = Pair.car( mas( l18, l12, l6 ) );
58 }
59
60 /*===========================================================================*/
61
62 public static void main (String args[])
63 {
64 int i;
65
66 for (i=0; i<200; i++)
67 test_takl();
68
69 if (result != 7)
70 System.out.println ("*** wrong result ***");
71 }
72
73 }