Debian Codegraph @

benchmarks.larceny/src/ntakl.c

raw
1 #include <stdio.h>
2
3 /*---------------------------------------------------------------------------*/
4
5 /* Support for Scheme like stuff */
6
7 #ifdef FAST_ALLOCATOR
8 #define CUSTOM_ALLOC 1 /* Set to 1 for custom allocation */
9 #else
10 #define CUSTOM_ALLOC 0
11 #endif
12
13 int *alloc;
14
15 #define CAR(pair) *(pair)
16 #define CDR(pair) *((pair)+1)
17
18 #if CUSTOM_ALLOC
19
20 int *old_alloc;
21
22 #define CONS(car,cdr) ( *(--alloc) = cdr, *(--alloc) = car, alloc )
23
24 #define HEAP_SIZE 1000000
25
26 void init()
27 {
28 /* char *heap = (char *) malloc( HEAP_SIZE ); */
29 int heap_size_in_bytes = HEAP_SIZE * sizeof(int);
30 int * heap = (int *) malloc( heap_size_in_bytes );
31
32 if (heap == NULL)
33 {
34 printf( "Not enough memory (%d bytes needed)\n", heap_size_in_bytes );
35 exit(1);
36 }
37
38 alloc = heap + HEAP_SIZE; /* alloc from end */
39 }
40
41 #else
42
43 #define CONS(car,cdr) ( alloc = (int *) malloc(2*sizeof(int)), \
44 *(alloc) = (car), \
45 *(alloc+1) = (cdr), \
46 alloc)
47
48 void init()
49 {
50 return;
51 }
52
53 #endif
54
55 /* Other globals */
56
57 int result;
58
59 /*=============================================================================
60
61 THE BENCHMARKS
62
63 =============================================================================*/
64
65 /*-----------------------------------------------------------------------------
66
67 TAKL
68
69 (define (listn n)
70 (if (not (= 0 n))
71 (cons n (listn (- n 1)))
72 '()))
73
74 (define l18 (listn 18))
75 (define l12 (listn 12))
76 (define l6 (listn 6))
77
78 (define (mas x y z)
79 (if (not (shorterp y x))
80 z
81 (mas (mas (cdr x)
82 y z)
83 (mas (cdr y)
84 z x)
85 (mas (cdr z)
86 x y))))
87
88 (define (shorterp x y)
89 (and y (or (null? x)
90 (shorterp (cdr x)
91 (cdr y)))))
92
93 ;;; call: (mas l18 l12 l6)
94
95 */
96
97 int *listn(n)
98 int n;
99 {
100 if (n != 0)
101 { int *rest = listn( n-1 ); return CONS( n, (int) rest ); }
102 else
103 return NULL;
104 }
105
106 int shorterp(x,y)
107 int *x, *y;
108 {
109 while ((x != NULL) && (y != NULL)) {
110 x = (int *) CDR(x);
111 y = (int *) CDR(y);
112 }
113 if (y == NULL)
114 return 0;
115 else return 1;
116
117 #if 0
118 /* This is Marc Feeley's code. I changed it to the above loop for ntakl. */
119 if (y == NULL)
120 return 0;
121 else
122 if (x == NULL)
123 return 1;
124 else
125 return shorterp( CDR(x), CDR(y) );
126 #endif
127 }
128
129 int *mas(x,y,z)
130 int *x, *y, *z;
131 {
132 if (!shorterp(y,x))
133 return z;
134 else
135 return mas( mas( CDR(x), y, z ),
136 mas( CDR(y), z, x ),
137 mas( CDR(z), x, y ) );
138 }
139
140 int *l18 = 0;
141 int *l12 = 0;
142 int *l6 = 0;
143
144 void test_takl()
145 {
146 result = CAR( mas( l18, l12, l6 ) );
147 }
148
149 /*===========================================================================*/
150
151 int main (argc, argv)
152 int argc;
153 char *argv[];
154 {
155 int i;
156
157 init();
158 l18 = listn(18);
159 l12 = listn(12);
160 l6 = listn(6);
161
162 for (i=0; i<200; i++)
163 test_takl();
164 if (result != 7)
165 printf ("*** wrong result ***\n");
166 return 0;
167 }