Debian Codegraph @

benchmarks.larceny/src/ntakl.java

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